freedombone-app-gogs 26KB

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