freedombone-app-gogs 25KB

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