| 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: confmanlib.sh 1306 2008-01-17 03:03:57Z mattea $ |
|---|
| 10 | |
|---|
| 11 | REPO_URI="${REPO_PROTOCOL}${REPO_HOSTNAME}${REPO_PATH}" |
|---|
| 12 | REPO_ACTION="${REPO_DB}/last_action" |
|---|
| 13 | REPO_SYNC_STATE="${REPO_DB}/state" |
|---|
| 14 | |
|---|
| 15 | # Checks out the conf tree if we don't already have it. Updates it if we do. |
|---|
| 16 | function conf_checkout_tree { |
|---|
| 17 | if [ ! -d ${WORK_PATH}/.svn ] ; then |
|---|
| 18 | local path=`echo ${WORK_PATH} | ${sed_cmd} 's:/[^/]+$::'` |
|---|
| 19 | mkdir -p ${WORK_PATH} |
|---|
| 20 | |
|---|
| 21 | # This makes sure nobody can enter your working copy. We relax the |
|---|
| 22 | # permissions during the rollout, and restrict them again at |
|---|
| 23 | # the end. |
|---|
| 24 | chmod 700 ${WORK_PATH} |
|---|
| 25 | |
|---|
| 26 | svn checkout ${REPO_URI} ${WORK_PATH} |
|---|
| 27 | else |
|---|
| 28 | svn update ${WORK_PATH} |
|---|
| 29 | fi |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | # I update the working copy of a given module to the repository's copy. |
|---|
| 33 | function conf_update_module { |
|---|
| 34 | local module="$1" |
|---|
| 35 | svn update ${WORK_PATH}/${module} |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | # Updates the whole source tree |
|---|
| 39 | function conf_update_tree { |
|---|
| 40 | svn update ${WORK_PATH} |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | function conf_setup_lock { |
|---|
| 44 | if [ -f ${WORK_PATH}/confman.lock ] |
|---|
| 45 | then |
|---|
| 46 | return 1 |
|---|
| 47 | else |
|---|
| 48 | touch ${WORK_PATH}/confman.lock |
|---|
| 49 | echo $MY_PID > "${WORK_PATH}/confman.lock" #Send PID to the lock to allow for recursive calls |
|---|
| 50 | return 0 |
|---|
| 51 | fi |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | function conf_lock_wcopy { |
|---|
| 55 | if [ ! -d ${WORK_PATH}/.svn ] |
|---|
| 56 | then |
|---|
| 57 | echo "${WORK_PATH} is not a valid svn working copy or does not exist." >&4 |
|---|
| 58 | exit 1 |
|---|
| 59 | fi |
|---|
| 60 | if [ -f ${WORK_PATH}/confman.lock ] |
|---|
| 61 | then |
|---|
| 62 | return 1 |
|---|
| 63 | else |
|---|
| 64 | touch ${WORK_PATH}/confman.lock |
|---|
| 65 | echo $MY_PID > "${WORK_PATH}/confman.lock" #Send PID to the lock to allow for recursive calls |
|---|
| 66 | return 0 |
|---|
| 67 | fi |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | function conf_unlock_wcopy { |
|---|
| 71 | if [ -f ${WORK_PATH}/confman.lock ] |
|---|
| 72 | then |
|---|
| 73 | rm ${WORK_PATH}/confman.lock |
|---|
| 74 | fi |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | function conf_lock_system { |
|---|
| 78 | if [ -f /var/run/confman.lock ] |
|---|
| 79 | then |
|---|
| 80 | return 1 |
|---|
| 81 | else |
|---|
| 82 | sudo touch /var/run/confman.lock |
|---|
| 83 | sudo chmod 666 /var/run/confman.lock |
|---|
| 84 | sudo echo $MY_PID > "/var/run/confman.lock" |
|---|
| 85 | return 0 |
|---|
| 86 | fi |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | function conf_unlock_system { |
|---|
| 90 | if [ -f /var/run/confman.lock ] |
|---|
| 91 | then |
|---|
| 92 | sudo rm /var/run/confman.lock |
|---|
| 93 | fi |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | function conf_wcopy_locked? { |
|---|
| 97 | if [ -f ${WORK_PATH}/confman.lock ] |
|---|
| 98 | then |
|---|
| 99 | WCOPY_LOCK_PID=`cat ${WORK_PATH}/confman.lock` #gets the PID of the confman instance that created the lock |
|---|
| 100 | else |
|---|
| 101 | return 1 |
|---|
| 102 | fi |
|---|
| 103 | if [ $WCOPY_LOCK_PID = $MY_PID ] |
|---|
| 104 | then |
|---|
| 105 | return 1 |
|---|
| 106 | else |
|---|
| 107 | return 0 |
|---|
| 108 | fi |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | function conf_system_locked? { |
|---|
| 112 | if [ -f /var/run/confman.lock ] |
|---|
| 113 | then |
|---|
| 114 | SYSTEM_LOCK_PID=`cat /var/run/confman.lock` #gets the PID of the confman instance that created the lock |
|---|
| 115 | else |
|---|
| 116 | return 1 |
|---|
| 117 | fi |
|---|
| 118 | if [ $SYSTEM_LOCK_PID = $MY_PID ] |
|---|
| 119 | then |
|---|
| 120 | return 1 |
|---|
| 121 | else |
|---|
| 122 | return 0 |
|---|
| 123 | fi |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | # Revert a working file |
|---|
| 127 | function conf_revert { |
|---|
| 128 | svn revert $* |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | # Assumes that we already have core setup in our work path. |
|---|
| 132 | function conf_create_module { |
|---|
| 133 | local module=$1 |
|---|
| 134 | svn mkdir ${WORK_PATH}/$module |
|---|
| 135 | svn mkdir ${WORK_PATH}/${REPO_CHECKPTS}/$module |
|---|
| 136 | svn commit ${WORK_PATH}/${module} ${WORK_PATH}/${REPO_CHECKPTS}/$module -m \ |
|---|
| 137 | "Created directory structure for ${module} --`whoami`" |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | # Commits module $1 with message $2 |
|---|
| 141 | function conf_commit { |
|---|
| 142 | local modules=$(echo $1 | tr ' ' ',') |
|---|
| 143 | local msg="$2" |
|---|
| 144 | local symlinks |
|---|
| 145 | |
|---|
| 146 | # $modules is in the form foo,bar,baz. This eval trick forces |
|---|
| 147 | # curly brace expansion. |
|---|
| 148 | if echo "$modules" | grep ',' > /dev/null ; then |
|---|
| 149 | for module in $(eval echo ${WORK_PATH}/{$modules}); do |
|---|
| 150 | symlinks="$symlinks `find ${module} -type l \ |
|---|
| 151 | -exec ${readlink_cmd} -m {} \;`" |
|---|
| 152 | done |
|---|
| 153 | svn commit -F "$msg" $(eval echo ${WORK_PATH}/{$modules}) \ |
|---|
| 154 | $symlinks |
|---|
| 155 | else |
|---|
| 156 | symlinks="$symlinks `find ${modules} -type l \ |
|---|
| 157 | -exec ${readlink_cmd} -m {} \;`" |
|---|
| 158 | svn commit -F "$msg" ${WORK_PATH}/$modules \ |
|---|
| 159 | $symlinks |
|---|
| 160 | fi |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | function conf_fetch { |
|---|
| 164 | local tmpfile=`${mktemp_file}` |
|---|
| 165 | local proto="${CONF_EXPORT_URI%:/*}" |
|---|
| 166 | local remote |
|---|
| 167 | |
|---|
| 168 | case $proto in |
|---|
| 169 | sftp|scp) |
|---|
| 170 | remote="${CONF_EXPORT_URI#*://}" |
|---|
| 171 | remote="${remote/\//:/}" |
|---|
| 172 | scp -p -i ${CONF_SSH_KEY} -o StrictHostKeyCHecking=no \ |
|---|
| 173 | $remote $tmpfile |
|---|
| 174 | ;; |
|---|
| 175 | http|https|ftp) |
|---|
| 176 | fetch -o $tmpfile $remote |
|---|
| 177 | ;; |
|---|
| 178 | file) |
|---|
| 179 | remote="${CONF_EXPORT_URI#*://}" |
|---|
| 180 | cp -p $remote $tmpfile |
|---|
| 181 | ;; |
|---|
| 182 | *) |
|---|
| 183 | echo "Unsupported Protocol in conf_fetch" >&2 |
|---|
| 184 | return 1 |
|---|
| 185 | ;; |
|---|
| 186 | esac |
|---|
| 187 | echo $tmpfile |
|---|
| 188 | return 0 |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | # A way to utilize the svn status feature. |
|---|
| 192 | function conf_status { |
|---|
| 193 | svn status $* |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | # This exports a working copy of the repository to a tarball |
|---|
| 197 | function conf_export { |
|---|
| 198 | local tarball="$1" |
|---|
| 199 | conf_checkout_tree |
|---|
| 200 | tar -czf "$tarball" -C "${WORK_PATH}" . |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | # Roll out the specified module, optionally at the specified checkpoint |
|---|
| 204 | # eg: conf_rollout MODULE [checkpoint] |
|---|
| 205 | function conf_rollout { |
|---|
| 206 | local module="$1" |
|---|
| 207 | if [ -z $2 ] ; then |
|---|
| 208 | local moduledir="${WORK_PATH}/$module" |
|---|
| 209 | else |
|---|
| 210 | local moduledir="${WORK_PATH}/checkpoints/${module}/${2}" |
|---|
| 211 | fi |
|---|
| 212 | # This is a hack to work around NFS home dirs, for now: |
|---|
| 213 | chmod o+rx ${WORK_PATH} |
|---|
| 214 | |
|---|
| 215 | for directory in `find -L $moduledir -mindepth 1 -type d | grep -v "\.svn"`; |
|---|
| 216 | do |
|---|
| 217 | local livedir=`echo $directory | sed "s:$moduledir::"` |
|---|
| 218 | livedir="${LIVE_ROOT}${livedir}" |
|---|
| 219 | local owner=`conf_get_prop ${directory} owner` |
|---|
| 220 | local group=`conf_get_prop ${directory} group` |
|---|
| 221 | local mode=`conf_get_prop ${directory} mode` |
|---|
| 222 | local opts="-d -o $owner -g $group -m $mode" |
|---|
| 223 | local cmd="sudo $install_cmd $opts $livedir" |
|---|
| 224 | echo $cmd |
|---|
| 225 | $cmd |
|---|
| 226 | done |
|---|
| 227 | for file in `find -L $moduledir -type f | grep -v "\.svn"` ; do |
|---|
| 228 | local livefile=`echo "$file" | sed "s:$moduledir::"` |
|---|
| 229 | local owner=`conf_get_prop ${file} owner` |
|---|
| 230 | local group=`conf_get_prop ${file} group` |
|---|
| 231 | local mode=`conf_get_prop ${file} mode` |
|---|
| 232 | local symlink="`conf_get_prop ${file} symlink`" |
|---|
| 233 | file=`$readlink_cmd -m $file` |
|---|
| 234 | if [ -n "$symlink" ]; then |
|---|
| 235 | local cmd="sudo ln -snf $symlink ${LIVE_ROOT}$livefile" |
|---|
| 236 | echo $cmd |
|---|
| 237 | $cmd |
|---|
| 238 | else |
|---|
| 239 | local opts="-o $owner -g $group -m $mode" |
|---|
| 240 | local cmd="sudo $install_cmd $opts $file ${LIVE_ROOT}$livefile" |
|---|
| 241 | echo $cmd |
|---|
| 242 | $cmd |
|---|
| 243 | fi |
|---|
| 244 | done |
|---|
| 245 | # This is a hack to work around NFS home dirs, for now: |
|---|
| 246 | chmod o-rx ${WORK_PATH} |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | function conf_install { |
|---|
| 250 | local module="$1" |
|---|
| 251 | shift |
|---|
| 252 | local file=`abspath $1` |
|---|
| 253 | shift |
|---|
| 254 | local moduledir="${WORK_PATH}/$module" |
|---|
| 255 | local livefile=`echo "$file" | ${sed_cmd} "s:${WORK_PATH}/[^/]+::"` |
|---|
| 256 | local pathmodule=`echo "$file" | ${sed_cmd} "s:${WORK_PATH}/([^/]+).*:\1:"` |
|---|
| 257 | local suffix |
|---|
| 258 | local targetfile=`${readlink_cmd} -m $file` |
|---|
| 259 | livefile=`echo "$livefile" | ${sed_cmd} "s:-${pathmodule}$::"` |
|---|
| 260 | if [[ $SINGULARITIES =~ $livefile ]] && [ ! -d "$targetfile" ] ; then |
|---|
| 261 | suffix="-${module}" |
|---|
| 262 | fi |
|---|
| 263 | livefile="${livefile}${suffix}" |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | # See if it even exists |
|---|
| 267 | file="${moduledir}${livefile}" |
|---|
| 268 | targetfile=`${readlink_cmd} -m $file` |
|---|
| 269 | if [ ! -e "$targetfile" ] && [ -z "$1" ] ; then |
|---|
| 270 | return 0 |
|---|
| 271 | elif [ ! -f "$targetfile" ] && [ ! -d "$targetfile" ] ; then |
|---|
| 272 | conf_install $module "$@" |
|---|
| 273 | return |
|---|
| 274 | fi |
|---|
| 275 | |
|---|
| 276 | # If we got here, figure it out |
|---|
| 277 | local owner=`conf_get_prop ${file} owner` |
|---|
| 278 | local group=`conf_get_prop ${file} group` |
|---|
| 279 | local mode=`conf_get_prop ${file} mode` |
|---|
| 280 | local symlink="`conf_get_prop ${file} symlink`" |
|---|
| 281 | |
|---|
| 282 | file="$targetfile" |
|---|
| 283 | if [ -f "$file" ] ; then |
|---|
| 284 | local opts="-o $owner -g $group -m $mode" |
|---|
| 285 | else |
|---|
| 286 | local opts="-d -o $owner -g $group -m $mode" |
|---|
| 287 | fi |
|---|
| 288 | |
|---|
| 289 | if [ -n "$symlink" ] ; then |
|---|
| 290 | local cmd="sudo ln -snf $symlink ${LIVE_ROOT}$livefile" |
|---|
| 291 | else |
|---|
| 292 | local cmd="sudo $install_cmd $opts $file ${LIVE_ROOT}$livefile" |
|---|
| 293 | fi |
|---|
| 294 | |
|---|
| 295 | chmod o+rx ${WORK_PATH} |
|---|
| 296 | echo $cmd |
|---|
| 297 | $cmd |
|---|
| 298 | chmod o-rx ${WORK_PATH} |
|---|
| 299 | |
|---|
| 300 | if [ -d "$file" ] ; then |
|---|
| 301 | conf_install $module "$file"/* |
|---|
| 302 | fi |
|---|
| 303 | if [ ! -z $1 ] ; then |
|---|
| 304 | conf_install $module "$@" |
|---|
| 305 | fi |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | function conf_list { |
|---|
| 310 | local file=$1 |
|---|
| 311 | local module=`echo ${file#$WORK_PATH} | ${sed_cmd} 's:/([^/]+)/.*:\1:'` |
|---|
| 312 | local livefile=`echo ${file#$WORK_PATH} | ${sed_cmd} 's:/([^/]+)/:/:'` |
|---|
| 313 | local owner=`conf_get_prop ${file} owner` |
|---|
| 314 | local group=`conf_get_prop ${file} group` |
|---|
| 315 | local mode=`conf_get_prop ${file} mode` |
|---|
| 316 | local comment=`conf_get_prop ${file} comment` |
|---|
| 317 | local symlink=`conf_get_prop ${file} symlink` |
|---|
| 318 | [ -n "$symlink" ] && symlink=" -> $symlink" |
|---|
| 319 | echo -e "$mode\t$owner\t$group\t$comment\t\t${livefile}${symlink}" |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | # This function assembles the singularities: |
|---|
| 325 | function conf_assemble_sing { |
|---|
| 326 | local file=$1 |
|---|
| 327 | local livefile="${LIVE_ROOT}${file}" |
|---|
| 328 | local tmpfile=`${mktemp_file}` || exit 1 |
|---|
| 329 | local owner group mode flag livepart msg |
|---|
| 330 | for layer in $LAYERS ; do |
|---|
| 331 | livepart="${LIVE_ROOT}${file}-${layer}" |
|---|
| 332 | myfile="${WORK_PATH}/${layer}/${file}-${layer}" |
|---|
| 333 | if [ -f $myfile ] && sudo [ -f $livepart ] ; then |
|---|
| 334 | owner=`conf_get_prop ${myfile} owner` |
|---|
| 335 | group=`conf_get_prop ${myfile} group` |
|---|
| 336 | mode=`conf_get_prop ${myfile} mode` |
|---|
| 337 | comment=`conf_get_prop ${myfile} comment` |
|---|
| 338 | sudo cat $livepart >> $tmpfile |
|---|
| 339 | sudo rm $livepart |
|---|
| 340 | flag=1 |
|---|
| 341 | fi |
|---|
| 342 | done |
|---|
| 343 | if [ ! -z $flag ] ; then |
|---|
| 344 | local opts="-o $owner -g $group -m $mode" |
|---|
| 345 | local cmd="sudo $install_cmd $opts $tmpfile $livefile" |
|---|
| 346 | echo $cmd |
|---|
| 347 | $cmd |
|---|
| 348 | fi |
|---|
| 349 | rm -f $tmpfile |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | # This function creates a symlink in your working copy and imports it |
|---|
| 353 | # into confman. If the third argument is "true", then the link is |
|---|
| 354 | # forced. Throws an erorr if link_name already exists and "force" |
|---|
| 355 | # option is not used. |
|---|
| 356 | # e.g. conf_ln TARGET ( LINK_NAME | DIRECTORY ) [ FORCE ] |
|---|
| 357 | function conf_ln { |
|---|
| 358 | local target=$1 |
|---|
| 359 | local link=$2 |
|---|
| 360 | local forced=$3 |
|---|
| 361 | local opts isdir |
|---|
| 362 | |
|---|
| 363 | if [ "$forced" == "true" ] ; then |
|---|
| 364 | opts="-f" |
|---|
| 365 | elif [ -e "$link" -a ! -d "$link" ] ; then |
|---|
| 366 | echo "$link already exists. Did you mean to use \"-f\"?" |
|---|
| 367 | return 1 |
|---|
| 368 | fi |
|---|
| 369 | [ -d "$link" ] && isdir="true" |
|---|
| 370 | |
|---|
| 371 | ln -s $opts "$target" "$link" |
|---|
| 372 | if [ -n "$isdir" ] ; then |
|---|
| 373 | link="${link}/${target/*\//}" |
|---|
| 374 | fi |
|---|
| 375 | svn add "$link" |
|---|
| 376 | svn commit -m "Creating symlink $link in $module module." "$link" |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | # This function generates a checkpoint called NAME for the given MODULE. |
|---|
| 380 | # eg: conf_new_checkpoint MODULE NAME |
|---|
| 381 | function conf_new_checkpoint { |
|---|
| 382 | local module=$1 |
|---|
| 383 | local checkpoint=$2 |
|---|
| 384 | local chkpath="${WORK_PATH}/${REPO_CHECKPTS}/${module}/${checkpoint}" |
|---|
| 385 | local revision=`conf_revision` |
|---|
| 386 | echo $revision > $chkpath |
|---|
| 387 | svn add $chkpath |
|---|
| 388 | local msg="Created a checkpoint, ${checkpoint} for ${module} --`whoami`" |
|---|
| 389 | svn commit ${WORK_PATH}/${REPO_CHECKPTS} -m "$msg" |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | # This function will remove the checkpoint NAME from MODULE. |
|---|
| 393 | # eg: conf_rm_checkpoint MODULE NAME |
|---|
| 394 | function conf_rm_checkpoint { |
|---|
| 395 | local module=$1 |
|---|
| 396 | local checkpoint=$2 |
|---|
| 397 | local chkpath="${WORK_PATH}/${REPO_CHECKPTS}/${module}/${checkpoint}" |
|---|
| 398 | svn rm ${chkpath} |
|---|
| 399 | local msg="Removed the checkpoint ${checkpoint} from ${module} --`whoami`" |
|---|
| 400 | svn commit ${WORK_PATH}/${REPO_CHECKPTS} -m "$msg" |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | # This function will restore the state of your module's working copy to the |
|---|
| 404 | # named checkpoint. eg: conf_rollback MODULE NAME |
|---|
| 405 | function conf_rollback { |
|---|
| 406 | local module=$1 |
|---|
| 407 | local checkpoint=$2 |
|---|
| 408 | local clock=$3 |
|---|
| 409 | local modpath="${WORK_PATH}/${module}" |
|---|
| 410 | local chkpath="${WORK_PATH}/${REPO_CHECKPTS}/${module}/${checkpoint}" |
|---|
| 411 | local revision |
|---|
| 412 | local date=`echo $checkpoint | ${sed_cmd} 's:(....)(..)(..):\1-\2-\3:'` |
|---|
| 413 | |
|---|
| 414 | # Named checkpoint |
|---|
| 415 | if [ -f "${chkpath}" ] ; then |
|---|
| 416 | revision=`cat $chkpath` |
|---|
| 417 | elif [ -z $clock ] ; then # Time checkpoint |
|---|
| 418 | revision="{${date}}" |
|---|
| 419 | else |
|---|
| 420 | clock=`echo $clock | ${sed_cmd} 's#(..)(..)#\1:\2#'` |
|---|
| 421 | revision="{${date}T${clock}}" |
|---|
| 422 | fi |
|---|
| 423 | |
|---|
| 424 | symlinks="$symlinks `find ${module} -type l \ |
|---|
| 425 | -exec ${readlink_cmd} -m {} \;`" |
|---|
| 426 | |
|---|
| 427 | #svn update --revision $revision $modpath |
|---|
| 428 | svn update --revision $revision $modpath $symlinks |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | # This function will print the value of the specified property to STDOUT |
|---|
| 432 | function conf_get_prop { |
|---|
| 433 | local file=$1 |
|---|
| 434 | local prop=$2 |
|---|
| 435 | local result=`svn propget "confman:${prop}" ${file}` |
|---|
| 436 | if [ -z "$result" ] ; then |
|---|
| 437 | file=`$readlink_cmd -m ${file}` |
|---|
| 438 | svn propget "confman:${prop}" ${file} |
|---|
| 439 | else |
|---|
| 440 | echo $result |
|---|
| 441 | fi |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | # This function will set the value of the specified property. |
|---|
| 445 | function conf_set_prop { |
|---|
| 446 | local file=$1 |
|---|
| 447 | local prop=$2 |
|---|
| 448 | local value=$3 |
|---|
| 449 | svn propset "confman:${prop}" "${value}" ${file} |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | function conf_gen_file { |
|---|
| 453 | local module=$1 |
|---|
| 454 | local file=$2 |
|---|
| 455 | local owner=$3 |
|---|
| 456 | local group=$4 |
|---|
| 457 | local mode=$5 |
|---|
| 458 | local comment="$6" |
|---|
| 459 | local usefile=$7 |
|---|
| 460 | local symlink="$8" |
|---|
| 461 | local myfile="${WORK_PATH}/${module}${file}" |
|---|
| 462 | local warning="${comment} ${CONF_WARNING}" |
|---|
| 463 | local morewarn="${comment} Managed under ${module} module." |
|---|
| 464 | local revision="${comment} \$Id\$" |
|---|
| 465 | echo $file |
|---|
| 466 | |
|---|
| 467 | if [ -z $usefile ] ; then |
|---|
| 468 | echo -e "${warning}\n${morewarn}\n${revision}\n" > $myfile |
|---|
| 469 | elif head -n 1 ${usefile} | grep '^#!' >/dev/null ; then |
|---|
| 470 | awk "{if (NR==2) |
|---|
| 471 | print \"\\n${warning}\\n${morewarn}\\n${revision}\\n\"\$0; |
|---|
| 472 | else print \$0 }" ${usefile} > $myfile |
|---|
| 473 | else |
|---|
| 474 | echo -e "${warning}\n${morewarn}\n${revision}\n" > $myfile |
|---|
| 475 | cat ${usefile} >> $myfile |
|---|
| 476 | fi |
|---|
| 477 | |
|---|
| 478 | svn add $myfile |
|---|
| 479 | svn ps svn:keywords "Id" $myfile |
|---|
| 480 | conf_set_prop $myfile owner $owner |
|---|
| 481 | conf_set_prop $myfile group $group |
|---|
| 482 | conf_set_prop $myfile mode $mode |
|---|
| 483 | conf_set_prop $myfile comment "$comment" |
|---|
| 484 | conf_set_prop $myfile symlink "$symlink" |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | function conf_rm_file { |
|---|
| 488 | svn rm $* |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | function conf_mkdir { |
|---|
| 492 | local directory=$1 |
|---|
| 493 | local owner=$2 |
|---|
| 494 | local group=$3 |
|---|
| 495 | local mode=$4 |
|---|
| 496 | local workdir="" |
|---|
| 497 | local directories=`echo "$directory" | sed 's:/: :g'` |
|---|
| 498 | local dir |
|---|
| 499 | |
|---|
| 500 | for dir in $directories ; do |
|---|
| 501 | workdir="${workdir}/${dir}" |
|---|
| 502 | if [ ! -d ${workdir} ] ; then |
|---|
| 503 | echo svn mkdir ${workdir} |
|---|
| 504 | svn mkdir ${workdir} |
|---|
| 505 | conf_set_prop $workdir owner $owner |
|---|
| 506 | conf_set_prop $workdir group $group |
|---|
| 507 | conf_set_prop $workdir mode $mode |
|---|
| 508 | conf_set_prop $workdir comment "dir" |
|---|
| 509 | fi |
|---|
| 510 | done |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | function conf_rmmod { |
|---|
| 514 | local module=$1 |
|---|
| 515 | svn rm ${WORK_PATH}/${module} |
|---|
| 516 | svn rm ${WORK_PATH}/${REPO_CHECKPTS}/${module} |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | function conf_rename { |
|---|
| 520 | local oldmod=$1 |
|---|
| 521 | local newmod=$2 |
|---|
| 522 | local file |
|---|
| 523 | |
|---|
| 524 | # First, we perform the easy operations |
|---|
| 525 | svn mv ${WORK_PATH}/${oldmod} ${WORK_PATH}/${newmod} || \ |
|---|
| 526 | { echo "There was a problem renaming the modules." >&4 ; \ |
|---|
| 527 | conf_cleanExit 1 |
|---|
| 528 | } |
|---|
| 529 | svn mv ${WORK_PATH}/${REPO_CHECKPTS}/${oldmod} \ |
|---|
| 530 | ${WORK_PATH}/${REPO_CHECKPTS}/${newmod} || \ |
|---|
| 531 | { echo "There was a problem renaming the checkpts." >&4 ; \ |
|---|
| 532 | conf_cleanExit 1 |
|---|
| 533 | } |
|---|
| 534 | |
|---|
| 535 | svn update ${WORK_PATH} || \ |
|---|
| 536 | { echo "Your working copy didn't update correctly." >&4 ; \ |
|---|
| 537 | conf_cleanExit 1 |
|---|
| 538 | } |
|---|
| 539 | svn commit -m "Renaming ${oldmod} to ${newmod}, phase 1." \ |
|---|
| 540 | ${WORK_PATH}/{,${REPO_CHECKPTS}}/{${oldmod},${newmod}} || \ |
|---|
| 541 | { echo "Committing the changes failed." >&4 ; conf_cleanExit 1 |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | # Next, we have to rename any singularities |
|---|
| 545 | for file in ${SINGULARITIES} ; do |
|---|
| 546 | if [ -f "${WORK_PATH}/${newmod}${file}-${oldmod}" ] ; then |
|---|
| 547 | svn mv ${WORK_PATH}/${newmod}${file}-${oldmod} \ |
|---|
| 548 | ${WORK_PATH}/${newmod}${file}-${newmod} |
|---|
| 549 | fi |
|---|
| 550 | done |
|---|
| 551 | |
|---|
| 552 | svn update ${WORK_PATH} || \ |
|---|
| 553 | { echo "Your working copy didn't update correctly." >&4 ; \ |
|---|
| 554 | conf_cleanExit 1 |
|---|
| 555 | } |
|---|
| 556 | svn commit -m "Renaming ${oldmod} to ${newmod}, phase 2." \ |
|---|
| 557 | ${WORK_PATH}/${newmod} || \ |
|---|
| 558 | { echo "Committing the changes failed." >&4 ; conf_cleanExit 1 |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | # Now, we have to go fix all the confman headers in all the files... |
|---|
| 562 | find ${WORK_PATH}/${newmod} -type f -not -path '*/.svn/*' -exec \ |
|---|
| 563 | gsed -i -r "s/^(# Managed under )${oldmod}( module)/\1${newmod}\2/" \ |
|---|
| 564 | {} \; |
|---|
| 565 | |
|---|
| 566 | svn update ${WORK_PATH} || \ |
|---|
| 567 | { echo "Your working copy didn't update correctly." >&4 ; \ |
|---|
| 568 | conf_cleanExit 1 |
|---|
| 569 | } |
|---|
| 570 | svn commit -m "Renaming ${oldmod} to ${newmod}, complete." \ |
|---|
| 571 | ${WORK_PATH}/${newmod} || \ |
|---|
| 572 | { echo "Committing the changes failed." >&4 ; conf_cleanExit 1 |
|---|
| 573 | } |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | function conf_mv { |
|---|
| 577 | local oldname=$1 |
|---|
| 578 | local newname=$2 |
|---|
| 579 | svn mv $oldname $newname |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | function conf_cp { |
|---|
| 583 | local oldname=$1 |
|---|
| 584 | local newname=$2 |
|---|
| 585 | svn cp $oldname $newname |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | function conf_diff { |
|---|
| 589 | svn diff $* |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | function conf_log { |
|---|
| 593 | svn log $* |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | # Accepts log messages on stdin until EOF |
|---|
| 597 | function conf_logger { |
|---|
| 598 | logger -t "$MYNAME[$$]: $USER" 2>&4 |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | function conf_chattr { |
|---|
| 602 | local attr="confman:$1" |
|---|
| 603 | shift |
|---|
| 604 | local value="$1" |
|---|
| 605 | shift |
|---|
| 606 | svn propset "$attr" "$value" "$@" |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | function conf_lsattr { |
|---|
| 610 | local file="$1" |
|---|
| 611 | shift |
|---|
| 612 | echo "$file" |
|---|
| 613 | for prop in $(svn proplist $file | grep 'confman:' | sort) ; do |
|---|
| 614 | echo -e "\t${prop#confman:}\t" $(svn propget $prop $file) |
|---|
| 615 | done |
|---|
| 616 | if [ -n "$1" ] ; then |
|---|
| 617 | conf_lsattr "$@" |
|---|
| 618 | fi |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | function conf_rmattr { |
|---|
| 622 | local attr="confman:$1" |
|---|
| 623 | shift |
|---|
| 624 | svn propdel "$attr" "$@" |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | function conf_syncheck { |
|---|
| 628 | local cmd |
|---|
| 629 | local toReturn=true |
|---|
| 630 | for file in "$@" ; do |
|---|
| 631 | cmd=$(svn propget "confman:syncheck" "$file") |
|---|
| 632 | if [ -z "$cmd" ] ; then |
|---|
| 633 | continue |
|---|
| 634 | fi |
|---|
| 635 | if ! $cmd "$file" ; then |
|---|
| 636 | echo "Syntax verification failed for $file" >&2 |
|---|
| 637 | toReturn=false |
|---|
| 638 | fi |
|---|
| 639 | done |
|---|
| 640 | $toReturn |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | function conf_markclean { |
|---|
| 644 | sudo sh -c "echo clean > $REPO_SYNC_STATE" |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | function conf_markdirty { |
|---|
| 648 | sudo sh -c "echo dirty > $REPO_SYNC_STATE" |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | function conf_isclean { |
|---|
| 652 | local state=`sudo cat $REPO_SYNC_STATE 2>/dev/null` |
|---|
| 653 | case $state in |
|---|
| 654 | clean) return 0 ;; |
|---|
| 655 | dirty) return 1 ;; |
|---|
| 656 | *) echo "Invalid state. Reporting dirty." >&2 ; return 1 ;; |
|---|
| 657 | esac |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | # Returns the current revision number of the repository on stdout |
|---|
| 661 | function conf_revision { |
|---|
| 662 | svn info ${WORK_PATH} | awk '/Last Changed Rev:/ {print $4}' |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | # Returns the revision of the last commit or install operation on stdout |
|---|
| 666 | function conf_sysrev { |
|---|
| 667 | cut -d ':' -f 2 $REPO_ACTION |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | # Returns the last action of the repository on stdout |
|---|
| 671 | function conf_lastact { |
|---|
| 672 | cut -d ':' -f 1 $REPO_ACTION |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | # Records the specified action |
|---|
| 676 | function conf_recordAction { |
|---|
| 677 | local action="$1" |
|---|
| 678 | local revision=`conf_revision` |
|---|
| 679 | sudo sh -c "echo '${action}:${revision}' > $REPO_ACTION" |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | # WARNING: If you refer to fd 2 in this code, this function will silently |
|---|
| 683 | # fail when called by trap. DO NOT ATTEMPT TO LOG TO fd 2 IN THIS FUNCTION! |
|---|
| 684 | # The SIGNAL seems to kill the logger process spawned by the parent |
|---|
| 685 | # process, which is receiving its input from our fd 2. |
|---|
| 686 | function conf_cleanExit { |
|---|
| 687 | echo "Received a signal. Exiting cleanly." | conf_logger |
|---|
| 688 | echo "Abort, Abort! Patience is a virtue." >&4 |
|---|
| 689 | echo "Please wait until I finish cleaning up after you." >&4 |
|---|
| 690 | |
|---|
| 691 | # And in case we got an interrupt during a rollout, we still want the |
|---|
| 692 | # permissions here to be in a consistent state. |
|---|
| 693 | chmod 700 ${WORK_PATH} |
|---|
| 694 | echo "Removing leftover temp files..." >&4 |
|---|
| 695 | find /tmp/confman* -maxdepth 0 -user `whoami` -exec rm -rf {} \; |
|---|
| 696 | |
|---|
| 697 | # And this clears any locks we may have created on our working copy: |
|---|
| 698 | echo "Undoing damage to your working copy..." >&4 |
|---|
| 699 | [ -d "${WORK_PATH}" ] && svn cleanup ${WORK_PATH} |
|---|
| 700 | |
|---|
| 701 | echo "Removing any system or working copy locks..." >&4 |
|---|
| 702 | conf_unlock_wcopy |
|---|
| 703 | conf_unlock_system |
|---|
| 704 | |
|---|
| 705 | echo "All clean. Terminating." >&4 |
|---|
| 706 | |
|---|
| 707 | exit ${1:-1} |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | function conf_checkgroup { |
|---|
| 711 | GROUP_FILE=$($mktemp_file) |
|---|
| 712 | GROUP_STAT=$(chgrp staff $GROUP_FILE 2>&1) |
|---|
| 713 | rm $GROUP_FILE |
|---|
| 714 | if [ -n "$GROUP_STAT" ] ; then |
|---|
| 715 | return 2 |
|---|
| 716 | else |
|---|
| 717 | return 0 |
|---|
| 718 | fi |
|---|
| 719 | } |
|---|
| 720 | |
|---|
| 721 | # vim:ts=4 |
|---|
| 722 | |
|---|