on master help sign in
utils/filtron.sh: generalize systemd, accounts and golang tasks

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

commit709ac51d331853ad29c4f6de26c695a7aeeca125 authorMarkus Heiser <markus.heiser@darmarit.de> dateSat 1 Feb 2020, 16:59 (7 years ago) parent56a93ee
3 files changed+134−116
utils/filtron.sh+10−89 utils/lib.sh+119−4 utils/searx.sh+5−23
utils/filtron.sh+10−89
@@ -109,7 +109,7 @@ main() {
109109
110110 shell)
111111 sudo_or_exit
112− interactive_shell
112+ interactive_shell "${SERVICE_USER}"
113113 ;;
114114 inspect)
115115 case $2 in
@@ -136,19 +136,19 @@ main() {
136136 sudo_or_exit
137137 case $2 in
138138 all) remove_all;;
139− user) remove_user ;;
139+ user) drop_service_account "${SERVICE_USER}" ;;
140140 *) usage "$_usage"; exit 42;;
141141 esac ;;
142142 activate)
143143 sudo_or_exit
144144 case $2 in
145− service) activate_service ;;
145+ service) systemd_activate_service "${SERVICE_NAME}" ;;
146146 *) usage "$_usage"; exit 42;;
147147 esac ;;
148148 deactivate)
149149 sudo_or_exit
150150 case $2 in
151− service) deactivate_service ;;
151+ service) systemd_deactivate_service "${SERVICE_NAME}" ;;
152152 *) usage "$_usage"; exit 42;;
153153 esac ;;
154154 apache)
@@ -174,11 +174,11 @@ install_all() {
174174 rst_title "Install $SERVICE_NAME (service)"
175175 assert_user
176176 wait_key
177− install_go
177+ install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
178178 wait_key
179179 install_filtron
180180 wait_key
181− install_service
181+ systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
182182 wait_key
183183 echo
184184 if ! service_is_available "http://${FILTRON_LISTEN}" ; then
@@ -203,56 +203,15 @@ remove_all() {
203203It goes without saying that this script can only be used to remove
204204installations that were installed with this script."
205205
206− remove_service
206+ systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
207207 wait_key
208− remove_user
208+ drop_service_account "${SERVICE_USER}"
209209 rm -r "$FILTRON_ETC" 2>&1 | prefix_stdout
210210 if service_is_available "${PUBLIC_URL}"; then
211211 MSG="** Don't forget to remove your public site! (${PUBLIC_URL}) **" wait_key 10
212212 fi
213213}
214214
215−install_service() {
216− rst_title "Install System-D Unit ${SERVICE_NAME}.service" section
217− echo
218− install_template "${SERVICE_SYSTEMD_UNIT}" root root 644
219− wait_key
220− activate_service
221−}
222−
223−remove_service() {
224− if ! ask_yn "Do you really want to deinstall $SERVICE_NAME?"; then
225− return
226− fi
227− deactivate_service
228− rm "${SERVICE_SYSTEMD_UNIT}" 2>&1 | prefix_stdout
229−}
230−
231−activate_service() {
232− rst_title "Activate $SERVICE_NAME (service)" section
233− echo
234− tee_stderr <<EOF | bash 2>&1
235−systemctl enable $SERVICE_NAME.service
236−systemctl restart $SERVICE_NAME.service
237−EOF
238− tee_stderr <<EOF | bash 2>&1
239−systemctl status --no-pager $SERVICE_NAME.service
240−EOF
241−}
242−
243−deactivate_service() {
244− rst_title "De-Activate $SERVICE_NAME (service)" section
245− echo
246− tee_stderr <<EOF | bash 2>&1 | prefix_stdout
247−systemctl stop $SERVICE_NAME.service
248−systemctl disable $SERVICE_NAME.service
249−EOF
250−}
251−
252−user_is_available() {
253− sudo -i -u "$SERVICE_USER" echo \$HOME &>/dev/null
254−}
255−
256215assert_user() {
257216 rst_title "user $SERVICE_USER" section
258217 echo
@@ -277,44 +236,6 @@ grep -qFs -- 'source $GO_ENV' ~/.profile || echo 'source $GO_ENV' >> ~/.profile
277236EOF
278237}
279238
280−remove_user() {
281− rst_title "Drop $SERVICE_USER HOME" section
282− if ask_yn "Do you really want to drop $SERVICE_USER home folder?"; then
283− userdel -r -f "$SERVICE_USER" 2>&1 | prefix_stdout
284− else
285− rst_para "Leave HOME folder $(du -sh "$SERVICE_HOME") unchanged."
286− fi
287−}
288−
289−interactive_shell(){
290− echo "// exit with ${_BCyan}CTRL-D${_creset}"
291− sudo -H -u ${SERVICE_USER} -i
292−}
293−
294−_service_prefix=" |$SERVICE_USER| "
295−
296−go_is_available() {
297− sudo -i -u "$SERVICE_USER" which go &>/dev/null
298−}
299−
300−install_go() {
301− rst_title "Install Go in user's HOME" section
302−
303− rst_para "download and install go binary .."
304− cache_download "${GO_PKG_URL}" "${GO_TAR}"
305−
306− tee_stderr 0.1 <<EOF | sudo -i -u "$SERVICE_USER" | prefix_stdout "$_service_prefix"
307−echo \$PATH
308−echo \$GOPATH
309−mkdir -p \$HOME/local
310−rm -rf \$HOME/local/go
311−tar -C \$HOME/local -xzf ${CACHE}/${GO_TAR}
312−EOF
313− sudo -i -u "$SERVICE_USER" <<EOF | prefix_stdout
314−! which go >/dev/null && echo "ERROR - Go Installation not found in PATH!?!"
315−which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
316−EOF
317−}
318239
319240filtron_is_installed() {
320241 [[ -f $SERVICE_HOME/go-apps/bin/filtron ]]
@@ -356,12 +277,12 @@ EOF
356277
357278 apache_is_installed && info_msg "Apache is installed."
358279
359− if user_is_available; then
280+ if service_account_is_available "$SERVICE_USER"; then
360281 info_msg "service account $SERVICE_USER available."
361282 else
362283 err_msg "service account $SERVICE_USER not available!"
363284 fi
364− if go_is_available; then
285+ if go_is_available "$SERVICE_USER"; then
365286 info_msg "~$SERVICE_USER: go is installed"
366287 else
367288 err_msg "~$SERVICE_USER: go is not installed"
utils/lib.sh+119−4
@@ -25,10 +25,6 @@ if [[ -z "$CACHE" ]]; then
2525 CACHE="${REPO_ROOT}/cache"
2626fi
2727
28−if [[ -z "$SYSTEMD_UNITS" ]]; then
29− SYSTEMD_UNITS="/lib/systemd/system"
30−fi
31−
3228if [[ -z ${DIFF_CMD} ]]; then
3329 DIFF_CMD="diff -u"
3430 if command -v colordiff >/dev/null; then
@@ -477,6 +473,125 @@ service_is_available() {
477473 return "$exit_val"
478474}
479475
476+# golang
477+# ------
478+
479+go_is_available() {
480+
481+ # usage: go_is_available $SERVICE_USER && echo "go is installed!"
482+
483+ sudo -i -u "${1}" which go &>/dev/null
484+}
485+
486+install_go() {
487+
488+ # usage: install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
489+
490+ local _service_prefix=" |${3}| "
491+
492+ rst_title "Install Go in user's HOME" section
493+
494+ rst_para "download and install go binary .."
495+ cache_download "${1}" "${2}"
496+
497+ tee_stderr 0.1 <<EOF | sudo -i -u "${3}" | prefix_stdout "$_service_prefix"
498+echo \$PATH
499+echo \$GOPATH
500+mkdir -p \$HOME/local
501+rm -rf \$HOME/local/go
502+tar -C \$HOME/local -xzf ${CACHE}/${2}
503+EOF
504+ sudo -i -u "${3}" <<EOF | prefix_stdout
505+! which go >/dev/null && echo "ERROR - Go Installation not found in PATH!?!"
506+which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
507+EOF
508+}
509+
510+# system accounts
511+# ---------------
512+
513+service_account_is_available() {
514+
515+ # usage: service_account_is_available "$SERVICE_USER" && echo "OK"
516+
517+ sudo -i -u "$1" echo \$HOME &>/dev/null
518+}
519+
520+drop_service_account() {
521+
522+ # usage: drop_service_account "${SERVICE_USER}"
523+
524+ rst_title "Drop ${1} HOME" section
525+ if ask_yn "Do you really want to drop ${1} home folder?"; then
526+ userdel -r -f "${1}" 2>&1 | prefix_stdout
527+ else
528+ rst_para "Leave HOME folder $(du -sh "${1}") unchanged."
529+ fi
530+}
531+
532+interactive_shell(){
533+
534+ # usage: interactive_shell "${SERVICE_USER}"
535+
536+ echo "// exit with ${_BCyan}CTRL-D${_creset}"
537+ sudo -H -u "${1}" -i
538+}
539+
540+
541+# systemd
542+# -------
543+
544+SYSTEMD_UNITS="${SYSTEMD_UNITS:-/lib/systemd/system}"
545+
546+systemd_install_service() {
547+
548+ # usage: systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
549+
550+ rst_title "Install System-D Unit ${1}" section
551+ echo
552+ install_template "${2}" root root 644
553+ wait_key
554+ systemd_activate_service "${1}"
555+}
556+
557+systemd_remove_service() {
558+
559+ # usage: systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
560+
561+ if ! ask_yn "Do you really want to deinstall ${1}?"; then
562+ return
563+ fi
564+ systemd_deactivate_service "${1}"
565+ rm "${2}" 2>&1 | prefix_stdout
566+}
567+
568+systemd_activate_service() {
569+
570+ # usage: systemd_activate_service "${SERVICE_NAME}"w
571+
572+ rst_title "Activate ${1} (service)" section
573+ echo
574+ tee_stderr <<EOF | bash 2>&1
575+systemctl enable ${1}.service
576+systemctl restart ${1}.service
577+EOF
578+ tee_stderr <<EOF | bash 2>&1
579+systemctl status --no-pager ${1}.service
580+EOF
581+}
582+
583+systemd_deactivate_service() {
584+
585+ # usage: systemd_deactivate_service "${SERVICE_NAME}"
586+
587+ rst_title "De-Activate ${1} (service)" section
588+ echo
589+ tee_stderr <<EOF | bash 2>&1 | prefix_stdout
590+systemctl stop ${1}.service
591+systemctl disable ${1}.service
592+EOF
593+}
594+
480595
481596# Apache
482597# ------
utils/searx.sh+5−23
@@ -124,7 +124,7 @@ main() {
124124
125125 shell)
126126 sudo_or_exit
127− interactive_shell
127+ interactive_shell "${SERVICE_USER}"
128128 ;;
129129 inspect)
130130 case $2 in
@@ -153,7 +153,7 @@ main() {
153153 sudo_or_exit
154154 case $2 in
155155 all) remove_all;;
156− user) remove_user ;;
156+ user) drop_service_account "${SERVICE_USER}";;
157157 pyenv) remove_pyenv ;;
158158 searx-src) remove_searx ;;
159159 *) usage "$_usage"; exit 42;;
@@ -250,7 +250,7 @@ cp -f ${SEARX_SETTINGS}.backup ${SEARX_SETTINGS}
250250EOF
251251 ;;
252252 "start interactiv shell")
253− interactive_shell
253+ interactive_shell "${SERVICE_USER}"
254254 ;;
255255 esac
256256 chown "${SERVICE_USER}:${SERVICE_USER}" "${SEARX_SETTINGS}"
@@ -275,16 +275,12 @@ installations that were installed with this script."
275275 fi
276276 remove_searx_uwsgi
277277 wait_key
278− remove_user
278+ drop_service_account "${SERVICE_USER}"
279279 if service_is_available "${PUBLIC_URL}"; then
280280 MSG="** Don't forgett to remove your public site! (${PUBLIC_URL}) **" wait_key 10
281281 fi
282282}
283283
284−user_is_available() {
285− sudo -i -u "$SERVICE_USER" echo \$HOME &>/dev/null
286−}
287−
288284assert_user() {
289285 rst_title "user $SERVICE_USER" section
290286 echo
@@ -299,15 +295,6 @@ EOF
299295 #echo "export SERVICE_HOME=$SERVICE_HOME"
300296}
301297
302−remove_user() {
303− rst_title "Drop $SERVICE_USER HOME" section
304− if ask_yn "Do you really want to drop $SERVICE_USER home folder?"; then
305− userdel -r -f "$SERVICE_USER" 2>&1 | prefix_stdout
306− else
307− rst_para "Leave HOME folder $(du -sh "$SERVICE_HOME") unchanged."
308− fi
309−}
310−
311298clone_is_available() {
312299 [[ -f "$SEARX_SETTINGS" ]]
313300}
@@ -445,11 +432,6 @@ deactivate_service() {
445432 uWSGI_restart
446433}
447434
448−interactive_shell(){
449− echo "// exit with ${_BCyan}CTRL-D${_creset}"
450− sudo -H -u ${SERVICE_USER} -i
451−}
452−
453435git_diff() {
454436 sudo -H -u "${SERVICE_USER}" -i <<EOF
455437cd ${SEARX_REPO_FOLDER}
@@ -491,7 +473,7 @@ EOF
491473
492474 apache_is_installed && info_msg "Apache is installed."
493475
494− if user_is_available; then
476+ if service_account_is_available "$SERVICE_USER"; then
495477 info_msg "Service account $SERVICE_USER exists."
496478 else
497479 err_msg "Service account $SERVICE_USER does not exists!"