freedombone-app-gogs 29KB

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