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.

222 lines
5.8 KiB

9 months ago
9 months ago
1 month ago
9 months ago
9 months ago
1 month ago
9 months ago
9 months ago
9 months ago
1 month ago
9 months ago
1 month ago
9 months ago
1 month 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 cagettepei 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 "template $longname.conf... 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 ! check_db_maria_exists "$longname"; then
  32. echo -n "create empty database <$longname> ... "
  33. db-maria create "$longname"
  34. PREFIX="" echoinfo "DONE"
  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. curl localhost:$port
  94. sleep 5
  95. if [[ $DB_INIT == true ]]; then
  96. echo "Force TABLES initialization"
  97. echo "Set administrator password..."
  98. 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
  99. echo "TODO: password \`cagette\` should be changed soon!!!"
  100. fi
  101. EOF
  102. echo "initialize cagettepei $shortname $longname ... OK"
  103. }
  104. function _update() {
  105. echo "update"
  106. }
  107. function _delete() {
  108. echo "delete"
  109. }
  110. function usage() {
  111. echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
  112. exit 2
  113. }
  114. ### MAIN
  115. # init_strict
  116. source "$(dirname "$0")/../base/common.sh"
  117. COMMAND_NAME=$(basename "$0")
  118. # read the options
  119. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn:,domain:,subdomain: -- "$@")
  120. # shellcheck disable=SC2181
  121. [[ "$?" -eq 0 ]] || usage
  122. eval set -- "$TEMP"
  123. action="unset"
  124. port="unset"
  125. container="unset"
  126. shortname="unset"
  127. longname="unset"
  128. fqdn="unset"
  129. # extract options and their arguments into variables.
  130. while true; do
  131. case "$1" in
  132. --port)
  133. port=$2
  134. shift 2
  135. ;;
  136. --fqdn)
  137. fqdn=$2
  138. shift 2
  139. ;;
  140. --container)
  141. container=$2
  142. shift 2
  143. ;;
  144. --name)
  145. shortname=$2
  146. longname="cagettepei-$shortname"
  147. shift 2
  148. ;;
  149. --domain)
  150. shift 2
  151. ;;
  152. --subdomain)
  153. shift 2
  154. ;;
  155. -c)
  156. [[ "$action" == "unset" ]] || usage
  157. action="_create"
  158. shift 1
  159. ;;
  160. -r)
  161. [[ "$action" == "unset" ]] || usage
  162. action="_read"
  163. shift 1
  164. ;;
  165. -u)
  166. [[ "$action" == "unset" ]] || usage
  167. action="_update"
  168. shift 1
  169. ;;
  170. -d)
  171. [[ "$action" == "unset" ]] || usage
  172. action="_delete"
  173. shift 1
  174. ;;
  175. --)
  176. shift
  177. break
  178. ;;
  179. *)
  180. echo "Internal error: unrecognized option: <$1>"
  181. exit 1
  182. ;;
  183. esac
  184. done
  185. [[
  186. "$action" != unset &&
  187. "$port" != unset &&
  188. "$container" != unset &&
  189. "$fqdn" != unset &&
  190. "$shortname" != unset ]] || usage
  191. . "$MIAOU_BASEDIR/lib/init.sh"
  192. $action