| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 15 | |
| 16 | # set -eux |
| 17 | |
| David Garcia | 0db6e9f | 2021-10-05 16:13:08 +0200 | [diff] [blame] | 18 | LXD_VERSION=4.0 |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 19 | JUJU_VERSION=2.9 |
| aticig | 1f2e2e9 | 2022-02-03 16:42:39 +0300 | [diff] [blame^] | 20 | JUJU_AGENT_VERSION=2.9.22 |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 21 | K8S_CLOUD_NAME="k8s-cloud" |
| beierlm | 3749e31 | 2020-07-02 14:21:09 -0400 | [diff] [blame] | 22 | KUBECTL="microk8s.kubectl" |
| aticig | 1f2e2e9 | 2022-02-03 16:42:39 +0300 | [diff] [blame^] | 23 | MICROK8S_VERSION=1.23 |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 24 | OSMCLIENT_VERSION=10.0 |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 25 | IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 26 | PATH=/snap/bin:${PATH} |
| 27 | |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 28 | MODEL_NAME=osm |
| 29 | |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 30 | # Latest bundles using old mongodb-k8s |
| 31 | # OSM_BUNDLE=cs:osm-68 |
| 32 | # OSM_HA_BUNDLE=cs:osm-ha-54 |
| 33 | # The charm store does not support referencing charms from CharmHub, |
| 34 | # therefore we will point to the local bundles until we migrate all |
| 35 | # charms to CharmHub. |
| 36 | OSM_BUNDLE=/usr/share/osm-devops/installers/charm/bundles/osm/bundle.yaml |
| 37 | OSM_HA_BUNDLE=/usr/share/osm-devops/installers/charm/bundles/osm-ha/bundle.yaml |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 38 | TAG=testing-daily |
| David Garcia | ab11f84 | 2020-12-16 17:25:15 +0100 | [diff] [blame] | 39 | |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 40 | function check_arguments(){ |
| 41 | while [ $# -gt 0 ] ; do |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 42 | case $1 in |
| 43 | --bundle) BUNDLE="$2" ;; |
| David Garcia | 4bd2bf0 | 2021-05-05 19:01:43 +0200 | [diff] [blame] | 44 | --overlay) OVERLAY="$2" ;; |
| Dominik Fleischmann | 7a97a4c | 2020-06-04 10:52:05 +0200 | [diff] [blame] | 45 | --k8s) KUBECFG="$2" ;; |
| 46 | --vca) CONTROLLER="$2" ;; |
| 47 | --lxd) LXD_CLOUD="$2" ;; |
| 48 | --lxd-cred) LXD_CREDENTIALS="$2" ;; |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 49 | --microstack) MICROSTACK=y ;; |
| David Garcia | ab11f84 | 2020-12-16 17:25:15 +0100 | [diff] [blame] | 50 | --ha) BUNDLE=$OSM_HA_BUNDLE ;; |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 51 | --tag) TAG="$2" ;; |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 52 | --registry) REGISTRY_INFO="$2" ;; |
| David Garcia | 1bc7102 | 2021-05-03 18:27:18 +0200 | [diff] [blame] | 53 | --only-vca) ONLY_VCA=y ;; |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 54 | esac |
| 55 | shift |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 56 | done |
| 57 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 58 | # echo $BUNDLE $KUBECONFIG $LXDENDPOINT |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 59 | } |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 60 | |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 61 | function install_snaps(){ |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 62 | if [ ! -v KUBECFG ]; then |
| beierlm | 481ae7d | 2020-12-14 09:59:19 -0500 | [diff] [blame] | 63 | sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable |
| David Garcia | 9a59e3d | 2021-02-08 15:18:27 +0100 | [diff] [blame] | 64 | sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || ( |
| David Garcia | 107010b | 2021-01-15 12:58:59 +0100 | [diff] [blame] | 65 | echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver |
| 66 | microk8s.stop |
| 67 | microk8s.start |
| 68 | ) |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 69 | sudo usermod -a -G microk8s `whoami` |
| 70 | mkdir -p ~/.kube |
| 71 | sudo chown -f -R `whoami` ~/.kube |
| 72 | KUBEGRP="microk8s" |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 73 | sg ${KUBEGRP} -c "microk8s status --wait-ready" |
| beierlm | df6de3d | 2020-12-16 08:46:40 -0500 | [diff] [blame] | 74 | KUBECONFIG=~/.osm/microk8s-config.yaml |
| David Garcia | 107010b | 2021-01-15 12:58:59 +0100 | [diff] [blame] | 75 | sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG} |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 76 | else |
| 77 | KUBECTL="kubectl" |
| 78 | sudo snap install kubectl --classic |
| 79 | export KUBECONFIG=${KUBECFG} |
| 80 | KUBEGRP=$(id -g -n) |
| 81 | fi |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 82 | sudo snap install juju --classic --channel=$JUJU_VERSION/stable |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | function bootstrap_k8s_lxd(){ |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 86 | [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 87 | [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca" |
| 88 | |
| 89 | if [ -v BOOTSTRAP_NEEDED ]; then |
| 90 | CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l) |
| 91 | if [ $CONTROLLER_PRESENT -ge 1 ]; then |
| 92 | cat << EOF |
| 93 | Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}". |
| 94 | You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it |
| 95 | using this command: |
| 96 | |
| 97 | juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME} |
| 98 | |
| 99 | Please retry the installation once this conflict has been resolved. |
| 100 | EOF |
| 101 | exit 1 |
| 102 | fi |
| beierlm | 9afb0ef | 2020-10-16 12:53:51 -0400 | [diff] [blame] | 103 | else |
| 104 | CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l) |
| 105 | if [ $CONTROLLER_PRESENT -le 0 ]; then |
| 106 | cat << EOF |
| 107 | Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA |
| 108 | that exists, or remove the --vca ${CONTROLLER_NAME} option. |
| 109 | |
| 110 | Please retry the installation with one of the solutions applied. |
| 111 | EOF |
| 112 | exit 1 |
| 113 | fi |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 114 | fi |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 115 | |
| 116 | if [ -v KUBECFG ]; then |
| 117 | cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS |
| beierlm | 2634cd3 | 2020-09-15 16:00:34 -0400 | [diff] [blame] | 118 | [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \ |
| 119 | --config controller-service-type=loadbalancer \ |
| David Garcia | a137601 | 2020-10-19 15:42:42 +0200 | [diff] [blame] | 120 | --agent-version=$JUJU_AGENT_VERSION |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 121 | else |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 122 | sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb" |
| beierlm | 9afb0ef | 2020-10-16 12:53:51 -0400 | [diff] [blame] | 123 | sg ${KUBEGRP} -c "microk8s.enable ingress" |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 124 | sg ${KUBEGRP} -c "microk8s.enable storage dns" |
| 125 | TIME_TO_WAIT=30 |
| 126 | start_time="$(date -u +%s)" |
| Dominik Fleischmann | c57296f | 2020-06-09 11:45:08 +0200 | [diff] [blame] | 127 | while true |
| 128 | do |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 129 | now="$(date -u +%s)" |
| 130 | if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then |
| 131 | echo "Microk8s storage failed to enable" |
| 132 | sg ${KUBEGRP} -c "microk8s.status" |
| 133 | exit 1 |
| 134 | fi |
| 135 | storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"` |
| 136 | if [[ $storage_status == "enabled" ]]; then |
| Dominik Fleischmann | c57296f | 2020-06-09 11:45:08 +0200 | [diff] [blame] | 137 | break |
| 138 | fi |
| 139 | sleep 1 |
| 140 | done |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 141 | |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 142 | [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS |
| beierlm | 2634cd3 | 2020-09-15 16:00:34 -0400 | [diff] [blame] | 143 | [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \ |
| David Garcia | a137601 | 2020-10-19 15:42:42 +0200 | [diff] [blame] | 144 | "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \ |
| beierlm | 2634cd3 | 2020-09-15 16:00:34 -0400 | [diff] [blame] | 145 | && K8S_CLOUD_NAME=microk8s |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 146 | fi |
| 147 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 148 | if [ -v LXD_CLOUD ]; then |
| 149 | if [ ! -v LXD_CREDENTIALS ]; then |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 150 | echo "The installer needs the LXD server certificate if the LXD is external" |
| 151 | exit 1 |
| 152 | fi |
| 153 | else |
| 154 | LXDENDPOINT=$DEFAULT_IP |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 155 | LXD_CLOUD=~/.osm/lxd-cloud.yaml |
| 156 | LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml |
| 157 | # Apply sysctl production values for optimal performance |
| 158 | sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf |
| 159 | sudo sysctl --system |
| 160 | # Install LXD snap |
| 161 | sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client |
| David Garcia | 956fcb7 | 2021-09-09 17:23:42 +0200 | [diff] [blame] | 162 | sudo snap install lxd --channel $LXD_VERSION/stable |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 163 | # Configure LXD |
| 164 | sudo usermod -a -G lxd `whoami` |
| 165 | cat /usr/share/osm-devops/installers/lxd-preseed.conf | sed 's/^config: {}/config:\n core.https_address: '$LXDENDPOINT':8443/' | sg lxd -c "lxd init --preseed" |
| 166 | sg lxd -c "lxd waitready" |
| 167 | DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}') |
| 168 | sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU" |
| David Garcia | d00e49c | 2020-06-19 10:33:37 +0200 | [diff] [blame] | 169 | sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU" |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 170 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 171 | cat << EOF > $LXD_CLOUD |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 172 | clouds: |
| 173 | lxd-cloud: |
| 174 | type: lxd |
| 175 | auth-types: [certificate] |
| 176 | endpoint: "https://$LXDENDPOINT:8443" |
| 177 | config: |
| 178 | ssl-hostname-verification: false |
| 179 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 180 | openssl req -nodes -new -x509 -keyout ~/.osm/client.key -out ~/.osm/client.crt -days 365 -subj "/C=FR/ST=Nice/L=Nice/O=ETSI/OU=OSM/CN=osm.etsi.org" |
| 181 | local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'` |
| 182 | local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'` |
| 183 | local client_key=`cat ~/.osm/client.key | sed 's/^/ /'` |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 184 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 185 | cat << EOF > $LXD_CREDENTIALS |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 186 | credentials: |
| 187 | lxd-cloud: |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 188 | lxd-cloud: |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 189 | auth-type: certificate |
| 190 | server-cert: | |
| 191 | $server_cert |
| 192 | client-cert: | |
| 193 | $client_cert |
| 194 | client-key: | |
| 195 | $client_key |
| 196 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 197 | lxc config trust add local: ~/.osm/client.crt |
| 198 | fi |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 199 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 200 | juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force |
| 201 | juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS |
| 202 | sg lxd -c "lxd waitready" |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 203 | juju controller-config features=[k8s-operators] |
| 204 | } |
| 205 | |
| 206 | function wait_for_port(){ |
| 207 | SERVICE=$1 |
| 208 | INDEX=$2 |
| 209 | TIME_TO_WAIT=30 |
| 210 | start_time="$(date -u +%s)" |
| 211 | while true |
| 212 | do |
| 213 | now="$(date -u +%s)" |
| 214 | if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then |
| 215 | echo "Failed to expose external ${SERVICE} interface port" |
| 216 | exit 1 |
| 217 | fi |
| 218 | |
| beierlm | 9afb0ef | 2020-10-16 12:53:51 -0400 | [diff] [blame] | 219 | if [ $(sg ${KUBEGRP} -c "${KUBECTL} get ingresses.networking -n osm -o json | jq -r '.items[$INDEX].metadata.name'") == ${SERVICE} ] ; then |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 220 | break |
| 221 | fi |
| 222 | sleep 1 |
| 223 | done |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | function deploy_charmed_osm(){ |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 227 | if [ -v REGISTRY_INFO ] ; then |
| 228 | registry_parts=(${REGISTRY_INFO//@/ }) |
| 229 | if [ ${#registry_parts[@]} -eq 1 ] ; then |
| 230 | # No credentials supplied |
| 231 | REGISTRY_USERNAME="" |
| 232 | REGISTRY_PASSWORD="" |
| 233 | REGISTRY_URL=${registry_parts[0]} |
| 234 | else |
| 235 | credentials=${registry_parts[0]} |
| 236 | credential_parts=(${credentials//:/ }) |
| 237 | REGISTRY_USERNAME=${credential_parts[0]} |
| 238 | REGISTRY_PASSWORD=${credential_parts[1]} |
| 239 | REGISTRY_URL=${registry_parts[1]} |
| 240 | fi |
| 241 | # Ensure the URL ends with a / |
| 242 | case $REGISTRY_URL in |
| 243 | */) ;; |
| 244 | *) REGISTRY_URL=${REGISTRY_URL}/ |
| 245 | esac |
| 246 | fi |
| 247 | |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 248 | echo "Creating OSM model" |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 249 | if [ -v KUBECFG ]; then |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 250 | juju add-model $MODEL_NAME $K8S_CLOUD_NAME |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 251 | else |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 252 | sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME" |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 253 | fi |
| 254 | echo "Deploying OSM with charms" |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 255 | images_overlay="" |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 256 | if [ -v REGISTRY_URL ]; then |
| 257 | [ ! -v TAG ] && TAG='latest' |
| 258 | fi |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 259 | [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE" |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 260 | |
| David Garcia | 4bd2bf0 | 2021-05-05 19:01:43 +0200 | [diff] [blame] | 261 | if [ -v OVERLAY ]; then |
| 262 | extra_overlay="--overlay $OVERLAY" |
| 263 | fi |
| 264 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 265 | if [ -v BUNDLE ]; then |
| David Garcia | 4bd2bf0 | 2021-05-05 19:01:43 +0200 | [diff] [blame] | 266 | juju deploy -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 267 | else |
| David Garcia | 4bd2bf0 | 2021-05-05 19:01:43 +0200 | [diff] [blame] | 268 | juju deploy -m $MODEL_NAME $OSM_BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 269 | fi |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 270 | |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 271 | if [ ! -v KUBECFG ]; then |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 272 | API_SERVER=${DEFAULT_IP} |
| 273 | else |
| 274 | API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ") |
| 275 | proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')" |
| 276 | url="$(echo ${API_SERVER/$proto/})" |
| 277 | user="$(echo $url | grep @ | cut -d@ -f1)" |
| 278 | hostport="$(echo ${url/$user@/} | cut -d/ -f1)" |
| 279 | API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')" |
| 280 | fi |
| David Garcia | de5fc1d | 2020-09-02 11:40:12 +0200 | [diff] [blame] | 281 | # Expose OSM services |
| David Garcia | c395a45 | 2021-05-05 14:22:26 +0200 | [diff] [blame] | 282 | juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io |
| 283 | juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io |
| 284 | juju config -m $MODEL_NAME grafana site_url=https://grafana.${API_SERVER}.nip.io |
| 285 | juju config -m $MODEL_NAME prometheus site_url=https://prometheus.${API_SERVER}.nip.io |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 286 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 287 | echo "Waiting for deployment to finish..." |
| 288 | check_osm_deployed |
| David Garcia | c537fa6 | 2021-11-09 08:45:49 +0100 | [diff] [blame] | 289 | grafana_leader=`juju status -m $MODEL_NAME grafana | grep "*" | cut -d "*" -f 1` |
| 290 | grafana_admin_password=`juju run -m $MODEL_NAME --unit $grafana_leader "echo \\$GF_SECURITY_ADMIN_PASSWORD"` |
| 291 | juju config -m $MODEL_NAME mon grafana_password=$grafana_admin_password |
| 292 | check_osm_deployed |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 293 | echo "OSM with charms deployed" |
| beierlm | 9afb0ef | 2020-10-16 12:53:51 -0400 | [diff] [blame] | 294 | } |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 295 | |
| 296 | function check_osm_deployed() { |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 297 | TIME_TO_WAIT=600 |
| 298 | start_time="$(date -u +%s)" |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 299 | total_service_count=14 |
| beierlm | 7e54dfc | 2020-09-24 15:27:53 -0400 | [diff] [blame] | 300 | previous_count=0 |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 301 | while true |
| 302 | do |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 303 | service_count=$(juju status -m $MODEL_NAME | grep kubernetes | grep active | wc -l) |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 304 | echo "$service_count / $total_service_count services active" |
| 305 | if [ $service_count -eq $total_service_count ]; then |
| 306 | break |
| 307 | fi |
| beierlm | 7e54dfc | 2020-09-24 15:27:53 -0400 | [diff] [blame] | 308 | if [ $service_count -ne $previous_count ]; then |
| 309 | previous_count=$service_count |
| 310 | start_time="$(date -u +%s)" |
| 311 | fi |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 312 | now="$(date -u +%s)" |
| 313 | if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then |
| 314 | echo "Timed out waiting for OSM services to become ready" |
| 315 | exit 1 |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 316 | fi |
| 317 | sleep 10 |
| 318 | done |
| 319 | } |
| 320 | |
| 321 | function create_overlay() { |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 322 | sudo snap install jq |
| David Garcia | c8660ad | 2020-12-16 13:13:20 +0100 | [diff] [blame] | 323 | sudo snap install yq |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 324 | local HOME=/home/$USER |
| calvinosanc1 | 83ea27a | 2021-01-04 11:42:18 +0100 | [diff] [blame] | 325 | local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - ) |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 326 | local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - ) |
| calvinosanc1 | 83ea27a | 2021-01-04 11:42:18 +0100 | [diff] [blame] | 327 | local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1) |
| 328 | local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2) |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 329 | local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\" |
| 330 | local vca_cloud="lxd-cloud" |
| 331 | # Get the VCA Certificate |
| calvinosanc1 | 83ea27a | 2021-01-04 11:42:18 +0100 | [diff] [blame] | 332 | local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n) |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 333 | |
| 334 | # Calculate the default route of this machine |
| Dominik Fleischmann | c57296f | 2020-06-09 11:45:08 +0200 | [diff] [blame] | 335 | local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'` |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 336 | |
| 337 | # Generate a new overlay.yaml, overriding any existing one |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 338 | cat << EOF > /tmp/vca-overlay.yaml |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 339 | applications: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 340 | lcm: |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 341 | options: |
| 342 | vca_user: $vca_user |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 343 | vca_secret: $vca_secret |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 344 | vca_host: $vca_host |
| 345 | vca_port: $vca_port |
| 346 | vca_pubkey: $vca_pubkey |
| 347 | vca_cacert: $vca_cacert |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 348 | vca_cloud: $vca_cloud |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 349 | vca_k8s_cloud: $K8S_CLOUD_NAME |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 350 | mon: |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 351 | options: |
| 352 | vca_user: $vca_user |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 353 | vca_secret: $vca_secret |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 354 | vca_host: $vca_host |
| 355 | vca_cacert: $vca_cacert |
| 356 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 357 | mv /tmp/vca-overlay.yaml ~/.osm/ |
| 358 | OSM_VCA_HOST=$vca_host |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 361 | function generate_images_overlay(){ |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 362 | echo "applications:" > /tmp/images-overlay.yaml |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 363 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 364 | charms_with_resources="nbi lcm mon pol ng-ui ro pla keystone" |
| 365 | for charm in $charms_with_resources; do |
| 366 | cat << EOF > /tmp/${charm}_registry.yaml |
| 367 | registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 368 | EOF |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 369 | if [ ! -z "$REGISTRY_USERNAME" ] ; then |
| 370 | echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml |
| 371 | echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml |
| 372 | fi |
| beierlm | f2782c5 | 2020-11-05 17:04:05 -0500 | [diff] [blame] | 373 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 374 | cat << EOF >> /tmp/images-overlay.yaml |
| 375 | ${charm}: |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 376 | resources: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 377 | image: /tmp/${charm}_registry.yaml |
| 378 | |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 379 | EOF |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 380 | done |
| 381 | |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 382 | mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE |
| 383 | } |
| 384 | |
| David Garcia | 107010b | 2021-01-15 12:58:59 +0100 | [diff] [blame] | 385 | function refresh_osmclient_snap() { |
| 386 | osmclient_snap_install_refresh refresh |
| 387 | } |
| 388 | |
| 389 | function install_osm_client_snap() { |
| 390 | osmclient_snap_install_refresh install |
| 391 | } |
| 392 | |
| 393 | function osmclient_snap_install_refresh() { |
| 394 | channel_preference="stable candidate beta edge" |
| 395 | for channel in $channel_preference; do |
| 396 | echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel" |
| 397 | sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break |
| 398 | done |
| 399 | } |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 400 | function install_osmclient() { |
| David Garcia | 107010b | 2021-01-15 12:58:59 +0100 | [diff] [blame] | 401 | snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| beierlm | df6de3d | 2020-12-16 08:46:40 -0500 | [diff] [blame] | 404 | function add_local_k8scluster() { |
| 405 | osm --all-projects vim-create \ |
| 406 | --name _system-osm-vim \ |
| 407 | --account_type dummy \ |
| 408 | --auth_url http://dummy \ |
| 409 | --user osm --password osm --tenant osm \ |
| 410 | --description "dummy" \ |
| 411 | --config '{management_network_name: mgmt}' |
| 412 | tmpfile=$(mktemp --tmpdir=${HOME}) |
| 413 | cp ${KUBECONFIG} ${tmpfile} |
| 414 | osm --all-projects k8scluster-add \ |
| 415 | --creds ${tmpfile} \ |
| 416 | --vim _system-osm-vim \ |
| 417 | --k8s-nets '{"net1": null}' \ |
| 418 | --version '1.19' \ |
| 419 | --description "OSM Internal Cluster" \ |
| 420 | _system-osm-k8s |
| 421 | rm -f ${tmpfile} |
| 422 | } |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 423 | |
| 424 | function install_microstack() { |
| beierlm | b403f06 | 2021-12-09 08:47:59 -0500 | [diff] [blame] | 425 | |
| 426 | sudo snap install microstack --beta --devmode |
| 427 | |
| 428 | CHECK=$(microstack.openstack server list) |
| 429 | if [ $? -ne 0 ] ; then |
| 430 | if [[ $CHECK == *"not initialized"* ]]; then |
| 431 | echo "Setting MicroStack dashboard to listen to port 8080" |
| 432 | sudo snap set microstack config.network.ports.dashboard=8080 |
| 433 | echo "Initializing MicroStack. This can take several minutes" |
| 434 | sudo microstack.init --auto --control |
| 435 | fi |
| 436 | fi |
| 437 | |
| 438 | sudo snap alias microstack.openstack openstack |
| 439 | |
| 440 | echo "Updating default security group in MicroStack to allow all access" |
| 441 | |
| 442 | for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do |
| 443 | for PROTO in icmp tcp udp ; do |
| 444 | echo " $PROTO ingress" |
| 445 | CHECK=$(microstack.openstack security group rule create $i --protocol $PROTO --remote-ip 0.0.0.0/0 2>&1) |
| 446 | if [ $? -ne 0 ] ; then |
| 447 | if [[ $CHECK != *"409"* ]]; then |
| 448 | echo "Error creating ingress rule for $PROTO" |
| 449 | echo $CHECK |
| 450 | fi |
| 451 | fi |
| 452 | done |
| 453 | done |
| 454 | |
| 455 | microstack.openstack network show osm-ext &>/dev/null |
| 456 | if [ $? -ne 0 ]; then |
| 457 | echo "Creating osm-ext network with router to bridge to MicroStack external network" |
| 458 | microstack.openstack network create --enable --no-share osm-ext |
| 459 | microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \ |
| 460 | --subnet-range 172.30.0.0/24 |
| 461 | microstack.openstack router create external-router |
| 462 | microstack.openstack router add subnet external-router osm-ext-subnet |
| 463 | microstack.openstack router set --external-gateway external external-router |
| 464 | fi |
| 465 | |
| 466 | microstack.openstack image list | grep ubuntu20.04 &> /dev/null |
| 467 | if [ $? -ne 0 ] ; then |
| 468 | echo "Fetching Ubuntu 20.04 image and upLoading to MicroStack" |
| 469 | wget -q -O- https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \ |
| 470 | | microstack.openstack image create --public --container-format=bare \ |
| 471 | --disk-format=qcow2 ubuntu20.04 | grep status |
| 472 | fi |
| 473 | |
| 474 | if [ ! -f ~/.ssh/microstack ]; then |
| 475 | ssh-keygen -t rsa -N "" -f ~/.ssh/microstack |
| 476 | microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack |
| 477 | fi |
| 478 | |
| 479 | echo "Creating VIM microstack-site in OSM" |
| 480 | . /var/snap/microstack/common/etc/microstack.rc |
| 481 | |
| 482 | osm vim-create \ |
| 483 | --name microstack-site \ |
| 484 | --user "$OS_USERNAME" \ |
| 485 | --password "$OS_PASSWORD" \ |
| 486 | --auth_url "$OS_AUTH_URL" \ |
| 487 | --tenant "$OS_USERNAME" \ |
| 488 | --account_type openstack \ |
| 489 | --config='{use_floating_ip: True, |
| 490 | insecure: True, |
| 491 | keypair: microstack, |
| 492 | management_network_name: osm-ext}' |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| beierlm | 4aad47b | 2021-12-07 16:15:53 -0500 | [diff] [blame] | 495 | DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5; exit}'` |
| 496 | DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]; exit}'` |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 497 | |
| 498 | check_arguments $@ |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 499 | mkdir -p ~/.osm |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 500 | install_snaps |
| 501 | bootstrap_k8s_lxd |
| David Garcia | 1bc7102 | 2021-05-03 18:27:18 +0200 | [diff] [blame] | 502 | create_overlay |
| 503 | if [ -v ONLY_VCA ]; then |
| 504 | HOME=/home/$USER |
| 505 | vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - ) |
| 506 | vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - ) |
| 507 | vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1) |
| 508 | vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2) |
| 509 | vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\" |
| 510 | vca_cloud="lxd-cloud" |
| 511 | vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n) |
| 512 | hostname=`cat /etc/hostname` |
| beierlm | df6de3d | 2020-12-16 08:46:40 -0500 | [diff] [blame] | 513 | |
| David Garcia | 1bc7102 | 2021-05-03 18:27:18 +0200 | [diff] [blame] | 514 | echo "Use the following command to register the installed VCA to your OSM:" |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 515 | echo -e " osm vca-add --endpoints $vca_host:$vca_port \\\n --user $vca_user \\\n --secret $vca_secret \\\n --cacert $vca_cacert \\\n --lxd-cloud lxd-cloud \\\n --lxd-credentials lxd-cloud \\\n --k8s-cloud microk8s \\\n --k8s-credentials microk8s\\\n $hostname-vca" |
| David Garcia | 1bc7102 | 2021-05-03 18:27:18 +0200 | [diff] [blame] | 516 | else |
| 517 | deploy_charmed_osm |
| 518 | install_osmclient |
| 519 | export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443 |
| 520 | sleep 10 |
| 521 | add_local_k8scluster |
| 522 | if [ -v MICROSTACK ]; then |
| 523 | install_microstack |
| 524 | fi |
| 525 | |
| 526 | echo "Your installation is now complete, follow these steps for configuring the osmclient:" |
| 527 | echo |
| 528 | echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP" |
| 529 | echo |
| 530 | echo "export OSM_HOSTNAME=$OSM_HOSTNAME" |
| 531 | echo |
| 532 | echo "2. Add the previous command to your .bashrc for other Shell sessions" |
| 533 | echo |
| 534 | echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc" |
| 535 | echo |
| 536 | echo "DONE" |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 537 | fi |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 538 | |