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.
 
 

192 lines
5.3 KiB

#!/bin/bash
function check_database_exists() {
db-psql list | grep -q "$longname"
}
function check_port_used() {
# shellcheck disable=SC2034
usedport=$(lxc exec "$container" -- bash -c "grep xmlrpc_port /etc/odoo12/$shortname.conf | cut -d' ' -f3")
[[ "$usedport" == "$port" ]]
}
function check_service_running() {
lxc exec "$container" -- bash -c "systemctl is-active --quiet ${longname}.service"
}
function _read() {
disable_trace
check_database_exists
check_container "$container"
check_port_used
check_service_running
enable_trace
return 0
}
function _create() {
echo "creating templates ... "
mkdir -p "$MIAOU_CONFIGDIR/apps/odoo12"
longport=$((port + 1000))
APP_PORT=$port LONG_PORT=$longport APP_NAME=$shortname tera -e -t "$MIAOU_BASEDIR/templates/apps/odoo12/odoo.conf.j2" -o "$MIAOU_CONFIGDIR/apps/odoo12/$shortname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml" >/dev/null
APP_NAME=$shortname tera -t "$MIAOU_BASEDIR/templates/apps/odoo12/odoo.service.j2" --env-only -o "$MIAOU_CONFIGDIR/apps/odoo12/$longname.service" >/dev/null
echo "creating templates ... OK"
echo "copying files over container <$container> ... "
lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/odoo12/$shortname.conf" "$container/etc/odoo12/$shortname.conf"
lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/odoo12/$longname.service" "$container/etc/systemd/system/$longname.service"
echo "copying files over container <$container> ... OK"
if ! (db-psql list | grep -q "$longname"); then
echo "create empty database <$longname> ... "
db-psql create "$longname"
echo "create empty database <$longname> ... OK"
admin_username=$(cat "$data" | yq '.admin.username')
[[ $admin_username == 'null ' ]] && echoerr "odoo12 recipe requires \`data:admin:username\` into miaou.yaml service definition of <$fqdn>" && exit 1
admin_password=$(cat "$data" | yq '.admin.password')
[[ $admin_password == 'null ' ]] && echoerr "odoo12 recipe requires \`data:admin:password\` into miaou.yaml service definition of <$fqdn>" && exit 1
cat <<EOF | lxc_exec "$container"
set -Eeuo pipefail
echo reloading systemd
systemctl daemon-reload
systemctl stop $longname
echo initialize database...
su odoo -c "/home/odoo/venv/bin/python3 /home/odoo/odoo12/odoo-bin -c /etc/odoo12/$shortname.conf -i base --without-demo=all --stop-after-init"
echo initialize database OK
echo install default modules ...
su odoo -c "/home/odoo/venv/bin/python3 /home/odoo/odoo12/odoo-bin -c /etc/odoo12/$shortname.conf -i account,contacts,l10n_fr,account,sale,point_of_sale -d $longname --worker=0 --stop-after-init"
echo default modules OK
chmod u+w -R "/home/odoo/data-$shortname"
echo "UPDATE res_users SET login='$admin_username', password='$admin_password' WHERE id=2" | PGPASSWORD=$longname psql -U $longname -h ct1.lxd
EOF
else
echo "database already exists!"
fi
echo "initialize odoo $shortname $longname ..."
cat <<EOF | lxc_exec "$container"
set -Eeuo pipefail
echo reloading systemd
systemctl daemon-reload
if ! systemctl is-active --quiet $longname; then
echo start service $longname
systemctl start $longname
systemctl is-active --quiet $longname
systemctl enable $longname
else
echo service $longname already started!
fi
EOF
echo "initialize odoo $shortname $longname ... OK"
}
function _update() {
echo "TODO: update"
}
function _delete() {
echo "TODO: delete"
}
function usage() {
echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --fqdn FQDN --container CONTAINER --name NAME --data DATA"
exit 2
}
### MAIN
# init_strict
COMMAND_NAME=$(basename "$0")
# read the options
TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn:,data: -- "$@")
# shellcheck disable=SC2181
[[ "$?" -eq 0 ]] || usage
eval set -- "$TEMP"
action="unset"
port="unset"
fqdn="unset"
container="unset"
shortname="unset"
longname="unset"
data="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="odoo12-$shortname"
shift 2
;;
--data)
data=$2
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!"
exit 1
;;
esac
done
[[
"$action" != unset &&
"$port" != unset &&
"$fqdn" != unset &&
"$container" != unset &&
"$shortname" != unset &&
"$data" != unset ]] || usage
. "$MIAOU_BASEDIR/lib/init.sh"
$action