| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [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 | # |
| 15 | |
| 16 | # set -eux |
| 17 | |
| 18 | K8S_CLOUD_NAME="k8s-cloud" |
| David Garcia | 24e99f9 | 2020-05-28 18:03:25 +0200 | [diff] [blame] | 19 | KUBECTL="microk8s.kubectl" |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 20 | IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml |
| 21 | function check_arguments(){ |
| 22 | while [ $# -gt 0 ] ; do |
| 23 | case $1 in |
| 24 | --bundle) BUNDLE="$2" ;; |
| 25 | --kubeconfig) KUBECFG="$2" ;; |
| 26 | --controller) CONTROLLER="$2" ;; |
| 27 | --lxd-cloud) LXD_CLOUD="$2" ;; |
| 28 | --lxd-credentials) LXD_CREDENTIALS="$2" ;; |
| 29 | --microstack) MICROSTACK=y ;; |
| 30 | --tag) TAG="$2" ;; |
| 31 | esac |
| 32 | shift |
| 33 | done |
| 34 | |
| 35 | # echo $BUNDLE $KUBECONFIG $LXDENDPOINT |
| 36 | } |
| 37 | function install_snaps(){ |
| 38 | sudo snap install juju --classic |
| 39 | [ ! -v KUBECFG ] && sudo snap install microk8s --classic && sudo usermod -a -G microk8s ubuntu && mkdir -p ~/.kube && sudo chown -f -R `whoami` ~/.kube |
| David Garcia | 24e99f9 | 2020-05-28 18:03:25 +0200 | [diff] [blame] | 40 | [ -v KUBECFG ] && sudo snap install kubectl --classic && KUBECTL="kubectl --kubeconfig $KUBECFG" |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | function bootstrap_k8s_lxd(){ |
| 44 | [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER |
| 45 | [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="controller" |
| 46 | |
| 47 | if [ -v KUBECFG ]; then |
| 48 | cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS |
| David Garcia | 3f9fa7f | 2020-05-25 13:55:14 +0200 | [diff] [blame] | 49 | [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME --config controller-service-type=loadbalancer |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 50 | else |
| David Garcia | 4298d14 | 2020-05-20 18:48:35 +0200 | [diff] [blame] | 51 | sg microk8s -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb" |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 52 | sg microk8s -c "microk8s.enable storage dns" |
| 53 | |
| 54 | [ ! -v BOOTSTRAP_NEEDED ] && sg microk8s -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS |
| David Garcia | 4298d14 | 2020-05-20 18:48:35 +0200 | [diff] [blame] | 55 | [ -v BOOTSTRAP_NEEDED ] && sg microk8s -c "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer" && K8S_CLOUD_NAME=microk8s |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 56 | fi |
| 57 | |
| 58 | if [ -v LXD_CLOUD ]; then |
| 59 | if [ ! -v LXD_CREDENTIALS ]; then |
| 60 | echo "The installer needs the LXD server certificate if the LXD is external" |
| 61 | exit 1 |
| 62 | fi |
| 63 | else |
| 64 | LXDENDPOINT=$DEFAULT_IP |
| 65 | LXD_CLOUD=~/.osm/lxd-cloud.yaml |
| 66 | LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml |
| 67 | # Apply sysctl production values for optimal performance |
| 68 | sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf |
| 69 | sudo sysctl --system |
| 70 | # Install LXD snap |
| 71 | sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client |
| 72 | sudo snap install lxd |
| 73 | sudo apt-get install zfsutils-linux -y |
| 74 | # Configure LXD |
| 75 | sudo usermod -a -G lxd `whoami` |
| 76 | 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" |
| 77 | sg lxd -c "lxd waitready" |
| 78 | DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}') |
| 79 | sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU" |
| 80 | |
| 81 | cat << EOF > $LXD_CLOUD |
| 82 | clouds: |
| 83 | lxd-cloud: |
| 84 | type: lxd |
| 85 | auth-types: [certificate] |
| 86 | endpoint: "https://$LXDENDPOINT:8443" |
| 87 | config: |
| 88 | ssl-hostname-verification: false |
| 89 | EOF |
| 90 | 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" |
| 91 | local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'` |
| 92 | local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'` |
| 93 | local client_key=`cat ~/.osm/client.key | sed 's/^/ /'` |
| 94 | |
| 95 | cat << EOF > $LXD_CREDENTIALS |
| 96 | credentials: |
| 97 | lxd-cloud: |
| 98 | lxd-cloud: |
| 99 | auth-type: certificate |
| 100 | server-cert: | |
| 101 | $server_cert |
| 102 | client-cert: | |
| 103 | $client_cert |
| 104 | client-key: | |
| 105 | $client_key |
| 106 | EOF |
| 107 | lxc config trust add local: ~/.osm/client.crt |
| 108 | fi |
| 109 | |
| 110 | juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force |
| 111 | juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS |
| 112 | sg lxd -c "lxd waitready" |
| 113 | juju add-model test lxd-cloud || true |
| 114 | } |
| 115 | |
| 116 | function deploy_charmed_osm(){ |
| 117 | create_overlay |
| 118 | echo "Creating OSM model" |
| 119 | if [ -v KUBECFG ]; then |
| 120 | juju add-model osm $K8S_CLOUD_NAME |
| 121 | else |
| 122 | sg microk8s -c "juju add-model osm $K8S_CLOUD_NAME" |
| 123 | fi |
| 124 | echo "Deploying OSM with charms" |
| 125 | # echo $BUNDLE |
| 126 | if [ -v BUNDLE ]; then |
| 127 | juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml |
| 128 | else |
| 129 | images_overlay="" |
| 130 | [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE" |
| 131 | juju deploy osm --overlay ~/.osm/vca-overlay.yaml $images_overlay |
| 132 | fi |
| 133 | echo "Waiting for deployment to finish..." |
| 134 | check_osm_deployed &> /dev/null |
| 135 | echo "OSM with charms deployed" |
| 136 | sg microk8s -c "microk8s.enable ingress" |
| 137 | juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io |
| 138 | juju expose ui-k8s |
| 139 | } |
| 140 | |
| 141 | function check_osm_deployed() { |
| 142 | while true |
| 143 | do |
| David Garcia | 24e99f9 | 2020-05-28 18:03:25 +0200 | [diff] [blame] | 144 | pod_name=`sg microk8s -c "$KUBECTL -n osm get pods | grep ui-k8s | grep -v operator" | awk '{print $1}'` |
| 145 | if [[ `sg microk8s -c "$KUBECTL -n osm wait pod $pod_name --for condition=Ready"` ]]; then |
| 146 | if [[ `sg microk8s -c "$KUBECTL -n osm wait pod lcm-k8s-0 --for condition=Ready"` ]]; then |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 147 | break |
| 148 | fi |
| 149 | fi |
| 150 | sleep 10 |
| 151 | done |
| 152 | } |
| 153 | |
| 154 | function create_overlay() { |
| 155 | sudo snap install jq |
| 156 | sudo apt install python3-pip -y |
| 157 | python3 -m pip install yq |
| 158 | PATH=$PATH:$HOME/.local/bin # make yq command available |
| 159 | local HOME=/home/$USER |
| 160 | local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].user') |
| 161 | local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].password') |
| David Garcia | bb97966 | 2020-05-20 15:06:41 +0200 | [diff] [blame] | 162 | local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' --raw-output | cut -d ":" -f 1) |
| 163 | local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' --raw-output | cut -d ":" -f 2) |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 164 | local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\" |
| 165 | local vca_cloud="lxd-cloud" |
| 166 | # Get the VCA Certificate |
| David Garcia | bb97966 | 2020-05-20 15:06:41 +0200 | [diff] [blame] | 167 | local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["ca-cert"]' --raw-output | base64 | tr -d \\n) |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 168 | |
| 169 | # Calculate the default route of this machine |
| 170 | local DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'` |
| 171 | local vca_apiproxy=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'` |
| 172 | |
| 173 | # Generate a new overlay.yaml, overriding any existing one |
| 174 | cat << EOF > /tmp/vca-overlay.yaml |
| 175 | applications: |
| 176 | lcm-k8s: |
| 177 | options: |
| 178 | vca_user: $vca_user |
| 179 | vca_password: $vca_password |
| 180 | vca_host: $vca_host |
| 181 | vca_port: $vca_port |
| 182 | vca_pubkey: $vca_pubkey |
| 183 | vca_cacert: $vca_cacert |
| 184 | vca_apiproxy: $vca_apiproxy |
| 185 | vca_cloud: $vca_cloud |
| 186 | mon-k8s: |
| 187 | options: |
| 188 | vca_user: $vca_user |
| 189 | vca_password: $vca_password |
| 190 | vca_host: $vca_host |
| 191 | vca_cacert: $vca_cacert |
| 192 | EOF |
| 193 | mv /tmp/vca-overlay.yaml ~/.osm/ |
| 194 | OSM_VCA_HOST=$vca_host |
| 195 | } |
| 196 | |
| 197 | function generate_images_overlay(){ |
| 198 | cat << EOF > /tmp/images-overlay.yaml |
| 199 | applications: |
| 200 | lcm-k8s: |
| 201 | options: |
| 202 | image: opensourcemano/lcm:$TAG |
| 203 | mon-k8s: |
| 204 | options: |
| 205 | image: opensourcemano/mon:$TAG |
| 206 | ro-k8s: |
| 207 | options: |
| 208 | image: opensourcemano/ro:$TAG |
| 209 | nbi-k8s: |
| 210 | options: |
| 211 | image: opensourcemano/nbi:$TAG |
| 212 | pol-k8s: |
| 213 | options: |
| 214 | image: opensourcemano/pol:$TAG |
| 215 | ui-k8s: |
| 216 | options: |
| 217 | image: opensourcemano/light-ui:$TAG |
| 218 | |
| 219 | EOF |
| 220 | mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE |
| 221 | } |
| 222 | |
| 223 | function install_osmclient() { |
| 224 | sudo snap install osmclient |
| 225 | sudo snap alias osmclient.osm osm |
| 226 | } |
| 227 | |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 228 | |
| 229 | function install_microstack() { |
| 230 | sudo snap install microstack --classic --beta |
| 231 | sudo microstack.init --auto |
| 232 | wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/ |
| 233 | microstack.openstack image create \ |
| 234 | --public \ |
| 235 | --disk-format qcow2 \ |
| 236 | --container-format bare \ |
| 237 | --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \ |
| 238 | ubuntu1604 |
| 239 | ssh-keygen -t rsa -N "" -f ~/.ssh/microstack |
| 240 | microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack |
| David Garcia | ac01400 | 2020-05-26 13:50:54 +0200 | [diff] [blame] | 241 | export OSM_HOSTNAME=`juju status --format yaml | yq -r '.applications["nbi-k8s"].address'` |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 242 | osm vim-create --name microstack-site \ |
| 243 | --user admin \ |
| 244 | --password keystone \ |
| 245 | --auth_url http://10.20.20.1:5000/v3 \ |
| 246 | --tenant admin \ |
| 247 | --account_type openstack \ |
| 248 | --config='{security_groups: default, |
| 249 | keypair: microstack, |
| 250 | project_name: admin, |
| 251 | user_domain_name: default, |
| 252 | region_name: microstack, |
| 253 | insecure: True, |
| 254 | availability_zone: nova, |
| 255 | version: 3}' |
| 256 | } |
| 257 | |
| 258 | DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'` |
| 259 | DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'` |
| 260 | |
| 261 | check_arguments $@ |
| 262 | mkdir -p ~/.osm |
| 263 | install_snaps |
| David Garcia | 4298d14 | 2020-05-20 18:48:35 +0200 | [diff] [blame] | 264 | sleep 5 |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 265 | bootstrap_k8s_lxd |
| 266 | deploy_charmed_osm |
| beierlm | 107d16a | 2020-05-15 21:47:43 -0400 | [diff] [blame] | 267 | install_osmclient |
| 268 | if [ -v MICROSTACK ]; then |
| 269 | install_microstack |
| 270 | fi |