freedombone-app-gogs 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Gogs functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='developer'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. GOGS_USERNAME='gogs'
  34. GOGS_VERSION='0.9.97'
  35. GIT_DOMAIN_NAME=
  36. GIT_CODE=
  37. GIT_ONION_PORT=8090
  38. GIT_ADMIN_PASSWORD=
  39. GOGS_BIN=
  40. gogs_variables=(ONION_ONLY
  41. GIT_ADMIN_PASSWORD
  42. GIT_DOMAIN_NAME
  43. GIT_CODE
  44. GIT_ONION_PORT
  45. MY_USERNAME
  46. GOGS_VERSION
  47. DDNS_PROVIDER
  48. ARCHITECTURE)
  49. function change_password_gogs {
  50. curr_username="$1"
  51. new_user_password="$2"
  52. }
  53. function install_interactive_gogs {
  54. if [[ $ONION_ONLY != "no" ]]; then
  55. GIT_DOMAIN_NAME='gogs.local'
  56. write_config_param "GIT_DOMAIN_NAME" "$GIT_DOMAIN_NAME"
  57. else
  58. function_check interactive_site_details
  59. interactive_site_details git
  60. fi
  61. APP_INSTALLED=1
  62. }
  63. function configure_interactive_gogs {
  64. dialog --title $"Gogs" \
  65. --backtitle $"Freedombone Control Panel" \
  66. --defaultno \
  67. --yesno $"\nAllow registration of new users?" 10 60
  68. sel=$?
  69. case $sel in
  70. 0)
  71. sed -i "s|DISABLE_REGISTRATION =.*|DISABLE_REGISTRATION = false|g" /home/gogs/custom/conf/app.ini
  72. sed -i "s|SHOW_REGISTRATION_BUTTON =.*|SHOW_REGISTRATION_BUTTON = true|g" /home/gogs/custom/conf/app.ini
  73. ;;
  74. 1)
  75. sed -i "s|DISABLE_REGISTRATION =.*|DISABLE_REGISTRATION = true|g" /home/gogs/custom/conf/app.ini
  76. sed -i "s|SHOW_REGISTRATION_BUTTON =.*|SHOW_REGISTRATION_BUTTON = false|g" /home/gogs/custom/conf/app.ini
  77. ;;
  78. 255) return;;
  79. esac
  80. systemctl restart gogs
  81. }
  82. function gogs_parameters {
  83. if [[ ${ARCHITECTURE} == *"386" || ${ARCHITECTURE} == *"686" ]]; then
  84. CURR_ARCH=386
  85. fi
  86. if [[ ${ARCHITECTURE} == *"amd64" || ${ARCHITECTURE} == "x86_64" ]]; then
  87. CURR_ARCH=amd64
  88. fi
  89. if [[ ${ARCHITECTURE} == *"arm"* ]]; then
  90. CURR_ARCH=arm
  91. fi
  92. if [ ! ${CURR_ARCH} ]; then
  93. echo $'No architecture specified'
  94. ARCHITECTURE=$(uname -m)
  95. if [[ ${ARCHITECTURE} == "arm"* ]]; then
  96. CURR_ARCH=arm
  97. fi
  98. if [[ ${ARCHITECTURE} == "amd"* || ${ARCHITECTURE} == "x86_64" ]]; then
  99. CURR_ARCH=amd64
  100. fi
  101. if [[ ${ARCHITECTURE} == *"386" || ${ARCHITECTURE} == *"686" ]]; then
  102. CURR_ARCH=386
  103. fi
  104. fi
  105. GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/linux_${CURR_ARCH}.tar.gz"
  106. }
  107. function gogs_create_database {
  108. if [ -f ${IMAGE_PASSWORD_FILE} ]; then
  109. GIT_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  110. else
  111. if [ ! ${GIT_ADMIN_PASSWORD} ]; then
  112. GIT_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  113. fi
  114. fi
  115. if [ ! $GIT_ADMIN_PASSWORD ]; then
  116. return
  117. fi
  118. function_check create_database
  119. create_database gogs "$GOGS_ADMIN_PASSWORD"
  120. }
  121. function reconfigure_gogs {
  122. echo -n ''
  123. }
  124. function upgrade_gogs {
  125. if ! grep -q "gogs version:" $COMPLETION_FILE; then
  126. return
  127. fi
  128. CURR_GOGS_VERSION=$(get_completion_param "gogs version")
  129. if [[ "${CURR_GOGS_VERSION}" == "${GOGS_VERSION}" ]]; then
  130. return
  131. fi
  132. gogs_parameters
  133. if [ ! -d ${INSTALL_DIR} ]; then
  134. mkdir -p ${INSTALL_DIR}
  135. fi
  136. cd ${INSTALL_DIR}
  137. if [ -d ${INSTALL_DIR}/gogs ]; then
  138. rm -rf ${INSTALL_DIR}/gogs
  139. fi
  140. GOGS_FILE=linux_${CURR_ARCH}.tar.gz
  141. if [ ! -f ${GOGS_FILE} ]; then
  142. wget ${GOGS_BIN}
  143. fi
  144. if [ ! -f ${GOGS_FILE} ]; then
  145. GOGS_FILE=linux_${CURR_ARCH}.zip
  146. GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/${GOGS_FILE}"
  147. if [ ! -f ${GOGS_FILE} ]; then
  148. wget ${GOGS_BIN}
  149. fi
  150. if [ ! -f ${GOGS_FILE} ]; then
  151. exit 37836
  152. else
  153. apt-get -yq install unzip
  154. unzip -o ${GOGS_FILE}
  155. fi
  156. else
  157. tar -xzf ${INSTALL_DIR}/${GOGS_FILE}
  158. fi
  159. if [ ! -d ${INSTALL_DIR}/gogs ]; then
  160. exit 37823
  161. fi
  162. rm -rf /home/${GOGS_USERNAME}/*
  163. cp -r ${INSTALL_DIR}/gogs/* /home/${GOGS_USERNAME}
  164. if [ -f ${GOGS_FILE} ]; then
  165. rm ${GOGS_FILE}
  166. fi
  167. sed -i "s|gogs version.*|gogs version:$GOGS_VERSION|g" ${COMPLETION_FILE}
  168. systemctl restart gogs
  169. }
  170. function backup_local_gogs {
  171. if ! grep -q "gogs domain" ${COMPLETION_FILE}; then
  172. return
  173. fi
  174. if [ ! -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
  175. return
  176. fi
  177. echo $"Backing up gogs"
  178. function_check backup_database_to_usb
  179. backup_database_to_usb gogs
  180. function_check backup_directory_to_usb
  181. backup_directory_to_usb /home/${GOGS_USERNAME}/custom gogs
  182. backup_directory_to_usb /home/${GOGS_USERNAME}/gogs-repositories gogsrepos
  183. backup_directory_to_usb /home/${GOGS_USERNAME}/.ssh gogsssh
  184. echo $"Gogs backup complete"
  185. }
  186. function restore_local_gogs {
  187. if ! grep -q "gogs domain" ${COMPLETION_FILE}; then
  188. return
  189. fi
  190. if [ ! -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
  191. return
  192. fi
  193. if [ ${#GIT_DOMAIN_NAME} -gt 2 ]; then
  194. function_check gogs_create_database
  195. gogs_create_database
  196. function_check restore_database
  197. restore_database gogs ${GIT_DOMAIN_NAME}
  198. temp_restore_dir=/root/tempgogs
  199. if [ -d ${USB_MOUNT}/backup/gogs ]; then
  200. echo $"Restoring Gogs settings"
  201. if [ ! -d /home/${GOGS_USERNAME}/custom ]; then
  202. mkdir -p /home/${GOGS_USERNAME}/custom
  203. fi
  204. cp -r ${temp_restore_dir}/home/${GOGS_USERNAME}/custom/* /home/${GOGS_USERNAME}/custom
  205. if [ ! "$?" = "0" ]; then
  206. function_check set_user_permissions
  207. set_user_permissions
  208. function_check backup_unmount_drive
  209. backup_unmount_drive
  210. exit 981
  211. fi
  212. echo $"Restoring Gogs repos"
  213. function_check restore_directory_from_usb
  214. restore_directory_from_usb ${temp_restore_dir}repos gogsrepos
  215. cp -r ${temp_restore_dir}repos/home/${GOGS_USERNAME}/gogs-repositories/* /home/${GOGS_USERNAME}/gogs-repositories/
  216. if [ ! "$?" = "0" ]; then
  217. function_check set_user_permissions
  218. set_user_permissions
  219. function_check backup_unmount_drive
  220. backup_unmount_drive
  221. exit 67574
  222. fi
  223. echo $"Restoring Gogs authorized_keys"
  224. function_check restore_directory_from_usb
  225. restore_directory_from_usb ${temp_restore_dir}ssh gogsssh
  226. if [ ! -d /home/${GOGS_USERNAME}/.ssh ]; then
  227. mkdir /home/${GOGS_USERNAME}/.ssh
  228. fi
  229. cp -r ${temp_restore_dir}ssh/home/${GOGS_USERNAME}/.ssh/* /home/${GOGS_USERNAME}/.ssh/
  230. if [ ! "$?" = "0" ]; then
  231. function_check set_user_permissions
  232. set_user_permissions
  233. function_check backup_unmount_drive
  234. backup_unmount_drive
  235. exit 8463
  236. fi
  237. rm -rf ${temp_restore_dir}
  238. rm -rf ${temp_restore_dir}repos
  239. rm -rf ${temp_restore_dir}ssh
  240. chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
  241. fi
  242. fi
  243. }
  244. function backup_remote_gogs {
  245. if [ -d /home/$GOGS_USERNAME ]; then
  246. function_check suspend_site
  247. suspend_site ${GIT_DOMAIN_NAME}
  248. function_check backup_database_to_friend
  249. backup_database_to_friend $GOGS_USERNAME
  250. echo $"Obtaining Gogs settings backup"
  251. function_check backup_directory_to_friend
  252. backup_directory_to_friend /home/$GOGS_USERNAME/custom gogs
  253. echo $"Obtaining Gogs repos backup"
  254. mv /home/$GOGS_USERNAME/gogs-repositories/*.git /home/$GOGS_USERNAME/gogs-repositories/bob
  255. backup_directory_to_friend /home/$GOGS_USERNAME/gogs-repositories gogsrepos
  256. echo $"Obtaining Gogs authorized_keys backup"
  257. backup_directory_to_friend /home/$GOGS_USERNAME/.ssh gogsssh
  258. function_check restart_site
  259. restart_site
  260. echo $"Gogs backup complete"
  261. fi
  262. }
  263. function restore_remote_gogs {
  264. if grep -q "gogs domain" $COMPLETION_FILE; then
  265. GIT_DOMAIN_NAME=$(get_completion_param "gogs domain")
  266. function_check gogs_create_database
  267. gogs_create_database
  268. function_check restore_database_from_friend
  269. restore_database_from_friend gogs ${GIT_DOMAIN_NAME}
  270. if [ -d ${SERVER_DIRECTORY}/backup/gogs ]; then
  271. if [ ! -d /home/${GOGS_USERNAME}/custom ]; then
  272. mkdir -p /home/${GOGS_USERNAME}/custom
  273. fi
  274. cp -r /root/tempgogs/home/${GOGS_USERNAME}/custom/* /home/${GOGS_USERNAME}/custom/
  275. if [ ! "$?" = "0" ]; then
  276. exit 58852
  277. fi
  278. echo $"Restoring Gogs repos"
  279. restore_directory_from_friend /root/tempgogsrepos gogsrepos
  280. cp -r /root/tempgogsrepos/home/${GOGS_USERNAME}/gogs-repositories/* /home/${GOGS_USERNAME}/gogs-repositories/
  281. if [ ! "$?" = "0" ]; then
  282. exit 7649
  283. fi
  284. echo $"Restoring Gogs authorized_keys"
  285. restore_directory_from_friend /root/tempgogsssh gogsssh
  286. if [ ! -d /home/${GOGS_USERNAME}/.ssh ]; then
  287. mkdir /home/${GOGS_USERNAME}/.ssh
  288. fi
  289. cp -r /root/tempgogsssh/home/${GOGS_USERNAME}/.ssh/* /home/${GOGS_USERNAME}/.ssh/
  290. if [ ! "$?" = "0" ]; then
  291. exit 74239
  292. fi
  293. rm -rf /root/tempgogs
  294. rm -rf /root/tempgogsrepos
  295. rm -rf /root/tempgogsssh
  296. chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
  297. echo $"Restore of Gogs complete"
  298. fi
  299. fi
  300. }
  301. function remove_gogs {
  302. if [ ${#GIT_DOMAIN_NAME} -eq 0 ]; then
  303. return
  304. fi
  305. systemctl stop gogs
  306. systemctl disable gogs
  307. nginx_dissite ${GIT_DOMAIN_NAME}
  308. remove_certs ${GIT_DOMAIN_NAME}
  309. if [ -d /var/www/${GIT_DOMAIN_NAME} ]; then
  310. rm -rf /var/www/${GIT_DOMAIN_NAME}
  311. fi
  312. if [ -f /etc/nginx/sites-available/${GIT_DOMAIN_NAME} ]; then
  313. rm /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  314. fi
  315. function_check drop_database
  316. drop_database gogs
  317. rm /etc/systemd/system/gogs.service
  318. rm -rf /home/${GOGS_USERNAME}/*
  319. remove_onion_service gogs ${GIT_ONION_PORT} 9418
  320. remove_completion_param "install_gogs"
  321. sed -i '/gogs /d' $COMPLETION_FILE
  322. remove_backup_database_local gogs
  323. function_check remove_ddns_domain
  324. remove_ddns_domain $GIT_DOMAIN_NAME
  325. }
  326. function install_gogs {
  327. if [ ! $GIT_DOMAIN_NAME ]; then
  328. return
  329. fi
  330. adduser --disabled-login --gecos 'Gogs' $GOGS_USERNAME
  331. gogs_parameters
  332. if [ ! -d ${INSTALL_DIR} ]; then
  333. mkdir -p ${INSTALL_DIR}
  334. fi
  335. cd ${INSTALL_DIR}
  336. if [ -d $INSTALL_DIR/gogs ]; then
  337. rm -rf $INSTALL_DIR/gogs
  338. fi
  339. GOGS_FILE=linux_${CURR_ARCH}.tar.gz
  340. if [ ! -f ${GOGS_FILE} ]; then
  341. wget ${GOGS_BIN}
  342. fi
  343. if [ ! -f ${GOGS_FILE} ]; then
  344. GOGS_FILE=linux_${CURR_ARCH}.zip
  345. GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/${GOGS_FILE}"
  346. wget ${GOGS_BIN}
  347. if [ ! -f ${GOGS_FILE} ]; then
  348. exit 37836
  349. else
  350. apt-get -yq install unzip
  351. unzip -o ${GOGS_FILE}
  352. fi
  353. else
  354. tar -xzf ${INSTALL_DIR}/${GOGS_FILE}
  355. fi
  356. if [ ! -d ${INSTALL_DIR}/gogs ]; then
  357. exit 37823
  358. fi
  359. rm -rf /home/${GOGS_USERNAME}/*
  360. cp -r ${INSTALL_DIR}/gogs/* /home/${GOGS_USERNAME}
  361. if [ -f ${GOGS_FILE} ]; then
  362. rm ${GOGS_FILE}
  363. fi
  364. if [ ! -f /home/${GOGS_USERNAME}/gogs ]; then
  365. echo 'Gogs binary not installed'
  366. exit 345562
  367. fi
  368. echo "export GOROOT=/home/go" >> /home/${GOGS_USERNAME}/.bashrc
  369. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> /home/${GOGS_USERNAME}/.bashrc
  370. echo 'export PATH=$PATH:$GOPATH' >> /home/${GOGS_USERNAME}/.bashrc
  371. chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
  372. function_check install_mariadb
  373. install_mariadb
  374. function_check get_mariadb_password
  375. get_mariadb_password
  376. function_check gogs_create_database
  377. gogs_create_database
  378. if [ ! -f /home/${GOGS_USERNAME}/scripts/mysql.sql ]; then
  379. echo $'MySql template for Gogs was not found'
  380. exit 72528
  381. fi
  382. function_check initialise_database
  383. initialise_database gogs /home/${GOGS_USERNAME}/scripts/mysql.sql
  384. chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
  385. echo '[Unit]' > /etc/systemd/system/gogs.service
  386. echo 'Description=Gogs (Go Git Service)' >> /etc/systemd/system/gogs.service
  387. echo 'After=syslog.target' >> /etc/systemd/system/gogs.service
  388. echo 'After=network.target' >> /etc/systemd/system/gogs.service
  389. echo 'After=mysqld.service' >> /etc/systemd/system/gogs.service
  390. echo '' >> /etc/systemd/system/gogs.service
  391. echo '[Service]' >> /etc/systemd/system/gogs.service
  392. echo '#LimitMEMLOCK=infinity' >> /etc/systemd/system/gogs.service
  393. echo '#LimitNOFILE=65535' >> /etc/systemd/system/gogs.service
  394. echo 'Type=simple' >> /etc/systemd/system/gogs.service
  395. echo 'User=gogs' >> /etc/systemd/system/gogs.service
  396. echo 'Group=gogs' >> /etc/systemd/system/gogs.service
  397. echo "WorkingDirectory=/home/${GOGS_USERNAME}" >> /etc/systemd/system/gogs.service
  398. echo "ExecStart=/home/${GOGS_USERNAME}/gogs web" >> /etc/systemd/system/gogs.service
  399. echo 'Restart=always' >> /etc/systemd/system/gogs.service
  400. echo 'RestartSec=10' >> /etc/systemd/system/gogs.service
  401. echo "Environment=\"USER=${GOGS_USERNAME}\" \"HOME=/home/${GOGS_USERNAME}\" \"GOPATH=/home/go/go${GO_VERSION}\"" >> /etc/systemd/system/gogs.service
  402. echo '' >> /etc/systemd/system/gogs.service
  403. echo '[Install]' >> /etc/systemd/system/gogs.service
  404. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/gogs.service
  405. systemctl enable gogs
  406. systemctl daemon-reload
  407. systemctl start gogs
  408. if [ ! -d /var/www/${GIT_DOMAIN_NAME} ]; then
  409. mkdir /var/www/${GIT_DOMAIN_NAME}
  410. fi
  411. if [ -d /var/www/${GIT_DOMAIN_NAME}/htdocs ]; then
  412. rm -rf /var/www/${GIT_DOMAIN_NAME}/htdocs
  413. fi
  414. if [[ ${ONION_ONLY} == "no" ]]; then
  415. function_check nginx_http_redirect
  416. nginx_http_redirect ${GIT_DOMAIN_NAME}
  417. echo 'server {' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  418. echo ' listen 443 ssl;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  419. echo ' listen [::]:443 ssl;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  420. echo " root /var/www/${GIT_DOMAIN_NAME}/htdocs;" >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  421. echo " server_name ${GIT_DOMAIN_NAME};" >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  422. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  423. echo " error_log /dev/null;" >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  424. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  425. function_check nginx_ssl
  426. nginx_ssl ${GIT_DOMAIN_NAME}
  427. function_check nginx_disable_sniffing
  428. nginx_disable_sniffing ${GIT_DOMAIN_NAME}
  429. echo ' add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  430. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  431. echo ' location / {' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  432. function_check nginx_limits
  433. nginx_limits ${GIT_DOMAIN_NAME} '10G'
  434. echo ' proxy_pass http://localhost:3000;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  435. echo ' }' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  436. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  437. echo ' fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  438. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  439. echo ' error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  440. echo ' error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  441. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  442. echo ' location = /robots.txt {' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  443. echo ' allow all;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  444. echo ' log_not_found off;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  445. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  446. echo ' }' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  447. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  448. nginx_keybase ${GIT_DOMAIN_NAME}
  449. echo '}' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  450. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  451. else
  452. echo -n '' > /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  453. fi
  454. echo 'server {' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  455. echo " listen 127.0.0.1:${GIT_ONION_PORT} default_server;" >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  456. echo " root /var/www/$GIT_DOMAIN_NAME/htdocs;" >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  457. echo " server_name $GIT_DOMAIN_NAME;" >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  458. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  459. echo " error_log /dev/null;" >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  460. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  461. function_check nginx_disable_sniffing
  462. nginx_disable_sniffing ${GIT_DOMAIN_NAME}
  463. echo ' add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  464. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  465. echo ' location / {' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  466. function_check nginx_limits
  467. nginx_limits ${GIT_DOMAIN_NAME} '10G'
  468. echo ' proxy_pass http://localhost:3000;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  469. echo ' }' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  470. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  471. echo ' fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  472. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  473. echo ' error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  474. echo ' error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  475. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  476. echo ' location = /robots.txt {' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  477. echo ' allow all;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  478. echo ' log_not_found off;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  479. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  480. echo ' }' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  481. echo '' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  482. nginx_keybase ${GIT_DOMAIN_NAME}
  483. echo '}' >> /etc/nginx/sites-available/${GIT_DOMAIN_NAME}
  484. function_check configure_php
  485. configure_php
  486. function_check create_site_certificate
  487. create_site_certificate ${GIT_DOMAIN_NAME} 'yes'
  488. nginx_ensite ${GIT_DOMAIN_NAME}
  489. if [ ! -d /var/lib/tor ]; then
  490. echo $'No Tor installation found. Gogs onion site cannot be configured.'
  491. exit 877367
  492. fi
  493. if ! grep -q "hidden_service_gogs" /etc/tor/torrc; then
  494. echo 'HiddenServiceDir /var/lib/tor/hidden_service_gogs/' >> /etc/tor/torrc
  495. echo "HiddenServicePort 80 127.0.0.1:${GIT_ONION_PORT}" >> /etc/tor/torrc
  496. echo "HiddenServicePort 9418 127.0.0.1:9418" >> /etc/tor/torrc
  497. echo $'Added onion site for Gogs'
  498. fi
  499. onion_update
  500. function_check wait_for_onion_service
  501. wait_for_onion_service 'gogs'
  502. GIT_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_gogs/hostname)
  503. systemctl restart php5-fpm
  504. systemctl restart nginx
  505. set_completion_param "gogs domain" "$GIT_DOMAIN_NAME"
  506. set_completion_param "gogs onion domain" "$GIT_ONION_HOSTNAME"
  507. function_check add_ddns_domain
  508. add_ddns_domain ${GIT_DOMAIN_NAME}
  509. # obtain the secret key
  510. GOGS_SECRET_KEY="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  511. ${PROJECT_NAME}-pass -u $MY_USERNAME -a gogs -p "*"
  512. # create the configuration
  513. GOGS_CONFIG_PATH=/home/${GOGS_USERNAME}/custom/conf
  514. if [ ! -d ${GOGS_CONFIG_PATH} ]; then
  515. mkdir -p ${GOGS_CONFIG_PATH}
  516. fi
  517. GOGS_CONFIG_FILE=${GOGS_CONFIG_PATH}/app.ini
  518. echo "RUN_USER = $GOGS_USERNAME" > ${GOGS_CONFIG_FILE}
  519. echo 'RUN_MODE = prod' >> ${GOGS_CONFIG_FILE}
  520. echo '' >> ${GOGS_CONFIG_FILE}
  521. echo '[database]' >> ${GOGS_CONFIG_FILE}
  522. echo 'DB_TYPE = mysql' >> ${GOGS_CONFIG_FILE}
  523. echo 'HOST = 127.0.0.1:3306' >> ${GOGS_CONFIG_FILE}
  524. echo 'NAME = gogs' >> ${GOGS_CONFIG_FILE}
  525. echo 'USER = root' >> ${GOGS_CONFIG_FILE}
  526. echo "PASSWD = $MARIADB_PASSWORD" >> ${GOGS_CONFIG_FILE}
  527. echo 'SSL_MODE = disable' >> ${GOGS_CONFIG_FILE}
  528. echo 'PATH = data/gogs.db' >> ${GOGS_CONFIG_FILE}
  529. echo '' >> ${GOGS_CONFIG_FILE}
  530. echo '[repository]' >> ${GOGS_CONFIG_FILE}
  531. echo "ROOT = /home/$GOGS_USERNAME/gogs-repositories" >> ${GOGS_CONFIG_FILE}
  532. echo '' >> ${GOGS_CONFIG_FILE}
  533. echo '[server]' >> ${GOGS_CONFIG_FILE}
  534. if [[ ${ONION_ONLY} == 'no' ]]; then
  535. echo "DOMAIN = ${GIT_DOMAIN_NAME}" >> ${GOGS_CONFIG_FILE}
  536. echo "ROOT_URL = https://$GIT_DOMAIN_NAME/" >> ${GOGS_CONFIG_FILE}
  537. else
  538. echo "DOMAIN = ${GIT_ONION_HOSTNAME}" >> ${GOGS_CONFIG_FILE}
  539. echo "ROOT_URL = http://$GIT_DOMAIN_NAME/" >> ${GOGS_CONFIG_FILE}
  540. fi
  541. echo 'HTTP_PORT = 3000' >> ${GOGS_CONFIG_FILE}
  542. echo "SSH_PORT = $SSH_PORT" >> ${GOGS_CONFIG_FILE}
  543. echo 'SSH_DOMAIN = %(DOMAIN)s' >> ${GOGS_CONFIG_FILE}
  544. echo "CERT_FILE = /etc/ssl/certs/${GIT_DOMAIN_NAME}.pem" >> ${GOGS_CONFIG_FILE}
  545. echo "KEY_FILE = /etc/ssl/private/${GIT_DOMAIN_NAME}.key" >> ${GOGS_CONFIG_FILE}
  546. echo 'DISABLE_ROUTER_LOG = true' >> ${GOGS_CONFIG_FILE}
  547. echo '' >> ${GOGS_CONFIG_FILE}
  548. echo '[session]' >> ${GOGS_CONFIG_FILE}
  549. echo 'PROVIDER = file' >> ${GOGS_CONFIG_FILE}
  550. echo '' >> ${GOGS_CONFIG_FILE}
  551. echo '[log]' >> ${GOGS_CONFIG_FILE}
  552. echo 'MODE = file' >> ${GOGS_CONFIG_FILE}
  553. echo 'LEVEL = Info' >> ${GOGS_CONFIG_FILE}
  554. echo '' >> ${GOGS_CONFIG_FILE}
  555. echo '[security]' >> ${GOGS_CONFIG_FILE}
  556. echo 'INSTALL_LOCK = true' >> ${GOGS_CONFIG_FILE}
  557. echo "SECRET_KEY = $GOGS_SECRET_KEY" >> ${GOGS_CONFIG_FILE}
  558. echo '' >> ${GOGS_CONFIG_FILE}
  559. echo '[service]' >> ${GOGS_CONFIG_FILE}
  560. echo 'DISABLE_REGISTRATION = false' >> ${GOGS_CONFIG_FILE}
  561. echo 'SHOW_REGISTRATION_BUTTON = true' >> ${GOGS_CONFIG_FILE}
  562. echo 'REQUIRE_SIGNIN_VIEW = false' >> ${GOGS_CONFIG_FILE}
  563. echo 'ENABLE_CAPTCHA = false' >> ${GOGS_CONFIG_FILE}
  564. echo '' >> ${GOGS_CONFIG_FILE}
  565. echo '[other]' >> ${GOGS_CONFIG_FILE}
  566. echo 'SHOW_FOOTER_BRANDING = false' >> ${GOGS_CONFIG_FILE}
  567. echo 'SHOW_FOOTER_VERSION = false' >> ${GOGS_CONFIG_FILE}
  568. chmod 750 ${GOGS_CONFIG_FILE}
  569. chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
  570. systemctl restart gogs
  571. if ! grep -q "gogs domain:" ${COMPLETION_FILE}; then
  572. echo "gogs domain:${GIT_DOMAIN_NAME}" >> ${COMPLETION_FILE}
  573. else
  574. sed -i "s|gogs domain.*|gogs domain:${GIT_DOMAIN_NAME}|g" ${COMPLETION_FILE}
  575. fi
  576. function_check configure_firewall_for_git
  577. configure_firewall_for_git
  578. if ! grep -q "gogs version:" ${COMPLETION_FILE}; then
  579. echo "gogs version:${GOGS_VERSION}" >> ${COMPLETION_FILE}
  580. else
  581. sed -i "s|gogs version.*|gogs version:${GOGS_VERSION}|g" ${COMPLETION_FILE}
  582. fi
  583. APP_INSTALLED=1
  584. }
  585. # NOTE: deliberately no exit 0