Repository navigation
Expand file tree
/
Copy pathpodbash.sh
More file actions
executable file
·339 lines (315 loc) · 11 KB
/
Copy pathpodbash.sh
File metadata and controls
executable file
·339 lines (315 loc) · 11 KB
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
335
336
337
338
339
#!/bin/bash
PARAMS=""
set -a
SCRIPTS_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
if [[ -e "${SCRIPTS_ROOT}/options" ]]
then
source "${SCRIPTS_ROOT}/options"
fi
if [[ -e "${CURRENT_PROJECT_PATH}/options" ]];
then
source "${CURRENT_PROJECT_PATH}/options"
fi
set +a
if [[ -s ${PROJECT_SETTINGS} ]];
then
source ${PROJECT_SETTINGS}
fi
if [[ -n ${CURRENT_PROJECT_PATH} ]];
then
if [[ ! -d ${CURRENT_PROJECT_PATH}/settings_files ]];
then
mkdir -p ${CURRENT_PROJECT_PATH}/settings_files/env_files;
chmod 0775 ${CURRENT_PROJECT_PATH}/settings_files ${CURRENT_PROJECT_PATH}/settings_files/env_files;
fi
fi
function local_install_check()
{
for install in $(find ${CONTAINER_SCRIPTS_ROOT} -type f -name "install.sh")
do
source ${install}
if [[ $(type -t install_check) == "function" ]]
then
new_install_check=$(type install_check)
fi
if [[ $new_install_check != $old_install_check ]];
then
install_check
else
function install_check() { test=""; }
fi
if [[ $(type -t install_check) == "function" ]];
then
old_install_check=$(type install_check)
fi
done
}
if [[ "$1" != "install" && "$1" != "uninstall" ]]; then
local_install_check;
fi
if ! [[ -s ${PROJECT_SETTINGS} ]];
then
echo -e "**!! PROJECT_SETTINGS is EMPTY !!**"
fi
while (( "$#" )); do
case "$1" in
install)
bash ${SCRIPTS_ROOT}/scripts/container_scripts_install.sh -r
exit $?
;;
uninstall)
bash ${SCRIPTS_ROOT}/scripts/container_scripts_uninstall.sh -r
exit $?
;;
create)
labels=()
iarray=()
alllabels=('variables' 'templates' 'directories' 'network' 'pull' 'build' 'settings' 'pods' 'containers' 'systemd')
parray=( "${@:2}" )
if [[ ${#parray} -gt 0 ]]
then
if [[ ${parray[0]^^} == 'ALL' ]]
then
labels=( ${alllabels[@]} )
else
# labels=${parray[@]:1}
declare -A vars
vars['variables']=0
vars['templates']=1
vars['directories']=2
vars['network']=3
vars['pull']=4
vars['build']=5
vars['settings']=6
vars['pods']=7
vars['containers']=8
vars['systemd']=9
i=0
for j in "${parray[@]}"
do
iarray[$i]=${vars[$j]}
i=$i+1
done
IFS=$'\n' sorted=($(sort <<<"${iarray[*]}"))
unset IFS
i=0
for j in "${sorted[@]}"
do
labels[i]="${alllabels[$j]}"
i=$i+1
done
fi
else
labels=( ${alllabels[@]} )
fi
if [[ ${#labels[@]} -eq 0 ]]
then
labels=( ${parray[@]} )
fi
for i in "${labels[@]}"
do
case "${i^^}" in
'VARIABLES')
echo -e "\nOkay, lets find out more about you...\n"
bash ${SCRIPTS_ROOT}/scripts/get_variables.sh
if [[ $? -ne 0 ]]
then
exit $?
fi
;;
'TEMPLATES')
echo -e "\nOkay, lets find out more about you...\n"
bash ${SCRIPTS_ROOT}/scripts/templates.sh
if [[ $? -ne 0 ]]
then
exit $?
fi
;;
'DIRECTORIES')
echo -e "\nNow I will create necessary directtories.\n"
bash ${SCRIPTS_ROOT}/scripts/create_directories.sh
if [[ $? -ne 0 ]]
then
exit $?
fi
;;
'NETWORK')
echo -e "\nNow for general network settings.\n"
bash ${SCRIPTS_ROOT}/scripts/create_network.sh
if [[ $? -ne 0 ]]
then
exit $?
fi
;;
'PULL')
echo -e "\nI will now download container images, if they are not already present.\n"
bash ${SCRIPTS_ROOT}/scripts/image_acq.sh
wait $!
if [[ $? -ne 0 ]]
then
exit $?
fi
;;
'BUILD')
echo -e "\nI will now build any custom container images, if they are not already present.\n"
bash ${SCRIPTS_ROOT}/scripts/image_build.sh
if [[ $? -ne 0 ]]
then
exit $?
fi
;;
'SETTINGS')
echo -e "\n please choose the settings file you want to use.\n"
bash ${SCRIPTS_ROOT}/scripts/settings.sh
;;
'PODS')
echo -e "\n I will create the pods.\n"
bash ${SCRIPTS_ROOT}/scripts/create_pods.sh
;;
'CONTAINERS')
echo -e "\n and now I will create the containers...\n"
bash ${SCRIPTS_ROOT}/scripts/create_containers.sh
;;
'SYSTEMD')
echo -e "\n fancy some systemd?...\n"
echo -e "Generate and install systemd --user unit files? : "
select yn in "Yes" "No"; do
case $yn in
Yes ) SYSD="TRUE"; break;;
No ) SYSD="FALSE"; break;;
esac
done
if [[ ${SYSD} == "TRUE" ]]
then
bash ${SCRIPTS_ROOT}/scripts/systemd.sh
fi
;;
*)
echo -e "Error: unknown option passed to create : ${i^^}"
exit 1
;;
esac
done
exit $?
;;
clean)
bash ${SCRIPTS_ROOT}/scripts/cleanup.sh
exit $?
;;
clean_save_settings)
bash ${SCRIPTS_ROOT}/scripts/clean_save_settings.sh
exit $?
;;
custom)
command=${2}
shift 2;
bash $(find ${CONTAINER_SCRIPTS_ROOT}/containers -name "${command}.sh") $@
exit $?
;;
status)
shift;
if [[ ${1} == "restricted" ]];
then
scripts_install_check
fi
if [[ -s "${PROJECT_SETTINGS}" ]]
then
echo -e "PROJECT_SETTINGS file exists and is not empty!"
else
echo -e "PROJECT_SETTINGS file is empty!"
fi
if [[ -f "${CURRENT_PROJECT_PATH}/status.sh" ]];
then
bash "${CURRENT_PROJECT_PATH}/status.sh";
fi
exit $?
;;
interact)
if [[ -n ${PROJECT_TYPE} && ${PROJECT_TYPE} == "RESTRICTED" ]];
then
if [[ -z ${USER_NAME} ]]
then
echo -e "A user name must be defined for the podman user for this command to work."
exit 1
fi
runuser --login ${USER_NAME} -P -c "XDG_RUNTIME_DIR=\"/run/user/$(id -u ${USER_NAME})\" DBUS_SESSION_BUS_ADDRESS=\"unix:path=${XDG_RUNTIME_DIR}/bus\" cd; ${2}"
exit $?
else
echo -e "Wrong PROJECT_TYPE!"
fi
;;
update)
if [[ ${PROJECT_TYPE} == "RESTRICTED" && -n ${USER_NAME} ]]
then
su ${USER_NAME} -c "pushd ~ &>/dev/null; podman ps --format=\"{{.Names}}\" | grep -oP '^((?!infra).)*$' | while read name; do podman exec -u root ${name} bash -c \"apt-get update; apt-get upgrade -y\"; done; popd &>/dev/null;"
else
pushd ~ &>/dev/null; podman ps --format=\"{{.Names}}\" | grep -oP '^((?!infra).)*$' | while read name; do podman exec -u root ${name} bash -c \"apt-get update; apt-get upgrade -y\"; done; popd &>/dev/null;
fi
exit $?
;;
refresh)
if [[ ${PROJECT_TYPE} == "RESTRICTED" && -n ${USER_NAME} ]]
then
su ${USER_NAME} -c "pushd ~ &/dev/null; podman pod stop ${POD_NAME}; podman image prune --all -f; popd &>/dev/null;"
else
pushd ~ &>/dev/null; podman pod stop ${POD_NAME}; podman image prune --all -f; popd &>/dev/null;
fi
${SCRIPTS_ROOT}/scripts/image_acq.sh
${SCRIPTS_ROOT}/scripts/image_build.sh
echo "now run the command 'systemctl reboot'."
;;
help|-h|-?|--help)
echo -e "$ ./podbash.sh command - where command is one of clean,
create [ variables, templates, directories, network, pull, build, settings, pods, containers, systemd ],
install, refresh, status, settings, update or help.
clean - cleans the project, deleting the containers and pod, and deleting
settings files etc.
create - on its own creates a new project, running through the various stages
which are:
variables - gets variables from user
templates - calls into container/pod directories templates.sh to
use envsubst to complete template files in appropriate
template directory
directories - calls into directories.sh in container directories to
make any directories and set permissions etc
network - calls net.sh in containers/pods directories to set network
settings for containers
pull - pulls the podman images that are mentioned in source.sh in
each container directory
build - builds any custom containers that are defined
settings - calls choose_settings.sh to choose the appropriate settings file
and copy or envsubst them to the appropriate place
pods - creates pods
containers - creates the containers from the container scripts, pre,run,post
in that order (default - see ${SCRIPTS_ROOT}/options)
systemd - creates and installs the systemd units
Use any combination of the stage names after the create verb to perform
those stages.
install - when you first clone the repository, you can set the appropriate
permissions using this verb.
interact - commands following the interact verb will be run inside the podman
pod using systemd context. You are often better to run
'podman pod exec -it container_name bash' in the user account.
postgit - in case you reinstall django_artisan filebase completes and copies
manage.py and wsgi.py and copies them to the appropriate places and
sets the file and directory permissions inside the container correctly
refresh - deletes all images, downloads them and rebuilds the custom images.
status - reports the current status of the project.
settings - replaces the settings file with one you choose from the
dev/production directory.
tests_on - changes database permissions sufficient to allow tests to be run
update - runs apt-get update in all the containers. Note that when you create a
project you can specify that the containers are updated where possible,
which will be done automatically by podman.
help - this text."
exit 0
;;
*) # unsupported flags
echo "Error: Unsupported action $1" >&2
exit 1
;;
esac
done # set positional arguments in their proper place
#eval set -- "$PARAMS"
echo "I need a command!!"
exit 1