freedombone-app-gogs 26KB

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