@@ -0,0 +1,214 @@
1+#!/usr/bin/env bash
2+# -*- coding: utf-8; mode: sh -*-
3+# shellcheck disable=SC2119
4+
5+# shellcheck source=utils/lib.sh
6+source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
7+
8+# ----------------------------------------------------------------------------
9+# config
10+# ----------------------------------------------------------------------------
11+
12+FILTRON_ETC="/etc/filtron"
13+
14+SERVICE_NAME="filtron"
15+SERVICE_USER="${SERVICE_NAME}"
16+SERVICE_HOME="/home/${SERVICE_USER}"
17+SERVICE_SYSTEMD_UNIT="${SYSTEMD_UNITS}/${SERVICE_NAME}.service"
18+
19+# shellcheck disable=SC2034
20+SERVICE_GROUP="${SERVICE_USER}"
21+
22+GO_ENV="${SERVICE_HOME}/.go_env"
23+GO_PKG_URL="https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz"
24+GO_TAR=$(basename "$GO_PKG_URL")
25+
26+# ----------------------------------------------------------------------------
27+usage(){
28+# ----------------------------------------------------------------------------
29+
30+ # shellcheck disable=SC1117
31+ cat <<EOF
32+
33+usage:
34+
35+ $(basename "$0") shell
36+ $(basename "$0") install [all|user]
37+ $(basename "$0") remove [all]
38+ $(basename "$0") activate [server]
39+ $(basename "$0") deactivate [server]
40+
41+shell - start interactive shell with user ${SERVICE_USER}
42+install user - add service user '$SERVICE_USER' at $SERVICE_HOME
43+
44+EOF
45+ [ ! -z ${1+x} ] && echo -e "$1"
46+}
47+
48+main(){
49+ rst_title "$SERVICE_NAME" part
50+
51+ local _usage="ERROR: unknown or missing $1 command $2"
52+
53+ case $1 in
54+ --source-only) ;;
55+ -h|--help) usage ;;
56+
57+ shell)
58+ sudo_or_exit
59+ interactive_shell
60+ ;;
61+ install)
62+ sudo_or_exit
63+ case $2 in
64+ all) install_all ;;
65+ user) assert_user ;;
66+ *) usage "$_usage"; exit 42;;
67+ esac ;;
68+ remove)
69+ sudo_or_exit
70+ case $2 in
71+ all) remove_all;;
72+ user) remove_user ;;
73+ *) usage "$_usage"; exit 42;;
74+ esac ;;
75+ activate)
76+ sudo_or_exit
77+ case $2 in
78+ server) activate_server ;;
79+ *) usage "$_usage"; exit 42;;
80+ esac ;;
81+ deactivate)
82+ sudo_or_exit
83+ case $2 in
84+ server) deactivate_server ;;
85+ *) usage "$_usage"; exit 42;;
86+ esac ;;
87+ *) usage "ERROR: unknown or missing command $1"; exit 42;;
88+ esac
89+}
90+
91+install_all() {
92+ rst_title "Install $SERVICE_NAME (service)"
93+ assert_user
94+ install_go
95+ install_filtron
96+ install_server
97+}
98+
99+remove_all() {
100+ rst_title "De-Install $SERVICE_NAME (service)"
101+ remove_server
102+ remove_user
103+ rm -rf "$FILTRON_ETC"
104+ wait_key
105+}
106+
107+install_server() {
108+ rst_title "Install System-D Unit ${SERVICE_NAME}.service ..." section
109+ install_template ${SERVICE_SYSTEMD_UNIT} root root 644
110+ wait_key
111+ activate_server
112+}
113+
114+remove_server() {
115+ if ! ask_yn "Do you really want to deinstall $SERVICE_NAME?"; then
116+ return
117+ fi
118+ deactivate_server
119+ rm "${SERVICE_SYSTEMD_UNIT}"
120+}
121+
122+
123+activate_server () {
124+ rst_title "Activate $SERVICE_NAME (service)" section
125+ tee_stderr <<EOF | bash 2>&1 | prefix_stdout
126+systemctl enable $SERVICE_NAME.service
127+systemctl restart $SERVICE_NAME.service
128+EOF
129+ tee_stderr <<EOF | bash 2>&1 | prefix_stdout
130+systemctl status $SERVICE_NAME.service
131+EOF
132+ wait_key
133+}
134+
135+deactivate_server () {
136+ rst_title "De-Activate $SERVICE_NAME (service)" section
137+ echo
138+ tee_stderr <<EOF | bash 2>&1 | prefix_stdout
139+systemctl stop $SERVICE_NAME.service
140+systemctl disable $SERVICE_NAME.service
141+EOF
142+ wait_key
143+}
144+
145+assert_user() {
146+ rst_title "user $SERVICE_USER" section
147+ echo
148+ tee_stderr 1 <<EOF | bash | prefix_stdout
149+sudo -H adduser --shell /bin/bash --system --home $SERVICE_HOME --group --gecos 'Filtron' $SERVICE_USER
150+sudo -H usermod -a -G shadow $SERVICE_USER
151+groups $SERVICE_USER
152+EOF
153+ SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME)"
154+ export SERVICE_HOME
155+ echo "export SERVICE_HOME=$SERVICE_HOME"
156+
157+ cat > "$GO_ENV" <<EOF
158+export GOPATH=\$HOME/go-apps
159+export PATH=\$PATH:\$HOME/local/go/bin:\$GOPATH/bin
160+EOF
161+ echo "Environment $GO_ENV has been setup."
162+
163+ tee_stderr <<EOF | sudo -i -u $SERVICE_USER
164+grep -qFs -- 'source $GO_ENV' ~/.profile || echo 'source $GO_ENV' >> ~/.profile
165+EOF
166+}
167+
168+remove_user() {
169+ rst_title "Drop $SERVICE_USER HOME" section
170+ if ask_yn "Do you really want to drop $SERVICE_USER home folder?"; then
171+ userdel -r -f "$SERVICE_USER"
172+ else
173+ rst_para "Leave HOME folder $(du -sh "$SERVICE_HOME") unchanged."
174+ fi
175+}
176+
177+interactive_shell(){
178+ echo "// exit with STRG-D"
179+ sudo -H -u ${SERVICE_USER} -i
180+}
181+
182+_service_prefix="$SERVICE_USER@$(hostname) -->| "
183+
184+install_go(){
185+ rst_title "Install Go in user's HOME" section
186+
187+ rst_para "download and install go binary .."
188+ cache_download "${GO_PKG_URL}" "${GO_TAR}"
189+
190+ tee_stderr 0.1 <<EOF | sudo -i -u "$SERVICE_USER" | prefix_stdout "$_service_prefix"
191+echo \$PATH
192+echo \$GOPATH
193+mkdir -p \$HOME/local
194+rm -rf \$HOME/local/go
195+tar -C \$HOME/local -xzf ${CACHE}/${GO_TAR}
196+EOF
197+ echo
198+ sudo -i -u "$SERVICE_USER" <<EOF | prefix_stdout
199+! which go >/dev/null && echo "Go Installation not found in PATH!?!"
200+which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
201+EOF
202+ wait_key
203+}
204+
205+install_filtron() {
206+ tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" | prefix_stdout "$_service_prefix"
207+go get -v -u github.com/asciimoo/filtron 2>&1
208+EOF
209+ install_template "$FILTRON_ETC/rules.json" root root 644
210+}
211+
212+# ----------------------------------------------------------------------------
213+main "$@"
214+# ----------------------------------------------------------------------------