forked from linux-system-roles/auto-maintenance
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync-template.sh
executable file
·535 lines (474 loc) · 13.4 KB
/
sync-template.sh
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/bin/bash
# SPDX-License-Identifier: MIT
set -euo pipefail
ME=$(basename $0)
HERE=${PWD}
WORKDIR="${HOME}/.cache/${ME%.*}"
STDOUT=$(mktemp /tmp/${ME%.*}-XXXXXX.stdout)
STDERR=$(mktemp /tmp/${ME%.*}-XXXXXX.stderr)
COLOR_RESET='\e[0m'
COLOR_RED='\e[31m'
COLOR_GREEN='\e[32m'
COLOR_BLUE='\e[34m'
GIT_USER_DEFAULT='systemroller'
GIT_EMAIL_DEFAULT='[email protected]'
declare -A USER2EMAIL_MAP=( [${GIT_USER_DEFAULT}]="${GIT_EMAIL_DEFAULT}" )
FROM_BRANCH_DEFAULT='master'
SYNC_BRANCH_DEFAULT='lsr-template-sync'
CONTACTS_DEFAULT='i386x,pcahyna,richm'
GITHUB="https://github.com"
LSR_GROUP="linux-system-roles"
LSR_TEMPLATE="template"
LSR_TEMPLATE_NS="${LSR_GROUP}/${LSR_TEMPLATE}"
LSR_TEMPLATE_REPO="${GITHUB}/${LSR_TEMPLATE_NS}.git"
FILES=(
'--copy-if-missing=.gitignore'
'--copy-if-missing=.lgtm.yml'
'--ensure-directory=.travis'
'--copy-if-missing=.travis/config.sh'
'--copy=.travis/custom_pylint.py'
'--copy-if-missing=.travis/custom.sh'
'--copy=.travis/preinstall'
'--copy=.travis/runblack.sh'
'--copy=.travis/runcoveralls.sh'
'--copy=.travis/runflake8.sh'
'--copy=.travis/runpylint.sh'
'--copy=.travis/runpytest.sh'
'--copy=.travis/runsyspycmd.sh'
'--copy=.travis/runtox'
'--copy=.travis/utils.sh'
'--copy=.travis.yml'
'--copy=.yamllint.yml'
'--copy-if-missing=.yamllint_custom.yml'
'--copy-if-missing=custom_requirements.txt'
'--copy-if-missing=LICENSE'
'--copy-recursively=molecule'
'--copy-if-missing=molecule_extra_requirements.txt'
'--copy-if-missing=pylint_extra_requirements.txt'
'--copy=pylintrc'
'--copy=tox.ini'
'--copy=tests/setup_module_utils.sh'
)
declare -A IGNORE_IF_MISSING_MAP
INDENT=""
INHELP=""
trap "rm -f ${STDOUT} ${STDERR}; cd ${HERE}" ABRT EXIT HUP INT QUIT
##
# inform ARGS
#
# Print ARGS to standard output (in blue).
function inform() {
echo -e "${COLOR_BLUE}$*${COLOR_RESET}"
}
##
# report_success ARGS
#
# Print ARGS to standard output (in green).
function report_success() {
echo -e "${COLOR_GREEN}$*${COLOR_RESET}"
}
##
# report_failure ARGS
#
# Print ARGS to standard error output (in red).
function report_failure() {
echo -e "${COLOR_RED}$*${COLOR_RESET}" >&2
}
##
# error $1 [$2]
#
# $1 - error message
# $2 - exit code (optional, default: 1)
#
# Print $1 (in red) to standard error output and exit with $2.
function error() {
report_failure "$1"
exit ${2:-1}
}
##
# runcmd $1 [$2 [$3]]
#
# $1 - command with arguments
# $2 - variable name to store $1's standard output
# $3 - default value of $2 if $1 cannot be run (i.e. if dry run is active)
#
# Run $1. If DRY_RUN has non-empty value, only print "[dry run] $1" to standard
# output (in blue) and return exit code 0. If $2 is given, save standard output
# to it.
function runcmd() {
local E=0
if [[ "${DRY_RUN}" ]]; then
inform "[dry run] $1"
if [[ "${2:-}" ]]; then
eval "$2='${3:-}'"
fi
return $E
fi
eval "$1" 1> ${STDOUT} 2> ${STDERR} || E=$?
if [[ "${2:-}" ]]; then
eval "$2=\"$(cat ${STDOUT})\""
else
cat ${STDOUT}
fi
if [[ $E -eq 0 ]]; then
report_success "Command '$1' has completed successfully."
else
report_failure "Command '$1' has failed with exit code $E and error message:"
cat ${STDERR} >&2
fi
return $E
}
##
# ensure_directory $1
#
# $1 - path to directory
#
# Create $1 if it does not exist. If INHELP is non-empty, only print what will
# be done to standard output, indented with ${INDENT}.
function ensure_directory() {
if [[ "${INHELP}" ]]; then
echo "${INDENT}create $1 if it does not exist"
else
runcmd "(test -d $1 || mkdir -vp $1)"
fi
}
##
# copy_file $1 $2 $3
#
# $1 - path to source
# $2 - path to destination
# $3 - file name
#
# Copy $1/$3 to $2/$3. If INHELP is non-empty, only print what will be done to
# standard output, indented with ${INDENT}. Do not fail if $3 is on
# ignore-missing-files list.
function copy_file() {
if [[ "${INHELP}" ]]; then
echo "${INDENT}copy $1/$3 to $2/$3"
else
COMMAND="cp -vf $1/$3 $2/$3"
if [[ ! -e "$1/$3" && "${IGNORE_IF_MISSING_MAP[$3]:-}" ]]; then
inform "[\`$COMMAND\` ignored with note: '$1/$3' does not exist]"
else
runcmd "${COMMAND}"
fi
fi
}
##
# copy_missing $1 $2 $3
#
# $1 - path to source
# $2 - path to destination
# $3 - file name
#
# Copy $1/$3 to $2/$3 if $2/$3 does not exist yet. If INHELP is non-empty, only
# print what will be done to standard output, indented with ${INDENT}. Do not
# fail if $3 is on ignore-missing-files list.
function copy_missing() {
if [[ "${INHELP}" ]]; then
echo "${INDENT}copy $1/$3 to $2/$3 if $2/$3 is missing"
else
COMMAND="cp -vf $1/$3 $2/$3"
if [[ ! -e "$1/$3" && "${IGNORE_IF_MISSING_MAP[$3]:-}" ]]; then
inform "[\`$COMMAND\` ignored with note: '$1/$3' does not exist]"
else
runcmd "(test -e $2/$3 || ${COMMAND})"
fi
fi
}
##
# copy_recursive $1 $2 $3
#
# $1 - path to source
# $2 - path to destination
# $3 - directory name
#
# Recursively copy $1/$3 to $2. If INHELP is non-empty, only print what will
# be done to standard output, indented with ${INDENT}. Do not fail if $3 is on
# ignore-missing-files list.
function copy_recursive() {
if [[ "${INHELP}" ]]; then
echo "${INDENT}copy $1/$3 to $2 recursively"
else
COMMAND="cp -vrf $1/$3 $2"
if [[ ! -d "$1/$3" && "${IGNORE_IF_MISSING_MAP[$3]:-}" ]]; then
inform "[\`$COMMAND\` ignored with note: '$1/$3' does not exist]"
else
runcmd "${COMMAND}"
fi
fi
}
##
# copy_template_files $1 $2
#
# $1 - source directory
# $2 - destination directory
#
# Iterate over actions in FILES list and perform every action. Supported
# actions are:
#
# --ensure-directory=DIR
# * create $2/DIR if it does not exist
# --copy=FILE
# * copy $1/FILE to $2/FILE
# --copy-if-missing=FILE
# * copy $1/FILE to $2/FILE if $2/FILE does not exist yet
# --copy-recursively=DIR
# * recursively copy $1/DIR to $2
#
function copy_template_files() {
for F in "${FILES[@]}"; do
case "$F" in
--ensure-directory=*)
ensure_directory "$2/${F#*=}"
;;
--copy=*)
copy_file "$1" "$2" "${F#*=}"
;;
--copy-if-missing=*)
copy_missing "$1" "$2" "${F#*=}"
;;
--copy-recursively=*)
copy_recursive "$1" "$2" "${F#*=}"
;;
*)
error "${ME}: In ${FUNCNAME[0]}: Unknown option '$F'."
;;
esac
done
}
##
# usage
#
# Print script usage to standard output.
function usage() {
cat <<EOF
Synchronize linux-system-roles repositories with the recent version of
template.
Usage: $ME [options]
where [options] are
--branch, -b
a name of branch from which to make the pull request
(default: "${SYNC_BRANCH_DEFAULT}");
--clean
remove ${WORKDIR} and exit;
--contacts, -c
comma separated list of contacts to be appeared in pull request message
under CC section; a contact is a GitHub user (default: "${CONTACTS_DEFAULT}");
--dry-run, -d
only write what will be done, do not touch anything;
--from-branch
a name of branch from which to take template files
(default: "${FROM_BRANCH_DEFAULT}");
--from-repo
specify the template repo (default: "${LSR_TEMPLATE_REPO}");
--help, -h
print this help and exit;
--ignore, -i
add file or directory to ignore list; if such a file or directory will be
missing then a file system operation will be skipped;
--repolist, -r
comma separeted list of repositories for which the synchronization is
applicable;
--token, -t
GitHub token;
--user, -u
set git user name and email, example
--user "John Doe <[email protected]>"
(default: "${GIT_USER_DEFAULT} <${GIT_EMAIL_DEFAULT}>").
The synchronization is done in the following way:
1. cd to ${WORKDIR}
2. clone or pull the latest template from ${LSR_TEMPLATE_REPO}
3. for every \$REPO from --repolist:
3.1 clone the \$REPO from ${GITHUB}/${LSR_GROUP}/\${REPO}.git
3.2 cd to \$REPO
3.3. configure git user.name and user.email for --user locally
3.4. create a --branch and checkout to it
3.5. then
$(INDENT=" " INHELP=yes copy_template_files ../template .)
3.6. add files and push the --branch to upstream
3.7. create a pull request
EXAMPLES
Open a pull request against <${GITHUB}/${LSR_GROUP}/myrole> with recent files
from <${LSR_TEMPLATE_REPO}> as a user John Doe:
./$ME -u "John Doe <[email protected]>" -r myrole -t fa1afe1caffebeef1ee7facadedecade7001f001
EOF
}
##
# process_options ARGS
#
# Process ARGS. ARGS reflects script options.
function process_options() {
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run | -d)
DRY_RUN=yes
;;
--user | -u)
shift
if [[ "$1" =~ ^(.*)[\ ]+\<(.*)\>$ ]]; then
GIT_USER="${BASH_REMATCH[1]}"
GIT_EMAIL="${BASH_REMATCH[2]}"
else
GIT_USER="$1"
fi
;;
--from-repo)
shift
FROM_REPO="$1"
;;
--from-branch)
shift
FROM_BRANCH="$1"
;;
--branch | -b)
shift
SYNC_BRANCH="$1"
;;
--ignore | -i)
shift
IGNORE_IF_MISSING_MAP["$1"]="yes"
;;
--contacts | -c)
shift
CONTACTS="$1"
;;
--token | -t)
shift
GITHUB_TOKEN="$1"
;;
--repolist | -r)
shift
REPOLIST="$1"
;;
--help | -h)
usage
exit 0
;;
--clean)
rm -rfd ${WORKDIR}
exit 0
;;
*)
error "${ME}: Unknown option '$1'. Type '$0 --help' for help."
;;
esac
shift
done || :
}
process_options "$@"
DRY_RUN="${DRY_RUN:-}"
GIT_USER="${GIT_USER:-${GIT_USER_DEFAULT}}"
GIT_EMAIL="${GIT_EMAIL:-${USER2EMAIL_MAP[${GIT_USER}]:-}}"
FROM_REPO="${FROM_REPO:-${LSR_TEMPLATE_REPO}}"
FROM_BRANCH="${FROM_BRANCH:-${FROM_BRANCH_DEFAULT}}"
SYNC_BRANCH="${SYNC_BRANCH:-${SYNC_BRANCH_DEFAULT}}"
CONTACTS="${CONTACTS:-${CONTACTS_DEFAULT}}"
export GITHUB_TOKEN="${GITHUB_TOKEN:-}"
REPOLIST="${REPOLIST:-}"
REPOLIST="${REPOLIST//,/ }"
##
# check_required_options
#
# Check if required options are provided.
function check_required_options() {
if [[ -z "${GITHUB_TOKEN}" ]]; then
error "${ME}: GitHub token (GITHUB_TOKEN) not set. Terminating."
fi
if [[ -z "${REPOLIST}" ]]; then
error "${ME}: No repos (REPOLIST) were specified. Terminating."
fi
if [[ -z "${GIT_USER}" ]]; then
error "${ME}: Git user name is missing. Terminating."
fi
if [[ -z "${GIT_EMAIL}" ]]; then
error "${ME}: Git user's email is missing. Terminating."
fi
}
##
# put_revision $1
#
# $1 - revision id
#
# Put a link to the revision into the payload.
function put_revision() {
echo -n '\nRevision: [`'"$1"'`]'"(${GITHUB}/${LSR_TEMPLATE_NS}/tree/$1)"
}
##
# expand_contacts
#
# If CONTACTS has a form "C1 C2 C3 ... Cn", return "\nCC: @C1, @C2, ..., @Cn.".
function expand_contacts() {
if [[ "${CONTACTS}" ]]; then
echo -n '\nCC:' "@${CONTACTS//,/, @}"
fi
}
##
# get_template_repo
#
# Get the repo with common template.
function get_template_repo() {
if [[ -d "${LSR_TEMPLATE}" ]]; then
runcmd "pushd ${LSR_TEMPLATE}"
runcmd "git fetch"
runcmd "git checkout '${FROM_BRANCH}'"
runcmd "git pull"
runcmd "popd"
else
runcmd "git clone -b '${FROM_BRANCH}' '${FROM_REPO}' '${LSR_TEMPLATE}'"
fi
}
##
# get_revision_id $1
#
# $1 - variable to store the result
#
# Store revision identifier as SHA-1 of common template's repository HEAD to $1.
function get_revision_id() {
runcmd "pushd ${LSR_TEMPLATE}"
runcmd "git rev-parse HEAD" "$1" "0000000000000000000000000000000000000000"
runcmd "popd"
}
##
# do_sync
#
# Synchronize common template files across system roles repositories.
function do_sync() {
check_required_options
ensure_directory ${WORKDIR}
runcmd "pushd ${WORKDIR}"
get_template_repo
get_revision_id GIT_HEAD
PAYLOAD=$(cat <<-EOF
{"title":"Synchronize files from ${LSR_TEMPLATE_NS}",
"base":"master",
"head":"${SYNC_BRANCH}",
"body":"This PR propagates files from [${LSR_TEMPLATE_NS}](${GITHUB}/${LSR_TEMPLATE_NS}) which should be in sync across [${LSR_GROUP}](${GITHUB}/${LSR_GROUP}) repos. In case of changing affected files via pushing to this PR, please do not forget also to push the changes to [${LSR_TEMPLATE_NS}](${GITHUB}/${LSR_TEMPLATE_NS}) repo.\n$(put_revision ${GIT_HEAD})\n$(expand_contacts)"}
EOF
)
for REPO in ${REPOLIST}; do
inform "Synchronizing ${REPO} wiht ../${LSR_TEMPLATE}."
runcmd "[[ -d \"${REPO}\" ]] && rm -rfd ${REPO} || :"
runcmd "git clone '${GITHUB}/${LSR_GROUP}/${REPO}.git' '${REPO}'"
runcmd "pushd ${REPO}"
runcmd "git config --local user.name '${GIT_USER}'"
runcmd "git config --local user.email '${GIT_EMAIL}'"
runcmd "git checkout -b '${SYNC_BRANCH}'"
copy_template_files ../${LSR_TEMPLATE} .
if [[ "${DRY_RUN}" || "$(git status --porcelain)" ]]; then
runcmd "git add ."
runcmd "git commit -m 'Synchronize files from ${LSR_GROUP}/${LSR_TEMPLATE}'"
if runcmd "git push 'https://${GITHUB_TOKEN}:@github.com/${LSR_GROUP}/${REPO}' -u '${SYNC_BRANCH}'"; then
runcmd "curl -u '${GIT_USER}:${GITHUB_TOKEN}' -X POST -d '${PAYLOAD}' 'https://api.github.com/repos/${LSR_GROUP}/${REPO}/pulls'"
fi
fi
runcmd "popd"
done
runcmd "popd"
report_success "All repositories was synchronized with the template successfully."
}
do_sync
# Local Variables:
# mode: Shell-script
# sh-basic-offset: 2
# End: