filtron.sh 5.5 KB

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