freedombone-app-gogs 26KB

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