-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite
executable file
·224 lines (207 loc) · 6.48 KB
/
suite
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
#!/usr/bin/env bash
# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
# SPDX-License-Identifier: GNU General Public License v3.0 or later
[[ -z "${REPO_ROOT}" ]] && REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
LXC_SUITES=()
for file in "${REPO_ROOT}"/*-env/suite.sh; do
file="$(basename "$(dirname "$file")")"
LXC_SUITES+=("${file%-env}")
done
LXC_SUITE_NAME="<suite-name>"
LXC_SUITE_IMAGE="<image-name>"
suite_usage() {
_cmd="$(basename "$0") $LXC_SUITE_NAME $LXC_SUITE_IMAGE"
if [[ $LXC_SUITE_NAME != "<suite-name>" ]]; then
_cmd="./${LXC_SUITE_NAME} ${LXC_SUITE_IMAGE}"
fi
cat <<EOF
usage::
$_cmd create
$_cmd build
$_cmd drop
$_cmd [start|stop]
$_cmd { command .. }
$_cmd cmd { command .. }
$_cmd pkg-[install|remove] { pkg-name .. }
$_cmd install [suite|base]
$_cmd uninstall [suite]
$_cmd show [images|suite|info|config]
$_cmd root
$_cmd --
create:
Build container from image $LXC_SUITE_IMAGE and install suite $LXC_SUITE_NAME.
build:
Build and launch container '$LXC_HOST_PREFIX-$LXC_SUITE_IMAGE'
and 'install base' packages
install:
:base: prepare LXC; install basic packages
:suite: install LXC ${LXC_SUITE_NAME} suite into container
uninstall
:suite: uninstall LXC ${LXC_SUITE_NAME} suite from container
drop:
Delete container from image $LXC_SUITE_IMAGE (suite $LXC_SUITE_NAME).
[start|stop]:
start/stop container
{ command .. }:
Execute { command .. } as user '${SERVICE_USER:-root}' in container $LXC_SUITE_IMAGE
from suite $LXC_SUITE_NAME.
cmd { command .. }:
Execute { command .. } in root's bash with './utils/lib.sh' sourced.
pkg-install / pkg-remove { pkg-name .. }:
Install / remove packages { pkg-name .. } in container from image
$LXC_SUITE_IMAGE (suite $LXC_SUITE_NAME)
show:
:info: show container info
:config: show container config
:suite: show services of the container
:images: show information of the image
root:
start a bash for user root in the container
--:
run additional commands from suite
EOF
if [[ $LXC_SUITE_NAME == "<suite-name>" ]]; then
echo "LXC suites:"
echo " ${LXC_SUITES[*]}" | $FMT
fi
if [[ -n ${LOCAL_IMAGES[*]} ]]; then
echo "suite images:"
echo " ${LOCAL_IMAGES[*]}" | $FMT
fi
}
init_suite(){
[[ -z $1 ]] \
&& echo "ERROR: missing argument: <suite-name>" >&2 && exit 42
LXC_ENV="$1-env/suite.sh"
[[ ! -e ${REPO_ROOT}/${LXC_ENV} ]] \
&& echo "ERROR: lxc suite does not exists: ${REPO_ROOT}/${LXC_ENV}.env " >&2 && exit 42
export LXC_ENV
if [[ $LXC_ENV != $("${REPO_ROOT}/utils/lxc.sh" --getenv LXC_ENV) ]];then
_load_lib
die 42 "LXC_ENV in subprocess is different!"
fi
# shellcheck source=utils/lxc.sh
source "${REPO_ROOT}/utils/lxc.sh" --source
}
_load_lib(){
# shellcheck source=utils/lib.sh
source "${REPO_ROOT}/utils/lib.sh"
}
check_help_option() {
case $1 in
''|-h|--help) _load_lib; suite_usage; exit
;;
esac
}
suite_main() {
check_help_option "$1"
LXC_SUITE_NAME="$1"; shift
check_help_option "$1"
init_suite "$LXC_SUITE_NAME"
LXC_SUITE_IMAGE="$1"; shift
check_help_option "$1"
[[ -z $LXC_SUITE_IMAGE ]] && echo "ERROR: missing argument: <image-name>" >&2 && exit 42
sudo_or_exit
local container="$LXC_HOST_PREFIX-$LXC_SUITE_IMAGE"
case $1 in
drop|root|pkg-*)
if ! lxc_exists "$container";then
err_msg "container ** $container ** does not yet exists"
exit 42
fi
esac
case $1 in
create)
suite_main_create "$LXC_SUITE_IMAGE"
;;
build)
build_container "$container"
;;
install)
case $2 in
suite|base)
lxc_exec_cmd "$container" "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2"
;;
'') die 42 "missing 'install' option" ;;
*) die 42 "unknow 'show' option: $2" ;;
esac
;;
uninstall)
case $2 in
suite)
lxc_exec_cmd "$container" "${LXC_REPO_ROOT}/utils/lxc.sh" __uninstall "$2"
;;
'') die 42 "missing 'uninstall' option" ;;
*) die 42 "unknow 'show' option: $2" ;;
esac
;;
drop)
suite_main_drop "$LXC_SUITE_IMAGE"
;;
start|stop)
lxc "$1" "$container"
;;
show)
case $2 in
suite)
lxc_exec_cmd "$container" "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite
;;
config)
lxc config show "$container"
;;
info)
lxc info "$container"
;;
image)
lxc image info "$LXC_SUITE_IMAGE"
;;
'')
die 42 "missing 'show' option"
;;
*)
die 42 "unknow 'show' option: $2"
;;
esac
;;
root)
shift
lxc_exec_cmd "$container" 'bash'
;;
pkg-install)
shift
lxc_exec_cmd "$container" 'source "./utils/lib.sh"; pkg_install' "$@"
;;
pkg-remove)
shift
lxc_exec_cmd "$container" 'source "./utils/lib.sh"; pkg_remove' "$@"
;;
cmd)
shift
if [[ -z "$*" ]]; then
suite_usage
die 42 "missing '{ command .. }'"
fi
lxc_exec_cmd "$container" 'source "./utils/lib.sh";' "$@"
;;
--)
shift
if ! in_container; then
if ! lxc_exists "$container"; then
warn_msg "container ** $container ** does not yet exists"
LXC_SUITE_IMAGE='<suite-image>'
fi
fi
suite_commands "$@"
;;
*)
if [[ -z "$*" ]]; then
suite_usage
die 42 "missing '{ command .. }'"
fi
suite_main_cmd "$LXC_SUITE_IMAGE" "$@"
;;
esac
}
# ----------------------------------------------------------------------------
suite_main "$@"
# ----------------------------------------------------------------------------