#!/bin/bash
echo $0 started at $(date)

sudo snap install microk8s --classic --channel=1.26/stable
sudo snap install jq

sudo mkdir -p /var/snap/microk8s/current/args/certs.d/docker.io
cat << EOF | sudo tee /var/snap/microk8s/current/args/certs.d/docker.io/hosts.toml
server = "http://172.21.1.1:5000"

[host."http://172.21.1.1:5000"]
capabilities = ["pull", "resolve"]
skip_verify = true
plain-http = true
EOF

sudo systemctl restart snap.microk8s.daemon-containerd.service
sudo microk8s.enable hostpath-storage

sudo usermod -a -G microk8s ubuntu
sudo chown -f -R ubuntu ~/.kube
sudo snap alias microk8s.kubectl kubectl

wget https://osm-download.etsi.org/ftp/osm-13.0-thirteen/install_osm.sh
chmod +x ./install_osm.sh
./install_osm.sh --charmed --bundle ~/bundle.yaml -R testing-daily -t testing-daily -r testing -y 2>&1 | tee charmed_install_log.txt

# Set our environment to talk to the new OSM
grep '^export OSM' ~/charmed_install_log.txt >> ~/.bashrc
grep '^export OSM' ~/charmed_install_log.txt >> ~/.profile

while [ 1 ] ; do
    . .profile;osm version
    if [ $? -eq 0 ]; then break ; fi
done

. .profile; osm user-update admin --password admin
echo "export OSM_PASSWORD=admin" >> .bashrc
echo "export OSM_PASSWORD=admin" >> .profile

# Set some sane defaults for LXD
lxc profile set default limits.memory 1GB
lxc profile set default limits.cpu 2

echo $0 $@ complete at $(date)