1
0

init.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. domain_name="http://127.0.0.1"
  18. modules=""
  19. meta_module="meta.sr.ht"
  20. git_module="git.sr.ht"
  21. hg_module="hg.sr.ht"
  22. build_module="builds.sr.ht"
  23. list_module="lists.sr.ht"
  24. man_module="man.sr.ht"
  25. paste_module="paste.sr.ht"
  26. todo_module="todo.sr.ht"
  27. function add_module() {
  28. read -r input
  29. echo
  30. if [[ "$input" -le 1 ]]; then
  31. modules=$modules" "$1
  32. echo -e "$yellow $1 is selected${none}"
  33. fi
  34. }
  35. function progress() {
  36. echo "$2 IS RUNNING..."
  37. printf "[▓"
  38. while kill -0 "$1" 2>/dev/null; do
  39. printf "▓"
  40. sleep 0.05
  41. done
  42. wait "$1"
  43. printf "▓] done!"
  44. }
  45. function generate_config() {
  46. service_key=$(grep <genkeys "Service" | awk -F: '{gsub(/[[:blank:]]*/,"");print $2}')
  47. network_key=$(grep <genkeys "Network" | awk -F: '{gsub(/[[:blank:]]*/,"");print $2}')
  48. webhook_key=$(grep <genkeys "Webhook Private" | awk -F: '{gsub(/[[:blank:]]*/,"");print $2}')
  49. sed -i "s/{{SERVICE_KEY}}/$service_key/" config.ini
  50. sed -i "s/{{NETWORK_KEY}}/$network_key/" config.ini
  51. sed -i "s@{{WEBHOOK_KEY}}@$webhook_key@" config.ini
  52. sed -i "s@{{DOMAIN}}@$domain_name@" config.ini
  53. }
  54. function generate_keys() {
  55. docker run 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"}'
  56. }
  57. function build_base_image() {
  58. docker build -t sr.ht-base:dev ./base/ && echo
  59. }
  60. function generate_launch_shell() {
  61. md="$(echo "$meta_module $1" | awk '{ gsub(/\./,""); print $0 }')"
  62. md_count=$(echo "$md" | awk '{print NF}')
  63. cp ./template/config.ini.template config.ini
  64. echo "" >"start.sh"
  65. for ((i = 1; i <= "$md_count"; i++)); do
  66. m=$(echo "$md" | awk -v i="$i" '{print $i }')
  67. mp="500$i"
  68. echo "$m-initdb" >>"start.sh"
  69. echo "/usr/bin/gunicorn $m.app:app -b 0.0.0.0:$mp -D" >>"start.sh"
  70. m_domain="http://$m.$domain_name"
  71. if [[ "$domain_name" == "http://127.0.0.1" ]]; then
  72. m_domain=$domain_name:$mp
  73. fi
  74. tp="{{"$m"_domain}}"
  75. sed -i "s@$tp@$m_domain@" config.ini
  76. done
  77. sed "s/{{PORTS}}/5001-500$md_count:5001-500$md_count/" ./template/docker-compose.yml.template >docker-compose.yml
  78. # fix builds launch
  79. sed -i "s/buildssrht/buildsrht/" start.sh
  80. echo "tail -f /dev/null" >>start.sh
  81. }
  82. function select_version_control() {
  83. # Git or Mercurial or Both
  84. echo -e "Select your distributed version control system ${cyan}1.Git${none} or ${cyan}2.Mercurial${none} or ${cyan}3.Both${none}?"
  85. read -r version_control_system_input
  86. echo
  87. if [[ "$version_control_system_input" -le 1 ]]; then
  88. modules=$modules" "$git_module
  89. echo -e "$yellow $modules is selected${none}"
  90. elif [[ "$version_control_system_input" -le 2 ]]; then
  91. modules=$modules" "$hg_module
  92. echo -e "$yellow $modules is selected${none}"
  93. else
  94. modules=$modules' '$git_module' '$hg_module
  95. echo -e "$yellow $modules are selected${none}"
  96. fi
  97. }
  98. function advance_config() {
  99. # CI
  100. # builds.sr.ht
  101. echo -e "Do you want to use ${cyan} Sourcehut CI ${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  102. add_module "$build_module"
  103. echo -e "Do you want to use ${cyan}Mailing list service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  104. add_module "$list_module"
  105. # wiki service
  106. # man.sr.ht
  107. echo -e "Do you want to use ${cyan}Wiki service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  108. add_module "$man_module"
  109. # Syntax highlighting
  110. # paste.sr.ht
  111. echo -e "Do you want to use ${cyan}Syntax highlighting${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  112. add_module "$paste_module"
  113. # issue and bug tracker service
  114. # todo.sr.ht
  115. echo -e "Do you want to use ${cyan}issue and bug tracker service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  116. add_module "$todo_module"
  117. }
  118. function select_mode() {
  119. min_mode="Minimal Installation(Install only required modules)"
  120. max_mod="Maximize Installation(Install all modules)"
  121. adv_mod="Advanced Mode(Choose your own personalized modules)"
  122. quit="Quit"
  123. mode=("$min_mode" "$max_mod" "$adv_mod" "$quit")
  124. PS3="Select the installation mode: "
  125. select item in "${mode[@]}"; do
  126. case $item in
  127. "$min_mode")
  128. echo "$item"
  129. select_version_control
  130. break
  131. ;;
  132. "$max_mod")
  133. echo "$item"
  134. modules=$modules"$git_module $hg_module $build_module $list_module $man_module $paste_module $todo_module"
  135. break
  136. ;;
  137. "$adv_mod")
  138. echo "$item"
  139. advance_config
  140. break
  141. ;;
  142. "$quit")
  143. exit
  144. ;;
  145. quit)
  146. break
  147. ;;
  148. *)
  149. echo "Invalid option $REPLY"
  150. ;;
  151. esac
  152. done
  153. }
  154. ## Start
  155. select_mode
  156. # Set Domain
  157. echo -e "Set your ${cyan} Domain name (no http(s) prefix )${none} or ${cyan}s (skip) to use localhost${none}?"
  158. read -r domain_input
  159. echo
  160. echo "domain_input :$domain_input"
  161. if [[ "$domain_input" == 's' || -z "$domain_input" ]]; then
  162. echo -e "$yellow skip${none}"
  163. else
  164. domain_name=$domain_input
  165. fi
  166. echo "domain: $domain_name"
  167. echo
  168. echo
  169. generate_launch_shell "$modules" &
  170. progress $! "🤖 Generate Launch Shell"
  171. echo
  172. echo
  173. sed "s/{{MODULES}}/$modules/" ./template/Dockerfile.template >Dockerfile &
  174. progress $! "🐋 Generate Dockerfile"
  175. echo
  176. echo
  177. build_base_image &
  178. progress $! "🐋 Build Base Image"
  179. echo
  180. echo
  181. generate_keys &
  182. progress $! "🔒 Generate Keys"
  183. echo
  184. echo
  185. echo -e "$(<genkeys)"
  186. echo -e "${yellow}Distribute the webhook public key to anyone who would want to verify ${none}"
  187. echo -e "${yellow}webhook payloads from your service.${none}"
  188. echo -e "${yellow}you can see generated keys in the 'genkeys' file ${none}"
  189. echo
  190. echo
  191. generate_config
  192. progress $! "🔧 Generate Config"
  193. echo
  194. echo
  195. # TODO modules config
  196. # TODO Database secret