-
Notifications
You must be signed in to change notification settings - Fork 0
/
solidblocks-cloud-init-bootstrap.sh
229 lines (175 loc) · 7.12 KB
/
solidblocks-cloud-init-bootstrap.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
################################################################################
# included 'solidblocks-shell/lib/text-include.sh'
################################################################################
function tput_wrapper() {
if [[ -t 0 ]]; then
tput $@
fi
}
COLOR_RED=$(tput_wrapper -Txterm-256color setaf 1)
COLOR_GREEN=$(tput_wrapper -Txterm-256color setaf 2)
COLOR_YELLOW=$(tput_wrapper -Txterm-256color setaf 3)
COLOR_BLACK=$(tput_wrapper -Txterm-256color setaf 0)
COLOR_BLUE=$(tput_wrapper -Txterm-256color setaf 4)
COLOR_MAGENTA=$(tput_wrapper -Txterm-256color setaf 5)
COLOR_CYAN=$(tput_wrapper -Txterm-256color setaf 6)
COLOR_WHITE=$(tput_wrapper -Txterm-256color setaf 15)
COLOR_RESET=$(tput_wrapper -Txterm-256color sgr0)
FORMAT_BOLD=$(tput_wrapper bold)
FORMAT_DIM=$(tput_wrapper dim)
FORMAT_UNDERLINE=$(tput_wrapper smul)
FORMAT_RESET=${COLOR_RESET}
################################################################################
################################################################################
# included 'solidblocks-shell/lib/utils-include.sh'
################################################################################
function ensure_command() {
local command=${1:-}
if ! type "${command}" &>/dev/null; then
log_echo_die "command '${command}' not installed"
fi
}
################################################################################
################################################################################
# included 'solidblocks-shell/lib/log-include.sh'
################################################################################
function log_echo_info() {
echo "${*}"
}
# see https://pellepelster.github.io/solidblocks/shell/log/#log_echo_error
function log_echo_error() {
echo -e "${COLOR_RED}${*}${COLOR_RESET}" 1>&2;
}
function log_echo_warning() {
echo -e "${COLOR_YELLOW}${*}${COLOR_RESET}" 1>&2;
}
# see https://pellepelster.github.io/solidblocks/shell/log/#log_echo_die
function log_echo_die() { log_echo_error "${*}"; exit 4;}
# see https://pellepelster.github.io/solidblocks/shell/log/#log_divider_header
function log_divider_header() {
echo ""
echo "================================================================================"
log_info "$@"
echo "--------------------------------------------------------------------------------"
}
# see https://pellepelster.github.io/solidblocks/shell/log/#log_divider_header
function log_divider_footer() {
echo "================================================================================"
echo
}
function log() {
local log_level=${1}
shift || true
local _message="${*}"
local color="${COLOR_WHITE}"
case "${log_level}" in
info) color="${COLOR_BLACK}" ;;
debug) color="${COLOR_CYAN}" ;;
warning) color="${COLOR_YELLOW}" ;;
success) color="${COLOR_GREEN}" ;;
error) color="${COLOR_RED}" ;;
emergency) color="${COLOR_RED}" ;;
esac
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") ${color}$(printf "[%9s]" "${log_level}")${COLOR_RESET} ${_message}" 1>&2
}
# see https://pellepelster.github.io/solidblocks/shell/log/#log
function log_info () { log info "${*}"; }
function log_success() { log success "${*}"; }
function log_warning() { log warning "${*}"; }
function log_debug() { log debug "${*}"; }
function log_error() { log error "${*}"; }
# see https://pellepelster.github.io/solidblocks/shell/log/#log_die
function log_die() { log emergency "${*}"; exit 4;}
################################################################################
################################################################################
# included 'solidblocks-shell/lib/curl-include.sh'
################################################################################
CURL_WRAPPER_RETRY_DELAY=${CURL_WRAPPER_RETRY_DELAY:-5}
CURL_WRAPPER_RETRIES=${CURL_WRAPPER_RETRIES:-10}
function curl_wrapper() {
ensure_command "curl"
local try=0
while [ $try -lt ${CURL_WRAPPER_RETRIES} ] && ! curl --retry-connrefused --fail --silent --location --show-error "$@"; do
try=$((try+1))
log_echo_error "curl call '$@' (${try}/${CURL_WRAPPER_RETRIES}) failed, retrying in ${CURL_WRAPPER_RETRY_DELAY} seconds"
sleep "${CURL_WRAPPER_RETRY_DELAY}"
done
}
################################################################################
################################################################################
# included 'solidblocks-shell/lib/apt.sh'
################################################################################
#!/usr/bin/env bash
function apt_update_repositories {
apt-get update
}
function apt_update_system() {
apt-get \
-o Dpkg::Options::="--force-confnew" \
--force-yes \
-fuy \
dist-upgrade
}
function apt_ensure_package {
local package=${1}
echo -n "checking if package '${package}' is installed..."
if [[ $(dpkg-query -W -f='${Status}' "${package}" 2>/dev/null | grep -c "ok installed") -eq 0 ]];
then
echo "not found, installing now"
while ! DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -qq -y "${package}"; do
echo "installing failed retrying in 30 seconds"
sleep 30
apt_update_repositories
done
else
echo "ok"
fi
}
################################################################################
################################################################################
# included 'solidblocks-shell/lib/package-include.sh'
################################################################################
function package_update_system() {
if which apt >/dev/null 2>&1; then
apt_update_system
fi
}
function package_update_repositories() {
if which apt >/dev/null 2>&1; then
apt_update_repositories
fi
}
function package_ensure_package() {
if which apt >/dev/null 2>&1; then
apt_ensure_package $@
fi
}
################################################################################
export SOLIDBLOCKS_DIR="${SOLIDBLOCKS_DIR:-/solidblocks}"
export SOLIDBLOCKS_VERSION="v0.2.7"
export SOLIDBLOCKS_CLOUD_INIT_CHECKSUM="cb641167cd2e57ad40ba7a499ad941a33a4575ae9103213c35fa030e89fda932"
export SOLIDBLOCKS_BASE_URL="${SOLIDBLOCKS_BASE_URL:-https://github.com}"
function solidblocks_bootstrap_cloud_init() {
package_update_repositories
package_ensure_package "unzip"
groupadd solidblocks
useradd solidblocks -g solidblocks
# shellcheck disable=SC2086
mkdir -p ${SOLIDBLOCKS_DIR}/{templates,lib,secrets}
chmod 770 ${SOLIDBLOCKS_DIR}
chown solidblocks:solidblocks ${SOLIDBLOCKS_DIR}
chmod -R 770 ${SOLIDBLOCKS_DIR}
chown -R solidblocks:solidblocks ${SOLIDBLOCKS_DIR}
chmod -R 700 ${SOLIDBLOCKS_DIR}/secrets
local temp_file="$(mktemp)"
curl_wrapper "${SOLIDBLOCKS_BASE_URL}/pellepelster/solidblocks/releases/download/${SOLIDBLOCKS_VERSION}/solidblocks-cloud-init-${SOLIDBLOCKS_VERSION}.zip" > "${temp_file}"
echo "${SOLIDBLOCKS_CLOUD_INIT_CHECKSUM} ${temp_file}" | sha256sum -c
(
cd "${SOLIDBLOCKS_DIR}" || exit 1
unzip "${temp_file}"
rm -rf "${temp_file}"
)
source "${SOLIDBLOCKS_DIR}/lib/storage.sh"
source "${SOLIDBLOCKS_DIR}/lib/lego.sh"
}
solidblocks_bootstrap_cloud_init