1
0

init.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/bin/bash
  2. #======================================
  3. # Author: ulyc
  4. #======================================
  5. set -eo pipefail
  6. red='\e[91m'
  7. green='\e[92m'
  8. yellow='\e[93m'
  9. magenta='\e[95m'
  10. cyan='\e[96m'
  11. none='\e[0m'
  12. _red() { echo -e "${red}$*${none}"; }
  13. _green() { echo -e "${green}$*${none}"; }
  14. _yellow() { echo -e "${yellow}$*${none}"; }
  15. _magenta() { echo -e "${magenta}$*${none}"; }
  16. _cyan() { echo -e "${cyan}$*${none}"; }
  17. local_domain="localtest.me"
  18. domain_name="${local_domain}"
  19. modules=""
  20. #meta_module="meta.sr.ht"
  21. git_module="git.sr.ht"
  22. hg_module="hg.sr.ht"
  23. build_module="builds.sr.ht"
  24. list_module="lists.sr.ht"
  25. man_module="man.sr.ht"
  26. paste_module="paste.sr.ht"
  27. todo_module="todo.sr.ht"
  28. function add_module() {
  29. read -r input
  30. echo
  31. if [[ "$input" -le 1 ]]; then
  32. modules=$modules" "$1
  33. echo -e "$yellow $1 is selected${none}"
  34. fi
  35. }
  36. function progress() {
  37. echo "$2 IS RUNNING..."
  38. printf "[▓"
  39. while kill -0 "$1" 2>/dev/null; do
  40. printf "▓"
  41. sleep 0.05
  42. done
  43. wait "$1"
  44. printf "▓] done!"
  45. }
  46. function generate_config() {
  47. service_key=$(grep <genkeys "Service" | awk -F: '{gsub(/[[:blank:]]*/,"");print $2}')
  48. network_key=$(grep <genkeys "Network" | awk -F: '{gsub(/[[:blank:]]*/,"");print $2}')
  49. webhook_key=$(grep <genkeys "Webhook Private" | awk -F: '{gsub(/[[:blank:]]*/,"");print $2}')
  50. sed -i "s/{{SERVICE_KEY}}/$service_key/" config.ini
  51. sed -i "s/{{NETWORK_KEY}}/$network_key/" config.ini
  52. sed -i "s@{{WEBHOOK_KEY}}@$webhook_key@" config.ini
  53. sed -i "s@{{DOMAIN}}@$domain_name@" config.ini
  54. }
  55. function generate_keys() {
  56. docker run --rm sr.ht-base:dev sh -c 'srht-keygen service && srht-keygen network && srht-keygen webhook' | awk '{n[1]="Service";n[2]="Network";n[3]=n[4]="Webhook";print n[NR]" "$0 > "genkeys"}'
  57. docker run --rm -v /tmp/gpg-output:/root/.gnupg vladgh/gpg --batch --passphrase '' --quick-gen-key CHANGEME@example.org
  58. docker run --rm -v /tmp/gpg-output:/root/.gnupg vladgh/gpg -a --export-secret-key CHANGEME@example.org >srht.priv
  59. docker run --rm -v /tmp/gpg-output:/root/.gnupg vladgh/gpg -a --export CHANGEME@example.org >srht.pub
  60. rm -r /tmp/gpg-output
  61. }
  62. function build_base_image() {
  63. docker build -t sr.ht-base:dev ./base/ && echo
  64. }
  65. function set_domain() {
  66. sed "s@$local_domain@$domain_name@g" template/config.ini.template >config.ini
  67. sed -i "s@$local_domain@$domain_name@g" $(grep -rl $local_domain nginx_conf)
  68. }
  69. function select_version_control() {
  70. # Git or Mercurial or Both
  71. echo -e "Select your distributed version control system ${cyan}1.Git${none} or ${cyan}2.Mercurial${none} or ${cyan}3.Both${none}?"
  72. read -r version_control_system_input
  73. echo
  74. if [[ "$version_control_system_input" -le 1 ]]; then
  75. modules=$modules" "$git_module
  76. echo -e "$yellow $modules is selected${none}"
  77. elif [[ "$version_control_system_input" -le 2 ]]; then
  78. modules=$modules" "$hg_module
  79. echo -e "$yellow $modules is selected${none}"
  80. else
  81. modules=$modules' '$git_module' '$hg_module
  82. echo -e "$yellow $modules are selected${none}"
  83. fi
  84. }
  85. function advance_config() {
  86. # CI
  87. # builds.sr.ht
  88. echo -e "Do you want to use ${cyan} Sourcehut CI ${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  89. add_module "$build_module"
  90. echo -e "Do you want to use ${cyan}Mailing list service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  91. add_module "$list_module"
  92. # wiki service
  93. # man.sr.ht
  94. echo -e "Do you want to use ${cyan}Wiki service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  95. add_module "$man_module"
  96. # issue and bug tracker service
  97. # todo.sr.ht
  98. echo -e "Do you want to use ${cyan}issue and bug tracker service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  99. add_module "$todo_module"
  100. # Syntax highlighting
  101. # paste.sr.ht
  102. echo -e "Do you want to use ${cyan} ad-hoc text file service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  103. add_module "$paste_module"
  104. }
  105. function select_mode() {
  106. min_mode="Minimal Installation(Install only required modules)"
  107. max_mod="Maximize Installation(Install all modules)"
  108. adv_mod="Advanced Mode(Choose your own personalized modules)"
  109. quit="Quit"
  110. mode=("$min_mode" "$max_mod" "$adv_mod" "$quit")
  111. PS3="Select the installation mode: "
  112. select item in "${mode[@]}"; do
  113. case $item in
  114. "$min_mode")
  115. echo "$item"
  116. select_version_control
  117. break
  118. ;;
  119. "$max_mod")
  120. echo "$item"
  121. modules=$modules"$git_module $hg_module $build_module $list_module $man_module $paste_module $todo_module"
  122. break
  123. ;;
  124. "$adv_mod")
  125. echo "$item"
  126. advance_config
  127. break
  128. ;;
  129. "$quit")
  130. exit
  131. ;;
  132. quit)
  133. break
  134. ;;
  135. *)
  136. echo "Invalid option $REPLY"
  137. ;;
  138. esac
  139. done
  140. }
  141. ## Start
  142. select_mode
  143. # Set Domain
  144. echo -e "Set your ${cyan} Domain name (no http(s) prefix )${none} or ${cyan}s (skip) to use localhost${none}?"
  145. read -r domain_input
  146. echo
  147. echo "domain_input :$domain_input"
  148. if [[ "$domain_input" == 's' || -z "$domain_input" ]]; then
  149. echo -e "$yellow skip${none}"
  150. cp template/config.ini.template config.ini
  151. else
  152. domain_name=$domain_input
  153. set_domain
  154. fi
  155. echo "domain: $domain_name"
  156. echo
  157. echo
  158. #generate_launch_shell "$modules" &
  159. #progress $! "🤖 Generate Launch Shell"
  160. echo
  161. echo
  162. sed "s/{{MODULES}}/$modules/" ./template/Dockerfile.template >Dockerfile &
  163. progress $! "🐋 Generate Dockerfile"
  164. echo
  165. echo
  166. db_names=$(echo "$modules" | tr -d '.' | tr " " ",")
  167. sed "s/{{database_name}}/$db_names/" ./template/docker-compose.yml.template >docker-compose.yml &
  168. progress $! "🐋 Generate DockerCompose file"
  169. echo
  170. echo
  171. build_base_image &
  172. progress $! "🐋 Build Base Image"
  173. echo
  174. echo
  175. generate_keys &
  176. progress $! "🔒 Generate Keys"
  177. echo
  178. echo
  179. echo -e "$(<genkeys)"
  180. echo -e "${yellow}Distribute the webhook public key to anyone who would want to verify ${none}"
  181. echo -e "${yellow}webhook payloads from your service.${none}"
  182. echo -e "${yellow}you can see generated keys in the 'genkeys' file ${none}"
  183. echo
  184. echo
  185. generate_config
  186. progress $! "🔧 Generate Config"
  187. echo
  188. echo