| 1 | #! /bin/bash |
|---|
| 2 | # |
|---|
| 3 | # This file provides shell libraries to our configuration management system. |
|---|
| 4 | # It will be sourced by the relevant Rescomp scripts. |
|---|
| 5 | # |
|---|
| 6 | # Author: Chris Cowart <ccowart@rescomp.berkeley.edu> |
|---|
| 7 | # Date: 30 March 2006 |
|---|
| 8 | # |
|---|
| 9 | # $Id$ |
|---|
| 10 | |
|---|
| 11 | REPO_URI="${REPO_PROTOCOL}${REPO_HOSTNAME}${REPO_PATH}" |
|---|
| 12 | |
|---|
| 13 | # Checks out the conf tree if we don't already have it. Updates it if we do. |
|---|
| 14 | function conf_checkout_tree { |
|---|
| 15 | if [ ! -d ${WORK_PATH} ] ; then |
|---|
| 16 | local path=`echo ${WORK_PATH} | sed -E 's:/[^/]+$::'` |
|---|
| 17 | mkdir -p ${WORK_PATH} |
|---|
| 18 | |
|---|
| 19 | # This makes sure nobody can enter your working copy. We relax the |
|---|
| 20 | # permissions during the rollout, and restrict them again at the end. |
|---|
| 21 | chmod 700 ${WORK_PATH} |
|---|
| 22 | |
|---|
| 23 | svn checkout ${REPO_URI} ${WORK_PATH} |
|---|
| 24 | rm -rf ${path}/.svn |
|---|
| 25 | else |
|---|
| 26 | svn update ${WORK_PATH} |
|---|
| 27 | fi |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | # I update the working copy of a given module to the repository's copy. |
|---|
| 31 | function conf_update_module { |
|---|
| 32 | local module="$1" |
|---|
| 33 | svn update ${WORK_PATH}/${module} |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | # Updates the whole source tree |
|---|
| 37 | function conf_update_tree { |
|---|
| 38 | svn update ${WORK_PATH} |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | # Revert a working file |
|---|
| 42 | function conf_revert { |
|---|
| 43 | svn revert $* |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | # Assumes that we already have core setup in our work path. |
|---|
| 47 | function conf_create_module { |
|---|
| 48 | local module=$1 |
|---|
| 49 | svn mkdir ${WORK_PATH}/$module |
|---|
| 50 | svn mkdir ${WORK_PATH}/${REPO_CHECKPTS}/$module |
|---|
| 51 | svn commit ${WORK_PATH}/${module} ${WORK_PATH}/${REPO_CHECKPTS}/$module -m \ |
|---|
| 52 | "Created directory structure for ${module} --`whoami`" |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | # Commits module $1 with message $2 |
|---|
| 56 | function conf_commit { |
|---|
| 57 | local module=$1 |
|---|
| 58 | local msg="$2" |
|---|
| 59 | svn commit -F "$msg" ${WORK_PATH}/$module |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | # A way to utilize the svn status feature. |
|---|
| 63 | function conf_status { |
|---|
| 64 | svn status $* |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | # Roll out the specified module, optionally at the specified checkpoint |
|---|
| 68 | # eg: conf_rollout MODULE [checkpoint] |
|---|
| 69 | function conf_rollout { |
|---|
| 70 | local module="$1" |
|---|
| 71 | if [ -z $2 ] ; then |
|---|
| 72 | local moduledir="${WORK_PATH}/$module" |
|---|
| 73 | else |
|---|
| 74 | local moduledir="${WORK_PATH}/checkpoints/${module}/${2}" |
|---|
| 75 | fi |
|---|
| 76 | # This is a hack to work around NFS home dirs, for now: |
|---|
| 77 | chmod o+rx ${WORK_PATH} |
|---|
| 78 | |
|---|
| 79 | for directory in `find $moduledir -type d | grep -v "\.svn"` ; do |
|---|
| 80 | local livedir=`echo $directory | sed "s:$moduledir::"` |
|---|
| 81 | livedir="${LIVE_ROOT}${livedir}" |
|---|
| 82 | if [ ! -d $livedir ] ; then |
|---|
| 83 | local owner=`conf_get_prop ${directory} owner` |
|---|
| 84 | local group=`conf_get_prop ${directory} group` |
|---|
| 85 | local mode=`conf_get_prop ${directory} mode` |
|---|
| 86 | local opts="-d -o $owner -g $group -m $mode" |
|---|
| 87 | local cmd="sudo install $opts $livedir" |
|---|
| 88 | echo $cmd |
|---|
| 89 | $cmd |
|---|
| 90 | fi |
|---|
| 91 | done |
|---|
| 92 | for file in `find $moduledir -type f | grep -v "\.svn"` ; do |
|---|
| 93 | local livefile=`echo "$file" | sed "s:$moduledir::"` |
|---|
| 94 | local owner=`conf_get_prop ${file} owner` |
|---|
| 95 | local group=`conf_get_prop ${file} group` |
|---|
| 96 | local mode=`conf_get_prop ${file} mode` |
|---|
| 97 | local opts="-Sp -o $owner -g $group -m $mode" |
|---|
| 98 | local cmd="sudo install $opts $file ${LIVE_ROOT}$livefile" |
|---|
| 99 | echo $cmd |
|---|
| 100 | $cmd |
|---|
| 101 | done |
|---|
| 102 | # This is a hack to work around NFS home dirs, for now: |
|---|
| 103 | chmod o-rx ${WORK_PATH} |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | function conf_install { |
|---|
| 107 | local module="$1" |
|---|
| 108 | shift |
|---|
| 109 | local file=`abspath $1` |
|---|
| 110 | shift |
|---|
| 111 | local moduledir="${WORK_PATH}/$module" |
|---|
| 112 | local livefile=`echo "$file" | sed -E "s:${WORK_PATH}/[^/]+::"` |
|---|
| 113 | local pathmodule=`echo "$file" | sed -E "s:${WORK_PATH}/([^/]+).*:\1:"` |
|---|
| 114 | local suffix |
|---|
| 115 | livefile=`echo "$livefile" | sed -E "s:-${pathmodule}$::"` |
|---|
| 116 | if [[ $SINGULARITIES =~ $livefile ]] ; then |
|---|
| 117 | suffix="-${module}" |
|---|
| 118 | fi |
|---|
| 119 | livefile="${livefile}${suffix}" |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | # See if it even exists |
|---|
| 123 | file="${moduledir}${livefile}" |
|---|
| 124 | if [ ! -f $file ] ; then |
|---|
| 125 | return 0 |
|---|
| 126 | fi |
|---|
| 127 | |
|---|
| 128 | # If we got here, figure it out |
|---|
| 129 | local owner=`conf_get_prop ${file} owner` |
|---|
| 130 | local group=`conf_get_prop ${file} group` |
|---|
| 131 | local mode=`conf_get_prop ${file} mode` |
|---|
| 132 | local opts="-Sp -o $owner -g $group -m $mode" |
|---|
| 133 | local cmd="sudo install $opts $file ${LIVE_ROOT}$livefile" |
|---|
| 134 | |
|---|
| 135 | chmod o+rx ${WORK_PATH} |
|---|
| 136 | echo $cmd |
|---|
| 137 | $cmd |
|---|
| 138 | chmod o-rx ${WORK_PATH} |
|---|
| 139 | |
|---|
| 140 | if [ ! -z $1 ] ; then |
|---|
| 141 | conf_install $module $* |
|---|
| 142 | fi |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | function conf_list { |
|---|
| 147 | local file=$1 |
|---|
| 148 | local module=`echo ${file#$WORK_PATH} | sed -E 's:/([^/]+)/.*:\1:'` |
|---|
| 149 | local livefile=`echo ${file#$WORK_PATH} | sed -E 's:/([^/]+)/:/:'` |
|---|
| 150 | local owner=`conf_get_prop ${file} owner` |
|---|
| 151 | local group=`conf_get_prop ${file} group` |
|---|
| 152 | local mode=`conf_get_prop ${file} mode` |
|---|
| 153 | local comment=`conf_get_prop ${file} comment` |
|---|
| 154 | echo -e "$mode\t$owner\t$group\t$comment\t\t$livefile" |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | # This function assembles the singularities: |
|---|
| 160 | function conf_assemble_sing { |
|---|
| 161 | local file=$1 |
|---|
| 162 | local livefile="${LIVE_ROOT}${file}" |
|---|
| 163 | local tmpfile=`mktemp -t confman` || exit 1 |
|---|
| 164 | local owner group mode flag livepart msg |
|---|
| 165 | for layer in $LAYERS ; do |
|---|
| 166 | livepart="${LIVE_ROOT}${file}-${layer}" |
|---|
| 167 | myfile="${WORK_PATH}/${layer}/${file}-${layer}" |
|---|
| 168 | if [ -f $myfile -a -f $livepart ] ; then |
|---|
| 169 | owner=`conf_get_prop ${myfile} owner` |
|---|
| 170 | group=`conf_get_prop ${myfile} group` |
|---|
| 171 | mode=`conf_get_prop ${myfile} mode` |
|---|
| 172 | comment=`conf_get_prop ${myfile} comment` |
|---|
| 173 | cat $livepart >> $tmpfile |
|---|
| 174 | sudo rm $livepart |
|---|
| 175 | flag=1 |
|---|
| 176 | fi |
|---|
| 177 | done |
|---|
| 178 | if [ ! -z $flag ] ; then |
|---|
| 179 | local opts="-o $owner -g $group -m $mode" |
|---|
| 180 | local cmd="sudo install $opts $tmpfile $livefile" |
|---|
| 181 | echo $cmd |
|---|
| 182 | $cmd |
|---|
| 183 | fi |
|---|
| 184 | rm -f $tmpfile |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | # This function generates a checkpoint called NAME for the given MODULE. |
|---|
| 188 | # eg: conf_new_checkpoint MODULE NAME |
|---|
| 189 | function conf_new_checkpoint { |
|---|
| 190 | local module=$1 |
|---|
| 191 | local checkpoint=$2 |
|---|
| 192 | local chkpath="${WORK_PATH}/${REPO_CHECKPTS}/${module}/${checkpoint}" |
|---|
| 193 | local revision=`svn info ${WORK_PATH} | awk '/Last Changed Rev:/ {print $4}'` |
|---|
| 194 | echo $revision > $chkpath |
|---|
| 195 | svn add $chkpath |
|---|
| 196 | local msg="Created a checkpoint, ${checkpoint} for ${module} --`whoami`" |
|---|
| 197 | svn commit ${WORK_PATH}/${REPO_CHECKPTS} -m "$msg" |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | # This function will remove the checkpoint NAME from MODULE. |
|---|
| 201 | # eg: conf_rm_checkpoint MODULE NAME |
|---|
| 202 | function conf_rm_checkpoint { |
|---|
| 203 | local module=$1 |
|---|
| 204 | local checkpoint=$2 |
|---|
| 205 | local chkpath="${WORK_PATH}/${REPO_CHECKPTS}/${module}/${checkpoint}" |
|---|
| 206 | svn rm ${chkpath} |
|---|
| 207 | local msg="Removed the checkpoint ${checkpoint} from ${module} --`whoami`" |
|---|
| 208 | svn commit ${WORK_PATH}/${REPO_CHECKPTS} -m "$msg" |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | # This function will restore the state of your module's working copy to the |
|---|
| 212 | # named checkpoint. eg: conf_rollback MODULE NAME |
|---|
| 213 | function conf_rollback { |
|---|
| 214 | local module=$1 |
|---|
| 215 | local checkpoint=$2 |
|---|
| 216 | local clock=$3 |
|---|
| 217 | local modpath="${WORK_PATH}/${module}" |
|---|
| 218 | local chkpath="${WORK_PATH}/${REPO_CHECKPTS}/${module}/${checkpoint}" |
|---|
| 219 | local revision |
|---|
| 220 | local date=`echo $checkpoint | sed -E 's:(....)(..)(..):\1-\2-\3:'` |
|---|
| 221 | |
|---|
| 222 | # Named checkpoint |
|---|
| 223 | if [ -f "${chkpath}" ] ; then |
|---|
| 224 | revision=`cat $chkpath` |
|---|
| 225 | elif [ -z $clock ] ; then # Time checkpoint |
|---|
| 226 | revision="{${date}}" |
|---|
| 227 | else |
|---|
| 228 | clock=`echo $clock | sed -E 's#(..)(..)#\1:\2#'` |
|---|
| 229 | revision="{${date}T${clock}}" |
|---|
| 230 | fi |
|---|
| 231 | |
|---|
| 232 | svn update --revision $revision $modpath |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | # This function will print the value of the specified property to STDOUT |
|---|
| 236 | function conf_get_prop { |
|---|
| 237 | local file=$1 |
|---|
| 238 | local prop=$2 |
|---|
| 239 | svn propget "confman:${prop}" ${file} |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | # This function will set the value of the specified property. |
|---|
| 243 | function conf_set_prop { |
|---|
| 244 | local file=$1 |
|---|
| 245 | local prop=$2 |
|---|
| 246 | local value=$3 |
|---|
| 247 | svn propset "confman:${prop}" "${value}" ${file} |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | function conf_gen_file { |
|---|
| 251 | local module=$1 |
|---|
| 252 | local file=$2 |
|---|
| 253 | local owner=$3 |
|---|
| 254 | local group=$4 |
|---|
| 255 | local mode=$5 |
|---|
| 256 | local comment="$6" |
|---|
| 257 | local usefile=$7 |
|---|
| 258 | local myfile="${WORK_PATH}/${module}${file}" |
|---|
| 259 | local warning="${comment} ${CONF_WARNING}" |
|---|
| 260 | local morewarn="${comment} Managed under ${module} module." |
|---|
| 261 | local revision="${comment} \$Id\$" |
|---|
| 262 | echo $file |
|---|
| 263 | |
|---|
| 264 | if [ -z $usefile ] ; then |
|---|
| 265 | echo -e "${warning}\n${morewarn}\n${revision}\n" > $myfile |
|---|
| 266 | elif head -n 1 ${usefile} | grep '^#!' >/dev/null ; then |
|---|
| 267 | awk "{if (NR==2) |
|---|
| 268 | print \"\\n${warning}\\n${morewarn}\\n${revision}\\n\"\$0; |
|---|
| 269 | else print \$0 }" ${usefile} > $myfile |
|---|
| 270 | else |
|---|
| 271 | echo -e "${warning}\n${morewarn}\n${revision}\n" > $myfile |
|---|
| 272 | cat ${usefile} >> $myfile |
|---|
| 273 | fi |
|---|
| 274 | |
|---|
| 275 | svn add $myfile |
|---|
| 276 | svn ps svn:keywords "Id" $myfile |
|---|
| 277 | conf_set_prop $myfile owner $owner |
|---|
| 278 | conf_set_prop $myfile group $group |
|---|
| 279 | conf_set_prop $myfile mode $mode |
|---|
| 280 | conf_set_prop $myfile comment "$comment" |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | function conf_rm_file { |
|---|
| 284 | svn rm $* |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | function conf_mkdir { |
|---|
| 288 | local directory=$1 |
|---|
| 289 | local owner=$2 |
|---|
| 290 | local group=$3 |
|---|
| 291 | local mode=$4 |
|---|
| 292 | local workdir="/" |
|---|
| 293 | local directories=`echo "$directory" | sed 's:/: :g'` |
|---|
| 294 | local dir |
|---|
| 295 | |
|---|
| 296 | for dir in $directories ; do |
|---|
| 297 | workdir="${workdir}/${dir}" |
|---|
| 298 | if [ ! -d ${workdir} ] ; then |
|---|
| 299 | echo svn mkdir ${workdir} |
|---|
| 300 | svn mkdir ${workdir} |
|---|
| 301 | conf_set_prop $workdir owner $owner |
|---|
| 302 | conf_set_prop $workdir group $group |
|---|
| 303 | conf_set_prop $workdir mode $mode |
|---|
| 304 | conf_set_prop $workdir comment "dir" |
|---|
| 305 | fi |
|---|
| 306 | done |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | function conf_rmmod { |
|---|
| 310 | local module=$1 |
|---|
| 311 | svn rm ${WORK_PATH}/${module} |
|---|
| 312 | svn rm ${WORK_PATH}/${REPO_CHECKPTS}/${module} |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | function conf_mv { |
|---|
| 316 | local oldname=$1 |
|---|
| 317 | local newname=$2 |
|---|
| 318 | svn mv $oldname $newname |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | function conf_cp { |
|---|
| 322 | local oldname=$1 |
|---|
| 323 | local newname=$2 |
|---|
| 324 | svn cp $oldname $newname |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | function conf_diff { |
|---|
| 328 | svn diff $* |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | function conf_log { |
|---|
| 332 | svn log $* |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | function conf_cleanexit { |
|---|
| 336 | echo "Abort, Abort! Patience is a virtue." >&2 |
|---|
| 337 | rm -f /tmp/confman* |
|---|
| 338 | |
|---|
| 339 | # And in case we got an interrupt during a rollout, we still want the |
|---|
| 340 | # permissions here to be in a consistent state. |
|---|
| 341 | chmod 700 ${WORK_PATH} |
|---|
| 342 | |
|---|
| 343 | # And this clears any locks we may have created on our working copy: |
|---|
| 344 | svn cleanup ${WORK_PATH} |
|---|
| 345 | echo "All clean. Terminating." >&2 |
|---|
| 346 | |
|---|
| 347 | if [ -z $1 ] ; then |
|---|
| 348 | exit 1 |
|---|
| 349 | else |
|---|
| 350 | exit $1 |
|---|
| 351 | fi |
|---|
| 352 | |
|---|
| 353 | } |
|---|
| 354 | |
|---|