From a70146818cd72375e44af907330e31791c2e37fe Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 3 Oct 2013 16:30:25 -0700 Subject: [PATCH 1/4] pull out this destfile value --- configure-syslog.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/configure-syslog.py b/configure-syslog.py index 0736e59..00553b4 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -931,28 +931,22 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details, config_file = open(file_path, "w") config_file.write(content) config_file.close() + destfile = os.path.join(syslog_configuration_details.get("path"), + LOGGLY_CONFIG_FILE) if user_type == NON_ROOT_USER: # print Instructions... - content = "mv -f %s %s\n%s" % (file_path, - os.path.join(syslog_configuration_details.get("path"), - LOGGLY_CONFIG_FILE), command_content) + content = "mv -f %s %s\n%s" % (file_path, destfile, command_content) create_bash_script(content) else: - if os.path.isfile(os.path.join( - syslog_configuration_details.get("path"), LOGGLY_CONFIG_FILE)): + if os.path.isfile(destfile): msg = ("Loggly configuration file (%s) is already present. " - "Do you want to overwrite it? [Yes|No]: " - % os.path.join(syslog_configuration_details.get("path"), - LOGGLY_CONFIG_FILE)) + "Do you want to overwrite it? [Yes|No]: " % destfile) for _ in range(5): user_input = usr_input(msg).lower() if len(user_input) > 0: if user_input in yes: - os.popen("mv -f %s %s" % (file_path, - os.path.join( - syslog_configuration_details.get("path"), - LOGGLY_CONFIG_FILE))) + os.popen("mv -f %s %s" % (file_path, destfile)) return elif user_input in no: printMessage("Finished") @@ -960,9 +954,7 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details, else: printLog("Not a valid input. Please retry.") else: - os.popen("mv -f %s %s" % (file_path, - os.path.join(syslog_configuration_details.get("path"), - LOGGLY_CONFIG_FILE))) + os.popen("mv -f %s %s" % (file_path, destfile)) return printLog("Invalid input received after maximum attempts.") From 856cf456e11f9065023c97aa1db5922e73e1fc38 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 3 Oct 2013 16:32:30 -0700 Subject: [PATCH 2/4] pull out confirm() helper --- configure-syslog.py | 180 +++++++++++++++++++++----------------------- 1 file changed, 85 insertions(+), 95 deletions(-) diff --git a/configure-syslog.py b/configure-syslog.py index 00553b4..09937bf 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -191,9 +191,6 @@ }; '''.strip() -yes = ['yes', 'ye', 'y'] -no = ['no', 'n'] - LOGGLY_HELP = ''' Instructions to manually re-configure syslog for Loggly ======================================================= @@ -916,6 +913,16 @@ def create_bash_script(content): "restart the syslog service" % file_path)) +def confirm(question): + for _ in range(5): + user_input = usr_input(question + ' [Yes|No]: ').lower() + if user_input in ['yes', 'ye', 'y']: + return True + elif user_input in ['no', 'n']: + return False + elif user_input: + printLog("Not a valid input. Please retry.") + def create_loggly_config_file(syslog_id, syslog_configuration_details, authorization_details, user_type): """ @@ -940,26 +947,23 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details, else: if os.path.isfile(destfile): msg = ("Loggly configuration file (%s) is already present. " - "Do you want to overwrite it? [Yes|No]: " % destfile) - - for _ in range(5): - user_input = usr_input(msg).lower() - if len(user_input) > 0: - if user_input in yes: - os.popen("mv -f %s %s" % (file_path, destfile)) - return - elif user_input in no: - printMessage("Finished") - sys.exit(0) - else: - printLog("Not a valid input. Please retry.") + "Do you want to overwrite it?" % destfile) + move = confirm(msg) + if move: + os.popen("mv -f %s %s" % (file_path, destfile)) + return + elif move is False: + printMessage("Finished") + sys.exit(0) + elif move is None: + printLog("Invalid input received after maximum attempts.") + printMessage("Aborting") + sys.exit(-1) + else: os.popen("mv -f %s %s" % (file_path, destfile)) return - printLog("Invalid input received after maximum attempts.") - printMessage("Aborting") - sys.exit(-1) except IOError as e: printLog("IOError %s" % e) @@ -981,73 +985,64 @@ def modify_syslog_config_file(syslog_id, syslog_configuration_details, if len(syslog_configuration_details.get("token")) <= 0: question = ("\nThe Loggly configuration will be appended to (%s) file." "\n\nDo you want this installer to modify " - "the configuration file? [Yes|No]: " + "the configuration file?" % default_config_file_name.get(syslog_id)) - for _ in range(5): - user_input = usr_input(question).lower() - if len(user_input) > 0: - if user_input in yes: - - backup_file_name = ("%s_%s.bak" - % (default_config_file_name.get(syslog_id), - datetime.now().strftime('%Y-%m-%dT%H:%M:%S'))) - - with tempfile.NamedTemporaryFile(delete=False) as temp_file: - temp_file.write(content.encode('utf-8')) - if user_type == ROOT_USER: - os.popen(("cp -p %s %s" - % (default_config_file_name.get(syslog_id), - backup_file_name))) - os.popen(("bash -c 'cat %s >> %s' " - % (temp_file.name, - default_config_file_name.get(syslog_id)))).read() - os.unlink(temp_file.name) - else: - bash_script_content = ("cp -p %s %s " - "\nbash -c 'cat %s >> %s'\n%s" - % (default_config_file_name.get(syslog_id), - backup_file_name, temp_file.name, - default_config_file_name.get(syslog_id), - command_content)) - create_bash_script(bash_script_content) - return backup_file_name - - elif user_input in no: - printLog("\nPlease add the following lines to " - "the syslog configuration file (%s)." - "\n\n%s%s" - % (default_config_file_name.get(syslog_id), - comment, content)) - printMessage("Finished") - sys.exit(0) + modify = confirm(question) + if modify: + backup_file_name = ("%s_%s.bak" + % (default_config_file_name.get(syslog_id), + datetime.now().strftime('%Y-%m-%dT%H:%M:%S'))) + + with tempfile.NamedTemporaryFile(delete=False) as temp_file: + temp_file.write(content.encode('utf-8')) + if user_type == ROOT_USER: + os.popen(("cp -p %s %s" + % (default_config_file_name.get(syslog_id), + backup_file_name))) + os.popen(("bash -c 'cat %s >> %s' " + % (temp_file.name, + default_config_file_name.get(syslog_id)))).read() + os.unlink(temp_file.name) + else: + bash_script_content = ("cp -p %s %s " + "\nbash -c 'cat %s >> %s'\n%s" + % (default_config_file_name.get(syslog_id), + backup_file_name, temp_file.name, + default_config_file_name.get(syslog_id), + command_content)) + create_bash_script(bash_script_content) + return backup_file_name + elif modify is False: + printLog("\nPlease add the following lines to " + "the syslog configuration file (%s)." + "\n\n%s%s" + % (default_config_file_name.get(syslog_id), + comment, content)) + printMessage("Finished") + sys.exit(0) + else: question = ("\nThis configuration currently uses \"%s\" as its Customer Token. " - "Do you want to overwrite it? [Yes|No]: " + "Do you want to overwrite it?" % syslog_configuration_details.get("token")) - for _ in range(5): - user_input = usr_input(question).lower() - if len(user_input) > 0: - if user_input in yes: - pattern = (r"s/[a-z0-9]\{8\}\-[a-z0-9]\{4\}\-[a-z0-9]" - r"\{4\}\-[a-z0-9]\{4\}\-[a-z0-9]\{12\}/%s/g" - % authorization_details.get("token")) - if user_type == ROOT_USER: - os.popen("sed -i '%s' %s" % (pattern, - default_config_file_name.get(syslog_id))) - else: - bash_script_content = "sed -i '%s' %s\n%s" % (pattern, - default_config_file_name.get(syslog_id), - command_content) - create_bash_script(bash_script_content) - return - elif user_input in no: - printMessage("Finished") - sys.exit(0) - return - else: - printLog("Not a valid input. Please retry.") - printMessage("Aborting") - sys.exit(-1) + overwrite = confirm(question) + if overwrite: + pattern = (r"s/[a-z0-9]\{8\}\-[a-z0-9]\{4\}\-[a-z0-9]" + r"\{4\}\-[a-z0-9]\{4\}\-[a-z0-9]\{12\}/%s/g" + % authorization_details.get("token")) + if user_type == ROOT_USER: + os.popen("sed -i '%s' %s" % (pattern, + default_config_file_name.get(syslog_id))) + else: + bash_script_content = "sed -i '%s' %s\n%s" % (pattern, + default_config_file_name.get(syslog_id), + command_content) + create_bash_script(bash_script_content) + return + elif overwrite is False: + printMessage("Finished") + sys.exit(0) + return printLog("Invalid input received after maximum attempts.") printMessage("Aborting") @@ -1117,19 +1112,14 @@ def confirm_syslog_restart(syslog_type): """ if PROCESS_ID != -1: question = ("Do you want the Loggly Syslog Configuration Script " - "to restart (SIGHUP) the syslog daemon. [Yes|No]: ") - for _ in range(5): - user_input = usr_input(question).lower() - if len(user_input) > 0: - if user_input in yes: - return restart_syslog_process(syslog_type, PROCESS_ID) - elif user_input in no: - printLog("Configuration file has been modified, " - "please restart %s daemon manually." - % syslog_type) - return False - else: - printLog("Not a valid input. Please retry.") + "to restart (SIGHUP) the syslog daemon.") + result = confirm(question) + if result: + return restart_syslog_process(syslog_type, PROCESS_ID) + elif result is False: + printLog("Configuration file has been modified, " + "please restart %s daemon manually." + % syslog_type) else: printLog("Syslog daemon (%s) is not running. " "Configuration file has been modified," From cd0cc783e304ee8167558cb80e4d050ab5ac668a Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 3 Oct 2013 16:33:55 -0700 Subject: [PATCH 3/4] add 'force' option --- configure-syslog.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/configure-syslog.py b/configure-syslog.py index 09937bf..bc14772 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -690,7 +690,7 @@ def get_installed_syslog_configuration(syslog_id): return { "path": default_directory, "token": auth_token, "source": source } def write_configuration(syslog_name_for_configuration, - authorization_details, user_type): + authorization_details, user_type, force=False): """ Function to create/modify configuration file """ @@ -709,12 +709,12 @@ def write_configuration(syslog_name_for_configuration, # LOGGLY_CONFIG_FILE))) create_loggly_config_file(syslog_id, syslog_configuration_details, - authorization_details, user_type) + authorization_details, user_type, force) return config_file else: modify_syslog_config_file(syslog_id, syslog_configuration_details, - authorization_details, user_type) + authorization_details, user_type, force) return config_file def remove_syslog_ng_source(default_config_file): @@ -924,7 +924,7 @@ def confirm(question): printLog("Not a valid input. Please retry.") def create_loggly_config_file(syslog_id, syslog_configuration_details, - authorization_details, user_type): + authorization_details, user_type, force=False): """ Create Loggly configuration file """ @@ -948,7 +948,7 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details, if os.path.isfile(destfile): msg = ("Loggly configuration file (%s) is already present. " "Do you want to overwrite it?" % destfile) - move = confirm(msg) + move = force or confirm(msg) if move: os.popen("mv -f %s %s" % (file_path, destfile)) return @@ -969,7 +969,7 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details, printLog("IOError %s" % e) def modify_syslog_config_file(syslog_id, syslog_configuration_details, - authorization_details, user_type): + authorization_details, user_type, force=False): """ Modifying configuration file by adding Loggly configuration text """ @@ -987,7 +987,7 @@ def modify_syslog_config_file(syslog_id, syslog_configuration_details, "\n\nDo you want this installer to modify " "the configuration file?" % default_config_file_name.get(syslog_id)) - modify = confirm(question) + modify = force or confirm(question) if modify: backup_file_name = ("%s_%s.bak" % (default_config_file_name.get(syslog_id), @@ -1025,7 +1025,7 @@ def modify_syslog_config_file(syslog_id, syslog_configuration_details, question = ("\nThis configuration currently uses \"%s\" as its Customer Token. " "Do you want to overwrite it?" % syslog_configuration_details.get("token")) - overwrite = confirm(question) + overwrite = force or confirm(question) if overwrite: pattern = (r"s/[a-z0-9]\{8\}\-[a-z0-9]\{4\}\-[a-z0-9]" r"\{4\}\-[a-z0-9]\{4\}\-[a-z0-9]\{12\}/%s/g" @@ -1106,14 +1106,14 @@ def restart_syslog_process(syslog_type, process_id): return False -def confirm_syslog_restart(syslog_type): +def confirm_syslog_restart(syslog_type, force=False): """ Ask the user if it's okay to restart their syslog daemon """ if PROCESS_ID != -1: question = ("Do you want the Loggly Syslog Configuration Script " "to restart (SIGHUP) the syslog daemon.") - result = confirm(question) + result = force or confirm(question) if result: return restart_syslog_process(syslog_type, PROCESS_ID) elif result is False: @@ -1280,11 +1280,11 @@ def install(current_environment): #place it in configuration directory path ($IncludeConfig), #default path for rsyslog will be /etc/rsyslog.d/ modified_config_file = write_configuration(syslog_name_for_configuration, - authorization_details, user_type) + authorization_details, user_type, options.force) selinux_status = check_selinux_service_status(syslog_name_for_configuration) if user_type == ROOT_USER and not selinux_status: # 6. SIGHUP the syslog daemon. - confirm_syslog_restart(syslog_name_for_configuration) + confirm_syslog_restart(syslog_name_for_configuration, options.force) printLog(AUTHTOKEN_MODIFICATION_TEXT % (authorization_details['token'], @@ -1312,7 +1312,8 @@ def uninstall(current_environment): remove_configuration(syslog_name_for_configuration) selinux_status = check_selinux_service_status(syslog_name_for_configuration) if not selinux_status: - confirm_syslog_restart(syslog_name_for_configuration) + force = current_environment['options'].force + confirm_syslog_restart(syslog_name_for_configuration, force) printLog("Uninstall completed") def rsyslog_dryrun(): @@ -1360,7 +1361,9 @@ def dryrun(current_environment): printLog("Dryrun started for syslog version %s" % syslogd) token = getattr(current_environment['options'], 'auth') or 'foofey' - config_file = write_configuration(syslogd, {'token': token, 'id': DISTRIBUTION_ID }, 1) + force = current_environment['options'].force + config_file = write_configuration( + syslogd, {'token': token, 'id': DISTRIBUTION_ID }, 1, force) errors = [] if syslogd == 'rsyslog': @@ -1444,6 +1447,7 @@ def parse_args(self, *args, **kwargs): Option: \tsubdomain Name of loggly account being connected to \tauth Loggly auth token to use for logging +\tforce Skip confirmations -- assume yes '''.lstrip() def parse_options(): @@ -1455,6 +1459,7 @@ def parse_options(): parser.add_posarg("action", dest='action', type="choice", choices=('install', 'uninstall', 'verify', 'sysinfo', 'loggly_help', 'dryrun')) + parser.add_option("-y", "--force", action="store_true") parser.add_option("-s", "--subdomain") parser.add_option("-a", "--auth") (options, args) = parser.parse_args() From 03f4636505f25febd3214f2ca24a4dd07aa58f55 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 3 Oct 2013 17:37:23 -0700 Subject: [PATCH 4/4] call it noconfirm --- configure-syslog.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/configure-syslog.py b/configure-syslog.py index bc14772..710ab00 100755 --- a/configure-syslog.py +++ b/configure-syslog.py @@ -690,7 +690,7 @@ def get_installed_syslog_configuration(syslog_id): return { "path": default_directory, "token": auth_token, "source": source } def write_configuration(syslog_name_for_configuration, - authorization_details, user_type, force=False): + authorization_details, user_type, noconfirm=False): """ Function to create/modify configuration file """ @@ -709,12 +709,12 @@ def write_configuration(syslog_name_for_configuration, # LOGGLY_CONFIG_FILE))) create_loggly_config_file(syslog_id, syslog_configuration_details, - authorization_details, user_type, force) + authorization_details, user_type, noconfirm) return config_file else: modify_syslog_config_file(syslog_id, syslog_configuration_details, - authorization_details, user_type, force) + authorization_details, user_type, noconfirm) return config_file def remove_syslog_ng_source(default_config_file): @@ -924,7 +924,7 @@ def confirm(question): printLog("Not a valid input. Please retry.") def create_loggly_config_file(syslog_id, syslog_configuration_details, - authorization_details, user_type, force=False): + authorization_details, user_type, noconfirm=False): """ Create Loggly configuration file """ @@ -948,7 +948,7 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details, if os.path.isfile(destfile): msg = ("Loggly configuration file (%s) is already present. " "Do you want to overwrite it?" % destfile) - move = force or confirm(msg) + move = noconfirm or confirm(msg) if move: os.popen("mv -f %s %s" % (file_path, destfile)) return @@ -969,7 +969,7 @@ def create_loggly_config_file(syslog_id, syslog_configuration_details, printLog("IOError %s" % e) def modify_syslog_config_file(syslog_id, syslog_configuration_details, - authorization_details, user_type, force=False): + authorization_details, user_type, noconfirm=False): """ Modifying configuration file by adding Loggly configuration text """ @@ -987,7 +987,7 @@ def modify_syslog_config_file(syslog_id, syslog_configuration_details, "\n\nDo you want this installer to modify " "the configuration file?" % default_config_file_name.get(syslog_id)) - modify = force or confirm(question) + modify = noconfirm or confirm(question) if modify: backup_file_name = ("%s_%s.bak" % (default_config_file_name.get(syslog_id), @@ -1025,7 +1025,7 @@ def modify_syslog_config_file(syslog_id, syslog_configuration_details, question = ("\nThis configuration currently uses \"%s\" as its Customer Token. " "Do you want to overwrite it?" % syslog_configuration_details.get("token")) - overwrite = force or confirm(question) + overwrite = noconfirm or confirm(question) if overwrite: pattern = (r"s/[a-z0-9]\{8\}\-[a-z0-9]\{4\}\-[a-z0-9]" r"\{4\}\-[a-z0-9]\{4\}\-[a-z0-9]\{12\}/%s/g" @@ -1106,14 +1106,14 @@ def restart_syslog_process(syslog_type, process_id): return False -def confirm_syslog_restart(syslog_type, force=False): +def confirm_syslog_restart(syslog_type, noconfirm=False): """ Ask the user if it's okay to restart their syslog daemon """ if PROCESS_ID != -1: question = ("Do you want the Loggly Syslog Configuration Script " "to restart (SIGHUP) the syslog daemon.") - result = force or confirm(question) + result = noconfirm or confirm(question) if result: return restart_syslog_process(syslog_type, PROCESS_ID) elif result is False: @@ -1280,11 +1280,11 @@ def install(current_environment): #place it in configuration directory path ($IncludeConfig), #default path for rsyslog will be /etc/rsyslog.d/ modified_config_file = write_configuration(syslog_name_for_configuration, - authorization_details, user_type, options.force) + authorization_details, user_type, options.noconfirm) selinux_status = check_selinux_service_status(syslog_name_for_configuration) if user_type == ROOT_USER and not selinux_status: # 6. SIGHUP the syslog daemon. - confirm_syslog_restart(syslog_name_for_configuration, options.force) + confirm_syslog_restart(syslog_name_for_configuration, options.noconfirm) printLog(AUTHTOKEN_MODIFICATION_TEXT % (authorization_details['token'], @@ -1312,8 +1312,8 @@ def uninstall(current_environment): remove_configuration(syslog_name_for_configuration) selinux_status = check_selinux_service_status(syslog_name_for_configuration) if not selinux_status: - force = current_environment['options'].force - confirm_syslog_restart(syslog_name_for_configuration, force) + noconfirm = current_environment['options'].noconfirm + confirm_syslog_restart(syslog_name_for_configuration, noconfirm) printLog("Uninstall completed") def rsyslog_dryrun(): @@ -1361,9 +1361,9 @@ def dryrun(current_environment): printLog("Dryrun started for syslog version %s" % syslogd) token = getattr(current_environment['options'], 'auth') or 'foofey' - force = current_environment['options'].force + noconfirm = current_environment['options'].noconfirm config_file = write_configuration( - syslogd, {'token': token, 'id': DISTRIBUTION_ID }, 1, force) + syslogd, {'token': token, 'id': DISTRIBUTION_ID }, 1, noconfirm) errors = [] if syslogd == 'rsyslog': @@ -1447,7 +1447,7 @@ def parse_args(self, *args, **kwargs): Option: \tsubdomain Name of loggly account being connected to \tauth Loggly auth token to use for logging -\tforce Skip confirmations -- assume yes +\tyes Skip confirmations -- assume yes '''.lstrip() def parse_options(): @@ -1459,7 +1459,7 @@ def parse_options(): parser.add_posarg("action", dest='action', type="choice", choices=('install', 'uninstall', 'verify', 'sysinfo', 'loggly_help', 'dryrun')) - parser.add_option("-y", "--force", action="store_true") + parser.add_option("-y", "--yes", action="store_true", dest='noconfirm') parser.add_option("-s", "--subdomain") parser.add_option("-a", "--auth") (options, args) = parser.parse_args()