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.

231 lines
6.3 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 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/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. admin_username=$(echo -e "$data" | yq '.admin.username')
  25. [[ $admin_username = 'null' ]] && echoerr "limesurver recipe requires \`data:admin:username\` into miaou.yaml service definition of <$fqdn>" && exit 1
  26. admin_email=$(echo -e "$data" | yq '.admin.email')
  27. [[ $admin_email = 'null' ]] && echoerr "limesurver recipe requires \`data:admin:email\` into miaou.yaml service definition of <$fqdn>" && exit 1
  28. echo "admin_email = [$admin_email]"
  29. admin_password=$(echo -e "$data" | yq '.admin.password')
  30. [[ $admin_password = 'null' ]] && echoerr "limesurver recipe requires \`data:admin:password\` into miaou.yaml service definition of <$fqdn>" && exit 1
  31. mkdir -p "$MIAOU_CONFIGDIR/apps/limesurvey"
  32. 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"
  33. echo "creating templates ... OK"
  34. echo "copying files to container <$container> ... "
  35. lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/limesurvey/$shortname.conf" "$container/etc/nginx/sites-available/$longname.conf"
  36. echo "copying files to container <$container> ... OK"
  37. if ! (db-maria list | grep -q "$longname"); then
  38. echo "create empty database <$longname> ... "
  39. db-maria create "$longname"
  40. echo "create empty database <$longname> ... OK"
  41. else
  42. echo "database already exists!"
  43. fi
  44. echo "initialize limesurvey $shortname $longname ..."
  45. lxc exec "$container" -- bash <<EOF
  46. set -Eeuo pipefail
  47. if [[ ! -d /var/www/limesurvey/$shortname ]]; then
  48. echo "installing new instance of limesurvey into /var/www/limesurvey/$shortname"
  49. unzip -q /var/www/limesurvey-latest.zip -d /var/www/limesurvey/$shortname
  50. mv /var/www/limesurvey/$shortname/limesurvey/* /var/www/limesurvey/$shortname
  51. rm -rf /var/www/limesurvey/$shortname/limesurvey
  52. else
  53. echo "instance of limesurvey /var/www/limesurvey/$shortname already defined!"
  54. fi
  55. if [[ ! -f /var/www/limesurvey/$shortname/application/config/config.php ]]; then
  56. echo "create config.php"
  57. cat << 'EOT2' > /var/www/limesurvey/$shortname/application/config/config.php
  58. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  59. return array(
  60. 'components' => array(
  61. 'db' => array(
  62. 'connectionString' => 'mysql:host=ct1.lxd;port=3306;dbname=$longname;',
  63. 'username' => '$longname',
  64. 'password' => '$longname',
  65. 'charset' => 'utf8mb4',
  66. 'emulatePrepare' => true,
  67. 'tablePrefix' => '',
  68. ),
  69. 'session' => array (
  70. 'sessionName'=>'$(genpasswd 10)',
  71. ),
  72. 'urlManager' => array(
  73. 'urlFormat' => 'get',
  74. 'rules' => array(),
  75. 'showScriptName' => true,
  76. ),
  77. ),
  78. 'config'=>array(
  79. 'debug'=>0,
  80. 'debugsql'=>0, // Set this to 1 to enanble sql logging, only active when debug = 2
  81. 'mysqlEngine' => 'MYISAM',
  82. 'force_ssl'=> true,
  83. )
  84. );
  85. EOT2
  86. echo 'initializing admin...'
  87. cd /var/www/limesurvey/$shortname/application/commands
  88. set +e
  89. php console.php install $admin_username $admin_password $admin_username $admin_email
  90. [[ \$? -ne 0 ]] && echo 'already initialized!'
  91. set -e
  92. echo OK
  93. echo 'Please visit URL with ?r=admin to log in!'
  94. else
  95. echo "config.php already defined!"
  96. fi
  97. chown -R www-data:www-data /var/www/limesurvey/$shortname
  98. echo "enabling limesurvey host into nginx"
  99. ln -sf "/etc/nginx/sites-available/$longname.conf" "/etc/nginx/sites-enabled/$longname.conf"
  100. echo "reloading nginx"
  101. nginx -tq && systemctl reload nginx
  102. EOF
  103. echo "initialize limesurvey $shortname $longname ... OK"
  104. }
  105. function _update() {
  106. echo "update"
  107. }
  108. function _delete() {
  109. echo "delete"
  110. }
  111. function usage() {
  112. echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
  113. exit 2
  114. }
  115. ### MAIN
  116. # init_strict
  117. COMMAND_NAME=$(basename "$0")
  118. # read the options
  119. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn:,domain:,subdomain:,data: -- "$@")
  120. # shellcheck disable=SC2181
  121. [[ "$?" -eq 0 ]] || usage
  122. eval set -- "$TEMP"
  123. action="unset"
  124. port="unset"
  125. fqdn="unset"
  126. container="unset"
  127. shortname="unset"
  128. longname="unset"
  129. data="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="limesurvey-$shortname"
  148. shift 2
  149. ;;
  150. --domain)
  151. shift 2
  152. ;;
  153. --subdomain)
  154. shift 2
  155. ;;
  156. --data)
  157. data=$2
  158. shift 2
  159. ;;
  160. -c)
  161. [[ "$action" == "unset" ]] || usage
  162. action="_create"
  163. shift 1
  164. ;;
  165. -r)
  166. [[ "$action" == "unset" ]] || usage
  167. action="_read"
  168. shift 1
  169. ;;
  170. -u)
  171. [[ "$action" == "unset" ]] || usage
  172. action="_update"
  173. shift 1
  174. ;;
  175. -d)
  176. [[ "$action" == "unset" ]] || usage
  177. action="_delete"
  178. shift 1
  179. ;;
  180. --)
  181. shift
  182. break
  183. ;;
  184. *)
  185. echoerr "Internal error: unrecognized option: <$1>"
  186. exit 1
  187. ;;
  188. esac
  189. done
  190. . "$MIAOU_BASEDIR/lib/init.sh"
  191. [[
  192. "$action" != unset &&
  193. "$port" != unset &&
  194. "$container" != unset &&
  195. "$data" != unset &&
  196. "$fqdn" != unset &&
  197. "$shortname" != unset ]] || usage
  198. $action