freedombone-app-gogs 25KB

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