mocha-carafe/carafe
2024-12-01 21:37:02 -05:00

185 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
# usage: ./mocha-carafe new BASE
if [ "$1" == "new" ]; then
if [ "$USER" != "root" ]; then
echo "must be run as root"
exit 2
fi
BASE=$2
BASEPATH=images/$BASE/$BASE
FLAVOR=noble
if [ ! -d "images" ]; then
mkdir "images"
fi
if [ ! -d "images/$BASE" ]; then
mkdir "images/$BASE"
fi
if [ ! -d "$BASEPATH" ]; then
debootstrap $FLAVOR "$BASEPATH"
fi
if [ ! -d "$BASEPATH" ]; then
echo "base path not found: $BASEPATH"
exit 2
fi
echo "deb http://archive.ubuntu.com/ubuntu noble main universe
deb http://archive.ubuntu.com/ubuntu noble-updates main universe
deb http://archive.ubuntu.com/ubuntu noble-backports main universe
deb http://archive.ubuntu.com/ubuntu noble-security main universe" > $BASEPATH/etc/apt/sources.list
if [ ! -d "$BASEPATH/usr/lib/mocha/carafe" ]; then
mkdir -p "$BASEPATH/usr/lib/mocha/carafe"
fi
if [ ! -d "$BASEPATH/usr/share/mocha/system" ]; then
mkdir -p "$BASEPATH/usr/share/mocha/system"
fi
cp -r libexec/mocha/carafe $BASEPATH/usr/lib/mocha
cp -r fs/* $BASEPATH/
if [ ! -d "$BASEPATH/usr/lib/mocha/oms" ]; then
mkdir -p $BASEPATH/usr/lib/mocha/oms
fi
./copy-oms $BASEPATH/usr/lib/mocha/oms
cp ./mocha-common/mocha-common/output/*.mcl $BASEPATH/usr/share/mocha/system
if [ ! -d "$BASEPATH/var/mocha/uploads" ]; then
mkdir -p "$BASEPATH/var/mocha/uploads"
fi
cp ./mocha-php/mocha-php/src/mocha-php/images/logo.svg $BASEPATH/var/mocha/uploads/c4f31b1aaede4e919fa0511537f098a5.svg
chroot $BASEPATH /usr/lib/mocha/carafe/preinstall.sh
if [ $? -ne 0 ]; then
echo "preinstall failed, try running 'chroot \$BASEPATH /usr/lib/mocha/carafe/preinstall.sh' again"
exit 2
fi
echo "Listen 443" > $BASEPATH/etc/apache2/ports.conf
cp site.conf $BASEPATH/etc/apache2/sites-available/000-default.conf
cp certs/localhost.crt certs/localhost.key $BASEPATH/etc/ssl/certs
cp -r mocha-php/mocha-php/src/mocha-php/* $BASEPATH/var/www/html
cp mocha-php/mocha-php/src/mocha-php/.htaccess $BASEPATH/var/www/html
rm -rf $BASEPATH/var/www/html/lib/phast
cp -r mocha-php/phast/lib/phast/server $BASEPATH/var/www/html/lib/phast
# mocha etc
if [ ! -d $BASEPATH/etc/mocha/include ]; then
mkdir -p $BASEPATH/etc/mocha/include
fi
cp mocha-php/mocha-php/src/mocha-php/include/Configuration.inc.php.template $BASEPATH/etc/mocha/include/Configuration.inc.php.template
# mocha libexec
if [ ! -d $BASEPATH/usr/lib/mocha ]; then
mkdir -p $BASEPATH/usr/lib/mocha
fi
cp libexec/mocha/mocha-* $BASEPATH/usr/lib/mocha
cp libexec/mocha/mocha $BASEPATH/usr/bin
chmod a+x $BASEPATH/usr/lib/mocha/*
chmod a+x $BASEPATH/usr/bin/mocha
if [ -d $BASEPATH/var/www/html/index.html ]; then
rm $BASEPATH/var/www/html/index.html
fi
# ! FIXME: we don't want to run this in chroot, we need to run it in lxc!
chroot $BASEPATH /usr/lib/mocha/carafe/postinstall.sh
echo "architecture: \"x86_64\"
creation_date: $(date +%s) # To get current date in Unix time, use \`date +%s\` command
properties:
architecture: \"x86_64\"
description: \"Ubuntu Noble with Apache2 and PHP (20171227)\"
os: \"ubuntu\"
release: \"noble\"" > images/$BASE/metadata.yaml
tar -cvzf images/$BASE/metadata.tar.gz -C images/$BASE metadata.yaml
rm images/$BASE/metadata.yaml
if [ ! -f images/$BASE/$BASE.tar.gz ]; then
tar -cvzf images/$BASE/$BASE.tar.gz -C $BASEPATH .
# rm -rf $BASEPATH
else
echo "$BASE.tar.gz already exists; not overwriting"
fi
EXISTS=$(lxc image list | grep $BASE )
if [ "$EXISTS" == "" ]; then
lxc image import images/$BASE/metadata.tar.gz images/$BASE/$BASE.tar.gz --alias $BASE
else
echo "not importing image; already exists as $BASE"
fi
SUV_ID=$(hexdump -vn8 -e'2/4 "%08x" 1 "\n"' /dev/urandom)
CONTAINER_NAME=i-0$SUV_ID
lxc init $BASE $CONTAINER_NAME
echo "Instance name is: $CONTAINER_NAME"
lxc start $CONTAINER_NAME
lxc shell $CONTAINER_NAME -- bash -c "echo \"$CONTAINER_NAME\" > /etc/mocha/container"
sleep 5
CONTAINER_IP=$(lxc exec $CONTAINER_NAME ip addr | grep 'scope global' | sed -e 's/ inet6 //' -e 's/\/64 scope global dynamic mngtmpaddr//')
lxc shell $CONTAINER_NAME mocha up
SUV_DOMAINNAME=".privatesuv.com"
echo "enter sudo password to add entry to /etc/hosts if desired"
echo "$CONTAINER_IP $CONTAINER_NAME$SUV_DOMAINNAME" | sudo tee -a /etc/hosts
elif [ "$1" == "list" ]; then
lxc list
elif [ "$1" == "up" ]; then
lxc start "$2"
elif [ "$1" == "shell" ]; then
lxc shell "$2"
elif [ "$1" == "reset" ]; then
BASE="$2"
echo "deleting compiled files..."
rm images/$BASE/*.gz
echo "removing the image..."
lxc image delete $BASE
elif [ "$1" == "destroy" ]; then
./carafe reset $2
rm -rf images/$2
# elif [ "$1" == "build" ]; then
#
#
else
echo "usage: mocha carafe new BASE"
fi