second commit

This commit is contained in:
pvincent
2024-02-21 23:32:34 +04:00
parent 9a4551ca3a
commit 7cdc45397d
82 changed files with 7172 additions and 6 deletions
+218
View File
@@ -0,0 +1,218 @@
#!/bin/bash
function check_database_exists() {
db-maria list | grep -q "$longname"
}
function check_port_used() {
# shellcheck disable=SC2034
usedport=$(lxc exec "$container" -- bash -c "grep Listen /etc/apache2/sites-enabled/$longname.conf | cut -d' ' -f2")
[[ "$usedport" == "$port" ]]
}
function check_service_running() {
lxc exec "$container" -- bash -c "systemctl is-active --quiet apache2.service"
}
function check_config_defined() {
lxc exec "$container" -- bash -c "test -f /var/www/cagettepei/$shortname/config.xml"
}
function _read() {
disable_trace
check_database_exists
check_container "$container"
check_port_used
check_config_defined
check_service_running
enable_trace
return 0
}
function _create() {
echo "creating CagettePéi instance for <$shortname> ... "
mkdir -p "$MIAOU_CONFIGDIR/apps/cagettepei"
APP_PORT=$port APP_NAME=$shortname tera -e --env-key env -t "$MIAOU_DIR/templates/apps/cagettepei/cagettepei-host.j2" -o "$MIAOU_CONFIGDIR/apps/cagettepei/$longname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml"
echo "creating templates ... OK"
echo "copying files over container <$container> ... "
lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/cagettepei/$longname.conf" "$container/etc/apache2/sites-available/$longname.conf"
echo "copying files over container <$container> ... OK"
if ! (db-maria list | grep -q "$longname"); then
echo "create empty database <$longname> ... "
db-maria create "$longname"
echo "create empty database <$longname> ... OK"
DB_INIT=true
else
echo "database already exists!"
DB_INIT=false
fi
credential_username=$(load_yaml_from_expanded credential.username)
credential_email=$(load_yaml_from_expanded credential.email)
echo "initialize cagettepei $shortname $longname ..."
lxc exec "$container" -- bash <<EOF
set -Eeuo pipefail
if [[ ! -d /var/www/cagettepei/$shortname ]]; then
echo "installing new instance of cagettepei into /var/www/cagettepei/$shortname"
cd /tmp
if [[ -d cagettepei ]]; then
echo "refreshing previous clone"
cd cagettepei
git pull
cd ..
else
echo "cloning"
git clone https://git.artcode.re/cagetters/cagettepei.git
fi
cp -r cagettepei /var/www/cagettepei/$shortname
cd /var/www/cagettepei/$shortname
echo COMPILING...
export HAXE_STD_PATH=/opt/haxe_20180221160843_bb7b827/std
haxelib setup .haxelib
make install ENV=dev
echo OK
else
echo "instance of cagettepei /var/www/cagettepei/$shortname already defined!"
fi
if diff -q /var/www/cagettepei/$shortname/config.xml /var/www/cagettepei/$shortname/config.xml.dist; then
echo "create config.xml"
cat << 'EOT2' > /var/www/cagettepei/$shortname/config.xml
<config
database="mysql://cagettepei-$shortname:cagettepei-$shortname@ct1.lxd/cagettepei-$shortname"
host="$fqdn"
name = "$shortname"
default_email = "postmaster@artcode.re"
webmaster_email = "postmaster@artcode.re"
key="carotteMagique"
lang="fr"
langs="fr"
langnames="Français"
sqllog="0"
debug="0"
cache="0"
maintain="0"
cachetpl="0"
/>
EOT2
else
echo "config.xml already defined!"
fi
a2ensite $longname.conf
mkdir -p /var/log/apache2/cagettepei/$shortname
systemctl restart apache2
if [[ $DB_INIT == true ]]; then
echo "Force TABLES initialization"
curl localhost:$port
echo "Set administrator password..."
echo "insert into User values (1,'fr','c3513c793b13471f3a49bdb22acb66de',1,'$credential_username','Admin', '$credential_email', null, null, null, null, null, null, null, null, null, now(), now(),6,null, null);" | mariadb cagettepei-$shortname -u cagettepei-$shortname -pcagettepei-$shortname -h ct1.lxd
echo "TODO: password \`cagette\` should be changed soon!!!"
fi
EOF
echo "initialize cagettepei $shortname $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
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"
fqdn="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="cagettepei-$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 &&
"$fqdn" != unset &&
"$shortname" != unset ]] || usage
. "$MIAOU_BASEDIR/lib/init.sh"
$action
+187
View File
@@ -0,0 +1,187 @@
#!/bin/bash
readonly UNWANTED_PACKAGES_STRING="nginx node python haxe"
readonly MANDATORY_PACKAGES_STRING="wget apache2 make git imagemagick gettext libapache2-mod-neko mariadb-client sendemail libio-socket-ssl-perl libnet-ssleay-perl"
### CHECK
function check() {
PREFIX="recipe:cagettepei:check"
check_unwanted_packages || return 21
check_mandatory_packages || return 22
check_apache_modules || return 23
check_node8 || return 24
check_python2 || return 25
check_haxe3 || return 26
check_cagettepei_batch || return 35
check_cagettepei_timers || return 36
echo "container <$CONTAINER> approved successfully!"
}
function check_apache_modules() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
test -L /etc/apache2/mods-enabled/neko.load
test -L /etc/apache2/mods-enabled/rewrite.load
true
EOF
}
function check_cagettepei_batch() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
test -f /var/www/cagettepei/cagettepei-batch
true
EOF
}
function check_cagettepei_timers() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
test -f /etc/systemd/system/cagettepei-batch-minute.service
test -f /etc/systemd/system/cagettepei-batch-minute.timer
test -f /etc/systemd/system/cagettepei-batch-day.service
test -f /etc/systemd/system/cagettepei-batch-day.timer
true
EOF
}
function check_node8() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
node --version | grep -q 'v8.17.0'
EOF
}
function check_python2() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
python --version 2>&1 | grep -q 'Python 2.7.18'
EOF
}
function check_haxe3() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
haxe -version 2>&1 | grep -q '3.4.7'
EOF
}
function check_unwanted_packages() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
mapfile -t PACKAGES <<< "$UNWANTED_PACKAGES_STRING"
for package in \${PACKAGES[@]}; do
! (dpkg -l "\$package" 2>/dev/null | grep -q ^ii)
done
true # useful because for might return last inexistant package
EOF
}
function check_mandatory_packages() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
mapfile -t PACKAGES <<< "$MANDATORY_PACKAGES_STRING"
for package in \${PACKAGES[@]}; do
dpkg-query -l "\$package" 2>/dev/null | grep -q ^ii
done
EOF
}
### INSTALL
function install() {
PREFIX="recipe:cagettepei:install"
: $PREFIX
launch_container "$CONTAINER"
echo "initializing CagettePéi ... "
echo -n "check unwanted packages..."
check_unwanted_packages
PREFIX="" echo "OK"
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
echo installing CagettePéi...
apt-get update
apt-get install -y $MANDATORY_PACKAGES_STRING
echo installing custom Node8...
wget https://nodejs.org/download/release/v8.17.0/node-v8.17.0-linux-x64.tar.gz -O /tmp/node-v8.17.0-linux-x64.tar.gz
tar -xzvf /tmp/node-v8.17.0-linux-x64.tar.gz -C /opt
chown root:root -R /opt/node-v8.17.0-linux-x64
ln -sf /opt/node-v8.17.0-linux-x64/bin/node /usr/local/bin/
ln -sf /opt/node-v8.17.0-linux-x64/bin/npm /usr/local/bin/
echo -n DONE
echo installing custom Python2 with pypy...
wget https://downloads.python.org/pypy/pypy2.7-v7.3.13-linux64.tar.bz2 -O /tmp/pypy2.7-v7.3.13-linux64.tar.bz2
apt install -y bzip2
bunzip2 -f /tmp/pypy2.7-v7.3.13-linux64.tar.bz2
tar -xvf /tmp/pypy2.7-v7.3.13-linux64.tar -C /opt
ln -sf /opt/pypy2.7-v7.3.13-linux64/bin/python /usr/local/bin/
ln -sf /opt/pypy2.7-v7.3.13-linux64/bin/python2 /usr/local/bin/
echo -n DONE
echo installing custom Haxe3...
wget https://github.com/HaxeFoundation/haxe/releases/download/3.4.7/haxe-3.4.7-linux64.tar.gz -O /tmp/haxe-3.4.7-linux64.tar.gz
tar -xzvf /tmp/haxe-3.4.7-linux64.tar.gz -C /opt
ln -sf /opt/haxe_20180221160843_bb7b827/haxe /usr/local/bin/
ln -sf /opt/haxe_20180221160843_bb7b827/haxelib /usr/local/bin/
echo -n DONE
systemctl stop apache2
rm -f /etc/apache2/sites-available/{000-default,default-ssl}.conf
rm -f /etc/apache2/sites-enabled/000-default.conf
rm -rf /var/www/html
sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
sed -i 's/^Listen 80$//' /etc/apache2/ports.conf
echo "prepare folder for cagettepei instances"
mkdir -p /var/www/cagettepei
echo "enable neko and rewrite apache2 modules"
a2enmod neko
a2enmod rewrite
EOF
echo -n "copy cagettepei-batch..."
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/cagettepei-batch" "$CONTAINER/var/www/cagettepei/cagettepei-batch"
lxc exec "$CONTAINER" -- chmod +x /var/www/cagettepei/cagettepei-batch
PREFIX="" echo "OK"
echo -n "copy cagettepei timers in systemd..."
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/systemd/cagettepei-batch-minute.service" "$CONTAINER/etc/systemd/system/cagettepei-batch-minute.service"
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/systemd/cagettepei-batch-minute.timer" "$CONTAINER/etc/systemd/system/cagettepei-batch-minute.timer"
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/systemd/cagettepei-batch-day.service" "$CONTAINER/etc/systemd/system/cagettepei-batch-day.service"
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/cagettepei/systemd/cagettepei-batch-day.timer" "$CONTAINER/etc/systemd/system/cagettepei-batch-day.timer"
PREFIX="" echo "OK"
echo -n "override apache2 service to launch cagettepei timers..."
lxc exec "$CONTAINER" -- bash -c "SYSTEMD_EDITOR=tee systemctl edit apache2 <<EOT
[Unit]
BindsTo = cagettepei-batch-minute.timer cagettepei-batch-day.timer
EOT"
PREFIX="" echo "OK"
echo "enable and start cagettepei timers in systemd..."
lxc exec "$CONTAINER" -- bash <<EOF
systemctl enable cagettepei-batch-minute.timer cagettepei-batch-day.timer
systemctl start cagettepei-batch-minute.timer cagettepei-batch-day.timer
EOF
PREFIX="" echo "OK"
}
### MAIN
. "$MIAOU_BASEDIR/lib/init.sh"
arg1_required "$@"
readonly CONTAINER="$1"
check || (
install
check
)
+124
View File
@@ -0,0 +1,124 @@
#!/bin/bash
readonly EXPANDED_CONF="$MIAOU_CONFIGDIR/miaou.expanded.yaml"
TARGET=$(yq '.target' "$EXPANDED_CONF")
readonly TARGET
function check() {
container_exists "$CONTAINER" || return 1
container_running "$CONTAINER" || return 2
check_reverseproxy || return 4
check_banner || return 5
check_certbot || return 6
PREFIX="recipe:dmz:check" echo "container <$CONTAINER> approved successfully!"
return 0
}
function check_reverseproxy() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
dpkg -l nginx | grep -q ^ii
systemctl is-active --quiet nginx
nginx -tq
EOF
}
function check_certbot() {
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
dpkg -l certbot | grep -q ^ii
dpkg -l python3-certbot-nginx | grep -q ^ii
EOF
}
function check_banner() {
if [[ $TARGET != "prod" ]]; then
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
test -f /etc/nginx/snippets/banner_$TARGET.conf
EOF
fi
}
function install() {
PREFIX="recipe:dmz:install"
: $PREFIX
echowarn "about to deploy new container <$CONTAINER> ..."
if ! container_exists "$CONTAINER"; then
echowarn "about to create new container <$CONTAINER> ..."
lxc-miaou-create "$CONTAINER"
echo OK
fi
if ! container_running "$CONTAINER"; then
echowarn "about to start asleep container <$CONTAINER> ..."
lxc start "$CONTAINER"
echo OK
fi
credential_email=$(load_yaml_from_expanded credential.email)
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
apt-get update && apt-get dist-upgrade -y
apt-get install -y nginx ssl-cert libnginx-mod-http-subs-filter certbot python3-certbot-nginx
echo "registering with your default credential email <$credential_email>"
certbot register --agree-tos --email $credential_email --no-eff-email || echo "already resgistered!"
rm /etc/nginx/sites-{enabled,available}/default -f
systemctl enable nginx
nginx -tq || rm /etc/nginx/sites-enabled/hosts
systemctl start nginx
EOF
if [[ "$TARGET" != "prod" ]]; then
echo "copying Nginx banner to container <$CONTAINER> ... "
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/nginx/snippets/banner_$TARGET.conf" "$CONTAINER/etc/nginx/snippets/banner_$TARGET.conf"
echo "copying files over container <$CONTAINER> ... OK"
else
echo "no Nginx banner on PROD!"
fi
echo "populate nftables entries into yaml"
local wan_interface dmz_ip
wan_interface=$(ip route show default | cut -d ' ' -f5)
dmz_ip=$(host "$CONTAINER.lxd" | cut -d ' ' -f4)
yq ".nftables.wan_interface=\"$wan_interface\"" "$EXPANDED_CONF" -i
yq ".nftables.dmz_ip=\"$dmz_ip\"" "$EXPANDED_CONF" -i
local nftables_reloading=false
if [[ "$TARGET" != "dev" ]]; then
mkdir -p "$MIAOU_CONFIGDIR/nftables.rules.d"
echo "nat http/s port to dmz"
tera -t "$MIAOU_BASEDIR/templates/nftables/nat.table.j2" "$EXPANDED_CONF" -o "$MIAOU_CONFIGDIR/nftables.rules.d/nat.table" &>/dev/null
sudo cp "$MIAOU_CONFIGDIR/nftables.rules.d/nat.table" /etc/nftables.rules.d/nat.table
nftables_reloading=true
else
if [[ -f /etc/nftables.rules.d/nat.table ]]; then
sudo_required "remove previous nat.table"
sudo rm -f /etc/nftables.rules.d/nat.table
nftables_reloading=true
fi
fi
if [[ "$nftables_reloading" == true ]]; then
sudo_required "reload nftables"
sudo systemctl reload nftables.service
fi
}
# MAIN
. "$MIAOU_BASEDIR/lib/init.sh"
arg1_required "$@"
readonly CONTAINER="$1"
check || (
install
check
)
+21
View File
@@ -0,0 +1,21 @@
#!/bin/bash
function check() {
echowarn "recipe:dokuwiki not yet checked!"
return 0
}
function install() {
echowarn "recipe:dokuwiki not yet initialized!"
}
. "$MIAOU_BASEDIR/lib/init.sh"
arg1_required "$@"
readonly CONTAINER="$1"
launch_container "$CONTAINER"
check || (
install
check
)
+180
View File
@@ -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
+56
View File
@@ -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
)
+65
View File
@@ -0,0 +1,65 @@
#!/bin/bash
function check() {
PREFIX="recipe:mariadb:check"
dpkg -l mariadb-client | grep -q ^ii || return 9
container_running "$CONTAINER" || return 10
cat <<EOF | lxc_exec "$CONTAINER" || return 20
set -Eeuo pipefail
systemctl is-active mariadb.service &>/dev/null
ss -tlnp | grep 0.0.0.0:3306 | grep -q maria
test -f /etc/default/automysqlbackup
grep -q BACKUPDIR=\"/mnt/BACKUP/mariadb\" /etc/default/automysqlbackup
EOF
echo "container <$CONTAINER> approved successfully!"
return 0
}
function build_device_backup() {
PREFIX="recipe:mariadb:backup"
if ! (lxc config device list "$CONTAINER" | grep -q BACKUP); then
local backup_dir="$HOME/LXD/BACKUP/databases-$CONTAINER"
mkdir -p "$backup_dir"
lxc config device add "$CONTAINER" BACKUP disk source=$backup_dir path=mnt/BACKUP
fi
}
function install() {
sudo_required
PREFIX="recipe:mariadb:install"
: $PREFIX
sudo /opt/debian-bash/tools/idem_apt_install mariadb-client
echowarn "initializing ..."
launch_container "$CONTAINER"
build_device_backup
echowarn "executing various commands onto container <$CONTAINER>, please be patient ..."
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
cloud-init status --wait >/dev/null
. /opt/debian-bash/lib/functions.sh
apt update && apt dist-upgrade -y
/opt/debian-bash/tools/idem_apt_install mariadb-server automysqlbackup
echo "change bind-adress"
/opt/debian-bash/tools/append_or_replace "^bind-address.*$" "bind-address = 0.0.0.0" /etc/mysql/mariadb.conf.d/50-server.cnf
systemctl restart mariadb.service
function systemctl-exists() ([ \$(systemctl list-unit-files "\${1}*" | wc -l) -gt 3 ])
systemctl-exists exim4.service && systemctl stop exim4.service && systemctl disable exim4.service
/opt/debian-bash/tools/append_or_replace "^BACKUPDIR=.*$" "BACKUPDIR=\"/mnt/BACKUP/mariadb\"" /etc/default/automysqlbackup
exit 0
EOF
echo DONE
}
# MAIN
. "$MIAOU_BASEDIR/lib/init.sh"
arg1_required "$@"
readonly CONTAINER="$1"
check || (
install
check
)
+180
View File
@@ -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" -- 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_DIR/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_DIR/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"
credential_username=$(load_yaml_from_expanded credential.username)
credential_password=$(load_yaml_from_expanded credential.password)
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 "TODO: change administrator password, default credential applied!"
echo "UPDATE res_users SET login='$credential_username', password='$credential_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 --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="odoo12-$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
+158
View File
@@ -0,0 +1,158 @@
#!/bin/bash
readonly EXPANDED_CONF="$MIAOU_CONFIGDIR/miaou.expanded.yaml"
readonly WKHTML_VERSION="0.12.6-1"
readonly WKHTML_RELEASE="$WKHTML_VERSION.buster_amd64"
function check_user_odoo() {
(lxc exec "$CONTAINER" -- id odoo &>/dev/null) || return 12
return 0
}
function check_target_bgcolor() {
(lxc exec "$CONTAINER" -- grep -Pq "^\\\$o-community-color: $BACKGROUND_COLOR" /home/odoo/odoo12/addons/web/static/src/scss/primary_variables.scss) || return 13
return 0
}
function check_wkhtmltox() {
(lxc exec "$CONTAINER" -- dpkg -l | grep -s wkhtmltox | grep -qs $WKHTML_VERSION) || return 1
}
function check_python() {
(lxc exec "$CONTAINER" -- test -d /opt/Python-3.7.13) || return 1
}
function check_venv() {
(lxc exec "$CONTAINER" -- test -d /home/odoo/venv) || return 1
}
function check_favicon() {
lxc exec "$CONTAINER" -- test -L /home/odoo/odoo12/addons/web/static/src/img/favicon.ico
}
function check_file_odoo-addon-install() {
(lxc exec "$CONTAINER" -- test -f /home/odoo/odoo12/odoo12-addon-install) || return 23
}
function check() {
PREFIX="recipe:odoo12:check"
check_wkhtmltox || return 10
check_python || return 11
check_user_odoo || return 12
check_target_bgcolor || return 13
check_venv || return 14
check_favicon || return 15
check_file_odoo-addon-install || return 23
echo "container <$CONTAINER> approved successfully!"
return 0
}
function install() {
PREFIX="recipe:odoo12:install"
: $PREFIX
launch_container "$CONTAINER"
echo "initializing Odoo12 ... "
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
echo "installing odoo12..."
apt-get update && apt-get dist-upgrade -y
if dpkg -l | grep -s wkhtmltox | grep -qs $WKHTML_VERSION; then
echo package=wkhtmltox version=$WKHTML_RELEASE already found!
else
echo "wkhtmltox version=$WKHTML_RELEASE has to be installed!"
wget https://github.com/wkhtmltopdf/packaging/releases/download/$WKHTML_VERSION/wkhtmltox_$WKHTML_RELEASE.deb
dpkg -i wkhtmltox_$WKHTML_RELEASE.deb || (apt -fy install && rm wkhtmltox_$WKHTML_RELEASE.deb)
fi
if [[ ! -d /opt/Python-3.7.13 ]]; then
echo "installing Python-3.7.13..."
apt-get install -y zlib1g-dev python3-pip python3-venv xz-utils build-essential libssl-dev python3-dev libxml2-dev libxslt1-dev libldap2-dev libsasl2-dev libpq-dev libffi-dev postgresql-client
wget -O- https://www.python.org/ftp/python/3.7.13/Python-3.7.13.tar.xz | tar -xJ -C /opt
chown -R root:root /opt/Python-3.7.13
cd /opt/Python-3.7.13
./configure --enable-optimizations --enable-shared
make -j8 altinstall
ldconfig /opt/Python3.7.13
echo "Python-3.7.13...OK"
else
echo "Python-3.7.13 already installed!"
fi
if ! id -u odoo &>/dev/null; then
echo "creating system user <odoo>"
useradd -rms /bin/bash odoo
else
echo "user <odoo> already exists!"
fi
cat <<EOT | su - odoo
if [[ ! -d odoo12 ]]; then
echo "git odoo12 from remote"
git clone https://github.com/odoo/odoo.git --depth 1 --branch 12.0 odoo12
else
echo "git odoo12 already downloaded!"
fi
if [[ ! -d venv ]]; then
echo "installing Python-3.7 virtual env (venv)"
python3.7 -m venv venv
source venv/bin/activate
python -m pip install --upgrade pip
pip install wheel
pip install -r odoo12/requirements.txt
else
echo "venv (Python-3.7) already installed!"
fi
echo "community-color change to $BACKGROUND_COLOR"
/opt/debian-bash/tools/append_or_replace "^.*o-community-color:.*" "\\\\\\\$o-community-color: $BACKGROUND_COLOR;" /home/odoo/odoo12/addons/web/static/src/scss/primary_variables.scss
EOT
mkdir -p /etc/odoo12
EOF
lxc file push "$MIAOU_BASEDIR/templates/apps/odoo12/odoo12-addon-install" "$CONTAINER/home/odoo/odoo12/odoo12-addon-install"
lxc exec "$CONTAINER" -- bash <<EOF
chown odoo:odoo /home/odoo/odoo12/odoo12-addon-install
chmod 740 /home/odoo/odoo12/odoo12-addon-install
echo "new script <odoo12-addon-install> added!"
EOF
echo "push various target-related favicons..."
for favicon in "$MIAOU_BASEDIR"/templates/apps/odoo12/favicon/*.ico; do
lxc file push --uid 0 --gid 0 "$favicon" "$CONTAINER/home/odoo/odoo12/addons/web/static/src/img/"
done
echo "OK"
echo "adjust symbolic link according to target=<$TARGET>"
lxc exec "$CONTAINER" -- rm -f /home/odoo/odoo12/addons/web/static/src/img/favicon.ico
lxc exec "$CONTAINER" -- ln -s /home/odoo/odoo12/addons/web/static/src/img/favicon-"$TARGET".ico /home/odoo/odoo12/addons/web/static/src/img/favicon.ico
echo "OK"
}
function compute_bgcolor_target() {
case "$TARGET" in
dev) echo "#17a2b8" ;;
beta) echo "#79A70A" ;;
prod) echo "#7C7BAD" ;;
*) echoerr "unknown target <$TARGET>" && exit 10 ;;
esac
}
### MAIN
. "$MIAOU_BASEDIR/lib/init.sh"
arg1_required "$@"
readonly CONTAINER="$1"
TARGET=$(yq '.target' "$EXPANDED_CONF")
readonly TARGET
BACKGROUND_COLOR=$(compute_bgcolor_target)
readonly BACKGROUND_COLOR
check || (
install
check
)
+196
View File
@@ -0,0 +1,196 @@
#!/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/odoo15/$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/odoo15"
longport=$((port + 1000))
APP_PORT=$port LONG_PORT=$longport APP_NAME=$shortname tera -e -t "$MIAOU_DIR/templates/apps/odoo15/odoo.conf.j2" -o "$MIAOU_CONFIGDIR/apps/odoo15/$shortname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml" >/dev/null
APP_NAME=$shortname tera -t "$MIAOU_DIR/templates/apps/odoo15/odoo.service.j2" --env-only -o "$MIAOU_CONFIGDIR/apps/odoo15/$longname.service" >/dev/null
echo "creating templates ... OK"
echo "copying files over container <$container> ... "
lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/odoo15/$shortname.conf" "$container/etc/odoo15/$shortname.conf"
lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/odoo15/$longname.service" "$container/etc/systemd/system/$longname.service"
echo "copying files over container <$container> ... OK"
echo "create data folder for $shortname"
cat <<EOF | lxc_exec "$container"
set -Eeuo pipefail
echo allow addons writable for user odoo
mkdir -p /home/odoo/data-$shortname/addons/15.0
chmod u+w /home/odoo/data-$shortname/addons/15.0
chown -R odoo:odoo /home/odoo/data-$shortname
EOF
if ! (db-psql list | grep -q "$longname"); then
echo "create empty database <$longname> ... "
db-psql create "$longname"
echo "create empty database <$longname> ... OK"
credential_username=$(load_yaml_from_expanded credential.username)
credential_password=$(load_yaml_from_expanded credential.password)
cat <<EOF | lxc_exec "$container"
set -Eeuo pipefail
echo reloading systemd
systemctl daemon-reload
systemctl stop $longname
echo initialize database...
su odoo -c "python3.9 /home/odoo/odoo15/odoo-bin -c /etc/odoo15/$shortname.conf -i base --without-demo=all --stop-after-init"
echo initialize database OK
echo install default modules ...
su odoo -c "python3.9 /home/odoo/odoo15/odoo-bin -c /etc/odoo15/$shortname.conf -i account,contacts,l10n_fr,account,sale,point_of_sale -d $longname --worker=0 --stop-after-init"
echo default modules OK
echo activate french language
su odoo -c "python3.9 /home/odoo/odoo15/odoo-bin -c /etc/odoo15/$shortname.conf --load-language fr_FR -d $longname --worker=0 --stop-after-init"
echo change administrator password, default credential applied!
echo "UPDATE res_users SET login='$credential_username', password='$credential_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 enable $longname
systemctl start $longname
systemctl is-active --quiet $longname
else
echo service $longname already started!
fi
EOF
echo "initialize odoo $shortname $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
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="odoo15-$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
+129
View File
@@ -0,0 +1,129 @@
#!/bin/bash
readonly EXPANDED_CONF="$MIAOU_CONFIGDIR/miaou.expanded.yaml"
readonly ODOO15_DIR="/home/odoo/odoo15"
readonly WKHTML_VERSION="0.12.6.1-3"
readonly WKHTML_RELEASE="$WKHTML_VERSION.bookworm_amd64"
function check_user_odoo() {
(lxc exec "$CONTAINER" -- id odoo &>/dev/null) || return 12
return 0
}
function check_target_bgcolor() {
(lxc exec "$CONTAINER" -- grep -Pq "^\\\$o-community-color: $BACKGROUND_COLOR" "$ODOO15_DIR/addons/web/static/src/legacy/scss/primary_variables.scss") || return 13
return 0
}
function check_file_odoo-addon-install() {
(lxc exec "$CONTAINER" -- test -f /home/odoo/odoo15/odoo-addon-install) || return 23
return 0
}
function check() {
PREFIX="recipe:odoo15:check"
check_user_odoo || return 21
check_target_bgcolor || return 22
check_file_odoo-addon-install || return 23
echo "container <$CONTAINER> approved successfully!"
return 0
}
function install() {
PREFIX="recipe:odoo15:install"
: $PREFIX
launch_container "$CONTAINER"
echo "initializing Odoo15 ... "
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
cloud-init status --wait > /dev/null
echo "installing odoo15..."
apt update && apt dist-upgrade -y
echo "required packages"
apt install -y postgresql-client build-essential zlib1g-dev libssl-dev libxml2-dev libxslt1-dev libldap2-dev libsasl2-dev libpq-dev libffi-dev
if [[ ! -d /usr/local/share/python3.9 ]]; then
echo "install python-3.9.18"
cd /tmp
wget https://www.python.org/ftp/python/3.9.18/Python-3.9.18.tgz
tar -xf Python-3.9.18.tgz
mv Python-3.9.18 /usr/local/share/python3.9
cd /usr/local/share/python3.9
./configure --enable-optimizations --enable-shared
make -j \$(nproc)
make altinstall
ldconfig /usr/local/share/python3.9
else
echo "python-3.9.18 already installed!"
fi
if dpkg -l | grep -s wkhtmltox | grep -qs $WKHTML_VERSION; then
echo package=wkhtmltox version=$WKHTML_RELEASE already found!
else
echo "wkhtmltox version=$WKHTML_RELEASE has to be installed!"
wget https://github.com/wkhtmltopdf/packaging/releases/download/$WKHTML_VERSION/wkhtmltox_$WKHTML_RELEASE.deb
dpkg -i wkhtmltox_$WKHTML_RELEASE.deb || (apt -fy install && rm wkhtmltox_$WKHTML_RELEASE.deb)
fi
if ! grep -q odoo /etc/passwd; then
echo "add user <odoo>"
useradd -ms /bin/bash odoo
else
echo "user <odoo> already set!"
fi
echo "install odoo15 in odoo userspace"
cat <<EOT | su - odoo
set -Eeuo pipefail
if [[ ! -d odoo15 ]]; then
echo "git odoo15 from remote"
git clone https://github.com/odoo/odoo.git --depth 1 --branch 15.0 odoo15
export MAKEFLAGS="-j\$(nproc)"
pip3.9 install --upgrade pip
pip3.9 install wheel pypdf2 slugify
pip3.9 install -r odoo15/requirements.txt
else
echo "git odoo15 already downloaded!"
fi
echo "community-color change to $BACKGROUND_COLOR"
/opt/debian-bash/tools/append_or_replace "^.*o-community-color:.*" "\\\\\\\$o-community-color: $BACKGROUND_COLOR !default;" /home/odoo/odoo15/addons/web/static/src/legacy/scss/primary_variables.scss
EOT
mkdir -p /etc/odoo15
EOF
lxc file push "$MIAOU_BASEDIR/templates/apps/odoo15/odoo-addon-install" "$CONTAINER/home/odoo/odoo15/odoo-addon-install"
lxc exec "$CONTAINER" -- bash <<EOF
chown odoo:odoo /home/odoo/odoo15/odoo-addon-install
chmod 740 /home/odoo/odoo15/odoo-addon-install
echo "new script <odoo-addon-install> added!"
EOF
}
function compute_bgcolor_target() {
target=$(yq '.target' "$EXPANDED_CONF")
case "$target" in
dev) builtin echo "#17a2b8" ;;
beta) builtin echo "#79A70A" ;;
prod) builtin echo "#7C7BAD" ;;
*) echoerr "unknown target <$target>" && exit 10 ;;
esac
}
### MAIN
. "$MIAOU_BASEDIR/lib/init.sh"
arg1_required "$@"
readonly CONTAINER="$1"
BACKGROUND_COLOR=$(compute_bgcolor_target)
readonly BACKGROUND_COLOR
check || (
install
check
)
+68
View File
@@ -0,0 +1,68 @@
#!/bin/bash
function check() {
PREFIX="recipe:postgresql:check"
container_running "$CONTAINER" || return 10
echo "checking postgresql regarding access to the bridge subnet <$BRIDGE_SUBNET>..."
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
systemctl is-active postgresql.service &>/dev/null
ss -tlnp | grep postgres | grep -q 0.0.0.0:5432
PG_VERSION=\$(pg_lsclusters -h | cut -d' ' -f1)
grep -Eq "^host.*all.*all.*$BRIDGE_SUBNET.*md5" /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
test -f /etc/default/autopostgresqlbackup
EOF
status="$?"
[[ $status -eq 0 ]] && echo "container <$CONTAINER> approved!"
return $status
}
function install() {
PREFIX="recipe:postgresql:install"
: "$PREFIX"
echowarn "initializing postgresql regarding access to the bridge subnet <$BRIDGE_SUBNET>..."
launch_container "$CONTAINER"
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
apt update
. /opt/debian-bash/lib/functions.sh
/opt/debian-bash/tools/idem_apt_install postgresql
echo -n "start postgresql now..."
PG_VERSION=\$(pg_lsclusters -h | cut -d' ' -f1)
pg_ctlcluster \$PG_VERSION main start
echo "OK"
function systemctl-exists() ([ \$(systemctl list-unit-files "\${1}*" | wc -l) -gt 3 ])
systemctl-exists exim4.service && systemctl disable exim4.service
/opt/debian-bash/tools/append_or_replace "^listen_addresses = .*$" "listen_addresses = '0.0.0.0'" /etc/postgresql/\$PG_VERSION/main/postgresql.conf
/opt/debian-bash/tools/append_or_replace "^host.*all.*all.*$BRIDGE_SUBNET.*md5" "host\tall\t\tall\t\t$BRIDGE_SUBNET\t\tmd5" /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
systemctl restart postgresql.service
EOF
echo -n "copying <autopostgresqlbackup> files over container <$CONTAINER> ... "
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/autopostgresqlbackup/script" "$CONTAINER/usr/sbin/autopostgresqlbackup"
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/autopostgresqlbackup/cron.daily" "$CONTAINER/etc/cron.daily/autopostgresqlbackup"
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/autopostgresqlbackup/default.conf" "$CONTAINER/etc/default/autopostgresqlbackup"
PREFIX="" echo OK
}
# MAIN
. "$MIAOU_BASEDIR/lib/init.sh"
arg1_required "$@"
CONTAINER="$1"
BRIDGE_SUBNET=$(lxc network get lxdbr0 ipv4.address)
readonly CONTAINER BRIDGE_SUBNET
check || (
install
check
)
+202
View File
@@ -0,0 +1,202 @@
#!/bin/bash
function check_database_exists() {
db-maria list | grep -q "$longname"
}
function check_port_used() {
# shellcheck disable=SC2034
usedport=$(lxc exec "$container" -- bash -c "grep listen /etc/nginx/sites-enabled/$longname.conf | cut -d' ' -f6")
[[ "$usedport" == "$port" ]]
}
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_service_running
enable_trace
return 0
}
function _create() {
echo "creating wordpress instance for <$shortname> ... "
mkdir -p "$MIAOU_CONFIGDIR/apps/wordpress"
APP_PORT=$port APP_NAME=$shortname tera -e --env-key env -t "$MIAOU_BASEDIR/templates/apps/wordpress/wp-host.j2" -o "$MIAOU_CONFIGDIR/apps/wordpress/$longname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml"
echo "creating templates ... OK"
echo "copying files over container <$container> ... "
lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/wordpress/$longname.conf" "$container/etc/nginx/sites-available/$longname.conf"
echo "copying files over container <$container> ... OK"
if ! (db-maria list | grep -q "$longname"); then
echo "create empty database <$longname> ... "
db-maria create "$longname"
echo "create empty database <$longname> ... OK"
else
echo "database already exists!"
fi
echo "initialize wordpress $shortname $longname ..."
lxc exec "$container" -- bash <<EOF
set -Eeuo pipefail
if [[ ! -d /var/www/wordpress/$shortname ]]; then
echo "installing new instance of wordpress into /var/www/wordpress/$shortname"
mkdir -p /tmp/$shortname
tar -xzf /var/www/wordpress-latest.tgz -C /tmp/$shortname
mv /tmp/$shortname/wordpress /var/www/wordpress/$shortname
else
echo "instance of wordpress /var/www/wordpress/$shortname already defined!"
fi
if [[ ! -f /var/www/wordpress/$shortname/wp-config.php ]]; then
echo "create wp-config.php"
cat << 'EOT2' > /var/www/wordpress/$shortname/wp-config.php
<?php
define( 'DB_NAME', '$longname' );
define( 'DB_USER', '$longname' );
define( 'DB_PASSWORD', '$longname' );
define( 'DB_HOST', 'ct1.lxd' );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );
define( 'AUTH_KEY', '$(genpasswd 20)' );
define( 'SECURE_AUTH_KEY', '$(genpasswd 20)' );
define( 'LOGGED_IN_KEY', '$(genpasswd 20)' );
define( 'NONCE_KEY', '$(genpasswd 20)' );
define( 'AUTH_SALT', '$(genpasswd 20)' );
define( 'SECURE_AUTH_SALT', '$(genpasswd 20)' );
define( 'LOGGED_IN_SALT', '$(genpasswd 20)' );
define( 'NONCE_SALT', '$(genpasswd 20)' );
\$table_prefix = 'wp_';
define( 'WP_DEBUG', false );
/* Add any custom values between this line and the "stop editing" line. */
\$_SERVER['HTTPS'] = 'on';
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
EOT2
else
echo "wp-config.php already defined!"
fi
chown -R www-data:www-data /var/www/wordpress/$shortname
echo "enabling wordpress host into nginx"
mkdir -p /var/log/nginx/$shortname
ln -sf "/etc/nginx/sites-available/$longname.conf" "/etc/nginx/sites-enabled/$longname.conf"
echo "reloading nginx"
nginx -tq && systemctl reload nginx
EOF
echo "initialize wordpress $shortname $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
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="wp-$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
. "$MIAOU_BASEDIR/lib/init.sh"
[[
"$action" != unset &&
"$port" != unset &&
"$container" != unset &&
"$shortname" != unset ]] || usage
$action
+83
View File
@@ -0,0 +1,83 @@
#!/bin/bash
MANDATORY_PACKAGES_STRING="nginx php-cli php-fpm php-mysql php-curl php-xml php-imagick php-zip php-gd php-intl composer mariadb-client"
### CHECK
function check() {
PREFIX="recipe:wordpress:check"
check_mandatory_packages || return 21
check_wordpress_tgz || return 22
check_wp-tool || return 23
check_wp-backup || return 24
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_wp-tool() {
lxc exec "$CONTAINER" -- test -f /usr/local/sbin/wp-tool
}
function check_wp-backup() {
lxc exec "$CONTAINER" -- test -f /usr/local/sbin/wp-backup
}
function check_wordpress_tgz() {
lxc exec "$CONTAINER" -- test -f /var/www/wordpress-latest.tgz
}
### INSTALL
function install() {
PREFIX="recipe:wordpress:install"
: $PREFIX
launch_container "$CONTAINER"
echo "initializing Wordpress ... "
lxc exec "$CONTAINER" -- bash <<EOF
set -Eeuo pipefail
echo installing wordpress...
apt-get install -y $MANDATORY_PACKAGES_STRING
rm -f /etc/nginx/sites-enabled/default
rm -f /etc/nginx/sites-available/default
systemctl reload nginx
/TOOLBOX/wget https://wordpress.org/latest.tar.gz -O /var/www/wordpress-latest.tgz
mkdir -p /var/www/wordpress
chown www-data:www-data /var/www/wordpress
EOF
echo "OK"
echo -n "copying wp-tool to /usr/local/sbin/..."
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/wordpress/wp-tool" "$CONTAINER/usr/local/sbin/wp-tool"
lxc exec "$CONTAINER" -- chmod +x /usr/local/sbin/wp-tool
PREFIX="" echo "OK"
echo -n "copying wp-backup to /usr/local/sbin/..."
lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/apps/wordpress/wp-backup" "$CONTAINER/usr/local/sbin/wp-backup"
lxc exec "$CONTAINER" -- chmod +x /usr/local/sbin/wp-backup
PREFIX="" echo "OK"
}
### MAIN
. "$MIAOU_BASEDIR/lib/init.sh"
arg1_required "$@"
readonly CONTAINER="$1"
check || (
install
check
)