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.

210 lines
5.4 KiB

1 month 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/nginx/sites-enabled/$longname.conf | cut -d' ' -f6")
  8. [[ "$usedport" == "$port" ]]
  9. }
  10. function check_service_running() {
  11. lxc exec "$container" -- bash -c "systemctl is-active --quiet nginx.service"
  12. }
  13. function _read() {
  14. disable_trace
  15. check_database_exists
  16. check_container "$container"
  17. check_port_used
  18. check_service_running
  19. enable_trace
  20. return 0
  21. }
  22. function _create() {
  23. echo "creating limesurvey instance for <$shortname> ..."
  24. mkdir -p "$MIAOU_CONFIGDIR/apps/limesurvey"
  25. APP_PORT=$port APP_NAME=$shortname tera -e --env-key env -t "$MIAOU_BASEDIR/templates/apps/limesurvey/limesurvey-host.j2" -o "$MIAOU_CONFIGDIR/apps/limesurvey/$shortname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml"
  26. echo "creating templates ... OK"
  27. echo "copying files to container <$container> ... "
  28. lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/limesurvey/$shortname.conf" "$container/etc/nginx/sites-available/$longname.conf"
  29. echo "copying files to container <$container> ... OK"
  30. if ! (db-maria list | grep -q "$longname"); then
  31. echo "create empty database <$longname> ... "
  32. db-maria create "$longname"
  33. echo "create empty database <$longname> ... OK"
  34. else
  35. echo "database already exists!"
  36. fi
  37. echo "initialize limesurvey $shortname $longname ..."
  38. lxc exec "$container" -- bash <<EOF
  39. set -Eeuo pipefail
  40. if [[ ! -d /var/www/limesurvey/$shortname ]]; then
  41. echo "installing new instance of limesurvey into /var/www/limesurvey/$shortname"
  42. unzip -q /var/www/limesurvey-latest.zip -d /var/www/limesurvey/$shortname
  43. mv /var/www/limesurvey/$shortname/limesurvey/* /var/www/limesurvey/$shortname
  44. rm -rf /var/www/limesurvey/$shortname/limesurvey
  45. else
  46. echo "instance of limesurvey /var/www/limesurvey/$shortname already defined!"
  47. fi
  48. if [[ ! -f /var/www/limesurvey/$shortname/application/config/config.php ]]; then
  49. echo "create config.php"
  50. cat << 'EOT2' > /var/www/limesurvey/$shortname/application/config/config.php
  51. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  52. return array(
  53. 'components' => array(
  54. 'db' => array(
  55. 'connectionString' => 'mysql:host=ct1.lxd;port=3306;dbname=$longname;',
  56. 'username' => '$longname',
  57. 'password' => '$longname',
  58. 'charset' => 'utf8mb4',
  59. 'emulatePrepare' => true,
  60. 'tablePrefix' => '',
  61. ),
  62. 'session' => array (
  63. 'sessionName'=>'$(genpasswd 10)',
  64. ),
  65. 'urlManager' => array(
  66. 'urlFormat' => 'get',
  67. 'rules' => array(),
  68. 'showScriptName' => true,
  69. ),
  70. ),
  71. 'config'=>array(
  72. 'debug'=>0,
  73. 'debugsql'=>0, // Set this to 1 to enanble sql logging, only active when debug = 2
  74. 'mysqlEngine' => 'MYISAM',
  75. )
  76. );
  77. EOT2
  78. echo 'initializing admin...'
  79. cd /var/www/limesurvey/$shortname/application/commands
  80. set +e
  81. php console.php install pvincent reunion pvincent pvincent@arcode.re
  82. [[ \$? -ne 0 ]] && echo 'already initialized!'
  83. set -e
  84. echo OK
  85. echo 'Please visit URL with ?r=admin to log in!'
  86. else
  87. echo "config.php already defined!"
  88. fi
  89. chown -R www-data:www-data /var/www/limesurvey/$shortname
  90. echo "enabling limesurvey host into nginx"
  91. ln -sf "/etc/nginx/sites-available/$longname.conf" "/etc/nginx/sites-enabled/$longname.conf"
  92. echo "reloading nginx"
  93. nginx -tq && systemctl reload nginx
  94. EOF
  95. echo "initialize limesurvey $shortname $longname ... OK"
  96. }
  97. function _update() {
  98. echo "update"
  99. }
  100. function _delete() {
  101. echo "delete"
  102. }
  103. function usage() {
  104. echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
  105. exit 2
  106. }
  107. ### MAIN
  108. # init_strict
  109. COMMAND_NAME=$(basename "$0")
  110. # read the options
  111. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn:,domain:,subdomain: -- "$@")
  112. # shellcheck disable=SC2181
  113. [[ "$?" -eq 0 ]] || usage
  114. eval set -- "$TEMP"
  115. action="unset"
  116. port="unset"
  117. container="unset"
  118. shortname="unset"
  119. longname="unset"
  120. # extract options and their arguments into variables.
  121. while true; do
  122. case "$1" in
  123. --port)
  124. port=$2
  125. shift 2
  126. ;;
  127. --fqdn)
  128. shift 2
  129. ;;
  130. --container)
  131. container=$2
  132. shift 2
  133. ;;
  134. --name)
  135. shortname=$2
  136. longname="limesurvey-$shortname"
  137. shift 2
  138. ;;
  139. --domain)
  140. shift 2
  141. ;;
  142. --subdomain)
  143. shift 2
  144. ;;
  145. -c)
  146. [[ "$action" == "unset" ]] || usage
  147. action="_create"
  148. shift 1
  149. ;;
  150. -r)
  151. [[ "$action" == "unset" ]] || usage
  152. action="_read"
  153. shift 1
  154. ;;
  155. -u)
  156. [[ "$action" == "unset" ]] || usage
  157. action="_update"
  158. shift 1
  159. ;;
  160. -d)
  161. [[ "$action" == "unset" ]] || usage
  162. action="_delete"
  163. shift 1
  164. ;;
  165. --)
  166. shift
  167. break
  168. ;;
  169. *)
  170. echoerr "Internal error: unrecognized option: <$1>"
  171. exit 1
  172. ;;
  173. esac
  174. done
  175. . "$MIAOU_BASEDIR/lib/init.sh"
  176. [[
  177. "$action" != unset &&
  178. "$port" != unset &&
  179. "$container" != unset &&
  180. "$shortname" != unset ]] || usage
  181. $action