init.sh 4.9 KB

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