freedombone-app-gogs 25KB

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