freedombone-app-gogs 27KB

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