provisioning tool for building opinionated architecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.7 KiB

9 months ago
  1. #!/bin/bash
  2. function check_database_exists() {
  3. db-maria list | grep -q "$longname"
  4. }
  5. function check_port_used() {
  6. # shellcheck disable=SC2034
  7. usedport=$(lxc exec "$container" -- bash -c "grep Listen /etc/apache2/sites-enabled/$longname.conf | cut -d' ' -f2")
  8. [[ "$usedport" == "$port" ]]
  9. }
  10. function check_service_running() {
  11. lxc exec "$container" -- bash -c "systemctl is-active --quiet apache2.service"
  12. }
  13. function check_config_defined() {
  14. lxc exec "$container" -- bash -c "test -f /var/www/cagettepei/$shortname/config.xml"
  15. }
  16. function _read() {
  17. disable_trace
  18. check_database_exists
  19. check_container "$container"
  20. check_port_used
  21. check_config_defined
  22. check_service_running
  23. enable_trace
  24. return 0
  25. }
  26. function _create() {
  27. echo "creating CagettePéi instance for <$shortname> ... "
  28. mkdir -p "$MIAOU_CONFIGDIR/apps/cagettepei"
  29. APP_PORT=$port APP_NAME=$shortname tera -e --env-key env -t "$MIAOU_DIR/templates/apps/cagettepei/cagettepei-host.j2" -o "$MIAOU_CONFIGDIR/apps/cagettepei/$longname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml"
  30. echo "creating templates ... OK"
  31. echo "copying files over container <$container> ... "
  32. lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/cagettepei/$longname.conf" "$container/etc/apache2/sites-available/$longname.conf"
  33. echo "copying files over container <$container> ... OK"
  34. if ! (db-maria list | grep -q "$longname"); then
  35. echo "create empty database <$longname> ... "
  36. db-maria create "$longname"
  37. echo "create empty database <$longname> ... OK"
  38. DB_INIT=true
  39. else
  40. echo "database already exists!"
  41. DB_INIT=false
  42. fi
  43. credential_username=$(load_yaml_from_expanded credential.username)
  44. credential_email=$(load_yaml_from_expanded credential.email)
  45. echo "initialize cagettepei $shortname $longname ..."
  46. lxc exec "$container" -- bash <<EOF
  47. set -Eeuo pipefail
  48. if [[ ! -d /var/www/cagettepei/$shortname ]]; then
  49. echo "installing new instance of cagettepei into /var/www/cagettepei/$shortname"
  50. cd /tmp
  51. if [[ -d cagettepei ]]; then
  52. echo "refreshing previous clone"
  53. cd cagettepei
  54. git pull
  55. cd ..
  56. else
  57. echo "cloning"
  58. git clone https://git.artcode.re/cagetters/cagettepei.git
  59. fi
  60. cp -r cagettepei /var/www/cagettepei/$shortname
  61. cd /var/www/cagettepei/$shortname
  62. echo COMPILING...
  63. export HAXE_STD_PATH=/opt/haxe_20180221160843_bb7b827/std
  64. haxelib setup .haxelib
  65. make install ENV=dev
  66. echo OK
  67. else
  68. echo "instance of cagettepei /var/www/cagettepei/$shortname already defined!"
  69. fi
  70. if diff -q /var/www/cagettepei/$shortname/config.xml /var/www/cagettepei/$shortname/config.xml.dist; then
  71. echo "create config.xml"
  72. cat << 'EOT2' > /var/www/cagettepei/$shortname/config.xml
  73. <config
  74. database="mysql://cagettepei-$shortname:cagettepei-$shortname@ct1.lxd/cagettepei-$shortname"
  75. host="$fqdn"
  76. name = "$shortname"
  77. default_email = "postmaster@artcode.re"
  78. webmaster_email = "postmaster@artcode.re"
  79. key="carotteMagique"
  80. lang="fr"
  81. langs="fr"
  82. langnames="Français"
  83. sqllog="0"
  84. debug="0"
  85. cache="0"
  86. maintain="0"
  87. cachetpl="0"
  88. />
  89. EOT2
  90. else
  91. echo "config.xml already defined!"
  92. fi
  93. a2ensite $longname.conf
  94. mkdir -p /var/log/apache2/cagettepei/$shortname
  95. systemctl restart apache2
  96. if [[ $DB_INIT == true ]]; then
  97. echo "Force TABLES initialization"
  98. curl localhost:$port
  99. echo "Set administrator password..."
  100. echo "insert into User values (1,'fr','c3513c793b13471f3a49bdb22acb66de',1,'$credential_username','Admin', '$credential_email', null, null, null, null, null, null, null, null, null, now(), now(),6,null, null);" | mariadb cagettepei-$shortname -u cagettepei-$shortname -pcagettepei-$shortname -h ct1.lxd
  101. echo "TODO: password \`cagette\` should be changed soon!!!"
  102. fi
  103. EOF
  104. echo "initialize cagettepei $shortname $longname ... OK"
  105. }
  106. function _update() {
  107. echo "update"
  108. }
  109. function _delete() {
  110. echo "delete"
  111. }
  112. function usage() {
  113. echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
  114. exit 2
  115. }
  116. ### MAIN
  117. # init_strict
  118. COMMAND_NAME=$(basename "$0")
  119. # read the options
  120. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn: -- "$@")
  121. # shellcheck disable=SC2181
  122. [[ "$?" -eq 0 ]] || usage
  123. eval set -- "$TEMP"
  124. action="unset"
  125. port="unset"
  126. container="unset"
  127. shortname="unset"
  128. longname="unset"
  129. fqdn="unset"
  130. # extract options and their arguments into variables.
  131. while true; do
  132. case "$1" in
  133. --port)
  134. port=$2
  135. shift 2
  136. ;;
  137. --fqdn)
  138. fqdn=$2
  139. shift 2
  140. ;;
  141. --container)
  142. container=$2
  143. shift 2
  144. ;;
  145. --name)
  146. shortname=$2
  147. longname="cagettepei-$shortname"
  148. shift 2
  149. ;;
  150. -c)
  151. [[ "$action" == "unset" ]] || usage
  152. action="_create"
  153. shift 1
  154. ;;
  155. -r)
  156. [[ "$action" == "unset" ]] || usage
  157. action="_read"
  158. shift 1
  159. ;;
  160. -u)
  161. [[ "$action" == "unset" ]] || usage
  162. action="_update"
  163. shift 1
  164. ;;
  165. -d)
  166. [[ "$action" == "unset" ]] || usage
  167. action="_delete"
  168. shift 1
  169. ;;
  170. --)
  171. shift
  172. break
  173. ;;
  174. *)
  175. echo "Internal error!"
  176. exit 1
  177. ;;
  178. esac
  179. done
  180. [[
  181. "$action" != unset &&
  182. "$port" != unset &&
  183. "$container" != unset &&
  184. "$fqdn" != unset &&
  185. "$shortname" != unset ]] || usage
  186. . "$MIAOU_BASEDIR/lib/init.sh"
  187. $action