-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_modules
executable file
·204 lines (166 loc) · 5.89 KB
/
update_modules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env bash
# A script for atomically updating the module files (via symlinks), without tempting people to modify them in place.
#
# by Ian Kirker, 2015
#
# Updated user detection stuff - OK, 20160225
moduledirs_path=/shared/ucl/apps
become_path="$(command -v become 2>/dev/null)"
ops_user=ccspapp
# OK what we want to do:
# 1 am I ccspapp?
# 2 am I in ccsprcop
# 3 if 2, does become exist?
if [ "$(whoami)" == "${ops_user}" ]; then
echo "I am ops user ${ops_user}. No need to use become."
elif (id -Gn | grep -v '\bccsprcop\b' >/dev/null 2>/dev/null); then
echo "Error: user should be in ccsprcop group to use this script." >&2
exit 1
elif [ ! -x "$become_path" ]; then
echo "Error: No become path provided." >&2
exit 3
else
echo "Becoming ${ops_user} and re-running..." >&2
if [[ "${0:0:1}" != "/" ]] && [[ "${0}" =~ ^[^\ ]*/ ]]; then
echo "Warning: relative paths will not work when this script has to change user" >&2
fi
$become_path $ops_user <<EOF
$0 "$@"
EOF
exit
fi
scriptname="$(basename "$0")"
usage="
Usage: $scriptname [-h] [-f|-i] [-r]
-h Prints this help
-f Force update even if it would be the same
-i Do not treat it as an error if the update would be the same
-k Do not remove old module trees after update
Branches:
If a file named 'modulefiles_branch_override' exists in the target
directory, it will be read and its contents used as the name of the
branch to check out from the modulefiles repository.
"
# Mode defaults
force_update="n"
idempotent="n"
remove_old_mfs="true"
# Parse options
while getopts ":hfik" opt; do
case ${opt} in
h )
printf "%s" "$usage"
exit 0
;;
f )
## Force mode -- deploys even if the current deployment is the same as the most recent
force_update="y"
;;
i )
## Idempotent mode -- does not treat it as an error if the current deployment is the same as the most recent
idempotent="y"
;;
k )
## Keep module_dirs older than 7 days after deployment -- default is to remove them
remove_old_mfs="false"
;;
\? )
printf "%s" "$usage"
exit 2
;;
esac
done
# Don't try to continue if something breaks
set -e
cd $moduledirs_path
if [[ ! -L "modulefiles" ]]; then
if [[ "$force_update" != "y" ]]; then
echo "Warning: this directory does not contain an existing modulefiles symlink. Attempting to run this script here may not do something sensible. Use \"-f\" to force run anyway."
exit 2
fi
if [[ -d "modulefiles" ]]; then
echo -e "Warning: an existing modulefiles directory is present. \n This script will attempt to move it out of the way, but this operation is not atomic and module accesses happening during the (hopefully very quick) transition may fail." >&2
RENDIR=modulefiles_prevcs_$(date +%Y-%m-%d)
mv -bv modulefiles "${RENDIR}" && ln -Tfs "${RENDIR}" modulefiles
fi
fi
# Repo config info
branch="master"
repo_url="https://github.com/UCL-RITS/rcps-modulefiles.git"
override_file="$moduledirs_path/modulefiles_branch_override"
if [[ -r "$override_file" ]]; then
echo "Found branch override file, reading..." >&2
branch="$(tr -d '\n' <"$override_file")"
echo "Set branch to: $branch" >&2
else
echo "No branch override file found, using default: $branch" >&2
fi
echo "Getting latest commit hash for ${branch} branch..." >&2
latest_sha=$(git ls-remote --exit-code "${repo_url}" "${branch}" | cut -f 1)
if [[ -n "$DEBUG" ]]; then
echo "latest_sha: ${latest_sha}" >&2
fi
req_commit_sha=${latest_sha}
short_sha=${req_commit_sha:0:10}
if [[ -n "$DEBUG" ]]; then
echo "short_sha: ${short_sha}" >&2
fi
if [[ "${short_sha}" == "" ]]; then
echo "Error: could not get SHA of latest commit " >&2
exit 2
fi
# Fail or reformat directory name if dir already exists
echo "Checking for existing directory from latest commit..." >&2
target_dir=.mf_${short_sha}
if [[ -d "$target_dir" || -d "$target_dir.*" ]]; then
if [[ "${force_update}" == "n" ]]; then
if [[ "$idempotent" == "y" ]]; then
exit
else
echo "Module files are already up-to-date: use \"-f\" to force the creation of a new copy and relinking." >&2
exit 2
fi
else
echo "Forcing update..." >&2
suffix_number=0
new_target_dir=${target_dir}
while [[ -a "${new_target_dir}" ]]; do
new_target_dir=${target_dir}.${suffix_number}
suffix_number=$(( suffix_number + 1 ))
done
target_dir=${new_target_dir}
fi
fi
git clone --depth=1 -b "${branch}" https://github.com/UCL-RITS/rcps-modulefiles.git "${target_dir}"
echo "${req_commit_sha}" >"${target_dir}/.commit_sha"
most_recent_log="$(cd "${moduledirs_path}/${target_dir}" && git log -n 1 --no-decorate --oneline)"
# We remove the repo information to dissuade people performing git operations on the prod copy
echo "Removing superfluous repo information..." >&2
rm -Rf "${target_dir:-/if_you_see_this_it_means_target_dir_variable_was_empty_somehow}/.git"
echo "Linking..." >&2
ln -Tfs "${target_dir}" modulefiles
if [[ "$remove_old_mfs" == "true" ]]; then
echo "Removing module dirs older than 7 days..." >&2
rm_count=0
skip_count=0
for dir in $(find -L \
"${moduledirs_path:?error: no module dirs path set}" \
-maxdepth 1 \
-type d \
-name ".mf_*" \
-ctime +7 \
| grep -v "${target_dir}" )
do
if [[ ! -a "$dir/.keep" ]]; then
'rm' -rf "${dir}"
rm_count=$(( rm_count + 1 ))
else
skip_count=$(( skip_count + 1 ))
fi
done
echo "Removed $rm_count dirs, skipped $skip_count dirs marked to keep." >&2
fi
echo "--Done--" >&2
num_old_module_dirs="$(find -L "$moduledirs_path" -maxdepth 1 -type d -name ".mf_*" | wc -l)"
echo "Module dirs in ${moduledirs_path}: ${num_old_module_dirs}" >&2
echo "Most recent log in active dir: $most_recent_log"