init.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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://localhost"
  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.h"
  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 "s/{{SERVICE_KEY}}/$service_key/" ./template/config.ini.template >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. m="$meta_module $1"
  62. m_count=$(echo "$m" | awk '{print NF}')
  63. echo "$m" | awk '{ gsub(/\./,"");for (i=1; i<=NF; i++) print "/usr/bin/gunicorn "$i ".app:app -b 0.0.0.0:500" i > "start.sh" }'
  64. sed "s/{{PORTS}}/5001-500$m_count:5001-500$m_count/" ./template/docker-compose.yml.template >docker-compose.yml
  65. echo "tail -f /dev/null" >> start.sh
  66. }
  67. function select_version_control() {
  68. # Git or Mercurial or Both
  69. echo -e "Select your distributed version control system ${cyan}1.Git${none} or ${cyan}2.Mercurial${none} or ${cyan}3.Both${none}?"
  70. read -r version_control_system_input
  71. echo
  72. if [[ "$version_control_system_input" -le 1 ]]; then
  73. modules=$modules" "$git_module
  74. echo -e "$yellow $modules is selected${none}"
  75. elif [[ "$version_control_system_input" -le 2 ]]; then
  76. modules=$modules" "$hg_module
  77. echo -e "$yellow $modules is selected${none}"
  78. else
  79. modules=$modules' '$git_module' '$hg_module
  80. echo -e "$yellow $modules are selected${none}"
  81. fi
  82. }
  83. function advance_config() {
  84. # CI
  85. # builds.sr.ht
  86. echo -e "Do you want to use ${cyan} Sourcehut CI ${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  87. add_module "$build_module"
  88. echo -e "Do you want to use ${cyan}Mailing list service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  89. add_module "$list_module"
  90. # wiki service
  91. # man.sr.ht
  92. echo -e "Do you want to use ${cyan}Wiki service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  93. add_module "$man_module"
  94. # Syntax highlighting
  95. # paste.sr.ht
  96. echo -e "Do you want to use ${cyan}Syntax highlighting${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  97. add_module "$paste_module"
  98. # issue and bug tracker service
  99. # todo.sr.ht
  100. echo -e "Do you want to use ${cyan}issue and bug tracker service${none}? ${cyan}1.Yes${none} or ${cyan}2.No${none}?"
  101. add_module "$todo_module"
  102. }
  103. function select_mode() {
  104. min_mode="Minimal Installation(Install only required modules)"
  105. max_mod="Maximize Installation(Install all modules)"
  106. adv_mod="Advanced Mode(Choose your own personalized modules)"
  107. quit="Quit"
  108. mode=("$min_mode" "$max_mod" "$adv_mod" "$quit")
  109. PS3="Select the installation mode: "
  110. select item in "${mode[@]}"; do
  111. case $item in
  112. "$min_mode")
  113. echo "$item"
  114. select_version_control
  115. break
  116. ;;
  117. "$max_mod")
  118. echo "$item"
  119. modules=$modules"$git_module $hg_module $build_module $list_module $man_module $paste_module $todo_module"
  120. break
  121. ;;
  122. "$adv_mod")
  123. echo "$item"
  124. advance_config
  125. break
  126. ;;
  127. "$quit")
  128. exit
  129. ;;
  130. quit)
  131. break
  132. ;;
  133. *)
  134. echo "Invalid option $REPLY"
  135. ;;
  136. esac
  137. done
  138. }
  139. ## Start
  140. select_mode
  141. # Set Domain
  142. echo -e "Set your ${cyan} Domain name ${none} or ${cyan}s (skip)${none}?"
  143. read -r domain_input
  144. echo
  145. if [[ "$domain_input" == 's' ]] || [[ -z "$null" ]]; then
  146. echo -e "$yellow skip${none}"
  147. else
  148. domain_name=$domain_input
  149. fi
  150. echo
  151. echo
  152. generate_launch_shell "$modules" &
  153. progress $! "🤖 Generate Launch Shell"
  154. echo
  155. echo
  156. sed "s/{{MODULES}}/$modules/" ./template/Dockerfile.template >Dockerfile &
  157. progress $! "🐋 Generate Dockerfile"
  158. echo
  159. echo
  160. build_base_image &
  161. progress $! "🐋 Build Base Image"
  162. echo
  163. echo
  164. generate_keys &
  165. progress $! "🔒 Generate Keys"
  166. echo
  167. echo
  168. echo -e "$(<genkeys)"
  169. echo -e "${yellow}Distribute the webhook public key to anyone who would want to verify ${none}"
  170. echo -e "${yellow}webhook payloads from your service.${none}"
  171. echo -e "${yellow}you can see generated keys in the 'genkeys' file ${none}"
  172. echo
  173. echo
  174. generate_config
  175. progress $! "🔧 Generate Config"
  176. echo
  177. echo
  178. # TODO modules config
  179. # TODO Database secret