freedombone-app-turtl 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # turtl app
  12. #
  13. # http://portallinux.es/instalacion-servidor-turtl-debian-8
  14. # http://framacloud.org/cultiver-son-jardin/installation-de-turtl/
  15. #
  16. # License
  17. # =======
  18. #
  19. # Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
  20. #
  21. # This program is free software: you can redistribute it and/or modify
  22. # it under the terms of the GNU Affero General Public License as published by
  23. # the Free Software Foundation, either version 3 of the License, or
  24. # (at your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU Affero General Public License for more details.
  30. #
  31. # You should have received a copy of the GNU Affero General Public License
  32. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. VARIANTS="full full-vim writer"
  34. IN_DEFAULT_INSTALL=0
  35. SHOW_ON_ABOUT=1
  36. TURTL_DOMAIN_NAME=
  37. TURTL_CODE=
  38. TURTL_ONION_PORT=8107
  39. TURTL_PORT=8181
  40. TURTL_REPO="https://github.com/turtl/api.git"
  41. TURTL_COMMIT='53e00a5583f52de8f86ef380fe11c176b5738dcf'
  42. TURTL_ADMIN_PASSWORD=
  43. TURTL_STORAGE_LIMIT_MB=100
  44. TURTL_BASE_DIR=/etc/turtl
  45. # part of a hack to enable/disable signups
  46. TURTL_SIGNUP_STRING='Signup a new user'
  47. turtl_users_file=$TURTL_BASE_DIR/api/controllers/users.lisp
  48. turtl_variables=(ONION_ONLY
  49. DEFAULT_DOMAIN_NAME
  50. TURTL_DOMAIN_NAME
  51. TURTL_CODE
  52. TURTL_STORAGE_LIMIT_MB
  53. DDNS_PROVIDER
  54. MY_EMAIL_ADDRESS
  55. MY_USERNAME)
  56. function logging_on_turtl {
  57. echo -n ''
  58. }
  59. function logging_off_turtl {
  60. echo -n ''
  61. }
  62. function change_password_turtl {
  63. change_username="$1"
  64. new_user_password="$2"
  65. }
  66. function remove_user_turtl {
  67. remove_username="$1"
  68. }
  69. function add_user_turtl {
  70. new_username="$1"
  71. new_user_password="$2"
  72. echo '0'
  73. }
  74. function install_interactive_turtl {
  75. if [ ! $ONION_ONLY ]; then
  76. ONION_ONLY='no'
  77. fi
  78. if [[ $ONION_ONLY != "no" ]]; then
  79. TURTL_DOMAIN_NAME='notes.local'
  80. write_config_param "TURTL_DOMAIN_NAME" "$TURTL_DOMAIN_NAME"
  81. else
  82. function_check interactive_site_details
  83. interactive_site_details "turtl" "TURTL_DOMAIN_NAME" "TURTL_CODE"
  84. fi
  85. APP_INSTALLED=1
  86. }
  87. function turtl_disable_registrations {
  88. if grep -q "$TURTL_SIGNUP_STRING" $turtl_users_file; then
  89. if [ -f $turtl_users_file ]; then
  90. cp $turtl_users_file $TURTL_BASE_DIR/.users.lisp
  91. sed -i '/(route (:post "\/users") (req res)/,/(send-json res user))))/{//!d}' $turtl_users_file
  92. sed -i 's|(send-json res user))))|())|g' $turtl_users_file
  93. chown -R turtl:turtl $TURTL_BASE_DIR
  94. systemctl restart turtl
  95. fi
  96. fi
  97. }
  98. function turtl_enable_registrations {
  99. if ! grep -q "$TURTL_SIGNUP_STRING" $turtl_users_file; then
  100. if [ -f $TURTL_BASE_DIR/.users.lisp ]; then
  101. cp $TURTL_BASE_DIR/.users.lisp $turtl_users_file
  102. rm $TURTL_BASE_DIR/.users.lisp
  103. chown -R turtl:turtl $TURTL_BASE_DIR
  104. systemctl restart turtl
  105. fi
  106. fi
  107. }
  108. function configure_interactive_turtl_signups {
  109. # This implements a hack which removes or adds the function needed
  110. # to sign up new users. It should eventually be removed once that
  111. # capability exists within the api
  112. dialog --title $"Allow new turtl signups" \
  113. --backtitle $"Freedombone Control Panel" \
  114. --defaultno \
  115. --yesno $"\nAllow registration of new users?" 10 60
  116. sel=$?
  117. case $sel in
  118. 0)
  119. turtl_enable_registrations
  120. dialog --title $"Allow new turtl signups" \
  121. --msgbox $"New turtl user registrations are now allowed" 6 60
  122. return;;
  123. 1)
  124. turtl_disable_registrations
  125. dialog --title $"Disable new turtl signups" \
  126. --msgbox $"New turtl user registrations are now disabled" 6 60
  127. return;;
  128. 255) return;;
  129. esac
  130. }
  131. function configure_interactive_turtl_storage {
  132. data=$(tempfile 2>/dev/null)
  133. trap "rm -f $data" 0 1 2 5 15
  134. dialog --title $"Change storage limit" \
  135. --backtitle $"Freedombone Control Panel" \
  136. --inputbox $"Enter a storage limit in megabytes." 8 75 "$TURTL_STORAGE_LIMIT_MB" 2>$data
  137. sel=$?
  138. case $sel in
  139. 0)
  140. STORAGE=$(<$data)
  141. if [ ${#STORAGE} -gt 0 ]; then
  142. TURTL_STORAGE_LIMIT_MB=$STORAGE
  143. sed -i "s|defparameter *default-storage-limit*.*|defparameter *default-storage-limit* ${TURTL_STORAGE_LIMIT_MB})|g" $TURTL_BASE_DIR/api/config/config.lisp
  144. systemctl restart turtl
  145. dialog --title $"Change storage limit" \
  146. --msgbox $"Storage limit changed to ${TURTL_STORAGE_LIMIT_MB}M" 6 50
  147. fi
  148. ;;
  149. esac
  150. }
  151. function configure_interactive_turtl {
  152. data=$(tempfile 2>/dev/null)
  153. trap "rm -f $data" 0 1 2 5 15
  154. dialog --backtitle $"Freedombone Control Panel" \
  155. --title $"turtl app settings" \
  156. --radiolist $"Choose an operation:" 12 70 3 \
  157. 1 $"Enable/disable new user registrations" off \
  158. 2 $"Change storage limit" off \
  159. 3 $"Exit" on 2> $data
  160. sel=$?
  161. case $sel in
  162. 1) exit 1;;
  163. 255) exit 1;;
  164. esac
  165. case $(cat $data) in
  166. 1) configure_interactive_turtl_signups;;
  167. 2) configure_interactive_turtl_storage;;
  168. 3) return;;
  169. esac
  170. }
  171. function reconfigure_turtl {
  172. if [ -d $TURTL_BASE_DIR/data ]; then
  173. rm -rf $TURTL_BASE_DIR/data/*
  174. fi
  175. }
  176. function upgrade_turtl {
  177. CURR_TURTL_COMMIT=$(get_completion_param "turtl commit")
  178. if [[ "$CURR_TURTL_COMMIT" == "$TURTL_COMMIT" ]]; then
  179. return
  180. fi
  181. read_config_param "TURTL_DOMAIN_NAME"
  182. function_check set_repo_commit
  183. set_repo_commit $TURTL_BASE_DIR/api "turtl commit" "$TURTL_COMMIT" $TURTL_REPO
  184. # this is used as a crude way of disabling signups and so
  185. # should be superceded in future
  186. if [ -f $TURTL_BASE_DIR/.users.lisp ]; then
  187. turtl_disable_registrations
  188. fi
  189. systemctl restart turtl
  190. nginx_dissite $TURTL_DOMAIN_NAME
  191. chown -R turtl:turtl $TURTL_BASE_DIR
  192. nginx_ensite $TURTL_DOMAIN_NAME
  193. }
  194. function backup_local_turtl {
  195. read_config_param "TURTL_DOMAIN_NAME"
  196. source_directory=$TURTL_BASE_DIR
  197. if [ -d $source_directory ]; then
  198. dest_directory=turtl
  199. function_check suspend_site
  200. suspend_site ${TURTL_DOMAIN_NAME}
  201. function_check backup_directory_to_usb
  202. backup_directory_to_usb $source_directory $dest_directory
  203. function_check restart_site
  204. restart_site
  205. fi
  206. source_directory=/var/lib/rethinkdb
  207. if [ -d $source_directory ]; then
  208. dest_directory=rethinkdb
  209. function_check suspend_site
  210. suspend_site ${TURTL_DOMAIN_NAME}
  211. function_check backup_directory_to_usb
  212. backup_directory_to_usb $source_directory $dest_directory
  213. function_check restart_site
  214. restart_site
  215. fi
  216. }
  217. function restore_local_turtl {
  218. read_config_param "TURTL_DOMAIN_NAME"
  219. if [ $TURTL_DOMAIN_NAME ]; then
  220. temp_restore_dir=/root/tempturtl
  221. restore_directory_from_usb $temp_restore_dir turtl
  222. if [ -d $TURTL_BASE_DIR ]; then
  223. if [ -d /etc/turtl_previous ]; then
  224. rm -rf /etc/turtl_previous
  225. fi
  226. mv $TURTL_BASE_DIR /etc/turtl_previous
  227. fi
  228. temp_source_dir=$(find ${temp_restore_dir} -name turtl)
  229. cp -r ${temp_source_dir} /etc/
  230. if [ ! "$?" = "0" ]; then
  231. if [ -d /etc/turtl_previous ]; then
  232. mv /etc/turtl_previous $TURTL_BASE_DIR
  233. fi
  234. set_user_permissions
  235. backup_unmount_drive
  236. exit 36723
  237. fi
  238. rm -rf ${temp_restore_dir}
  239. chown -R turtl:turtl $TURTL_BASE_DIR
  240. temp_restore_dir=/root/temprethinkdb
  241. restore_directory_from_usb $temp_restore_dir rethinkdb
  242. if [ -d /var/lib/rethinkdb ]; then
  243. if [ -d /var/lib/previous_rethinkdb ]; then
  244. rm -rf /var/lib/previous_rethinkdb
  245. fi
  246. mv /var/lib/rethinkdb /var/lib/previous_rethinkdb
  247. fi
  248. temp_source_dir=$(find ${temp_restore_dir} -name rethinkdb)
  249. cp -r ${temp_source_dir} /var/lib/
  250. if [ ! "$?" = "0" ]; then
  251. if [ -d /var/lib/previous_rethinkdb ]; then
  252. mv /var/lib/previous_rethinkdb /var/lib/rethinkdb
  253. fi
  254. set_user_permissions
  255. backup_unmount_drive
  256. exit 378324
  257. fi
  258. rm -rf ${temp_restore_dir}
  259. fi
  260. }
  261. function backup_remote_turtl {
  262. read_config_param "TURTL_DOMAIN_NAME"
  263. if [ $TURTL_DOMAIN_NAME ]; then
  264. temp_backup_dir=$TURTL_BASE_DIR
  265. if [ -d $temp_backup_dir ]; then
  266. echo $"Backing up turtl"
  267. backup_directory_to_friend $temp_backup_dir turtl
  268. echo $"Backup of turtl complete"
  269. else
  270. echo $"turtl domain specified but not found in $temp_backup_dir"
  271. exit 68725
  272. fi
  273. temp_backup_dir=/var/lib/rethinkdb
  274. if [ -d $temp_backup_dir ]; then
  275. echo $"Backing up rethinkdb"
  276. backup_directory_to_friend $temp_backup_dir rethinkdb
  277. echo $"Backup of rethinkdb complete"
  278. fi
  279. fi
  280. }
  281. function restore_remote_turtl {
  282. read_config_param "TURTL_DOMAIN_NAME"
  283. if [ $TURTL_DOMAIN_NAME ]; then
  284. temp_restore_dir=/root/tempturtl
  285. mkdir $temp_restore_dir
  286. function_check restore_directory_from_friend
  287. restore_directory_from_friend $temp_restore_dir turtl
  288. if [ -d $TURTL_BASE_DIR ]; then
  289. if [ -d /etc/turtl_previous ]; then
  290. rm -rf /etc/turtl_previous
  291. fi
  292. mv $TURTL_BASE_DIR /etc/turtl_previous
  293. fi
  294. temp_source_dir=$(find ${temp_restore_dir} -name turtl)
  295. cp -r ${temp_source_dir} /etc/
  296. if [ ! "$?" = "0" ]; then
  297. if [ -d /etc/turtl_previous ]; then
  298. mv /etc/turtl_previous $TURTL_BASE_DIR
  299. fi
  300. exit 37823
  301. fi
  302. rm -rf ${temp_restore_dir}
  303. temp_restore_dir=/root/temprethinkdb
  304. mkdir $temp_restore_dir
  305. function_check restore_directory_from_friend
  306. restore_directory_from_friend $temp_restore_dir rethinkdb
  307. if [ -d /var/lib/rethinkdb ]; then
  308. if [ -d /var/lib/previous_rethinkdb ]; then
  309. rm -rf /var/lib/previous_rethinkdb
  310. fi
  311. mv /var/lib/rethinkdb /var/lib/previous_rethinkdb
  312. fi
  313. temp_source_dir=$(find ${temp_restore_dir} -name rethinkdb)
  314. cp -r ${temp_source_dir} /var/lib/
  315. if [ ! "$?" = "0" ]; then
  316. if [ -d /var/lib/previous_rethinkdb ]; then
  317. mv /var/lib/previous_rethinkdb /var/lib/rethinkdb
  318. fi
  319. exit 26783
  320. fi
  321. rm -rf ${temp_restore_dir}
  322. fi
  323. }
  324. function remove_turtl {
  325. if [ ! -d $TURTL_BASE_DIR ]; then
  326. return
  327. fi
  328. systemctl stop turtl
  329. systemctl disable turtl
  330. rm /etc/systemd/system/turtl.service
  331. systemctl daemon-reload
  332. remove_rethinkdb
  333. remove_app turtl
  334. remove_completion_param install_turtl
  335. sed -i '/turtl/d' $COMPLETION_FILE
  336. nginx_dissite $TURTL_DOMAIN_NAME
  337. if [ -f /etc/nginx/sites-available/$TURTL_DOMAIN_NAME ]; then
  338. rm /etc/nginx/sites-available/$TURTL_DOMAIN_NAME
  339. fi
  340. remove_certs $TURTL_DOMAIN_NAME
  341. function_check remove_onion_service
  342. remove_onion_service turtl ${TURTL_ONION_PORT}
  343. function_check remove_ddns_domain
  344. remove_ddns_domain $TURTL_DOMAIN_NAME
  345. rm -rf /etc/rethinkdb
  346. rm -rf /var/lib/rethinkdb
  347. rm -rf $TURTL_BASE_DIR
  348. groupdel -f turtl
  349. userdel -r turtl
  350. }
  351. function turtl_setup {
  352. PIDFILE=${PIDFILE:-nil}
  353. BINDADDR=${BINDADDR:-0.0.0.0}
  354. BINDPORT=${BINDPORT:-8181}
  355. PROD_ERR_HANDLING=${PROD_ERR_HANDLING:-t}
  356. if [[ $ONION_ONLY == 'no' ]]; then
  357. FQDN=${FQDN:-$TURTL_DOMAIN_NAME}
  358. SITE_URL=${SITE_URL:-https://$TURTL_DOMAIN_NAME}
  359. else
  360. FQDN=${FQDN:-$TURTL_ONION_HOSTNAME}
  361. SITE_URL=${SITE_URL:-http://$TURTL_ONION_HOSTNAME}
  362. fi
  363. ADMIN_EMAIL=${ADMIN_EMAIL:-$MY_EMAIL_ADDRESS}
  364. EMAIL_FROM=${EMAIL_FROM:-noreply@$DEFAULT_DOMAIN_NAME}
  365. SMTP_USER=${SMTP_USER:-}
  366. SMTP_PASS=${SMTP_PASS:-}
  367. DISPLAY_ERRORS=${DISPLAY_ERRORS:-t}
  368. DEFAULT_STORAGE_LIMIT=${DEFAULT_STORAGE_LIMIT:-100}
  369. STORAGE_INVITE_CREDIT=${STORAGE_INVITE_CREDIT:-25}
  370. if [[ $ONION_ONLY == 'no' ]]; then
  371. LOCAL_UPLOAD_URL=${LOCAL_UPLOAD_URL:-https://$TURTL_DOMAIN_NAME}
  372. else
  373. LOCAL_UPLOAD_URL=${LOCAL_UPLOAD_URL:-http://$TURTL_ONION_HOSTNAME}
  374. fi
  375. LOCAL_UPLOAD_PATH=${LOCAL_UPLOAD_PATH:-"$TURTL_BASE_DIR/data"}
  376. AWS_S3_TOKEN=${AWS_S3_TOKEN:-(:token ''
  377. :secret ''
  378. :bucket ''
  379. :endpoint 'https://s3.amazonaws.com')}
  380. # generates the config-file
  381. cat << __ENDCONFIG__ > $TURTL_BASE_DIR/api/config/config.lisp
  382. (in-package :turtl)
  383. (defparameter *root* (asdf:system-relative-pathname :turtl #P""))
  384. (defparameter *pid-file* "${PIDFILE}")
  385. (defvar *server-bind* "${BINDADDR}")
  386. (defvar *server-port* ${BINDPORT})
  387. (defvar *db-name* "turtl")
  388. (defvar *db-host* "127.0.0.1")
  389. (defvar *db-port* 28015)
  390. (defvar *production-error-handling* ${PROD_ERR_HANDLING})
  391. (defvar *enable-hsts-header* nil)
  392. (defvar *site-url* "${SITE_URL}")
  393. (defvar *api-path* "")
  394. (defvar *admin-email* "${ADMIN_EMAIL}")
  395. (defvar *email-from* "${EMAIL_FROM}")
  396. (defvar *email-user* "${SMTP_USER}")
  397. (defvar *email-pass* "${SMTP_PASS}")
  398. (defvar *display-errors* ${DISPLAY_ERRORS})
  399. (defparameter *default-storage-limit* ${DEFAULT_STORAGE_LIMIT})
  400. (defparameter *storage-invite-credit* ${STORAGE_INVITE_CREDIT})
  401. (vom:config :turtl :info)
  402. (defvar *local-upload* "${LOCAL_UPLOAD_PATH}")
  403. (defvar *local-upload-url* "${LOCAL_UPLOAD_URL}")
  404. (defvar *amazon-s3* "${AWS_S3_TOKEN}")
  405. __ENDCONFIG__
  406. cat $TURTL_BASE_DIR/api/config/config.footer >> $TURTL_BASE_DIR/api/config/config.lisp
  407. # start the turtl server
  408. systemctl restart rethinkdb
  409. if [ ! -f $TURTL_BASE_DIR/quicklisp/setup.lisp ]; then
  410. echo $"$TURTL_BASE_DIR/quicklisp/setup.lisp was not found"
  411. exit 6238234
  412. fi
  413. echo '[Unit]' > /etc/systemd/system/turtl.service
  414. echo 'Description=Note taking service' >> /etc/systemd/system/turtl.service
  415. echo 'Documentation=http://turtl.it' >> /etc/systemd/system/turtl.service
  416. echo 'Requires=network.target' >> /etc/systemd/system/turtl.service
  417. echo 'Requires=rethinkdb.service' >> /etc/systemd/system/turtl.service
  418. echo 'After=network.target' >> /etc/systemd/system/turtl.service
  419. echo 'After=rethinkdb.service' >> /etc/systemd/system/turtl.service
  420. echo '' >> /etc/systemd/system/turtl.service
  421. echo '[Service]' >> /etc/systemd/system/turtl.service
  422. echo 'Type=simple' >> /etc/systemd/system/turtl.service
  423. echo 'User=turtl' >> /etc/systemd/system/turtl.service
  424. echo "WorkingDirectory=$TURTL_BASE_DIR/api/" >> /etc/systemd/system/turtl.service
  425. if [[ "$check_architecture" == *"64"* && "$check_architecture" != *"arm"* ]]; then
  426. echo "ExecStart=$TURTL_BASE_DIR/ccl/lx86cl64 -l $TURTL_BASE_DIR/quicklisp/setup.lisp -l launch.lisp" >> /etc/systemd/system/turtl.service
  427. else
  428. if [[ "$check_architecture" != *"arm"* ]]; then
  429. echo "ExecStart=$TURTL_BASE_DIR/ccl/lx86cl -l $TURTL_BASE_DIR/quicklisp/setup.lisp -l launch.lisp" >> /etc/systemd/system/turtl.service
  430. else
  431. echo "ExecStart=$TURTL_BASE_DIR/ccl/armcl -l $TURTL_BASE_DIR/quicklisp/setup.lisp -l launch.lisp" >> /etc/systemd/system/turtl.service
  432. fi
  433. fi
  434. echo '' >> /etc/systemd/system/turtl.service
  435. echo '[Install]' >> /etc/systemd/system/turtl.service
  436. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/turtl.service
  437. chmod +x /etc/systemd/system/turtl.service
  438. chown -R turtl:turtl $TURTL_BASE_DIR
  439. systemctl enable turtl
  440. systemctl daemon-reload
  441. systemctl start turtl
  442. }
  443. function install_turtl_api {
  444. # https://github.com/ArthurGarnier/turtl-docker
  445. apt-get -yq install wget libterm-readline-perl-perl gcc libuv1-dev
  446. if [ ! -d $TURTL_BASE_DIR ]; then
  447. mkdir -p $TURTL_BASE_DIR
  448. fi
  449. cd $TURTL_BASE_DIR
  450. mkdir cd $TURTL_BASE_DIR/data
  451. check_architecture=$(uname -a)
  452. # Install ccl
  453. if [[ "$check_architecture" != *"arm"* ]]; then
  454. wget -P $TURTL_BASE_DIR/ ftp://ftp.clozure.com/pub/release/1.11/ccl-1.11-linuxx86.tar.gz
  455. mkdir -p $TURTL_BASE_DIR/ccl
  456. tar xvzf $TURTL_BASE_DIR/ccl-1.11-linuxx86.tar.gz -C $TURTL_BASE_DIR/ccl --strip-components=1
  457. else
  458. wget -P $TURTL_BASE_DIR/ ftp://ftp.clozure.com/pub/release/1.11/ccl-1.11-linuxarm.tar.gz
  459. mkdir -p $TURTL_BASE_DIR/ccl
  460. tar xvzf $TURTL_BASE_DIR/ccl-1.11-linuxarm.tar.gz -C $TURTL_BASE_DIR/ccl --strip-components=1
  461. fi
  462. # install quicklisp
  463. cat << __ENDCONFIG__ > $TURTL_BASE_DIR/quicklisp_install
  464. (load (compile-file "asdf.lisp"))
  465. (load (compile-file "quicklisp.lisp"))
  466. (quicklisp-quickstart:install)
  467. (ql:system-apropos "vecto")
  468. (ql:quickload "alexandria")
  469. (ql:quickload "babel")
  470. (ql:quickload "blackbird")
  471. (ql:quickload "bordeaux-threads")
  472. (ql:quickload "cffi")
  473. (ql:quickload "chipz")
  474. (ql:quickload "chunga")
  475. (ql:quickload "cl-annot")
  476. (ql:quickload "cl-async")
  477. (ql:quickload "cl-async-future")
  478. (ql:quickload "cl-base64")
  479. (ql:quickload "cl-fad")
  480. (ql:quickload "cl-libuv")
  481. (ql:quickload "cl-mongo-id")
  482. (ql:quickload "cl-ppcre")
  483. (ql:quickload "cl-rethinkdb")
  484. (ql:quickload "cl-smtp")
  485. (ql:quickload "cl+ssl")
  486. (ql:quickload "cl-syntax")
  487. (ql:quickload "cl-utilities")
  488. (ql:quickload "cl-vectors")
  489. (ql:quickload "do-urlencode")
  490. (ql:quickload "drakma")
  491. (ql:quickload "drakma-async")
  492. (ql:quickload "event-glue")
  493. (ql:quickload "fast-http")
  494. (ql:quickload "fast-io")
  495. (ql:quickload "flexi-streams")
  496. (ql:quickload "ironclad")
  497. (ql:quickload "jonathan")
  498. (ql:quickload "local-time")
  499. (ql:quickload "md5")
  500. (ql:quickload "named-readtables")
  501. (ql:quickload "nibbles")
  502. (ql:quickload "proc-parse")
  503. (ql:quickload "puri")
  504. (ql:quickload "quri")
  505. (ql:quickload "salza2")
  506. (ql:quickload "secure-random")
  507. (ql:quickload "smart-buffer")
  508. (ql:quickload "split-sequence")
  509. (ql:quickload "static-vectors")
  510. (ql:quickload "trivial-backtrace")
  511. (ql:quickload "trivial-features")
  512. (ql:quickload "trivial-garbage")
  513. (ql:quickload "trivial-gray-streams")
  514. (ql:quickload "trivial-types")
  515. (ql:quickload "usocket")
  516. (ql:quickload "vecto")
  517. (ql:quickload "vom")
  518. (ql:quickload "wookie")
  519. (ql:quickload "xmls")
  520. (ql:quickload "xsubseq")
  521. (ql:quickload "yason")
  522. (ql:quickload "zpb-ttf")
  523. (ql:quickload "zpng")
  524. (ql:add-to-init-file)
  525. (ccl::quit)
  526. __ENDCONFIG__
  527. if [ ! -f asdf.lisp ]; then
  528. wget https://common-lisp.net/project/asdf/asdf.lisp
  529. fi
  530. if [ ! -f quicklisp.lisp ]; then
  531. wget https://beta.quicklisp.org/quicklisp.lisp
  532. fi
  533. if [ -d $TURTL_BASE_DIR ]; then
  534. chown -R turtl:turtl $TURTL_BASE_DIR
  535. fi
  536. adduser --disabled-login --home=$TURTL_BASE_DIR --gecos 'turtl' turtl
  537. if [ ! -d $TURTL_BASE_DIR ]; then
  538. echo $"$TURTL_BASE_DIR directory not created"
  539. exit 263493
  540. fi
  541. groupadd turtl
  542. chown -R turtl:turtl $TURTL_BASE_DIR
  543. if [[ "$check_architecture" != *"arm"* ]]; then
  544. if [[ "$check_architecture" == *"64"* ]]; then
  545. su -c "cat $TURTL_BASE_DIR/quicklisp_install | $TURTL_BASE_DIR/ccl/lx86cl64" - turtl
  546. else
  547. su -c "cat $TURTL_BASE_DIR/quicklisp_install | $TURTL_BASE_DIR/ccl/lx86cl" - turtl
  548. fi
  549. else
  550. su -c "cat $TURTL_BASE_DIR/quicklisp_install | $TURTL_BASE_DIR/ccl/larmcl" - turtl
  551. fi
  552. rm $TURTL_BASE_DIR/quicklisp_install
  553. install_rethinkdb
  554. echo "http-port=8091" > /etc/rethinkdb/instances.d/turtl.conf
  555. chown -R rethinkdb:rethinkdb /var/lib/rethinkdb
  556. # install turtl API
  557. cd $TURTL_BASE_DIR/
  558. if [ -d /repos/turtl ]; then
  559. mkdir $TURTL_BASE_DIR/api
  560. cp -r -p /repos/turtl/. $TURTL_BASE_DIR/api
  561. cd $TURTL_BASE_DIR/api
  562. git pull
  563. else
  564. git clone $TURTL_REPO $TURTL_BASE_DIR/api
  565. fi
  566. cd $TURTL_BASE_DIR/api
  567. git checkout $TURTL_COMMIT -b $TURTL_COMMIT
  568. set_completion_param "turtl commit" "$TURTL_COMMIT"
  569. cd $TURTL_BASE_DIR/quicklisp/local-projects
  570. git clone git://github.com/orthecreedence/cl-hash-util
  571. if [[ "$check_architecture" != *"arm"* ]]; then
  572. if [[ "$check_architecture" == *"64"* ]]; then
  573. su -c "cat '(ccl:quit)' | $TURTL_BASE_DIR/ccl/lx86cl64 -l $TURTL_BASE_DIR/quicklisp/setup.lisp" - turtl
  574. else
  575. su -c "cat '(ccl:quit)' | $TURTL_BASE_DIR/ccl/lx86cl -l $TURTL_BASE_DIR/quicklisp/setup.lisp" - turtl
  576. fi
  577. else
  578. su -c "cat '(ccl:quit)' | $TURTL_BASE_DIR/ccl/larmcl -l $TURTL_BASE_DIR/quicklisp/setup.lisp" - turtl
  579. fi
  580. # config
  581. echo '(defvar *enabled-cors-resources* "resource://turtl-at-lyonbros-dot-com"' > $TURTL_BASE_DIR/api/config/config.footer
  582. echo ' "When set, will enable CORS for resource:// origins if they match the given' >> $TURTL_BASE_DIR/api/config/config.footer
  583. echo ' string. Entries should be comma separated (this string is passed verbatim in' >> $TURTL_BASE_DIR/api/config/config.footer
  584. echo ' the Access-Control-Allow-Origin header).")' >> $TURTL_BASE_DIR/api/config/config.footer
  585. echo '(defparameter *public-actions*' >> $TURTL_BASE_DIR/api/config/config.footer
  586. echo " \`((:post . ,(concatenate 'string *api-path* \"/users\"))" >> $TURTL_BASE_DIR/api/config/config.footer
  587. echo " (:post . ,(concatenate 'string *api-path* \"/log/error\"))" >> $TURTL_BASE_DIR/api/config/config.footer
  588. echo ' (:post . "/cla/sign")' >> $TURTL_BASE_DIR/api/config/config.footer
  589. echo ' (:get . "/ping")' >> $TURTL_BASE_DIR/api/config/config.footer
  590. echo ' (:get . "/admin")' >> $TURTL_BASE_DIR/api/config/config.footer
  591. echo " (:get . ,(cl-ppcre:create-scanner (concatenate 'string *api-path* \"/invites/codes/([0-9a-f-]+)\"))))" >> $TURTL_BASE_DIR/api/config/config.footer
  592. echo " \"A list of public resources/actions that do not require authentication.\")" >> $TURTL_BASE_DIR/api/config/config.footer
  593. echo "(defvar *analytics* '(:enabled t" >> $TURTL_BASE_DIR/api/config/config.footer
  594. echo ' :db "analytics"))' >> $TURTL_BASE_DIR/api/config/config.footer
  595. cp $TURTL_BASE_DIR/asdf.lisp $TURTL_BASE_DIR/api
  596. echo '(load (compile-file "asdf.lisp"))' > $TURTL_BASE_DIR/api/launch.lisp
  597. echo "(pushnew \"./\" asdf:*central-registry* :test #'equal)" >> $TURTL_BASE_DIR/api/launch.lisp
  598. echo '(load "start")' >> $TURTL_BASE_DIR/api/launch.lisp
  599. TURTL_ONION_HOSTNAME=$(add_onion_service turtl 80 ${TURTL_ONION_PORT})
  600. turtl_setup
  601. }
  602. function install_turtl_nginx {
  603. turtl_nginx_site=/etc/nginx/sites-available/$TURTL_DOMAIN_NAME
  604. if [[ $ONION_ONLY == "no" ]]; then
  605. function_check nginx_http_redirect
  606. nginx_http_redirect $TURTL_DOMAIN_NAME
  607. echo 'server {' >> $turtl_nginx_site
  608. echo ' listen 443 ssl;' >> $turtl_nginx_site
  609. echo ' listen [::]:443 ssl;' >> $turtl_nginx_site
  610. echo " server_name ${TURTL_DOMAIN_NAME};" >> $turtl_nginx_site
  611. echo '' >> $turtl_nginx_site
  612. echo ' # Security' >> $turtl_nginx_site
  613. function_check nginx_ssl
  614. nginx_ssl $TURTL_DOMAIN_NAME
  615. function_check nginx_disable_sniffing
  616. nginx_disable_sniffing $TURTL_DOMAIN_NAME
  617. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $turtl_nginx_site
  618. echo '' >> $turtl_nginx_site
  619. echo ' # Logs' >> $turtl_nginx_site
  620. echo ' access_log /dev/null;' >> $turtl_nginx_site
  621. echo ' error_log /dev/null;' >> $turtl_nginx_site
  622. echo '' >> $turtl_nginx_site
  623. echo ' location / {' >> $turtl_nginx_site
  624. function_check nginx_limits
  625. nginx_limits $TURTL_DOMAIN_NAME '15m'
  626. echo " proxy_pass http://localhost:${TURTL_PORT}/;" >> $turtl_nginx_site
  627. echo ' proxy_set_header Host $host;' >> $turtl_nginx_site
  628. echo ' proxy_buffering off;' >> $turtl_nginx_site
  629. echo ' }' >> $turtl_nginx_site
  630. echo '}' >> $turtl_nginx_site
  631. else
  632. echo -n '' > $turtl_nginx_site
  633. fi
  634. echo 'server {' >> $turtl_nginx_site
  635. echo " listen 127.0.0.1:${TURTL_ONION_PORT};" >> $turtl_nginx_site
  636. echo " server_name ${TURTL_ONION_HOSTNAME};" >> $turtl_nginx_site
  637. echo '' >> $turtl_nginx_site
  638. function_check nginx_disable_sniffing
  639. nginx_disable_sniffing $TURTL_DOMAIN_NAME
  640. echo '' >> $turtl_nginx_site
  641. echo ' # Logs' >> $turtl_nginx_site
  642. echo ' access_log /dev/null;' >> $turtl_nginx_site
  643. echo ' error_log /dev/null;' >> $turtl_nginx_site
  644. echo '' >> $turtl_nginx_site
  645. echo ' location / {' >> $turtl_nginx_site
  646. function_check nginx_limits
  647. nginx_limits $TURTL_DOMAIN_NAME '15m'
  648. echo " proxy_pass http://localhost:${TURTL_PORT}/;" >> $turtl_nginx_site
  649. echo ' proxy_set_header Host $host;' >> $turtl_nginx_site
  650. echo ' proxy_buffering off;' >> $turtl_nginx_site
  651. echo ' }' >> $turtl_nginx_site
  652. echo '}' >> $turtl_nginx_site
  653. function_check add_ddns_domain
  654. add_ddns_domain $TURTL_DOMAIN_NAME
  655. set_completion_param "turtl domain" "$TURTL_DOMAIN_NAME"
  656. function_check create_site_certificate
  657. create_site_certificate $TURTL_DOMAIN_NAME 'yes'
  658. function_check nginx_ensite
  659. nginx_ensite $TURTL_DOMAIN_NAME
  660. systemctl restart nginx
  661. }
  662. function install_turtl {
  663. install_turtl_api
  664. install_turtl_nginx
  665. APP_INSTALLED=1
  666. }