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