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