freedombone-app-gogs 25KB

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