-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-env
252 lines (219 loc) · 6.6 KB
/
base-env
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
# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
# SPDX-License-Identifier: GNU General Public License v3.0 or later
# shellcheck shell=bash
lxc_set_suite_env() {
export LXC_HOST_PREFIX="${LXC_SUITE_NAME:-dev}"
export LXC_SUITE=(
# to disable/enable containers
# just comment/uncomment lines ..
# end of standard support see https://wiki.ubuntu.com/Releases
#"images:ubuntu/16.04" "ubu1604" # April 2021
#"images:ubuntu/18.04" "ubu1804" # April 2023
"images:ubuntu/20.04" "ubu2004" # (EOL 2030)
# EOL see https://fedoraproject.org/wiki/Releases
"images:fedora/31" "fedora31"
# rolling releases see https://www.archlinux.org/releng/releases/
"images:archlinux" "archlinux"
)
}
# ----------------------------------------------------------------------------
# This file is a part of all LXC suites. It is sourced from different context,
# do not manipulate the environment directly, implement functions and manipulate
# environment only is subshells!
# ----------------------------------------------------------------------------
lxc_suite_install_info() {
(
lxc_set_suite_env
cat <<EOF
LXC suite: ${LXC_SUITE_NAME} -- ${PUBLIC_URL:-...}
suite images:
$(echo " ${LOCAL_IMAGES[*]}" | $FMT)
suite containers:
$(echo " ${CONTAINERS[*]}" | $FMT)
EOF
)
}
lxc_suite_install() {
(
lxc_set_suite_env
local ret_val=0
rst_title "install suite '${LXC_SUITE_NAME}' ($(hostname))" part
suite_install
ret_val=$?
if [[ $ret_val == 0 ]]; then
info_msg "suite '${LXC_SUITE_NAME}' installation finished ($(hostname))"
lxc_suite_info
else
err_msg "got exit code $ret_val from installation procedure"
fi
echo
return $ret_val
)
}
lxc_suite_uninstall() {
(
lxc_set_suite_env
local ret_val=0
rst_title "uninstall suite '${LXC_SUITE_NAME}' ($(hostname))" part
echo
suite_uninstall
ret_val=$?
return $ret_val
)
}
__lxc_suite_info() {
echo "IPs of container ${LXC_SUITE_IMAGE:-<image-name>}"
for ip in $(global_IPs) ; do
if [[ $ip =~ .*:.* ]]; then
echo " (${ip%|*}) IPv6: http://[${ip#*|}]"
else
# IPv4:
# shellcheck disable=SC2034,SC2031
echo " (${ip%|*}) IPv4: http://${ip#*|}"
fi
done
local cmd_prefix="./${LXC_SUITE_NAME:-./suite <suite-name>} ${LXC_SUITE_IMAGE:-<image-name>}"
cat <<EOF
Start a interactive bash (root or $SERVICE_USER) using::
${cmd_prefix} root
${cmd_prefix} bash
EOF
}
lxc_suite_info() {
(
lxc_set_suite_env
__lxc_suite_info
)
}
assert_user() {
(
rst_title "user $SERVICE_USER" section
echo
suite_root_shell <<EOF
useradd --shell /bin/bash --system \
--home-dir "$SERVICE_HOME" \
$SERVICE_USER
mkdir "$SERVICE_HOME"
chown -R "$SERVICE_GROUP:$SERVICE_GROUP" "$SERVICE_HOME"
groups $SERVICE_USER
EOF
local cmd_prefix="./${LXC_SUITE_NAME:-./suite <suite-name>} ${LXC_SUITE_IMAGE:-<image-name>}"
info_msg "to start bash for this user use::"
info_msg " ${cmd_prefix} bash"
)
}
create_pyenv() {
(
rst_title "Create virtualenv (python)" section
echo
info_msg "create pyenv in ${SERVICE_PYENV}"
suite_service_user_shell <<EOF
rm -rf "${SERVICE_PYENV}"
python3 -m venv "${SERVICE_PYENV}"
grep -qFs -- 'source ${SERVICE_PYENV}/bin/activate' ~/.profile \
|| echo 'source ${SERVICE_PYENV}/bin/activate' >> ~/.profile
EOF
info_msg "inspect python's virtual environment"
suite_service_user_shell <<EOF
command -v python && python --version
EOF
wait_key
info_msg "install needed python packages"
suite_service_user_shell <<EOF
pip install --upgrade pip
pip install wheel
EOF
)
}
suite_service_user_shell(){
# shellcheck disable=SC2154
tee_stderr 0.1 \
| sudo -H -u "${SERVICE_USER}" -i 2>&1 \
| prefix_stdout "${_Yellow}|${SERVICE_USER}|${_creset} "
return "${PIPESTATUS[1]}"
}
suite_root_shell(){
tee_stderr 0.1 \
| bash 2>&1 \
| prefix_stdout
}
suite_main_create(){
(
[[ -z $1 ]] && echo "ERROR: missing argument: <image-name>" >&2 && exit 42
container="$LXC_HOST_PREFIX-$1"
sudo_or_exit
build_container "$container"
lxc_exec_cmd "$container" "${LXC_REPO_ROOT}/utils/lxc.sh" __install suite
)
}
suite_main_drop(){
(
[[ -z $1 ]] && echo "ERROR: missing argument: <image-name>" >&2 && exit 42
container="$LXC_HOST_PREFIX-$1"
sudo_or_exit
lxc_delete_container "$container"
)
}
suite_main_cmd(){
(
[[ -z $1 ]] && echo "ERROR: missing argument: <image-name>" >&2 && exit 42
container="$LXC_HOST_PREFIX-$1"; shift
sudo_or_exit
if ! lxc_exists "$container";then
err_msg "container ** $container ** does not yet exists"
exit 42
fi
lxc_exec_cmd "$container" sudo -u "${SERVICE_USER:-root}" -i bash -c \""$*"\"
)
}
suite_install() {
die 42 "[${LXC_SUITE_NAME}] Sorry, suite_install not yet implemented! (can't install)"
}
suite_uninstall() {
die 42 "[${LXC_SUITE_NAME}] Sorry, suite_uninstall not yet implemented! (can't un-install)"
}
suite_commands_usage() {
_cmd="./${LXC_SUITE_NAME} ${LXC_SUITE_IMAGE}"
cat <<EOF
usage:
$_cmd -- [docs|test]
docs: print documentation of the suite
test: run some tests in the container
EOF
}
suite_test() {
echo "nothing to test"
}
__suite_commands() {
local container="$LXC_HOST_PREFIX-$LXC_SUITE_IMAGE"
case $1 in
docs)
[[ $LXC_SUITE_IMAGE == '<suite-image>' ]] \
&& die 42 "container does not exists"
rst_title "services of $container"
echo
lxc_exec_cmd \
"$container" "${LXC_REPO_ROOT}/utils/lxc.sh" \
__show suite 2>/dev/null
;;
test)
# this command has to be executed in the conatiner
if ! in_container; then
lxc_exec_cmd "$container" "./${LXC_SUITE_NAME} ${LXC_SUITE_IMAGE}" -- test
else
suite_test
fi
;;
*)
suite_commands_usage
if [[ -z "$*" ]]; then
die 42 "missing '{ command .. }'"
else
die 42 "command: '$*' is not a suite command"
fi
;;
esac
}
suite_commands() {
__suite_commands "$@"
}