freedombone-app-gogs 25KB

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