filtron.sh 7.3 KB

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