freedombone-app-gogs 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Gogs functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. VARIANTS='developer'
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. GOGS_USERNAME='gogs'
  32. GOGS_VERSION='0.11.29'
  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. GIT_ADMIN_PASSWORD
  40. GIT_DOMAIN_NAME
  41. GIT_CODE
  42. GIT_ONION_PORT
  43. MY_USERNAME
  44. DDNS_PROVIDER
  45. ARCHITECTURE)
  46. function logging_on_gogs {
  47. echo -n ''
  48. }
  49. function logging_off_gogs {
  50. echo -n ''
  51. }
  52. function change_password_gogs {
  53. curr_username="$1"
  54. new_user_password="$2"
  55. }
  56. function install_interactive_gogs {
  57. if [[ $ONION_ONLY != "no" ]]; then
  58. GIT_DOMAIN_NAME='gogs.local'
  59. write_config_param "GIT_DOMAIN_NAME" "$GIT_DOMAIN_NAME"
  60. else
  61. function_check interactive_site_details
  62. interactive_site_details git
  63. fi
  64. APP_INSTALLED=1
  65. }
  66. function configure_interactive_gogs {
  67. dialog --title $"Gogs" \
  68. --backtitle $"Freedombone Control Panel" \
  69. --defaultno \
  70. --yesno $"\nAllow registration of new users?" 10 60
  71. sel=$?
  72. case $sel in
  73. 0)
  74. sed -i "s|DISABLE_REGISTRATION =.*|DISABLE_REGISTRATION = false|g" /home/gogs/custom/conf/app.ini
  75. sed -i "s|SHOW_REGISTRATION_BUTTON =.*|SHOW_REGISTRATION_BUTTON = true|g" /home/gogs/custom/conf/app.ini
  76. ;;
  77. 1)
  78. sed -i "s|DISABLE_REGISTRATION =.*|DISABLE_REGISTRATION = true|g" /home/gogs/custom/conf/app.ini
  79. sed -i "s|SHOW_REGISTRATION_BUTTON =.*|SHOW_REGISTRATION_BUTTON = false|g" /home/gogs/custom/conf/app.ini
  80. ;;
  81. 255) return;;
  82. esac
  83. systemctl restart gogs
  84. }
  85. function gogs_parameters {
  86. if [[ ${ARCHITECTURE} == *"386" || ${ARCHITECTURE} == *"686" ]]; then
  87. CURR_ARCH=386
  88. fi
  89. if [[ ${ARCHITECTURE} == *"amd64" || ${ARCHITECTURE} == "x86_64" ]]; then
  90. CURR_ARCH=amd64
  91. fi
  92. if [[ ${ARCHITECTURE} == *"arm"* ]]; then
  93. CURR_ARCH=armv5
  94. fi
  95. if [ ! ${CURR_ARCH} ]; then
  96. echo $'No architecture specified'
  97. ARCHITECTURE=$(uname -m)
  98. if [[ ${ARCHITECTURE} == "arm"* ]]; then
  99. CURR_ARCH=armv5
  100. echo $"Using $CURR_ARCH"
  101. fi
  102. if [[ ${ARCHITECTURE} == "amd"* || ${ARCHITECTURE} == "x86_64" ]]; then
  103. CURR_ARCH=amd64
  104. echo $"Using $CURR_ARCH"
  105. fi
  106. if [[ ${ARCHITECTURE} == *"386" || ${ARCHITECTURE} == *"686" ]]; then
  107. CURR_ARCH=386
  108. echo $"Using $CURR_ARCH"
  109. fi
  110. fi
  111. GOGS_FILE=linux_${CURR_ARCH}.tar.gz
  112. GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/${GOGS_FILE}"
  113. }
  114. function gogs_create_database {
  115. if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
  116. GIT_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  117. else
  118. if [ ! "${GIT_ADMIN_PASSWORD}" ]; then
  119. GIT_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  120. fi
  121. fi
  122. if [ ! "$GIT_ADMIN_PASSWORD" ]; then
  123. return
  124. fi
  125. function_check create_database
  126. create_database gogs "$GIT_ADMIN_PASSWORD"
  127. }
  128. function reconfigure_gogs {
  129. echo -n ''
  130. }
  131. function upgrade_gogs {
  132. if ! grep -q 'gogs version:' "$COMPLETION_FILE"; then
  133. return
  134. fi
  135. CURR_GOGS_VERSION=$(get_completion_param "gogs version")
  136. echo "gogs current version: ${CURR_GOGS_VERSION}"
  137. echo "gogs app version: ${GOGS_VERSION}"
  138. if [[ "${CURR_GOGS_VERSION}" == "${GOGS_VERSION}" ]]; then
  139. return
  140. fi
  141. GOGS_CONFIG_PATH=/home/${GOGS_USERNAME}/custom/conf
  142. GOGS_CONFIG_FILE=$GOGS_CONFIG_PATH/app.ini
  143. cp "$GOGS_CONFIG_FILE $INSTALL_DIR/gogs_config.ini"
  144. if [ -d "$INSTALL_DIR/gogs-repositories" ]; then
  145. rm -rf "$INSTALL_DIR/gogs-repositories"
  146. fi
  147. if [ -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
  148. # shellcheck disable=SC2086
  149. mv /home/${GOGS_USERNAME}/gogs-repositories $INSTALL_DIR
  150. fi
  151. gogs_parameters
  152. echo "gogs binary upgrade: ${GOGS_BIN}"
  153. if [ ! -d "${INSTALL_DIR}" ]; then
  154. mkdir -p "${INSTALL_DIR}"
  155. fi
  156. cd "${INSTALL_DIR}" || exit 26784427
  157. if [ -d "${INSTALL_DIR}/gogs" ]; then
  158. rm -rf "${INSTALL_DIR}/gogs"
  159. fi
  160. GOGS_FILE=linux_${CURR_ARCH}.tar.gz
  161. if [ ! -f ${GOGS_FILE} ]; then
  162. wget ${GOGS_BIN}
  163. fi
  164. if [ ! -f ${GOGS_FILE} ]; then
  165. GOGS_FILE=linux_${CURR_ARCH}.zip
  166. GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/${GOGS_FILE}"
  167. if [ ! -f ${GOGS_FILE} ]; then
  168. wget ${GOGS_BIN}
  169. fi
  170. if [ ! -f ${GOGS_FILE} ]; then
  171. exit 37836
  172. else
  173. apt-get -yq install unzip
  174. unzip -o ${GOGS_FILE}
  175. fi
  176. else
  177. tar -xzf "${INSTALL_DIR}/${GOGS_FILE}"
  178. fi
  179. if [ ! -d "${INSTALL_DIR}/gogs" ]; then
  180. exit 37823
  181. fi
  182. rm -rf "/home/${GOGS_USERNAME:?}/"*
  183. cp -r "${INSTALL_DIR}/gogs/"* "/home/${GOGS_USERNAME}"
  184. if [ -f ${GOGS_FILE} ]; then
  185. rm ${GOGS_FILE}
  186. fi
  187. if [ ! -d ${GOGS_CONFIG_PATH} ]; then
  188. mkdir -p ${GOGS_CONFIG_PATH}
  189. fi
  190. cp "$INSTALL_DIR/gogs_config.ini" "$GOGS_CONFIG_FILE"
  191. if [ ! -f $GOGS_CONFIG_FILE ]; then
  192. echo $'Gogs ini file not upgraded'
  193. exit 873535
  194. fi
  195. rm "$INSTALL_DIR/gogs_config.ini"
  196. if [ -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
  197. rm -rf /home/${GOGS_USERNAME}/gogs-repositories
  198. fi
  199. if [ -d "$INSTALL_DIR/gogs-repositories" ]; then
  200. # shellcheck disable=SC2086
  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. # shellcheck disable=SC2086
  314. mv /home/$GOGS_USERNAME/gogs-repositories/*.git /home/$GOGS_USERNAME/gogs-repositories/bob
  315. backup_directory_to_friend /home/$GOGS_USERNAME/gogs-repositories gogsrepos
  316. echo $"Obtaining Gogs authorized_keys backup"
  317. backup_directory_to_friend /home/$GOGS_USERNAME/.ssh gogsssh
  318. function_check restart_site
  319. restart_site
  320. echo $"Gogs backup complete"
  321. fi
  322. }
  323. function restore_remote_gogs {
  324. if grep -q "gogs domain" "$COMPLETION_FILE"; then
  325. GIT_DOMAIN_NAME=$(get_completion_param "gogs domain")
  326. function_check gogs_create_database
  327. gogs_create_database
  328. GOGS_CONFIG_PATH=/home/${GOGS_USERNAME}/custom/conf
  329. GOGS_CONFIG_FILE=${GOGS_CONFIG_PATH}/app.ini
  330. function_check restore_database_from_friend
  331. restore_database_from_friend gogs "${GIT_DOMAIN_NAME}"
  332. if [ -d "${SERVER_DIRECTORY}/backup/gogs" ]; then
  333. if [ ! -d $GOGS_CONFIG_PATH ]; then
  334. mkdir -p $GOGS_CONFIG_PATH
  335. fi
  336. if [ -d /root/tempgogs/home/${GOGS_USERNAME}/custom ]; then
  337. cp -r /root/tempgogs/home/${GOGS_USERNAME}/custom/* /home/${GOGS_USERNAME}/custom/
  338. else
  339. cp -r /root/tempgogs/* /home/${GOGS_USERNAME}/custom/
  340. fi
  341. # shellcheck disable=SC2181
  342. if [ ! "$?" = "0" ]; then
  343. exit 58852
  344. fi
  345. echo $"Restoring Gogs repos"
  346. restore_directory_from_friend /root/tempgogsrepos gogsrepos
  347. if [ ! -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
  348. mkdir /home/${GOGS_USERNAME}/gogs-repositories
  349. fi
  350. if [ -d /root/tempgogsrepos/home/${GOGS_USERNAME}/gogs-repositories ]; then
  351. cp -r /root/tempgogsrepos/home/${GOGS_USERNAME}/gogs-repositories/* /home/${GOGS_USERNAME}/gogs-repositories/
  352. else
  353. cp -r /root/tempgogsrepos/* /home/${GOGS_USERNAME}/gogs-repositories/
  354. fi
  355. # shellcheck disable=SC2181
  356. if [ ! "$?" = "0" ]; then
  357. exit 7649
  358. fi
  359. echo $"Restoring Gogs authorized_keys"
  360. restore_directory_from_friend /root/tempgogsssh gogsssh
  361. if [ ! -d /home/${GOGS_USERNAME}/.ssh ]; then
  362. mkdir /home/${GOGS_USERNAME}/.ssh
  363. fi
  364. if [ -d /root/tempgogsssh/home/${GOGS_USERNAME}/.ssh ]; then
  365. cp -r /root/tempgogsssh/home/${GOGS_USERNAME}/.ssh/* /home/${GOGS_USERNAME}/.ssh/
  366. else
  367. cp -r /root/tempgogsssh/* /home/${GOGS_USERNAME}/.ssh/
  368. fi
  369. # shellcheck disable=SC2181
  370. if [ ! "$?" = "0" ]; then
  371. exit 74239
  372. fi
  373. rm -rf /root/tempgogs
  374. rm -rf /root/tempgogsrepos
  375. rm -rf /root/tempgogsssh
  376. chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
  377. echo $"Restore of Gogs complete"
  378. fi
  379. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  380. sed -i "s|PASSWD =.*|PASSWD = $MARIADB_PASSWORD|g" ${GOGS_CONFIG_FILE}
  381. MARIADB_PASSWORD=
  382. systemctl restart gogs
  383. fi
  384. }
  385. function remove_gogs {
  386. if [ ${#GIT_DOMAIN_NAME} -eq 0 ]; then
  387. return
  388. fi
  389. systemctl stop gogs
  390. systemctl disable gogs
  391. nginx_dissite "${GIT_DOMAIN_NAME}"
  392. remove_certs "${GIT_DOMAIN_NAME}"
  393. if [ -d "/var/www/${GIT_DOMAIN_NAME}" ]; then
  394. rm -rf "/var/www/${GIT_DOMAIN_NAME}"
  395. fi
  396. if [ -f "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}" ]; then
  397. rm "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
  398. fi
  399. function_check drop_database
  400. drop_database gogs
  401. rm /etc/systemd/system/gogs.service
  402. systemctl daemon-reload
  403. rm -rf "/home/${GOGS_USERNAME:?}/"*
  404. remove_onion_service gogs ${GIT_ONION_PORT} 9418
  405. remove_completion_param "install_gogs"
  406. sed -i '/gogs /d' "$COMPLETION_FILE"
  407. remove_backup_database_local gogs
  408. groupdel -f gogs
  409. userdel -r gogs
  410. function_check remove_ddns_domain
  411. remove_ddns_domain "$GIT_DOMAIN_NAME"
  412. }
  413. function install_gogs {
  414. if [ ! "$GIT_DOMAIN_NAME" ]; then
  415. return
  416. fi
  417. adduser --disabled-login --gecos 'Gogs' $GOGS_USERNAME
  418. if [ ! -d /home/$GOGS_USERNAME ]; then
  419. echo $"/home/$GOGS_USERNAME directory not created"
  420. exit 783528
  421. fi
  422. groupadd gogs
  423. gogs_parameters
  424. if [ ! -d "${INSTALL_DIR}" ]; then
  425. mkdir -p "${INSTALL_DIR}"
  426. fi
  427. cd "${INSTALL_DIR}" || exit 24682467284
  428. if [ -d "$INSTALL_DIR/gogs" ]; then
  429. rm -rf "$INSTALL_DIR/gogs"
  430. fi
  431. GOGS_FILE="linux_${CURR_ARCH}.tar.gz"
  432. if [ ! -f ${GOGS_FILE} ]; then
  433. wget ${GOGS_BIN}
  434. fi
  435. if [ ! -f ${GOGS_FILE} ]; then
  436. GOGS_FILE=linux_${CURR_ARCH}.zip
  437. GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/${GOGS_FILE}"
  438. wget ${GOGS_BIN}
  439. if [ ! -f ${GOGS_FILE} ]; then
  440. exit 37836
  441. else
  442. apt-get -yq install unzip
  443. unzip -o ${GOGS_FILE}
  444. fi
  445. else
  446. tar -xzf "${INSTALL_DIR}/${GOGS_FILE}"
  447. fi
  448. if [ ! -d "${INSTALL_DIR}/gogs" ]; then
  449. exit 37823
  450. fi
  451. rm -rf "/home/${GOGS_USERNAME:?}/"*
  452. cp -r "${INSTALL_DIR}/gogs/"* "/home/${GOGS_USERNAME}"
  453. if [ -f ${GOGS_FILE} ]; then
  454. rm ${GOGS_FILE}
  455. fi
  456. if [ ! -f /home/${GOGS_USERNAME}/gogs ]; then
  457. echo 'Gogs binary not installed'
  458. exit 345562
  459. fi
  460. { echo "export GOROOT=/home/go";
  461. # shellcheck disable=SC2153
  462. echo "export GOPATH=\${GOROOT}/go${GO_VERSION}/bin";
  463. echo "export PATH=\$PATH:\$GOPATH"; } >> "/home/${GOGS_USERNAME}/.bashrc"
  464. chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
  465. function_check install_mariadb
  466. install_mariadb
  467. function_check get_mariadb_password
  468. get_mariadb_password
  469. function_check gogs_create_database
  470. gogs_create_database
  471. if [ ! -f /home/${GOGS_USERNAME}/scripts/mysql.sql ]; then
  472. echo $'MySql template for Gogs was not found'
  473. exit 72528
  474. fi
  475. function_check initialise_database
  476. initialise_database gogs /home/${GOGS_USERNAME}/scripts/mysql.sql
  477. chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
  478. { echo '[Unit]';
  479. echo 'Description=Gogs (Go Git Service)';
  480. echo 'After=syslog.target';
  481. echo 'After=network.target';
  482. echo 'After=mysqld.service';
  483. echo '';
  484. echo '[Service]';
  485. echo '#LimitMEMLOCK=infinity';
  486. echo '#LimitNOFILE=65535';
  487. echo 'Type=simple';
  488. echo 'User=gogs';
  489. echo 'Group=gogs';
  490. echo "WorkingDirectory=/home/${GOGS_USERNAME}";
  491. echo "ExecStart=/home/${GOGS_USERNAME}/gogs web";
  492. echo 'Restart=always';
  493. echo 'RestartSec=10';
  494. echo "Environment=\"USER=${GOGS_USERNAME}\" \"HOME=/home/${GOGS_USERNAME}\" \"GOPATH=/home/go/go${GO_VERSION}\"";
  495. echo '';
  496. echo '[Install]';
  497. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/gogs.service
  498. systemctl enable gogs
  499. systemctl daemon-reload
  500. systemctl start gogs
  501. if [ ! -d "/var/www/${GIT_DOMAIN_NAME}" ]; then
  502. mkdir "/var/www/${GIT_DOMAIN_NAME}"
  503. fi
  504. if [ -d "/var/www/${GIT_DOMAIN_NAME}/htdocs" ]; then
  505. rm -rf "/var/www/${GIT_DOMAIN_NAME}/htdocs"
  506. fi
  507. if [[ "${ONION_ONLY}" == "no" ]]; then
  508. function_check nginx_http_redirect
  509. nginx_http_redirect "${GIT_DOMAIN_NAME}"
  510. { echo 'server {';
  511. echo ' listen 443 ssl;';
  512. echo ' #listen [::]:443 ssl;';
  513. echo " root /var/www/${GIT_DOMAIN_NAME}/htdocs;";
  514. echo " server_name ${GIT_DOMAIN_NAME};";
  515. echo ' access_log /dev/null;';
  516. echo " error_log /dev/null;";
  517. echo ''; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
  518. function_check nginx_ssl
  519. nginx_ssl "${GIT_DOMAIN_NAME}"
  520. function_check nginx_security_options
  521. nginx_security_options "${GIT_DOMAIN_NAME}"
  522. { echo ' add_header Strict-Transport-Security max-age=0;';
  523. echo '';
  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;';
  528. echo ' }';
  529. echo '';
  530. echo ' fastcgi_buffers 64 4K;';
  531. echo '';
  532. echo ' error_page 403 /core/templates/403.php;';
  533. echo ' error_page 404 /core/templates/404.php;';
  534. echo '';
  535. echo ' location = /robots.txt {';
  536. echo ' allow all;';
  537. echo ' log_not_found off;';
  538. echo ' access_log /dev/null;';
  539. echo ' }';
  540. echo '}';
  541. echo ''; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
  542. else
  543. echo -n '' > "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
  544. fi
  545. { echo 'server {';
  546. echo " listen 127.0.0.1:${GIT_ONION_PORT} default_server;";
  547. echo " root /var/www/$GIT_DOMAIN_NAME/htdocs;";
  548. echo " server_name $GIT_DOMAIN_NAME;";
  549. echo ' access_log /dev/null;';
  550. echo " error_log /dev/null;";
  551. echo ''; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
  552. function_check nginx_security_options
  553. nginx_security_options "${GIT_DOMAIN_NAME}"
  554. { echo ' add_header Strict-Transport-Security max-age=0;';
  555. echo '';
  556. echo ' location / {'; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
  557. function_check nginx_limits
  558. nginx_limits "${GIT_DOMAIN_NAME}" '10G'
  559. { echo ' proxy_pass http://localhost:3000;';
  560. echo ' }';
  561. echo '';
  562. echo ' fastcgi_buffers 64 4K;';
  563. echo '';
  564. echo ' error_page 403 /core/templates/403.php;';
  565. echo ' error_page 404 /core/templates/404.php;';
  566. echo '';
  567. echo ' location = /robots.txt {';
  568. echo ' allow all;';
  569. echo ' log_not_found off;';
  570. echo ' access_log /dev/null;';
  571. echo ' }';
  572. echo '}'; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
  573. function_check configure_php
  574. configure_php
  575. function_check create_site_certificate
  576. create_site_certificate "${GIT_DOMAIN_NAME}" 'yes'
  577. nginx_ensite "${GIT_DOMAIN_NAME}"
  578. if [ ! -d /var/lib/tor ]; then
  579. echo $'No Tor installation found. Gogs onion site cannot be configured.'
  580. exit 877367
  581. fi
  582. if ! grep -q "hidden_service_gogs" /etc/tor/torrc; then
  583. { echo 'HiddenServiceDir /var/lib/tor/hidden_service_gogs/';
  584. echo 'HiddenServiceVersion 3';
  585. echo "HiddenServicePort 80 127.0.0.1:${GIT_ONION_PORT}";
  586. echo "HiddenServicePort 9418 127.0.0.1:9418"; } >> /etc/tor/torrc
  587. echo $'Added onion site for Gogs'
  588. fi
  589. onion_update
  590. function_check wait_for_onion_service
  591. wait_for_onion_service 'gogs'
  592. GIT_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_gogs/hostname)
  593. systemctl restart mariadb
  594. systemctl restart php7.0-fpm
  595. systemctl restart nginx
  596. set_completion_param "gogs domain" "$GIT_DOMAIN_NAME"
  597. set_completion_param "gogs onion domain" "$GIT_ONION_HOSTNAME"
  598. function_check add_ddns_domain
  599. add_ddns_domain "${GIT_DOMAIN_NAME}"
  600. # obtain the secret key
  601. GOGS_SECRET_KEY="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  602. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a gogs -p "*"
  603. # create the configuration
  604. GOGS_CONFIG_PATH="/home/${GOGS_USERNAME}/custom/conf"
  605. if [ ! -d ${GOGS_CONFIG_PATH} ]; then
  606. mkdir -p ${GOGS_CONFIG_PATH}
  607. fi
  608. GOGS_CONFIG_FILE=${GOGS_CONFIG_PATH}/app.ini
  609. { echo "RUN_USER = $GOGS_USERNAME";
  610. echo 'RUN_MODE = prod';
  611. echo '';
  612. echo '[database]';
  613. echo 'DB_TYPE = mysql';
  614. echo 'HOST = 127.0.0.1:3306';
  615. echo 'NAME = gogs';
  616. echo 'USER = root';
  617. echo "PASSWD = $MARIADB_PASSWORD";
  618. echo 'SSL_MODE = disable';
  619. echo 'PATH = data/gogs.db';
  620. echo '';
  621. echo '[repository]';
  622. echo "ROOT = /home/$GOGS_USERNAME/gogs-repositories";
  623. echo '';
  624. echo '[server]'; } >> ${GOGS_CONFIG_FILE}
  625. if [[ ${ONION_ONLY} == 'no' ]]; then
  626. echo "DOMAIN = ${GIT_DOMAIN_NAME}" >> ${GOGS_CONFIG_FILE}
  627. echo "ROOT_URL = https://$GIT_DOMAIN_NAME/" >> ${GOGS_CONFIG_FILE}
  628. else
  629. echo "DOMAIN = ${GIT_ONION_HOSTNAME}" >> ${GOGS_CONFIG_FILE}
  630. echo "ROOT_URL = http://$GIT_DOMAIN_NAME/" >> ${GOGS_CONFIG_FILE}
  631. fi
  632. { echo 'HTTP_PORT = 3000';
  633. echo "SSH_PORT = $SSH_PORT";
  634. echo 'SSH_DOMAIN = %(DOMAIN)s';
  635. echo "CERT_FILE = /etc/ssl/certs/${GIT_DOMAIN_NAME}.pem";
  636. echo "KEY_FILE = /etc/ssl/private/${GIT_DOMAIN_NAME}.key";
  637. echo 'DISABLE_ROUTER_LOG = true';
  638. echo '';
  639. echo '[session]';
  640. echo 'PROVIDER = file';
  641. echo '';
  642. echo '[log]';
  643. echo 'MODE = file';
  644. echo 'LEVEL = Info';
  645. echo '';
  646. echo '[security]';
  647. echo 'INSTALL_LOCK = true';
  648. echo "SECRET_KEY = $GOGS_SECRET_KEY";
  649. echo '';
  650. echo '[service]';
  651. echo 'DISABLE_REGISTRATION = false';
  652. echo 'SHOW_REGISTRATION_BUTTON = true';
  653. echo 'REQUIRE_SIGNIN_VIEW = false';
  654. echo 'ENABLE_CAPTCHA = false';
  655. echo '';
  656. echo '[other]';
  657. echo 'SHOW_FOOTER_BRANDING = false';
  658. echo 'SHOW_FOOTER_VERSION = false'; } >> ${GOGS_CONFIG_FILE}
  659. chmod 750 ${GOGS_CONFIG_FILE}
  660. chown -R "${GOGS_USERNAME}":"${GOGS_USERNAME}" "/home/${GOGS_USERNAME}"
  661. systemctl restart gogs
  662. if ! grep -q "gogs domain:" "${COMPLETION_FILE}"; then
  663. echo "gogs domain:${GIT_DOMAIN_NAME}" >> "${COMPLETION_FILE}"
  664. else
  665. sed -i "s|gogs domain.*|gogs domain:${GIT_DOMAIN_NAME}|g" "${COMPLETION_FILE}"
  666. fi
  667. function_check configure_firewall_for_git
  668. configure_firewall_for_git
  669. if ! grep -q "gogs version:" "${COMPLETION_FILE}"; then
  670. echo "gogs version:${GOGS_VERSION}" >> "${COMPLETION_FILE}"
  671. else
  672. sed -i "s|gogs version.*|gogs version:${GOGS_VERSION}|g" "${COMPLETION_FILE}"
  673. fi
  674. APP_INSTALLED=1
  675. }
  676. # NOTE: deliberately no exit 0