reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[bootmanager.git] / source / setup_bash_history_scripts.sh
1 #!/bin/bash
2
3 BASH_PROFILE=/root/.bash_profile
4 HISTORY_PROFILE=/etc/profile.d/histlog.sh
5 PERIODIC_SCRIPT=/usr/bin/periodic_upload.sh
6
7 cat <<\EOF > $BASH_PROFILE
8 # NOTE: only upload incremental diffs
9 if [ -f /tmp/source/configuration ] ; then
10     source /tmp/source/configuration
11 fi
12 if [ -z "$MONITOR_SERVER" ] ; then
13     MONITOR_SERVER=monitor.planet-lab.org
14 fi
15 function upload_log ()
16 {
17     file=$1
18     path=$2
19     base=$( basename $file )
20     old=/tmp/${base}.old
21     new=/tmp/${base}.new
22     log=/tmp/${base}.log
23     if [ ! -f $file ] ; then
24         return
25     fi
26     if [ -f $new ] ; then
27         cp $new $old
28     else
29         touch $old
30     fi
31     cp $file $new
32     comm -1 -3 $old $new > $log
33     if [ $( stat -c %s $log ) -ne 0 ] ; then
34         curl --max-time 60 --silent --insecure https://$MONITOR_SERVER/monitor/uploadlogs --form "dir=$path" --form "log=@$log"
35         if [ $? -ne 0 ] ; then
36             # the upload has failed, so remove new file so no data is lost
37             rm -f /tmp/$( basename $file ).new
38         fi
39     fi
40 }
41
42 function upload_logs ()
43 {
44     upload_log $1 histfail
45 }
46
47 # NOTE: these aliases aim to upload the history before losing it.
48 alias reboot="upload_logs /root/.bash_eternal_history ; /sbin/reboot"
49 alias shutdown="upload_logs /root/.bash_eternal_history ; /sbin/shutdown"
50 EOF
51
52 cat <<\EOF > $HISTORY_PROFILE
53 export HISTTIMEFORMAT="%s ";
54 # NOTE: HOSTNAME is not reliably set in failboot or safeboot mode
55 # NOTE: These steps assign at least a default hostname based on IP
56 # NOTE: This hostname is used in the bash-prompt-script commands
57 if [[ -z "$HOSTNAME" || "$HOSTNAME" = "(none)" ]] ; then
58     HOSTNAME=`ip addr show dev eth0 | grep inet | tr '/' ' ' | sed -e 's/^ *//g' | cut -f2 -d' '`
59 fi
60 if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0 ] ; then
61     source /etc/sysconfig/network-scripts/ifcfg-eth0 
62     if [ -n "$DHCP_HOSTNAME" ] ; then
63         HOSTNAME=$DHCP_HOSTNAME
64     else 
65         if [ -n "$IPADDR" ] ; then
66             HOSTNAME=$IPADDR
67         fi
68     fi
69 fi
70 hostname $HOSTNAME &> /dev/null
71 if [ -n "$BASH_EXECUTION_STRING" ]; then
72     # NOTE: does not work on 2.x versions of bash.
73     # NOTE: log commands executed over ssh
74     echo "$HOSTNAME $$ ssh:$USER xx `date +%s` $BASH_EXECUTION_STRING" >> /root/.bash_eternal_history;
75 fi
76 if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
77     PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
78 fi
79 EOF
80 chmod 755 $HISTORY_PROFILE
81
82 cat <<\EOF > bash-prompt-script
83 # NOTE: intended to run after and log every interactive-command 
84 echo $HOSTNAME $$ $USER "$(history 1)" >> /root/.bash_eternal_history
85 EOF
86
87 for f in bash-prompt-default bash-prompt-xterm ; do
88     cp bash-prompt-script /etc/sysconfig/$f
89     chmod 755 /etc/sysconfig/$f
90 done
91
92 # NOTE: allow command run directly over ssh to be logged also.
93 echo "source /etc/profile ; source $BASH_PROFILE" > /root/.bashrc
94
95 # NOTE 1: crond is not installed on the boot image, so this maintains a
96 #         persistent process to upload logs on legacy nodes.
97 # NOTE 2: A day has 86400 seconds, $RANDOM is between 0-32767
98 # NOTE 2: So, $RANDOM * 3 is between 0 and 27 hours.
99 # NOTE 3: The initial delay is randomized in case many nodes reboot at the
100 #         same time.
101 initial_delay=$(( $RANDOM * 3 )) 
102
103 cat <<EOF > $PERIODIC_SCRIPT
104 #!/bin/bash
105 if [ -f $BASH_PROFILE ] ; then
106     source $BASH_PROFILE
107 else
108     echo "Cannot source upload_logs() definition!"
109     exit 1
110 fi
111
112 # NOTE: exit if anoter process is already running.
113 if [ \$$ -ne \`pgrep -o periodic\` ] ; then
114     # the current PID differs from the oldest periodic_upload pid
115     exit 0
116 fi
117 sleep $initial_delay
118 while /bin/true ; do
119     upload_logs /root/.bash_eternal_history
120     sleep 86400   # sleep for a day
121 done
122 EOF
123
124 chmod 755 $PERIODIC_SCRIPT
125 $PERIODIC_SCRIPT < /dev/null > /tmp/upload.log 2>&1 &