filtron.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  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. APACHE_SITE="searx.conf"
  24. # shellcheck disable=SC2034
  25. CONFIG_FILES=(
  26. "${FILTRON_RULES}"
  27. "${SERVICE_SYSTEMD_UNIT}"
  28. )
  29. # ----------------------------------------------------------------------------
  30. usage() {
  31. # ----------------------------------------------------------------------------
  32. # shellcheck disable=SC1117
  33. cat <<EOF
  34. usage:
  35. $(basename "$0") shell
  36. $(basename "$0") install [all|user]
  37. $(basename "$0") update [filtron]
  38. $(basename "$0") remove [all]
  39. $(basename "$0") activate [service]
  40. $(basename "$0") deactivate [service]
  41. $(basename "$0") show [service]
  42. shell
  43. start interactive shell from user ${SERVICE_USER}
  44. install / remove all
  45. complete setup of filtron service
  46. update filtron
  47. Update filtron installation of user ${SERVICE_USER}
  48. activate
  49. activate and start service daemon (systemd unit)
  50. deactivate service
  51. stop and deactivate service daemon (systemd unit)
  52. install user
  53. add service user '$SERVICE_USER' at $SERVICE_HOME
  54. show service
  55. show service status and log
  56. EOF
  57. [ ! -z ${1+x} ] && echo -e "$1"
  58. }
  59. main() {
  60. rst_title "$SERVICE_NAME" part
  61. local _usage="ERROR: unknown or missing $1 command $2"
  62. case $1 in
  63. --source-only) ;;
  64. -h|--help) usage; exit 0;;
  65. shell)
  66. sudo_or_exit
  67. interactive_shell
  68. ;;
  69. show)
  70. case $2 in
  71. service)
  72. sudo_or_exit
  73. show_service
  74. ;;
  75. *) usage "$_usage"; exit 42;;
  76. esac ;;
  77. install)
  78. sudo_or_exit
  79. case $2 in
  80. all) install_all ;;
  81. user) assert_user ;;
  82. *) usage "$_usage"; exit 42;;
  83. esac ;;
  84. update)
  85. sudo_or_exit
  86. case $2 in
  87. filtron) update_filtron ;;
  88. *) usage "$_usage"; exit 42;;
  89. esac ;;
  90. remove)
  91. sudo_or_exit
  92. case $2 in
  93. all) remove_all;;
  94. user) remove_user ;;
  95. *) usage "$_usage"; exit 42;;
  96. esac ;;
  97. activate)
  98. sudo_or_exit
  99. case $2 in
  100. service) activate_service ;;
  101. *) usage "$_usage"; exit 42;;
  102. esac ;;
  103. deactivate)
  104. sudo_or_exit
  105. case $2 in
  106. service) deactivate_service ;;
  107. *) usage "$_usage"; exit 42;;
  108. esac ;;
  109. *) usage "ERROR: unknown or missing command $1"; exit 42;;
  110. esac
  111. }
  112. install_all() {
  113. rst_title "Install $SERVICE_NAME (service)"
  114. assert_user
  115. wait_key
  116. install_go
  117. wait_key
  118. install_filtron
  119. wait_key
  120. install_service
  121. wait_key
  122. if apache_is_installed; then
  123. install_apache_site
  124. wait_key
  125. fi
  126. }
  127. remove_all() {
  128. rst_title "De-Install $SERVICE_NAME (service)"
  129. remove_service
  130. wait_key
  131. remove_user
  132. rm -r "$FILTRON_ETC" 2>&1 | prefix_stdout
  133. wait_key
  134. }
  135. filtron_is_available() {
  136. curl --insecure "http://${FILTRON_LISTEN}" &>/dev/null
  137. }
  138. api_is_available() {
  139. curl --insecure "http://${FILTRON_API}" &>/dev/null
  140. }
  141. target_is_available() {
  142. curl --insecure "http://${FILTRON_TARGET}" &>/dev/null
  143. }
  144. install_service() {
  145. rst_title "Install System-D Unit ${SERVICE_NAME}.service" section
  146. echo
  147. install_template "${SERVICE_SYSTEMD_UNIT}" root root 644
  148. wait_key
  149. activate_service
  150. }
  151. remove_service() {
  152. if ! ask_yn "Do you really want to deinstall $SERVICE_NAME?"; then
  153. return
  154. fi
  155. deactivate_service
  156. rm "${SERVICE_SYSTEMD_UNIT}" 2>&1 | prefix_stdout
  157. }
  158. activate_service() {
  159. rst_title "Activate $SERVICE_NAME (service)" section
  160. echo
  161. tee_stderr <<EOF | bash 2>&1
  162. systemctl enable $SERVICE_NAME.service
  163. systemctl restart $SERVICE_NAME.service
  164. EOF
  165. tee_stderr <<EOF | bash 2>&1
  166. systemctl status $SERVICE_NAME.service
  167. EOF
  168. }
  169. deactivate_service() {
  170. rst_title "De-Activate $SERVICE_NAME (service)" section
  171. echo
  172. tee_stderr <<EOF | bash 2>&1 | prefix_stdout
  173. systemctl stop $SERVICE_NAME.service
  174. systemctl disable $SERVICE_NAME.service
  175. EOF
  176. }
  177. user_is_available() {
  178. sudo -i -u "$SERVICE_USER" echo \$HOME &>/dev/null
  179. }
  180. assert_user() {
  181. rst_title "user $SERVICE_USER" section
  182. echo
  183. tee_stderr 1 <<EOF | bash | prefix_stdout
  184. sudo -H adduser --shell /bin/bash --system --home $SERVICE_HOME \
  185. --disabled-password --group --gecos 'Filtron' $SERVICE_USER
  186. sudo -H usermod -a -G shadow $SERVICE_USER
  187. groups $SERVICE_USER
  188. EOF
  189. SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME)"
  190. export SERVICE_HOME
  191. echo "export SERVICE_HOME=$SERVICE_HOME"
  192. cat > "$GO_ENV" <<EOF
  193. export GOPATH=\$HOME/go-apps
  194. export PATH=\$PATH:\$HOME/local/go/bin:\$GOPATH/bin
  195. EOF
  196. echo "Environment $GO_ENV has been setup."
  197. tee_stderr <<EOF | sudo -i -u $SERVICE_USER
  198. grep -qFs -- 'source $GO_ENV' ~/.profile || echo 'source $GO_ENV' >> ~/.profile
  199. EOF
  200. }
  201. remove_user() {
  202. rst_title "Drop $SERVICE_USER HOME" section
  203. if ask_yn "Do you really want to drop $SERVICE_USER home folder?"; then
  204. userdel -r -f "$SERVICE_USER" 2>&1 | prefix_stdout
  205. else
  206. rst_para "Leave HOME folder $(du -sh "$SERVICE_HOME") unchanged."
  207. fi
  208. }
  209. interactive_shell(){
  210. echo "// exit with CTRL-D"
  211. sudo -H -u ${SERVICE_USER} -i
  212. }
  213. _service_prefix=" |$SERVICE_USER| "
  214. go_is_available() {
  215. sudo -i -u "$SERVICE_USER" which go &>/dev/null
  216. }
  217. install_go() {
  218. rst_title "Install Go in user's HOME" section
  219. rst_para "download and install go binary .."
  220. cache_download "${GO_PKG_URL}" "${GO_TAR}"
  221. tee_stderr 0.1 <<EOF | sudo -i -u "$SERVICE_USER" | prefix_stdout "$_service_prefix"
  222. echo \$PATH
  223. echo \$GOPATH
  224. mkdir -p \$HOME/local
  225. rm -rf \$HOME/local/go
  226. tar -C \$HOME/local -xzf ${CACHE}/${GO_TAR}
  227. EOF
  228. echo
  229. sudo -i -u "$SERVICE_USER" <<EOF | prefix_stdout
  230. ! which go >/dev/null && echo "Go Installation not found in PATH!?!"
  231. which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
  232. EOF
  233. }
  234. filtron_is_installed() {
  235. [[ -f $SERVICE_HOME/go-apps/bin/filtron ]]
  236. }
  237. install_filtron() {
  238. rst_title "Install filtron in user's ~/go-apps" section
  239. echo
  240. tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_service_prefix"
  241. go get -v -u github.com/asciimoo/filtron
  242. EOF
  243. install_template --no-eval "$FILTRON_RULES" root root 644
  244. }
  245. update_filtron() {
  246. rst_title "Update filtron" section
  247. echo
  248. tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_service_prefix"
  249. go get -v -u github.com/asciimoo/filtron
  250. EOF
  251. }
  252. show_service() {
  253. rst_title "service status & log"
  254. echo
  255. apache_is_installed && info_msg "Apache is installed."
  256. if user_is_available; then
  257. info_msg "service account $SERVICE_USER available."
  258. else
  259. err_msg "service account $SERVICE_USER not available!"
  260. fi
  261. if go_is_available; then
  262. info_msg "~$SERVICE_USER: go is installed"
  263. else
  264. err_msg "~$SERVICE_USER: go is not installed"
  265. fi
  266. if filtron_is_installed; then
  267. info_msg "~$SERVICE_USER: filtron app is installed"
  268. else
  269. err_msg "~$SERVICE_USER: filtron app is not installed!"
  270. fi
  271. if api_is_available; then
  272. info_msg "API available at: http://${FILTRON_API}"
  273. else
  274. err_msg "API not available at: http://${FILTRON_API}"
  275. fi
  276. if filtron_is_available; then
  277. info_msg "Filtron listening on: http://${FILTRON_LISTEN}"
  278. else
  279. err_msg "Filtron does not listening on: http://${FILTRON_LISTEN}"
  280. fi
  281. if target_is_available; then
  282. info_msg "Filtron's target is available at: http://${FILTRON_TARGET}"
  283. else
  284. err_msg "Filtron's target is not available at: http://${FILTRON_TARGET}"
  285. fi
  286. wait_key
  287. echo
  288. systemctl --no-pager -l status filtron.service
  289. echo
  290. read -r -s -n1 -t 2 -p "// use CTRL-C to stop monitoring the log"
  291. echo
  292. while true; do
  293. trap break 2
  294. journalctl -f -u filtron
  295. done
  296. return 0
  297. }
  298. install_apache_site() {
  299. rst_title "Install Apache site $APACHE_SITE" section
  300. echo
  301. err_msg "not yet implemented (${APACHE_SITE})"; return 42
  302. # apache_install_site "${APACHE_SITE}"
  303. }
  304. # ----------------------------------------------------------------------------
  305. main "$@"
  306. # ----------------------------------------------------------------------------