|
| 1 | +# Helper functions used by ctl scripts |
| 2 | + |
| 3 | +# links a job file (probably a config file) into a package |
| 4 | +# Example usage: |
| 5 | +# link_job_file_to_package config/redis.yml [config/redis.yml] |
| 6 | +# link_job_file_to_package config/wp-config.php wp-config.php |
| 7 | +link_job_file_to_package() { |
| 8 | + source_job_file=$1 |
| 9 | + target_package_file=${2:-$source_job_file} |
| 10 | + full_package_file=$WEBAPP_DIR/${target_package_file} |
| 11 | + |
| 12 | + link_job_file ${source_job_file} ${full_package_file} |
| 13 | +} |
| 14 | + |
| 15 | +# links a job file (probably a config file) somewhere |
| 16 | +# Example usage: |
| 17 | +# link_job_file config/bashrc /home/vcap/.bashrc |
| 18 | +link_job_file() { |
| 19 | + source_job_file=$1 |
| 20 | + target_file=$2 |
| 21 | + full_job_file=$JOB_DIR/${source_job_file} |
| 22 | + |
| 23 | + echo link_job_file ${full_job_file} ${target_file} |
| 24 | + if [[ ! -f ${full_job_file} ]] |
| 25 | + then |
| 26 | + echo "file to link ${full_job_file} does not exist" |
| 27 | + else |
| 28 | + # Create/recreate the symlink to current job file |
| 29 | + # If another process is using the file, it won't be |
| 30 | + # deleted, so don't attempt to create the symlink |
| 31 | + mkdir -p $(dirname ${target_file}) |
| 32 | + ln -nfs ${full_job_file} ${target_file} |
| 33 | + fi |
| 34 | +} |
| 35 | + |
| 36 | +# If loaded within monit ctl scripts then pipe output |
| 37 | +# If loaded from 'source ../utils.sh' then normal STDOUT |
| 38 | +redirect_output() { |
| 39 | + SCRIPT=$1 |
| 40 | + mkdir -p /var/vcap/sys/log/monit |
| 41 | + exec 1>> /var/vcap/sys/log/monit/$SCRIPT.log 2>&1 |
| 42 | +} |
| 43 | + |
| 44 | +pid_guard() { |
| 45 | + pidfile=$1 |
| 46 | + name=$2 |
| 47 | + |
| 48 | + if [ -f "$pidfile" ]; then |
| 49 | + pid=$(head -1 "$pidfile") |
| 50 | + |
| 51 | + if [ -n "$pid" ] && [ -e /proc/$pid ]; then |
| 52 | + echo "$name is already running, please stop it first" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | + |
| 56 | + echo "Removing stale pidfile..." |
| 57 | + rm $pidfile |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +wait_pid() { |
| 62 | + pid=$1 |
| 63 | + try_kill=$2 |
| 64 | + timeout=${3:-0} |
| 65 | + force=${4:-0} |
| 66 | + countdown=$(( $timeout * 10 )) |
| 67 | + |
| 68 | + echo wait_pid $pid $try_kill $timeout $force $countdown |
| 69 | + if [ -e /proc/$pid ]; then |
| 70 | + if [ "$try_kill" = "1" ]; then |
| 71 | + echo "Killing $pidfile: $pid " |
| 72 | + kill $pid |
| 73 | + fi |
| 74 | + while [ -e /proc/$pid ]; do |
| 75 | + sleep 0.1 |
| 76 | + [ "$countdown" != '0' -a $(( $countdown % 10 )) = '0' ] && echo -n . |
| 77 | + if [ $timeout -gt 0 ]; then |
| 78 | + if [ $countdown -eq 0 ]; then |
| 79 | + if [ "$force" = "1" ]; then |
| 80 | + echo -ne "\nKill timed out, using kill -9 on $pid... " |
| 81 | + kill -9 $pid |
| 82 | + sleep 0.5 |
| 83 | + fi |
| 84 | + break |
| 85 | + else |
| 86 | + countdown=$(( $countdown - 1 )) |
| 87 | + fi |
| 88 | + fi |
| 89 | + done |
| 90 | + if [ -e /proc/$pid ]; then |
| 91 | + echo "Timed Out" |
| 92 | + else |
| 93 | + echo "Stopped" |
| 94 | + fi |
| 95 | + else |
| 96 | + echo "Process $pid is not running" |
| 97 | + echo "Attempting to kill pid anyway..." |
| 98 | + kill $pid |
| 99 | + fi |
| 100 | +} |
| 101 | + |
| 102 | +wait_pidfile() { |
| 103 | + pidfile=$1 |
| 104 | + try_kill=$2 |
| 105 | + timeout=${3:-0} |
| 106 | + force=${4:-0} |
| 107 | + countdown=$(( $timeout * 10 )) |
| 108 | + |
| 109 | + if [ -f "$pidfile" ]; then |
| 110 | + pid=$(head -1 "$pidfile") |
| 111 | + if [ -z "$pid" ]; then |
| 112 | + echo "Unable to get pid from $pidfile" |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + |
| 116 | + wait_pid $pid $try_kill $timeout $force |
| 117 | + |
| 118 | + rm -f $pidfile |
| 119 | + else |
| 120 | + echo "Pidfile $pidfile doesn't exist" |
| 121 | + fi |
| 122 | +} |
| 123 | + |
| 124 | +kill_and_wait() { |
| 125 | + pidfile=$1 |
| 126 | + # Monit default timeout for start/stop is 30s |
| 127 | + # Append 'with timeout {n} seconds' to monit start/stop program configs |
| 128 | + timeout=${2:-25} |
| 129 | + force=${3:-1} |
| 130 | + if [[ -f ${pidfile} ]] |
| 131 | + then |
| 132 | + wait_pidfile $pidfile 1 $timeout $force |
| 133 | + else |
| 134 | + # TODO assume $1 is something to grep from 'ps ax' |
| 135 | + pid="$(ps auwwx | grep "$1" | awk '{print $2}')" |
| 136 | + wait_pid $pid 1 $timeout $force |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +check_nfs_mount() { |
| 141 | + opts=$1 |
| 142 | + exports=$2 |
| 143 | + mount_point=$3 |
| 144 | + |
| 145 | + if grep -qs $mount_point /proc/mounts; then |
| 146 | + echo "Found NFS mount $mount_point" |
| 147 | + else |
| 148 | + echo "Mounting NFS..." |
| 149 | + mount $opts $exports $mount_point |
| 150 | + if [ $? != 0 ]; then |
| 151 | + echo "Cannot mount NFS from $exports to $mount_point, exiting..." |
| 152 | + exit 1 |
| 153 | + fi |
| 154 | + fi |
| 155 | +} |
0 commit comments