filtron.sh 6.6 KB

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