freedombone-app-gogs 23KB

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