second commit
This commit is contained in:
Executable
+180
@@ -0,0 +1,180 @@
|
||||
#!/bin/bash
|
||||
|
||||
function check_database_exists() {
|
||||
db-psql list | grep -q "$longname"
|
||||
}
|
||||
|
||||
function check_port_used() {
|
||||
# shellcheck disable=SC2034
|
||||
usedport=$(lxc exec "$container" -- cat /etc/nginx/sites-enabled/"$longname".conf | grep listen | cut -d ' ' -f2)
|
||||
[[ "$usedport" == "$port" ]]
|
||||
}
|
||||
|
||||
function check_directory_exists() {
|
||||
lxc exec "$container" -- test -d /var/www/"$longname"
|
||||
}
|
||||
|
||||
function check_service_running() {
|
||||
lxc exec "$container" -- bash -c "systemctl is-active --quiet nginx.service"
|
||||
}
|
||||
|
||||
function _read() {
|
||||
disable_trace
|
||||
check_database_exists
|
||||
check_container "$container"
|
||||
check_port_used
|
||||
check_directory_exists
|
||||
check_service_running
|
||||
enable_trace
|
||||
return 0
|
||||
}
|
||||
|
||||
function _create() {
|
||||
PREFIX="recipe:dolibarr:create"
|
||||
: $PREFIX
|
||||
|
||||
echo "create a Dolibarr instance for $shortname"
|
||||
|
||||
lxc exec "$container" -- bash <<EOF
|
||||
set -Eeuo pipefail
|
||||
echo "install latest release ... "
|
||||
cd /var/www
|
||||
PATH="\$PATH:/opt/debian-bash/tools"
|
||||
VERSION="\$(wget_semver github Dolibarr/dolibarr)"
|
||||
if [[ ! -f "dolibarr-\$VERSION.tgz" ]]; then
|
||||
wget_release github Dolibarr/dolibarr
|
||||
else
|
||||
echo "dolibarr version=\$VERSION already downloaded!"
|
||||
fi
|
||||
|
||||
if [[ ! -d "$longname" ]]; then
|
||||
tar -xzvf "dolibarr-\$VERSION.tgz"
|
||||
mv dolibarr-\$VERSION $longname
|
||||
chown www-data:www-data -R $longname
|
||||
else
|
||||
echo "$longname already created!"
|
||||
fi
|
||||
|
||||
EOF
|
||||
|
||||
echo "generating configuration files from templates... "
|
||||
mkdir -p "$MIAOU_CONFIGDIR/apps/dolibarr/$shortname"
|
||||
PHP_VERSION=$(lxc exec "$container" -- dpkg -l php-fpm | grep "^ii" | cut -d ':' -f2 | cut -d '+' -f1)
|
||||
APP_PORT=$port APP_NAME=$longname PHP_VERSION=$PHP_VERSION tera -e -t "$MIAOU_DIR/templates/apps/dolibarr/host.j2" -o "$MIAOU_CONFIGDIR/apps/dolibarr/$shortname/host.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml" >/dev/null
|
||||
|
||||
echo "copying configuration files onto container <$container>... "
|
||||
lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/dolibarr/$shortname/host.conf" "$container/etc/nginx/sites-available/$longname.conf"
|
||||
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"
|
||||
|
||||
else
|
||||
echo "database already exists!"
|
||||
fi
|
||||
|
||||
echo "enable host config..."
|
||||
lxc exec "$container" -- bash <<EOF
|
||||
set -Eeuo pipefail
|
||||
cd /etc/nginx/sites-enabled
|
||||
ln -sf ../sites-available/$longname.conf
|
||||
mkdir -p /var/log/nginx/$longname
|
||||
nginx -t
|
||||
systemctl reload nginx
|
||||
EOF
|
||||
echo "enable host config... OK"
|
||||
}
|
||||
|
||||
function _update() {
|
||||
echo "TODO: update"
|
||||
}
|
||||
|
||||
function _delete() {
|
||||
echo "TODO: delete"
|
||||
}
|
||||
|
||||
function usage() {
|
||||
echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
|
||||
exit 2
|
||||
}
|
||||
|
||||
### MAIN
|
||||
|
||||
# init_strict
|
||||
|
||||
COMMAND_NAME=$(basename "$0")
|
||||
|
||||
# read the options
|
||||
|
||||
TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn: -- "$@")
|
||||
# shellcheck disable=SC2181
|
||||
[[ "$?" -eq 0 ]] || usage
|
||||
eval set -- "$TEMP"
|
||||
|
||||
action="unset"
|
||||
port="unset"
|
||||
container="unset"
|
||||
shortname="unset"
|
||||
longname="unset"
|
||||
|
||||
# extract options and their arguments into variables.
|
||||
while true; do
|
||||
case "$1" in
|
||||
--port)
|
||||
port=$2
|
||||
shift 2
|
||||
;;
|
||||
--fqdn)
|
||||
shift 2
|
||||
;;
|
||||
--container)
|
||||
container=$2
|
||||
shift 2
|
||||
;;
|
||||
--name)
|
||||
shortname=$2
|
||||
longname="dolibarr-$shortname"
|
||||
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
|
||||
;;
|
||||
*)
|
||||
echo "Internal error!"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[
|
||||
"$action" != unset &&
|
||||
"$port" != unset &&
|
||||
"$container" != unset &&
|
||||
"$shortname" != unset ]] || usage
|
||||
|
||||
. "$MIAOU_BASEDIR/lib/init.sh"
|
||||
|
||||
$action
|
||||
Executable
+56
@@ -0,0 +1,56 @@
|
||||
#!/bin/bash
|
||||
|
||||
MANDATORY_PACKAGES_STRING="nginx php-fpm postgresql-client php-pgsql php-intl php-curl php-gd php-zip php-imap php-xml php-mbstring"
|
||||
|
||||
function check() {
|
||||
PREFIX="recipe:dolibarr:check"
|
||||
: $PREFIX
|
||||
|
||||
check_mandatory_packages || return 11
|
||||
check_one_release || return 12
|
||||
|
||||
echo "container <$CONTAINER> approved successfully!"
|
||||
return 0
|
||||
}
|
||||
|
||||
function check_mandatory_packages() {
|
||||
lxc exec "$CONTAINER" -- bash <<EOF
|
||||
set -Eeuo pipefail
|
||||
mapfile -t PACKAGES <<< "$MANDATORY_PACKAGES_STRING"
|
||||
for package in \${PACKAGES[@]}; do
|
||||
dpkg -l "\$package" 2>/dev/null | grep -q ^ii
|
||||
done
|
||||
EOF
|
||||
}
|
||||
|
||||
function check_one_release() {
|
||||
lxc exec "$CONTAINER" -- /TOOLBOX/fd -1q -tf "dolibarr-" /var/www
|
||||
}
|
||||
|
||||
function install() {
|
||||
echo "recipe:dolibarr installing..."
|
||||
lxc exec "$CONTAINER" -- bash <<EOF
|
||||
cloud-init status --wait >/dev/null
|
||||
apt update
|
||||
apt install -y $MANDATORY_PACKAGES_STRING
|
||||
cd /var/www
|
||||
PATH="\$PATH:/opt/debian-bash/tools"
|
||||
VERSION="\$(wget_semver github Dolibarr/dolibarr)"
|
||||
if [[ ! -f "dolibarr-\$VERSION.tgz" ]]; then
|
||||
wget_release github Dolibarr/dolibarr
|
||||
else
|
||||
echo "dolibarr version=\$VERSION already downloaded!"
|
||||
fi
|
||||
EOF
|
||||
}
|
||||
|
||||
. "$MIAOU_BASEDIR/lib/init.sh"
|
||||
|
||||
arg1_required "$@"
|
||||
readonly CONTAINER="$1"
|
||||
launch_container "$CONTAINER"
|
||||
|
||||
check || (
|
||||
install
|
||||
check
|
||||
)
|
||||
Reference in New Issue
Block a user