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.
 
 

163 lines
4.3 KiB

#!/bin/bash
function check_service_running() {
lxc exec "$container" -- bash -c "docker ps --format '{{.Names}},{{.Ports}}' | grep $1 | grep -q $2"
}
function _read() {
disable_trace
check_db_postgres_exists "$longname"
check_container "$container"
check_service_running "$longname" "$port"
enable_trace
}
function _create() {
echo "creating discourse instance for <$shortname> ... "
echo "initialize discourse $shortname $longname ... OK"
if ! (db-psql list | grep -q "$longname"); then
echo "create empty database <$longname> ... "
db-psql create "$longname"
db-psql use "$longname" "CREATE EXTENSION IF NOT EXISTS hstore"
db-psql use "$longname" "CREATE EXTENSION IF NOT EXISTS pg_trgm"
echo "create empty database <$longname> ... OK"
else
echo "database already exists!"
fi
admin_username=$(load_yaml_from_expanded services[\""$domain"\"][\""$subdomain"\"].data.admin.username)
admin_email=$(load_yaml_from_expanded services[\""$domain"\"][\""$subdomain"\"].data.admin.email)
admin_password=$(load_yaml_from_expanded services[\""$domain"\"][\""$subdomain"\"].data.admin.password)
redis_password=$(load_yaml_from_expanded credential.redis)
mkdir -p "$MIAOU_CONFIGDIR/apps/discourse"
APP_REDIS_PASSWORD=$redis_password APP_DOMAIN=$domain APP_SUBDOMAIN=$subdomain APP_FQDN=$fqdn APP_PORT=$port APP_NAME=$longname tera -e --env-key env -t "$MIAOU_BASEDIR/templates/apps/discourse/forum.yml.j2" -o "$MIAOU_CONFIGDIR/apps/discourse/$longname.yml" "$MIAOU_CONFIGDIR/miaou.expanded.yaml" >/dev/null
echo "creating templates ... OK"
echo "copying files to container <$container> ... "
lxc file push --uid 0 --gid 0 --mode 600 "$MIAOU_CONFIGDIR/apps/discourse/$longname.yml" "$container/var/discourse/containers/$longname.yml"
echo "copying files to container <$container> ... OK"
console="u=User.create_with(email: '$admin_email', password: '$admin_password').find_or_initialize_by(username: '$admin_username'); u.save; u.activate"
# console="puts User.count"
activate_admin="rails runner \"$console\""
echo "build docker container $longname ..."
lxc exec "$container" -- /var/discourse/launcher rebuild "$longname"
echo "initialize admin user <$admin_username>"
lxc exec "$container" -- /var/discourse/launcher run "$longname" "$activate_admin"
echo "initialize discourse $longname ... OK"
}
function _update() {
echo "update"
}
function _delete() {
echo "delete"
}
function usage() {
echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
exit 2
}
### MAIN
# init_strict
source "$(dirname "$0")/../base/common.sh"
COMMAND_NAME=$(basename "$0")
# read the options
TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn:,domain:,subdomain:,data: -- "$@")
# shellcheck disable=SC2181
[[ "$?" -eq 0 ]] || usage
eval set -- "$TEMP"
action="unset"
port="unset"
container="unset"
shortname="unset"
longname="unset"
fqdn="unset"
domain="unset"
subdomain="unset"
# extract options and their arguments into variables.
while true; do
case "$1" in
--port)
port=$2
shift 2
;;
--fqdn)
fqdn=$2
shift 2
;;
--container)
container=$2
shift 2
;;
--name)
shortname=$2
longname="discourse-$shortname"
shift 2
;;
--domain)
domain=$2
shift 2
;;
--subdomain)
subdomain=$2
shift 2
;;
--data)
# don't care
shift 2
;;
-c)
[[ "$action" == "unset" ]] || usage
action="_create"
shift 1
;;
-r)
[[ "$action" == "unset" ]] || usage
action="_read"
shift 1
;;
-u)
[[ "$action" == "unset" ]] || usage
action="_update"
shift 1
;;
-d)
[[ "$action" == "unset" ]] || usage
action="_delete"
shift 1
;;
--)
shift
break
;;
*)
echoerr "Internal error: unrecognized option: <$1>"
exit 1
;;
esac
done
[[
"$action" != unset &&
"$port" != unset &&
"$container" != unset &&
"$fqdn" != unset &&
"$domain" != unset &&
"$subdomain" != unset &&
"$shortname" != unset ]] || usage
. "$MIAOU_BASEDIR/lib/init.sh"
$action