filtron.sh 6.4 KB

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