forked from foursquare/fsqio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upkeep
executable file
·155 lines (140 loc) · 5.27 KB
/
upkeep
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
#!/bin/bash
# Copyright 2015 Foursquare Labs Inc. All Rights Reserved.
# Pass variables to children
set -eao pipefail
if [[ $(arch) = "arm64" ]]
then
exec arch -arch x86_64 $0 $@
fi
export BUILD_ROOT="${BUILD_ROOT:-$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )}"
export UPKEEPROOT="${BUILD_ROOT}/build-support"
UPKEEP_HOME="${UPKEEPROOT}/fsqio/upkeep"
ENV_FILE="libs/env.sh"
# Source the environment env files.
UPKEEP_FSQIO_ENV="${UPKEEP_HOME}/${ENV_FILE}"
UPKEEP_FS_ENV="${UPKEEPROOT}/foursquare/upkeep/${ENV_FILE}"
[[ -e ${UPKEEP_FS_ENV} ]] && source "${UPKEEP_FS_ENV}"
[[ -e ${UPKEEP_FSQIO_ENV} ]] && source "${UPKEEP_FSQIO_ENV}"
source "${UPKEEP_HOME}/libs/util.sh"
# Default to not SKIP_UPKEEP
SKIP_UPKEEP=${SKIP_UPKEEP:-0}
# Check for options.
upkeep_args=()
for opt in $@; do
case ${opt} in
"--all" )
DOWNSTREAM_TASKS="true"
task_names=( $(all_task_names) )
# Force removing nuke-pvenvs from ./upkeep --all as it adds extra ~5 mins each run (up to ~10 when including py3).
# It nukes venv-faster and prod virtualenv cache on each run intentionally - but that is only desirable in
# extreme circumstances.
if [[ ! $@ =~ "--clean" ]]; then
task_names=( "${task_names[@]/clean}" )
task_names=( "${task_names[@]/nuke-pvenvs}" )
./upkeep check clean --no-downstream
./upkeep check nuke-pvenvs --no-downstream
fi
upkeep_args=( "${upkeep_args[@]}" "${task_names[@]}" )
;;
"--no-downstream" )
DOWNSTREAM_TASKS="false"
;;
"--downstream" )
DOWNSTREAM_TASKS="true"
;;
"--skip-tasks" )
SKIP_UPKEEP=1
;;
"--no-skip-tasks" )
SKIP_UPKEEP=0
;;
"--no-cache" )
# This could and probably should be per-task but the delete would have to happen in the check.sh
colorized_warn "Deleting the upkeep cache...\n"
# We have reluctantly introduced a leak with this and again have several types of clean.
# * "./upkeep clean"
# - Removes created environments
# - Delete .pants.d and any directories defined by FSQ_CLEAN_DIRS
# ./upkeep <anything> --no-cache
# - Wipes the third party tools and download caches.
# This will delete the current files and in almost all cases safely trigger a rebuild.
# Run ./upkeep --all in the off chance your environment ends up unhealthy.
(rm -rf "${DEPENDENCIES_ROOT}" "${FS_DOWNLOAD_CACHE}")
;;
* )
# Try and catch bad options - while allowing passthrough for ./upkeep run.
if [[ "${upkeep_args[0]}" != 'run' ]] && [[ ${opt} != -*help ]] && [[ ${opt} == -* ]]; then
colorized_error "Bad task name: ${opt}"
print_help
exit_with_failure "Bad invocation: No upkeep option: ${opt}\n"
fi
upkeep_args=( "${upkeep_args[@]}" "${opt}")
;;
esac
done
function upkeep_cmd() {
# SKIP_UPKEEP_TASKS allows callers (like the git hooks) to skip upkeep tasks but still execute scripts, like ./python.
# Essentially means "void SKIP_UPKEEP for this command."
if [[ ${SKIP_UPKEEP} -eq 0 ]] || [[ $SKIP_UPKEEP_TASKS -eq 1 ]]; then
if [ ! -f "${1}" ]; then
exit_with_failure "No upkeep file found at: ${1}"
fi
echo "$@"
fi
}
# Route based on arguments.
case "${upkeep_args[0]}" in
"--help"|"help"|"-h" )
print_help
exit 0
;;
"force" )
DOWNSTREAM_TASKS=${DOWNSTREAM_TASKS:-"true"}
action="${UPKEEP_HOME}/force.sh"
args=( "${upkeep_args[@]:1}" )
;;
"check"|'')
# Check disables downstream - honestly it should probably not be surfaced as a top-level command anymore.
DOWNSTREAM_TASKS="false"
action="${UPKEEP_HOME}/check.sh"
args=( "${upkeep_args[@]:1}" )
;;
"tasks" )
DOWNSTREAM_TASKS=${DOWNSTREAM_TASKS:-"false"}
print_all_tasks "${upkeep_args[@]}"
exit 0
;;
"task-list" )
DOWNSTREAM_TASKS=${DOWNSTREAM_TASKS:-"false"}
all_task_names
exit 0
;;
"run" )
script_name="${upkeep_args[1]}"
# The script can be ${script_name}.sh under <xyz>/upkeep/scripts or be a full file path relative to the BUILD_ROOT.
action=$(find_upkeep_file "scripts" "${script_name}") || action="${BUILD_ROOT}/${script_name}"
[ -f "${action}" ] || exit_with_failure "Could not find matching script: ${script_name}"
args=( "${upkeep_args[@]:2}" )
# Execute `./upkeep check' in order to run any required tasks and then turn on SKIP for future upkeep calls.
if [[ ! SKIP_UPKEEP -eq 1 ]]; then
${UPKEEP_HOME}/check.sh
fi
# Disable task-related upkeep operations but allow others (essentially run) to continue.
SKIP_UPKEEP_TASKS=1
SKIP_UPKEEP=1
;;
*)
# This is should already default to "false", but adding a guard here. The downstream task implementation uses git
# operations and is probably not safe to automate. Until the implementation improves, the --downstream flag should
# only be invoked from the command line, akin to a `git reset` for upkeep.
DOWNSTREAM_TASKS=${DOWNSTREAM_TASKS:-"false"}
action="${UPKEEP_HOME}/execute_task.sh"
args=( "${upkeep_args[@]:0}" )
;;
esac
if [ "$0" = "$BASH_SOURCE" ]; then
# This must be two separate shell commands or else exec will return the exit code of the function's `echo` call.
cmd="$(upkeep_cmd ${action} ${args[@]})"
export SKIP_UPKEEP=$SKIP_UPKEEP
${cmd}
fi