Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / sudo-logs.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> sudo-logs.sh"
12
13 set -eu -o pipefail -o noglob
14
15 # Copy/Generate 'sudo' log and copy to archive directory
16 copy_log () {
17     case $os in
18         fedora|centos|redhat|ubuntu|debian)
19             if ! sudo cp "$sudo_log" /tmp; then
20                 echo "Unable to archive 'sudo' logs ($sudo_log)"
21                 return
22             fi
23             ;;
24         suse)
25             # Do I need 'sudo' to run 'journalctl'?
26             journalctl | grep sudo > "$sudo_log"
27             ;;
28         *)  echo "Unexpected 'operatingsystem': $os"
29             exit 1
30             ;;
31     esac
32     sudo_log=$(basename "$sudo_log")
33     sudo chown "$(id -nu)": "/tmp/$sudo_log"
34     chmod 0644 "/tmp/$sudo_log"
35     mkdir -p "$WORKSPACE/archives/sudo"
36     mv "/tmp/$sudo_log" "$WORKSPACE/archives/sudo/$sudo_log"
37
38 }    # End copy_log()
39
40 echo "Archiving 'sudo' log.."
41 os=$(facter operatingsystem | tr '[:upper:]' '[:lower:]')
42 case $os in
43     fedora|centos|redhat) sudo_log=/var/log/secure   ;;
44     ubuntu|debian)        sudo_log=/var/log/auth.log ;;
45     suse)                 sudo_log=/tmp/sudo.log     ;;
46     *)  echo "Unexpected 'operatingsystem': $os"
47         exit 1
48         ;;
49 esac
50
51 copy_log