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