#!/bin/bash
#   Copyright 2019 Canonical Ltd.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

#
# This script will create lxd images that will be used by the
# lxd provider in juju 2.1+ It is for use with the lxd provider for local
# development and preinstalls a common set of production packages.
#
# This is important, as between them, basenode and layer-basic install ~111
# packages, before we even get to any packages installed by your charm.
#
# It also installs some helpful development tools, and pre-downloads some
# commonly used packages.
#
# This dramatically speeds up the install hooks for lxd deploys. On my slow
# laptop, average install hook time went from ~7min down to ~1 minute.
function usage() {
    echo -e "usage: update-juju-lxc-images [Optional flags]"
    echo -e "This script will automatically cache all LTS series by default (trusty, xenial, bionic)"
    echo -e ""
    echo -e "Optional flags"
    echo -e "=================="
    echo -e "--trusty                                   It will download only the trusty series"
    echo -e "--xenial                                   It will download only the xenial series"
    echo -e "--bionic                                   It will download only the bionic series"
    echo -e ""
    echo -e "Help flags"
    echo -e "=================="
    echo -e "-h | --help                                Print full help."
    exit
}

FLAGS=0
trusty=0
xenial=0
bionic=0
while :; do
    case $1 in
        --trusty)
            FLAGS=1
            trusty=1
            ;;
        --xenial)
            FLAGS=1
            xenial=1
            ;;
        --bionic)
            FLAGS=1
            bionic=1
            ;;
        -h|--help)
            usage
            ;;
        *)
             break
    esac
    shift
done


set -eux

# The basic charm layer also installs all the things. 47 packages.
LAYER_BASIC="gcc build-essential python3-pip python3-setuptools python3-yaml"

# the basic layer also installs virtualenv, but the name changed in xenial.
TRUSTY_PACKAGES="python-virtualenv"
XENIAL_PACKAGES="virtualenv"
BIONIC_PACKAGES="virtualenv"

# Predownload common packages used by your charms in development
DOWNLOAD_PACKAGES=

CLOUD_INIT_PACKAGES="curl cpu-checker bridge-utils cloud-utils tmux ubuntu-fan"

PACKAGES="$LAYER_BASIC $DOWNLOAD_PACKAGES"

JUJU_FULL_VERSION=`juju version` # 2.4.4-bionic-amd64
JUJU_VERSION=`echo $JUJU_FULL_VERSION | awk -F"-" '{print $1}'`
OS_VERSION=`echo $JUJU_FULL_VERSION | awk -F"-" '{print $2}'`
ARCH=`echo $JUJU_FULL_VERSION | awk -F"-" '{print $3}'`

function cache() {
    series=$1
    container=juju-${series}-base
    alias=juju/$series/amd64

    lxc delete $container -f || true
    lxc launch ubuntu:$series $container
    sleep 15  # wait for network

    lxc exec $container -- apt-get update -y
    lxc exec $container -- apt-get upgrade -y
    lxc exec $container -- apt-get install -y $CLOUD_INIT_PACKAGES $PACKAGES $2

    # Install juju agent
    echo "Installing Juju agent $JUJU_FULL_VERSION"
    # TODO: verify if the version exists

    lxc exec $container -- mkdir -p /var/lib/juju/tools/$JUJU_FULL_VERSION/

    lxc exec $container -- curl -sS --connect-timeout 20 --noproxy \* --insecure -o /var/lib/juju/tools/$JUJU_FULL_VERSION/tools.tar.gz  https://streams.canonical.com/juju/tools/agent/$JUJU_VERSION/juju-$JUJU_VERSION-ubuntu-$ARCH.tgz

    lxc exec $container -- tar zxf /var/lib/juju/tools/$JUJU_FULL_VERSION/tools.tar.gz -C /var/lib/juju/tools/$JUJU_FULL_VERSION || true

    # Cache pip packages so installation into venv is faster?
    # pip3 download --cache-dir ~/.cache/pip charmhelpers

    lxc stop $container

    lxc image delete $alias || true
    lxc publish $container --alias $alias description="$series juju dev image ($(date +%Y%m%d))"

    lxc delete $container -f || true
}

# Enable caching of the serie(s) you're developing for.
if [ $FLAGS == 0 ]; then
    cache xenial "$XENIAL_PACKAGES"
else
    [ $trusty == 1 ] && cache trusty "$TRUSTY_PACKAGES"
    [ $xenial == 1 ] && cache xenial "$XENIAL_PACKAGES"
    [ $bionic == 1 ] && cache bionic "$BIONIC_PACKAGES"
fi
