freedombone-app-gogs 25KB

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