diff --git a/README.md b/README.md index e28d12e..8ebb1af 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ How to configure config.json { "log": "/tmp/gitlab-ldap-sync.log", // Where to store the log file. If not set, will log to stdout "log_level": "INFO", // The log level + "cron": false, // Should the script run as cronjob suppressing any output to stdout "gitlab": { "api": "https://gitlab.example.com", // Url of your GitLab "ssl_verify": true, // Verify SSL certificate when using HTTPs (true, false, path to own CA bundle) diff --git a/config.json b/config.json index 5614e75..8b9f40d 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,7 @@ { "log": "/tmp/gitlab-ldap-sync.log", "log_level": "INFO", + "cron": false, "gitlab": { "api": "", "ssl_verify": true, diff --git a/gitlab-ldap-sync.py b/gitlab-ldap-sync.py index a50e45d..aaf707a 100755 --- a/gitlab-ldap-sync.py +++ b/gitlab-ldap-sync.py @@ -8,16 +8,26 @@ import ldap.asyncsearch import logging +CRON = False + +def logstdout(s): + if not CRON: + print(s) + if __name__ == "__main__": - print('Initializing gitlab-ldap-sync.') config = None with open('config.json') as f: config = json.load(f) + if config is None: + logstdout('Initializing gitlab-ldap-sync failed.') if config is not None: - print('Done.') - print('Updating logger configuration') + logstdout('Initializing gitlab-ldap-sync successfull.') + if config['cron']: + CRON = True + logstdout('Updating logger configuration') if not config['gitlab']['group_visibility']: config['gitlab']['group_visibility'] = 'private' + log_option = { 'format': '[%(asctime)s] [%(levelname)s] %(message)s' } @@ -26,7 +36,11 @@ if config['log_level']: log_option['level'] = getattr(logging, str(config['log_level']).upper()) logging.basicConfig(**log_option) - print('Done.') + if config['cron'] and config['log']: + console = logging.StreamHandler() + console.setLevel(logging.ERROR) + logging.getLogger('').addHandler(console) + logstdout('Done.') logging.info('Connecting to GitLab') if config['gitlab']['api']: gl = None