freedombone-app-gogs 29KB

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