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.

221 lines
5.9 KiB

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