Pin Juju Version
[osm/devops.git] / installers / charmed_install.sh
index 6d89a34..d4f0b8f 100755 (executable)
@@ -35,32 +35,72 @@ function check_arguments(){
 
     # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
 }
+
 function install_snaps(){
-    [ ! -v KUBECFG ] && sudo snap install microk8s --classic && sudo usermod -a -G microk8s `whoami` && mkdir -p ~/.kube && sudo chown -f -R `whoami` ~/.kube
+    if [ ! -v KUBECFG ]; then
+        sudo snap install microk8s --classic
+        sudo usermod -a -G microk8s `whoami`
+        mkdir -p ~/.kube
+        sudo chown -f -R `whoami` ~/.kube
+        KUBEGRP="microk8s"
+    else
+        KUBECTL="kubectl"
+        sudo snap install kubectl --classic
+        export KUBECONFIG=${KUBECFG}
+        KUBEGRP=$(id -g -n)
+    fi
     sudo snap install juju --classic --channel=2.8/stable
 }
 
 function bootstrap_k8s_lxd(){
     [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
-    [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="controller"
+    [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
+
+    if [ -v BOOTSTRAP_NEEDED ]; then
+        CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
+        if [ $CONTROLLER_PRESENT -ge 1 ]; then
+            cat << EOF
+Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
+You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
+using this command:
+
+   juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
+
+Please retry the installation once this conflict has been resolved.
+EOF
+            exit 1
+        fi
+    fi
 
     if [ -v KUBECFG ]; then
         cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
-        [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME --config controller-service-type=loadbalancer
+        [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
+            --config controller-service-type=loadbalancer \
+            --agent-version=2.8.1
     else
-        sg microk8s -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
-        sg microk8s -c "microk8s.enable storage dns"
+        sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
+        sg ${KUBEGRP} -c "microk8s.enable storage dns"
+        TIME_TO_WAIT=30
+        start_time="$(date -u +%s)"
         while true
         do
-            sg microk8s -c "microk8s.status" | grep 'storage: enabled'
-            if [ $? -eq 0 ]; then
+            now="$(date -u +%s)"
+            if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
+                echo "Microk8s storage failed to enable"
+                sg ${KUBEGRP} -c "microk8s.status"
+                exit 1
+            fi
+            storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
+            if [[ $storage_status == "enabled" ]]; then
                 break
             fi
             sleep 1
         done
 
-        [ ! -v BOOTSTRAP_NEEDED ] && sg microk8s -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
-        [ -v BOOTSTRAP_NEEDED ] && sg microk8s -c "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer" && K8S_CLOUD_NAME=microk8s
+        [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
+        [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
+            "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=2.8.1" \
+            && K8S_CLOUD_NAME=microk8s
     fi
 
     if [ -v LXD_CLOUD ]; then
@@ -119,17 +159,37 @@ EOF
     juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
     juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
     sg lxd -c "lxd waitready"
-    juju add-model test lxd-cloud || true
+    #juju add-model test lxd-cloud || true
     juju controller-config features=[k8s-operators]
 }
 
+function wait_for_port(){
+    SERVICE=$1
+    INDEX=$2
+    TIME_TO_WAIT=30
+    start_time="$(date -u +%s)"
+    while true
+    do
+        now="$(date -u +%s)"
+        if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
+            echo "Failed to expose external ${SERVICE} interface port"
+            exit 1
+        fi
+
+        if [ $(sg ${KUBEGRP} -c "${KUBECTL} get ingress -n osm -o json | jq -r '.items[$INDEX].metadata.name'") == ${SERVICE} ] ; then
+            break
+        fi
+        sleep 1
+    done
+}
+
 function deploy_charmed_osm(){
     create_overlay
     echo "Creating OSM model"
     if [ -v KUBECFG ]; then
         juju add-model osm $K8S_CLOUD_NAME
     else
-        sg microk8s -c "juju add-model osm $K8S_CLOUD_NAME"
+        sg ${KUBEGRP} -c "juju add-model osm $K8S_CLOUD_NAME"
     fi
     echo "Deploying OSM with charms"
     images_overlay=""
@@ -139,23 +199,57 @@ function deploy_charmed_osm(){
     else
         juju deploy osm --overlay ~/.osm/vca-overlay.yaml $images_overlay
     fi
+
     echo "Waiting for deployment to finish..."
-    check_osm_deployed &> /dev/null
+    check_osm_deployed
     echo "OSM with charms deployed"
-    sg microk8s -c "microk8s.enable ingress"
-    juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io
+    if [ ! -v KUBECFG ]; then
+        sg ${KUBEGRP} -c "microk8s.enable ingress"
+        API_SERVER=${DEFAULT_IP}
+    else
+        API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
+        proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
+        url="$(echo ${API_SERVER/$proto/})"
+        user="$(echo $url | grep @ | cut -d@ -f1)"
+        hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
+        API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
+    fi
+
+    juju config nbi-k8s juju-external-hostname=nbi.${API_SERVER}.xip.io
+    juju expose nbi-k8s
+
+    wait_for_port nbi-k8s 0
+    sg ${KUBEGRP} -c "${KUBECTL} get ingress -n osm -o json | jq '.items[0].metadata.annotations += {\"nginx.ingress.kubernetes.io/backend-protocol\": \"HTTPS\"}' | ${KUBECTL} --validate=false replace -f -"
+    sg ${KUBEGRP} -c "${KUBECTL} get ingress -n osm -o json | jq '.items[0].metadata.annotations += {\"nginx.ingress.kubernetes.io/proxy-body-size\": \"0\"}' | ${KUBECTL} replace -f -"
+
+    juju config ng-ui juju-external-hostname=ui.${API_SERVER}.xip.io
+    juju expose ng-ui
+
+    wait_for_port ng-ui 1
+    sg ${KUBEGRP} -c "${KUBECTL} get ingress -n osm -o json | jq '.items[2].metadata.annotations += {\"nginx.ingress.kubernetes.io/proxy-body-size\": \"0\"}' | ${KUBECTL} --validate=false replace -f -"
+
+    juju config ui-k8s juju-external-hostname=osm.${API_SERVER}.xip.io
     juju expose ui-k8s
+
+    wait_for_port ui-k8s 2
+    sg ${KUBEGRP} -c "${KUBECTL} get ingress -n osm -o json | jq '.items[1].metadata.annotations += {\"nginx.ingress.kubernetes.io/proxy-body-size\": \"0\"}' | ${KUBECTL} --validate=false replace -f -"
 }
 
 function check_osm_deployed() {
+    TIME_TO_WAIT=600
+    start_time="$(date -u +%s)"
+    total_service_count=14
     while true
     do
-        pod_name=`sg microk8s -c "microk8s.kubectl -n osm get pods | grep ui-k8s | grep -v operator" | awk '{print $1; exit}'`
-
-        if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod $pod_name --for condition=Ready"` ]]; then
-            if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod lcm-k8s-0 --for condition=Ready"` ]]; then
-                break
-            fi
+        service_count=$(juju status | grep kubernetes | grep active | wc -l)
+        echo "$service_count / $total_service_count services active"
+        if [ $service_count -eq $total_service_count ]; then
+            break
+        fi
+        now="$(date -u +%s)"
+        if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
+            echo "Timed out waiting for OSM services to become ready"
+            exit 1
         fi
         sleep 10
     done
@@ -276,10 +370,21 @@ DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
 check_arguments $@
 mkdir -p ~/.osm
 install_snaps
-sleep 5
 bootstrap_k8s_lxd
 deploy_charmed_osm
 install_osmclient
 if [ -v MICROSTACK ]; then
     install_microstack
 fi
+
+echo "Your installation is now complete, follow these steps for configuring the osmclient:"
+echo
+echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
+echo
+echo "export OSM_HOSTNAME=nbi.$API_SERVER.xip.io:443"
+echo
+echo "2. Add the previous command to your .bashrc for other Shell sessions"
+echo
+echo "echo \"export OSM_HOSTNAME=nbi.$API_SERVER.xip.io:443\" >> ~/.bashrc"
+echo
+echo "DONE"