forked from gruntwork-io/gruntwork-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgruntwork-install
executable file
·334 lines (285 loc) · 10.3 KB
/
gruntwork-install
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
#!/bin/bash
#
# This script installs Gruntwork Script Modules. The main motivation in writing it is to make installing
# Gruntwork Script Modules feel as easy as installing a package using apt-get, brew, or yum.
#
# Note that if the user specifies neither --tag nor --branch, the latest tag is downloaded.
#
set -e
readonly MODULES_DIR="modules"
readonly MODULE_INSTALL_FILE_NAME="install.sh"
readonly DEFAULT_MODULES_DOWNLOAD_DIR="/tmp/gruntwork-script-modules"
readonly BIN_DIR="/usr/local/bin"
function print_usage {
echo
echo "Usage: gruntwork-install [OPTIONS]"
echo
echo "Download a Gruntwork Script Module and install it."
echo
echo "Required Arguments:"
echo
echo -e " --repo\t\tThe repo to install from."
echo -e " --tag\t\t\tThe version of --repo to install. Follows the syntax described at https://github.com/gruntwork-io/fetch#tag-constraint-expressions."
echo
echo "Specify exactly one of:"
echo
echo -e " --module-name\t\tThe name of a module to install. Can be any folder within the $MODULES_DIR directory of --repo."
echo -e " --binary-name\t\tThe name of a binary to install. Can be any file uploaded as a release asset in --repo."
echo -e " --binary-sha256-checksum\t\tThe SHA256 checksum of the binary specified by --binary-name. Should be exactly 64 characters."
echo -e " --binary-sha512-checksum\t\tThe SHA512 checksum of the binary specified by --binary-name. Should be exactly 128 characters."
echo -e " --binary-name\t\tThe name of a binary to install. Can be any file uploaded as a release asset in --repo."
echo
echo "Optional Arguments:"
echo
echo -e " --module-param\tA key-value pair of the format key=value you wish to pass to the module as a parameter. May be used multiple times."
echo -e " --download-dir\tThe directory to which the module will be downloaded and from which it will be installed. Default: $DEFAULT_MODULES_DOWNLOAD_DIR"
echo -e " --branch\t\tDownload the latest commit from this branch in --repo. This is an alternative to --tag, used only for testing."
echo -e " --help\t\tShow this help text and exit."
echo
echo "Example:"
echo
echo " gruntwork-install \\"
echo " --module-name 'vault-ssh-helper' \\"
echo " --repo 'https://github.com/gruntwork-io/script-modules' \\"
echo " --tag '~>0.0.3' \\"
echo " --module-param 'install-dir=/opt/vault-ssh-helper' \\"
echo " --module-param 'owner=ubuntu'"
echo
}
function log {
local readonly level="$1"
local readonly message="$2"
local readonly timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local readonly scriptname="$(basename "$0")"
>&2 echo -e "${timestamp} [${level}] [$scriptname] ${message}"
}
function log_info {
local readonly message="$1"
log "INFO" "$message"
}
function log_warn {
local readonly message="$1"
log "WARN" "$message"
}
function log_error {
local readonly message="$1"
log "ERROR" "$message"
}
# Assert that a given binary is installed on this box
function assert_is_installed {
local readonly name="$1"
if [[ ! $(command -v ${name}) ]]; then
log_error "The binary '$name' is required by this script but is not installed or in the system's PATH."
exit 1
fi
}
function assert_not_empty {
local readonly arg_name="$1"
local readonly arg_value="$2"
if [[ -z "$arg_value" ]]; then
log_error "The value for '$arg_name' cannot be empty"
print_usage
exit 1
fi
}
function assert_env_var_not_empty {
local readonly var_name="$1"
local readonly var_value="${!var_name}"
if [[ -z "$var_value" ]]; then
log_error "Required environment variable $var_name not set."
exit 1
fi
}
# Download the files of the given Script Module using fetch (https://github.com/gruntwork-io/fetch)
function fetch_script_module {
local readonly module_name="$1"
local readonly tag="$2"
local readonly branch="$3"
local readonly download_dir="$4"
local readonly repo="$5"
# We want to make sure that all folders down to $download_path/$module_name exists, but that $download_path/$module_name itself is empty.
mkdir -p "$download_dir/$module_name/"
rm -Rf "$download_dir/$module_name/"
# Note that fetch can safely handle blank arguments for --tag or --branch
# If both --tag and --branch are specified, --branch will be used
log_info "Downloading module $module_name from $repo"
fetch --repo="$repo" --tag="$tag" --branch="$branch" --source-path="/modules/$module_name" "$download_dir/$module_name"
}
# Download a binary asset from a GitHub release using fetch (https://github.com/gruntwork-io/fetch)
function fetch_binary {
local readonly binary_name="$1"
local readonly tag="$2"
local readonly download_dir="$3"
local readonly repo="$4"
local readonly sha256_checksum="$5"
local readonly sha512_checksum="$6"
local binary_name_full=""
binary_name_full=$(determine_binary_name "$binary_name")
local readonly full_download_path="$download_dir/$binary_name_full"
local readonly full_dest_path="$BIN_DIR/$binary_name"
# We want to make sure that all folders down to $download_path exist, but that $download_path/$binary_name_full does not
mkdir -p "$download_dir"
rm -f "$download_dir/$binary_name_full"
if [[ ! -z "$sha256_checksum" ]]; then
fetch --repo="$repo" --tag="$tag" --release-asset="$binary_name_full" "$download_dir" --release-asset-checksum-algo "sha256" --release-asset-checksum "$sha256_checksum"
elif [[ ! -z "$sha512_checksum" ]]; then
fetch --repo="$repo" --tag="$tag" --release-asset="$binary_name_full" "$download_dir" --release-asset-checksum-algo "sha512" --release-asset-checksum "$sha512_checksum"
else
fetch --repo="$repo" --tag="$tag" --release-asset="$binary_name_full" "$download_dir"
fi
log_info "Moving $full_download_path to $full_dest_path and setting execute permissions"
sudo mv "$full_download_path" "$full_dest_path"
sudo chmod u+x "$full_dest_path"
}
# Validate that at least one file was downloaded from the module; otherwise throw an error.
function validate_module {
local readonly module_name="$1"
local readonly download_dir="$2"
local readonly tag="$3"
local readonly branch="$4"
local reaodnly repo="$5"
if [[ ! -e "$download_dir/$module_name" ]]; then
log_error "No files were downloaded. Are you sure \"$module_name\" is a valid Script Module in $repo (tag = $tag, branch = $branch)?"
exit 1
fi
}
# http://stackoverflow.com/a/2264537/483528
function to_lower_case {
tr '[:upper:]' '[:lower:]'
}
function get_os_name {
uname | to_lower_case
}
function get_os_arch {
uname -m
}
function string_contains {
local readonly str="$1"
local readonly contains="$2"
[[ "$str" == *"$contains"* ]]
}
function get_os_arch_gox_format {
local readonly arch=$(get_os_arch)
if $(string_contains "$arch" "64"); then
echo "amd64"
elif $(string_contains "$arch" "386"); then
echo "386"
elif $(string_contains "$arch" "arm"); then
echo "arm"
fi
}
# We release binaries with the name following the format <NAME>_<OS>_<ARCH> (e.g. foo_linux_amd64). Given the NAME of
# a binary, this function adds the proper OS and ARCH to it for the current OS.
function determine_binary_name {
local readonly binary_name="$1"
local readonly os_name=$(get_os_name)
local readonly os_arch=$(get_os_arch_gox_format)
echo "${binary_name}_${os_name}_${os_arch}"
}
# Take in a key-value pair of the format key=value and convert it to the format that Gruntwork bash scripts expect:
# --key value
function convert_module_params_format {
local readonly key_value_pair="$1"
local readonly key="${key_value_pair%%=*}"
local readonly val="${key_value_pair#*=}"
echo "--${key} \"${val}\""
}
function run_module {
local readonly module_name="$1"
local readonly download_dir="$2"
shift 2
local readonly module_params=($@)
local readonly install_script_path="${download_dir}/${module_name}/${MODULE_INSTALL_FILE_NAME}"
log_info "Executing $install_script_path ${module_params[@]}"
chmod u+x "$install_script_path"
eval "$install_script_path ${module_params[@]}"
}
function install_script_module {
local tag=""
local branch=""
local module_name=""
local binary_name=""
local binary_sha256_checksum=""
local binary_sha512_checksum=""
local repo=""
local download_dir="$DEFAULT_MODULES_DOWNLOAD_DIR"
local module_params=()
while [[ $# > 0 ]]; do
local key="$1"
case "$key" in
--tag)
tag="$2"
shift
;;
--branch)
branch="$2"
shift
;;
--module-name)
module_name="$2"
shift
;;
--binary-name)
binary_name="$2"
shift
;;
--binary-sha256-checksum)
binary_sha256_checksum="$2"
shift
;;
--binary-sha512-checksum)
binary_sha512_checksum="$2"
shift
;;
--repo)
repo="$2"
shift
;;
--module-param)
local readonly param=$(convert_module_params_format "$2")
module_params+=("$param")
shift
;;
--download-dir)
download_dir="$2"
shift
;;
--help)
print_usage
exit
;;
*)
echo "ERROR: Unrecognized option: $key"
print_usage
exit 1
;;
esac
shift
done
assert_is_installed fetch
assert_env_var_not_empty "GITHUB_OAUTH_TOKEN"
assert_not_empty "--repo" "$repo"
if [[ ( -z "$module_name" && -z "$binary_name" ) || ( ! -z "$module_name" && ! -z "$binary_name" ) ]]; then
log_error "You must specify exactly one of --module-name or --binary-name."
exit 1
fi
if [[ ! -z "$binary_name" && -z "$tag" ]]; then
log_error "--binary-name can only be used if you specify a release via --tag."
exit 1
fi
if [[ ! -z "$binary_sha256_checksum" && ! -z "$binary_sha512_checksum" ]]; then
log_error "You must specify at most one of --binary-sha256-checksum and --binary-sha512-checksum"
exit 1
fi
if [[ ! -z "$module_name" ]]; then
log_info "Installing from $module_name..."
fetch_script_module "$module_name" "$tag" "$branch" "$download_dir" "$repo"
validate_module "$module_name" "$download_dir" "$tag" "$branch" "$repo"
run_module "$module_name" "$download_dir" "${module_params[@]}"
else
log_info "Installing $binary_name..."
fetch_binary "$binary_name" "$tag" "$download_dir" "$repo" "$binary_sha256_checksum" "$binary_sha512_checksum"
fi
log_info "Success!"
}
install_script_module "$@"