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.
 
 

187 lines
6.5 KiB

#!/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
)