|
| 1 | +#! /bin/bash |
| 2 | +# -*- mode: shell-script -*- |
| 3 | + |
| 4 | +# deploy script for pbench background tasks |
| 5 | +# depends on ./config/deploy.conf |
| 6 | +# Dependcency: configtools |
| 7 | + |
| 8 | +while getopts dh x ;do |
| 9 | + case $x in |
| 10 | + d) |
| 11 | + debug=1 |
| 12 | + ;; |
| 13 | + h) |
| 14 | + help=1 |
| 15 | + ;; |
| 16 | + esac |
| 17 | +done |
| 18 | + |
| 19 | +if [[ $help == 1 ]] ;then |
| 20 | + echo "Usage: deploy [-dh]" |
| 21 | + exit 0 |
| 22 | +fi |
| 23 | + |
| 24 | +if [[ $debug == 1 ]] ;then |
| 25 | + rsync="echo /usr/bin/rsync" |
| 26 | + ssh="echo /usr/bin/ssh" |
| 27 | +else |
| 28 | + rsync=/usr/bin/rsync |
| 29 | + ssh=/usr/bin/ssh |
| 30 | +fi |
| 31 | + |
| 32 | +# override for debugging, comment out for production |
| 33 | + |
| 34 | + |
| 35 | +opts=$SHELLOPTS |
| 36 | +case $opts in |
| 37 | + *xtrace*) |
| 38 | + dir=$(dirname $(which $0)) |
| 39 | + PROG=$(basename $(which $0)) |
| 40 | + ;; |
| 41 | + *) |
| 42 | + dir=$(dirname $0) |
| 43 | + PROG=$(basename $0) |
| 44 | + ;; |
| 45 | +esac |
| 46 | + |
| 47 | +export CONFIG=$dir/config/deploy.conf |
| 48 | + |
| 49 | +if [ ! -f $CONFIG ] ;then |
| 50 | + echo "$prog: required config file $CONFIG does not exist" > /dev/stdout |
| 51 | + exit 1 |
| 52 | +fi |
| 53 | + |
| 54 | +PATH=/opt/configtools/bin:$PATH |
| 55 | + |
| 56 | +if which getconf.py > /dev/null 2>&1 ;then |
| 57 | + : |
| 58 | +else |
| 59 | + echo "The configtools package must be installed." |
| 60 | + exit 2 |
| 61 | +fi |
| 62 | + |
| 63 | +TMP=/tmp/deploy-bgtasks.$$ |
| 64 | +mkdir -p $TMP |
| 65 | +trap "rm -rf $TMP" EXIT INT QUIT |
| 66 | + |
| 67 | +function do_scripts { |
| 68 | + role=$1 |
| 69 | + user=$2 |
| 70 | + host=$3 |
| 71 | + shift 3 |
| 72 | + scripts=$* |
| 73 | + |
| 74 | + scriptdir=$dir/$(getconf.py script-dir deploy) |
| 75 | + files="" |
| 76 | + for script in $scripts ;do |
| 77 | + if [ -f $scriptdir/$script ] ;then |
| 78 | + files="$files $scriptdir/$script" |
| 79 | + fi |
| 80 | + done |
| 81 | + $rsync -avz $files $user@$host:$(getconf.py deploy-script-dir $role) |
| 82 | +} |
| 83 | + |
| 84 | +# refactor |
| 85 | +function do_libs { |
| 86 | + role=$1 |
| 87 | + user=$2 |
| 88 | + host=$3 |
| 89 | + shift 3 |
| 90 | + libs=$* |
| 91 | + |
| 92 | + libdir=$dir/$(getconf.py lib-dir deploy) |
| 93 | + files="" |
| 94 | + for lib in $libs ;do |
| 95 | + if [ -e $libdir/$lib ] ;then |
| 96 | + files="$files $libdir/$lib" |
| 97 | + fi |
| 98 | + done |
| 99 | + $rsync -avz $files $user@$host:$(getconf.py deploy-lib-dir $host deploy) |
| 100 | +} |
| 101 | + |
| 102 | +function do_crontab { |
| 103 | + role=$1 |
| 104 | + user=$2 |
| 105 | + host=$3 |
| 106 | + crontabs=$4 |
| 107 | + |
| 108 | + # make sure the directory exists |
| 109 | + $ssh $user@$host mkdir -p $(getconf.py deploy-crontab-dir $role deploy) |
| 110 | + # copy the file |
| 111 | + $rsync -avz $crontabs $user@$host:$(getconf.py deploy-crontab-dir $host deploy)/crontab |
| 112 | + # refresh cron |
| 113 | + $ssh $user@$host crontab $(getconf.py deploy-crontab-dir $role deploy)/crontab |
| 114 | +} |
| 115 | + |
| 116 | +function deploy_host { |
| 117 | + role=$1 |
| 118 | + host=$(getconf.py host $role) |
| 119 | + |
| 120 | + user=$(getconf.py user $role deploy) |
| 121 | + tasks=$(getconf.py -l tasks $role) |
| 122 | + |
| 123 | + hostscripts="" |
| 124 | + if [ ! -z "$tasks" ] ;then |
| 125 | + # general scripts |
| 126 | + hostscripts=$(getconf.py -l scripts deploy) |
| 127 | + hostscripts="$hostscripts $(getconf.py -l scripts $role)" |
| 128 | + fi |
| 129 | + # scripts and libs are concatenated and passed as arguments |
| 130 | + scripts="$hostscripts" |
| 131 | + libs="" |
| 132 | + # Don't pass crontab entries as arguments: newlines are mangled. |
| 133 | + # Use a file instead. |
| 134 | + crontabs=$TMP/crontab.$host |
| 135 | + # make sure the file starts out empty. |
| 136 | + > $crontabs |
| 137 | + for task in $tasks ;do |
| 138 | + # avoid introducing unnecessary spaces: the -z below won't work. |
| 139 | + if [ -z "$scripts" ] ;then |
| 140 | + scripts="$(getconf.py -l scripts $task)" |
| 141 | + else |
| 142 | + scripts="$scripts $(getconf.py -l scripts $task)" |
| 143 | + fi |
| 144 | + |
| 145 | + if [ -z "$libs" ] ;then |
| 146 | + libs="$(getconf.py -l libs $task)" |
| 147 | + else |
| 148 | + libs="$libs $(getconf.py -l libs $task)" |
| 149 | + fi |
| 150 | + |
| 151 | + getconf.py -l crontab $task >> $crontabs |
| 152 | + done |
| 153 | + |
| 154 | + if [ ! -z "$scripts" ] ;then |
| 155 | + do_scripts $role $user $host $scripts |
| 156 | + fi |
| 157 | + if [ ! -z "$libs" ] ;then |
| 158 | + do_libs $role $user $host $libs |
| 159 | + fi |
| 160 | + if [ -s "$crontabs" ] ;then |
| 161 | + do_crontab $role $user $host "$crontabs" |
| 162 | + fi |
| 163 | +} |
| 164 | + |
| 165 | +roles=$(getconf.py -l roles deploy) |
| 166 | +for role in $roles ;do |
| 167 | + deploy_host $role |
| 168 | +done |
| 169 | + |
| 170 | +exit 0 |
0 commit comments