| Adam Israel | c7f2074 | 2019-09-16 10:19:48 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2019 Canonical Ltd. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # |
| 17 | # This script will create lxd images that will be used by the |
| 18 | # lxd provider in juju 2.1+ It is for use with the lxd provider for local |
| 19 | # development and preinstalls a common set of production packages. |
| 20 | # |
| 21 | # This is important, as between them, basenode and layer-basic install ~111 |
| 22 | # packages, before we even get to any packages installed by your charm. |
| 23 | # |
| 24 | # It also installs some helpful development tools, and pre-downloads some |
| 25 | # commonly used packages. |
| 26 | # |
| 27 | # This dramatically speeds up the install hooks for lxd deploys. On my slow |
| 28 | # laptop, average install hook time went from ~7min down to ~1 minute. |
| 29 | function usage() { |
| 30 | echo -e "usage: update-juju-lxc-images [Optional flags]" |
| 31 | echo -e "This script will automatically cache all LTS series by default (trusty, xenial, bionic)" |
| 32 | echo -e "" |
| 33 | echo -e "Optional flags" |
| 34 | echo -e "==================" |
| 35 | echo -e "--trusty It will download only the trusty series" |
| 36 | echo -e "--xenial It will download only the xenial series" |
| 37 | echo -e "--bionic It will download only the bionic series" |
| 38 | echo -e "" |
| 39 | echo -e "Help flags" |
| 40 | echo -e "==================" |
| 41 | echo -e "-h | --help Print full help." |
| 42 | exit |
| 43 | } |
| 44 | |
| 45 | FLAGS=0 |
| 46 | trusty=0 |
| 47 | xenial=0 |
| 48 | bionic=0 |
| 49 | while :; do |
| 50 | case $1 in |
| 51 | --trusty) |
| 52 | FLAGS=1 |
| 53 | trusty=1 |
| 54 | ;; |
| 55 | --xenial) |
| 56 | FLAGS=1 |
| 57 | xenial=1 |
| 58 | ;; |
| 59 | --bionic) |
| 60 | FLAGS=1 |
| 61 | bionic=1 |
| 62 | ;; |
| 63 | -h|--help) |
| 64 | usage |
| 65 | ;; |
| 66 | *) |
| 67 | break |
| 68 | esac |
| 69 | shift |
| 70 | done |
| 71 | |
| 72 | |
| 73 | set -eux |
| 74 | |
| 75 | # The basic charm layer also installs all the things. 47 packages. |
| 76 | LAYER_BASIC="gcc build-essential python3-pip python3-setuptools python3-yaml" |
| 77 | |
| 78 | # the basic layer also installs virtualenv, but the name changed in xenial. |
| 79 | TRUSTY_PACKAGES="python-virtualenv" |
| 80 | XENIAL_PACKAGES="virtualenv" |
| 81 | BIONIC_PACKAGES="virtualenv" |
| 82 | |
| 83 | # Predownload common packages used by your charms in development |
| 84 | DOWNLOAD_PACKAGES= |
| 85 | |
| 86 | CLOUD_INIT_PACKAGES="curl cpu-checker bridge-utils cloud-utils tmux ubuntu-fan" |
| 87 | |
| 88 | PACKAGES="$LAYER_BASIC $DOWNLOAD_PACKAGES" |
| 89 | |
| 90 | JUJU_FULL_VERSION=`juju version` # 2.4.4-bionic-amd64 |
| 91 | JUJU_VERSION=`echo $JUJU_FULL_VERSION | awk -F"-" '{print $1}'` |
| 92 | OS_VERSION=`echo $JUJU_FULL_VERSION | awk -F"-" '{print $2}'` |
| 93 | ARCH=`echo $JUJU_FULL_VERSION | awk -F"-" '{print $3}'` |
| 94 | |
| 95 | function cache() { |
| 96 | series=$1 |
| 97 | container=juju-${series}-base |
| 98 | alias=juju/$series/amd64 |
| 99 | |
| 100 | lxc delete $container -f || true |
| calvinosanc1 | 0c82da0 | 2020-08-17 11:45:15 +0200 | [diff] [blame] | 101 | lxc image copy ubuntu:$series local: --alias clean-$series |
| Adam Israel | c7f2074 | 2019-09-16 10:19:48 -0400 | [diff] [blame] | 102 | lxc launch ubuntu:$series $container |
| 103 | sleep 15 # wait for network |
| 104 | |
| 105 | lxc exec $container -- apt-get update -y |
| 106 | lxc exec $container -- apt-get upgrade -y |
| 107 | lxc exec $container -- apt-get install -y $CLOUD_INIT_PACKAGES $PACKAGES $2 |
| 108 | |
| 109 | # Install juju agent |
| 110 | echo "Installing Juju agent $JUJU_FULL_VERSION" |
| 111 | # TODO: verify if the version exists |
| 112 | |
| 113 | lxc exec $container -- mkdir -p /var/lib/juju/tools/$JUJU_FULL_VERSION/ |
| 114 | |
| 115 | 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 |
| 116 | |
| 117 | lxc exec $container -- tar zxf /var/lib/juju/tools/$JUJU_FULL_VERSION/tools.tar.gz -C /var/lib/juju/tools/$JUJU_FULL_VERSION || true |
| 118 | |
| 119 | # Cache pip packages so installation into venv is faster? |
| 120 | # pip3 download --cache-dir ~/.cache/pip charmhelpers |
| 121 | |
| 122 | lxc stop $container |
| 123 | |
| 124 | lxc image delete $alias || true |
| calvinosanc1 | 0c82da0 | 2020-08-17 11:45:15 +0200 | [diff] [blame] | 125 | lxc image delete clean-$series || true |
| Adam Israel | c7f2074 | 2019-09-16 10:19:48 -0400 | [diff] [blame] | 126 | lxc publish $container --alias $alias description="$series juju dev image ($(date +%Y%m%d))" |
| 127 | |
| 128 | lxc delete $container -f || true |
| 129 | } |
| 130 | |
| 131 | # Enable caching of the serie(s) you're developing for. |
| 132 | if [ $FLAGS == 0 ]; then |
| 133 | cache xenial "$XENIAL_PACKAGES" |
| 134 | else |
| 135 | [ $trusty == 1 ] && cache trusty "$TRUSTY_PACKAGES" |
| 136 | [ $xenial == 1 ] && cache xenial "$XENIAL_PACKAGES" |
| 137 | [ $bionic == 1 ] && cache bionic "$BIONIC_PACKAGES" |
| 138 | fi |