#!/bin/bash -ex

cd /root

apt-get update -y

ARCH="$(uname -m)"
FC_VERSION="{{ firecracker_version }}"

# Download firecracker
wget --no-clobber https://github.com/firecracker-microvm/firecracker/releases/download/${FC_VERSION}/firecracker-${FC_VERSION}-${ARCH}.tgz
tar -xvf firecracker-${FC_VERSION}-${ARCH}.tgz
cp release-${FC_VERSION}-${ARCH}/firecracker-${FC_VERSION}-${ARCH} /usr/local/bin/firecracker
cp release-${FC_VERSION}-${ARCH}/jailer-${FC_VERSION}-${ARCH} /usr/local/bin/jailer
rm -rf firecracker-${FC_VERSION}-${ARCH}.tgz release-${FC_VERSION}-${ARCH}/

# Install docker
apt-get remove docker docker-engine docker.io containerd runc -y
apt-get install ca-certificates curl gnupg -y
mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update -y
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

# Install hotbox
apt install software-properties-common -y
add-apt-repository ppa:deadsnakes/ppa -y
apt install python3.10 python3.10-distutils -y
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10
python3.10 -m pip install --upgrade pip
python3.10 -m pip install hotbox

# Run hotbox
hotbox server run --port 8420 &

# Install nginx
apt-get install nginx -y
rm /etc/nginx/sites-enabled/default
cat <<EOF > /etc/nginx/sites-enabled/hotbox
server {
    listen 8088;
    server_name _;
    location / {
        proxy_pass http://localhost:8420;
    }
}
EOF
systemctl restart nginx
