freedombone-app-gogs 27KB

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