|
| 1 | +require 'date' |
| 2 | + |
| 3 | +def file_glob(host, path) |
| 4 | + result = on(host, "ls #{path}", :acceptable_exit_codes => [0, 2]) |
| 5 | + return [] if result.exit_code != 0 |
| 6 | + return result.stdout.strip.split("\n") |
| 7 | +end |
| 8 | + |
| 9 | +# This test is prefixed with zzz so it will hopefully run last. |
| 10 | +test_name 'Backup puppet logs and app data on all hosts' do |
| 11 | + today = Date.today().to_s |
| 12 | + # truncate the job name so it only has the name-y part and no parameters |
| 13 | + job_name = (ENV['JOB_NAME'] || 'unknown_jenkins_job') |
| 14 | + .sub(/[A-Z0-9_]+=.*$/, '') |
| 15 | + .gsub(/[\/,.]/, '_')[0..200] |
| 16 | + archive_name = "#{job_name}__#{ENV['BUILD_ID']}__#{today}__sut-files.tgz" |
| 17 | + archive_root = "SUT_#{today}" |
| 18 | + |
| 19 | + hosts.each do |host| |
| 20 | + step("Capturing log errors for #{host}") do |
| 21 | + case host[:platform] |
| 22 | + when /windows/ |
| 23 | + # on Windows, all of the desired data (including logs) is in the data dir |
| 24 | + puppetlabs_data = 'C:/ProgramData/PuppetLabs' |
| 25 | + archive_file_from(host, puppetlabs_data, {}, archive_root, archive_name) |
| 26 | + |
| 27 | + # Note: Windows `ls` uses absolute paths for all matches when an absolute path is supplied. |
| 28 | + tempdir = 'C:/Windows/TEMP' |
| 29 | + file_glob(host, File.join(tempdir, 'install-puppet-*.log')).each do |install_log| |
| 30 | + archive_file_from(host, install_log, {}, archive_root, archive_name) |
| 31 | + end |
| 32 | + file_glob(host, File.join(tempdir, 'puppet-*-installer.log')).each do |install_log| |
| 33 | + archive_file_from(host, install_log, {}, archive_root, archive_name) |
| 34 | + end |
| 35 | + else |
| 36 | + puppetlabs_logdir = '/var/log/puppetlabs' |
| 37 | + grep_for_alerts = if host[:platform] =~ /solaris/ |
| 38 | + "egrep -i 'warn|error|fatal'" |
| 39 | + elsif host[:platform] =~ /aix/ |
| 40 | + "grep -iE -B5 -A10 'warn|error|fatal'" |
| 41 | + else |
| 42 | + "grep -i -B5 -A10 'warn\\|error\\|fatal'" |
| 43 | + end |
| 44 | + |
| 45 | + ## If there are any PL logs, try to echo all warning, error, and fatal |
| 46 | + ## messages from all PL logs to the job's output |
| 47 | + on(host, <<-GREP_FOR_ALERTS, :accept_all_exit_codes => true ) |
| 48 | + if [ -d #{puppetlabs_logdir} ] && [ -n "$(find #{puppetlabs_logdir} -name '*.log*')" ]; then |
| 49 | + for log in $(find #{puppetlabs_logdir} -name '*.log*'); do |
| 50 | + # grep /dev/null only to get grep to print filenames, since -H is not in POSIX spec for grep |
| 51 | + #{grep_for_alerts} $log /dev/null; |
| 52 | + echo "" |
| 53 | + done |
| 54 | + fi |
| 55 | + GREP_FOR_ALERTS |
| 56 | + |
| 57 | + step("Archiving logs for #{host} into #{archive_name} (muzzling everything but :warn or higher beaker logs...)") do |
| 58 | + ## turn the logger off to avoid getting hundreds of lines of scp progress output |
| 59 | + previous_level = @logger.log_level |
| 60 | + @logger.log_level = :warn |
| 61 | + |
| 62 | + pxp_cache = '/opt/puppetlabs/pxp-agent/spool' |
| 63 | + puppetlabs_data = '/etc/puppetlabs' |
| 64 | + |
| 65 | + version_lookup_result = on(host, "cat /opt/puppetlabs/puppet/VERSION", :accept_all_exit_codes => true) |
| 66 | + |
| 67 | + # If we can't find a VERSION file, chances are puppet wasn't |
| 68 | + # installed and these paths aren't present. Beaker's |
| 69 | + # archive_file_from() will fail if it can't find the file, and we |
| 70 | + # want to proceed... |
| 71 | + if version_lookup_result.exit_code == 0 |
| 72 | + agent_version = version_lookup_result.output.strip |
| 73 | + archive_file_from(host, pxp_cache, {}, archive_root, archive_name) unless version_is_less(agent_version, "1.3.2") |
| 74 | + archive_file_from(host, puppetlabs_data, {}, archive_root, archive_name) |
| 75 | + archive_file_from(host, puppetlabs_logdir, {}, archive_root, archive_name) |
| 76 | + end |
| 77 | + |
| 78 | + syslog_dir = '/var/log' |
| 79 | + syslog_name = 'messages' |
| 80 | + if host[:platform] =~ /ubuntu|debian/ |
| 81 | + syslog_name = 'syslog' |
| 82 | + elsif host[:platform] =~ /solaris/ |
| 83 | + syslog_dir = '/var/adm' |
| 84 | + # Next few lines are for debugging POOLER-200, once that is resolved this can be removed |
| 85 | + @logger.log_level = previous_level |
| 86 | + on(host, 'egrep -i \'reboot after panic\' /var/adm/messages', :acceptable_exit_codes => [0,1,2]) |
| 87 | + @logger.log_level = :warn |
| 88 | + elsif host[:platform] =~ /osx/ |
| 89 | + syslog_name = "system.log" |
| 90 | + elsif host[:platform] =~ /fedora/ |
| 91 | + on(host, "journalctl --no-pager > /var/log/messages") |
| 92 | + elsif host[:platform] =~ /aix/ |
| 93 | + on(host, "alog -o -t console > /var/log/messages") |
| 94 | + end |
| 95 | + |
| 96 | + syslog_path = File.join(syslog_dir, syslog_name) |
| 97 | + if host.file_exist?(syslog_path) |
| 98 | + archive_file_from(host, syslog_path, {}, archive_root, archive_name) |
| 99 | + end |
| 100 | + |
| 101 | + ## turn the logger back on in case someone else wants to log things |
| 102 | + @logger.log_level = previous_level |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments