def wait_for_puppet(currently_running, messages): log_len = 0 twirl = ["-", "\\", "|", "/"] while currently_running: for hostname, finished_logfile in currently_running: log_file = os.path.splitext(os.path.basename(finished_logfile))[0] space_len = basedefs.SPACE_LEN - len(log_file) if len(log_file) > log_len: log_len = len(log_file) if hasattr(sys.stdout, "isatty") and sys.stdout.isatty(): twirl = twirl[-1:] + twirl[:-1] sys.stdout.write(("\rTesting if puppet apply is finished: %s" % log_file).ljust(40 + log_len)) sys.stdout.write("[ %s ]" % twirl[0]) sys.stdout.flush() try: # Once a remote puppet run has finished, we retrieve the log # file and check it for errors local_server = utils.ScriptRunner() log = os.path.join(basedefs.PUPPET_MANIFEST_DIR, os.path.basename(finished_logfile)) log = log.replace(".finished", ".log") local_server.append('scp -o StrictHostKeyChecking=no ' '-o UserKnownHostsFile=/dev/null ' 'root@%s:%s %s' % (hostname, finished_logfile, log)) # To not pollute logs we turn of logging of command execution local_server.execute(log=False) # If we got to this point the puppet apply has finished currently_running.remove((hostname, finished_logfile)) # clean off the last "testing apply" msg if hasattr(sys.stdout, "isatty") and sys.stdout.isatty(): sys.stdout.write(("\r").ljust(45 + log_len)) except ScriptRuntimeError: # the test raises an exception if the file doesn't exist yet # TO-DO: We need to start testing 'e' for unexpected exceptions time.sleep(3) continue # check log file for relevant notices messages.extend(scan_logfile(log)) # check the log file for errors sys.stdout.write('\r') try: validate_logfile(log) state = utils.state_message('%s:' % log_file, 'DONE', 'green') sys.stdout.write('%s\n' % state) sys.stdout.flush() except PuppetError: state = utils.state_message('%s:' % log_file, 'ERROR', 'red') sys.stdout.write('%s\n' % state) sys.stdout.flush() raise
def wait_for_puppet(currently_running, messages): log_len = 0 twirl = ["-", "\\", "|", "/"] while currently_running: for hostname, finished_logfile in currently_running: log_file = os.path.splitext(os.path.basename(finished_logfile))[0] space_len = basedefs.SPACE_LEN - len(log_file) if len(log_file) > log_len: log_len = len(log_file) if hasattr(sys.stdout, "isatty") and sys.stdout.isatty(): twirl = twirl[-1:] + twirl[:-1] sys.stdout.write(("\rTesting if puppet apply is finished: %s" % log_file).ljust(40 + log_len)) sys.stdout.write("[ %s ]" % twirl[0]) sys.stdout.flush() try: # Once a remote puppet run has finished, we retrieve the log # file and check it for errors local_server = utils.ScriptRunner() log = os.path.join(basedefs.PUPPET_MANIFEST_DIR, os.path.basename(finished_logfile)) log = log.replace(".finished", ".log") local_server.append('scp -o StrictHostKeyChecking=no ' '-o UserKnownHostsFile=/dev/null ' 'root@[%s]:%s %s' % (hostname, finished_logfile, log)) # To not pollute logs we turn of logging of command execution local_server.execute(log=False) # If we got to this point the puppet apply has finished currently_running.remove((hostname, finished_logfile)) # clean off the last "testing apply" msg if hasattr(sys.stdout, "isatty") and sys.stdout.isatty(): sys.stdout.write(("\r").ljust(45 + log_len)) except ScriptRuntimeError: # the test raises an exception if the file doesn't exist yet # TO-DO: We need to start testing 'e' for unexpected exceptions time.sleep(3) continue # check log file for relevant notices messages.extend(scan_logfile(log)) # check the log file for errors sys.stdout.write('\r') try: validate_logfile(log) state = utils.state_message('%s:' % log_file, 'DONE', 'green') sys.stdout.write('%s\n' % state) sys.stdout.flush() except PuppetError: state = utils.state_message('%s:' % log_file, 'ERROR', 'red') sys.stdout.write('%s\n' % state) sys.stdout.flush() raise
def test_validate_logfile(self): """Test packstack.modules.validate_logfile""" filename = os.path.join(self.tempdir, "puppet.log") # test valid run with open(filename, "w") as fp: fp.write("Everything went ok") validate_logfile(filename) # test invalid run with open(filename, "w") as fp: fp.write("No matching value for selector param 'Fedora' ...") self.assertRaises(PuppetError, validate_logfile, filename) # test run with error exception with open(filename, "w") as fp: err = ("err: Could not prefetch database_grant provider 'mysql': " "Execution of '/usr/bin/mysql --defaults-file=/root/.my.cnf" " mysql -Be describe user' returned 1: Could not open " "required defaults file: /root/.my.cnf") fp.write(err) validate_logfile(filename) # test surrogate with open(filename, "w") as fp: err = ("err: /Stage[main]/Vswitch::Ovs/Package[openvswitch]/ensure" ": change from absent to present failed: Execution of " "'/usr/bin/yum -d 0 -e 0 -y install openvswitch' returned " "1: Error: Nothing to do") fp.write(err) self.assertRaises(PuppetError, validate_logfile, filename) try: validate_logfile(filename) except PuppetError as ex: ex_msg = str(ex) sr_msg = ("Package openvswitch has not been found in enabled Yum " "repos") assert sr_msg in ex_msg
def test_validate_logfile(self): """Test packstack.modules.validate_logfile.""" filename = os.path.join(self.tempdir, "puppet.log") # test valid run with open(filename, "w") as fp: fp.write("Everything went ok") validate_logfile(filename) # test invalid run with open(filename, "w") as fp: fp.write("No matching value for selector param 'Fedora' ...") self.assertRaises(PuppetError, validate_logfile, filename) # test run with error exception with open(filename, "w") as fp: err = ("err: Could not prefetch database_grant provider 'mysql': " "Execution of '/usr/bin/mysql --defaults-file=/root/.my.cnf" " mysql -Be describe user' returned 1: Could not open " "required defaults file: /root/.my.cnf") fp.write(err) validate_logfile(filename) # test surrogate with open(filename, "w") as fp: err = ("err: /Stage[main]/Vswitch::Ovs/Package[openvswitch]/ensure" ": change from absent to present failed: Execution of " "'/usr/bin/yum -d 0 -e 0 -y install openvswitch' returned " "1: Error: Nothing to do") fp.write(err) self.assertRaises(PuppetError, validate_logfile, filename) try: validate_logfile(filename) except PuppetError as ex: ex_msg = str(ex) sr_msg = ("Package openvswitch has not been found in enabled Yum " "repos") assert sr_msg in ex_msg