Update juju agent to 2.9.17
[osm/devops.git] / installers / full_install_osm.sh
index 817bb7f..90e5c1f 100755 (executable)
@@ -183,8 +183,6 @@ function remove_k8s_namespace() {
 function remove_helm() {
     if [ "$(helm ls -q)" == "" ] ; then
         sudo helm reset --force
-        kubectl delete --namespace kube-system serviceaccount tiller
-        kubectl delete clusterrolebinding tiller-cluster-rule
         sudo rm /usr/local/bin/helm
         rm -rf $HOME/.helm
     fi
@@ -292,7 +290,7 @@ function install_lxd() {
 
     # Install LXD snap
     sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
-    sudo snap install lxd
+    sudo snap install lxd --channel $LXD_VERSION/stable
 
     # Configure LXD
     sudo usermod -a -G lxd `whoami`
@@ -464,17 +462,19 @@ function juju_createcontroller() {
 }
 
 function juju_addk8s() {
-    cat $HOME/.kube/config | juju add-k8s $OSM_VCA_K8S_CLOUDNAME --controller $OSM_STACK_NAME --storage openebs-hostpath
+    cat $HOME/.kube/config | juju add-k8s $OSM_VCA_K8S_CLOUDNAME --controller $OSM_STACK_NAME --storage openebs-hostpath \
+    || FATAL "Failed to add K8s endpoint and credential for controller $OSM_STACK_NAME in cloud $OSM_VCA_K8S_CLOUDNAME"
 }
 
 function juju_createcontroller_k8s(){
-    cat $HOME/.kube/config | juju add-k8s $OSM_VCA_K8S_CLOUDNAME --client
+    cat $HOME/.kube/config | juju add-k8s $OSM_VCA_K8S_CLOUDNAME --client \
+    || FATAL "Failed to add K8s endpoint and credential for client in cloud $OSM_VCA_K8S_CLOUDNAME"
     juju bootstrap $OSM_VCA_K8S_CLOUDNAME $OSM_STACK_NAME  \
             --config controller-service-type=loadbalancer \
-            --agent-version=$JUJU_AGENT_VERSION
+            --agent-version=$JUJU_AGENT_VERSION \
+    || FATAL "Failed to bootstrap controller $OSM_STACK_NAME in cloud $OSM_VCA_K8S_CLOUDNAME"
 }
 
-
 function juju_addlxd_cloud(){
     mkdir -p /tmp/.osm
     OSM_VCA_CLOUDNAME="lxd-cloud"
@@ -515,7 +515,6 @@ EOF
     juju controller-config features=[k8s-operators]
 }
 
-
 function juju_createproxy() {
     check_install_iptables_persistent
 
@@ -804,7 +803,8 @@ function install_kube() {
     sudo add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
     sudo apt-get update
     echo "Installing Kubernetes Packages ..."
-    sudo apt-get install -y kubelet=1.15.0-00 kubeadm=1.15.0-00 kubectl=1.15.0-00
+    K8S_VERSION=1.20.11-00
+    sudo apt-get install -y kubelet=${K8S_VERSION} kubeadm=${K8S_VERSION} kubectl=${K8S_VERSION}
     sudo apt-mark hold kubelet kubeadm kubectl
 }
 
@@ -824,10 +824,11 @@ function kube_config_dir() {
 }
 
 function install_k8s_storageclass() {
-    OPENEBS_DIR="$(mktemp -d -q --tmpdir "openebs.XXXXXX")"
-    trap 'rm -rf "${OPENEBS_DIR}"' EXIT
-    wget -q https://openebs.github.io/charts/openebs-operator-1.6.0.yaml -P $OPENEBS_DIR
-    kubectl apply -f $OPENEBS_DIR
+    echo "Installing OpenEBS"
+    helm repo add openebs https://openebs.github.io/charts
+    helm repo update
+    helm install --create-namespace --namespace openebs openebs openebs/openebs --version 1.12.0
+    helm ls -n openebs
     local storageclass_timeout=400
     local counter=0
     local storageclass_ready=""
@@ -850,8 +851,9 @@ function install_k8s_storageclass() {
 }
 
 function install_k8s_metallb() {
-    METALLB_IP_RANGE=$DEFAULT_IP-$DEFAULT_IP
-    cat ${OSM_DEVOPS}/installers/k8s/metallb/metallb.yaml | kubectl apply -f -
+    METALLB_IP_RANGE=$DEFAULT_IP/32
+    kubectl apply -f ${OSM_DEVOPS}/installers/k8s/metallb/metallb.yaml \
+    || FATAL "Cannot install MetalLB"
     echo "apiVersion: v1
 kind: ConfigMap
 metadata:
@@ -863,8 +865,105 @@ data:
     - name: default
       protocol: layer2
       addresses:
-      - $METALLB_IP_RANGE" | kubectl apply -f -
+      - $METALLB_IP_RANGE" | kubectl apply -f - \
+    || FATAL "Cannot apply MetalLB ConfigMap"
+}
+
+#installs metallb from helm
+function install_helm_metallb() {
+    METALLB_IP_RANGE=$DEFAULT_IP/32
+    echo "configInline:
+  address-pools:
+   - name: default
+     protocol: layer2
+     addresses:
+     - $METALLB_IP_RANGE" | sudo tee -a $OSM_DOCKER_WORK_DIR/metallb-config.yaml
+    helm repo add metallb https://metallb.github.io/metallb
+    helm install --create-namespace --namespace metallb-system metallb metallb/metallb -f $OSM_DOCKER_WORK_DIR/metallb-config.yaml
+}
+
+#checks openebs and metallb readiness
+function check_for_readiness() {
+    # Default input values
+    sampling_period=2       # seconds
+    time_for_readiness=20   # seconds ready
+    time_for_failure=200    # seconds broken
+    OPENEBS_NAMESPACE=openebs
+    METALLB_NAMESPACE=metallb-system
+    # STACK_NAME=osm          # By default, "osm"
+
+    # Equivalent number of samples
+    oks_threshold=$((time_for_readiness/${sampling_period}))     # No. ok samples to declare the system ready
+    failures_threshold=$((time_for_failure/${sampling_period}))  # No. nok samples to declare the system broken
+    failures_in_a_row=0
+    oks_in_a_row=0
+
+    ####################################################################################
+    # Loop to check system readiness
+    ####################################################################################
+    while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
+    do
+        # State of OpenEBS
+        OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
+        OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
+        OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
+        COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
+        COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
+
+        # State of MetalLB
+        METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
+        METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
+        METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
+        COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
+        COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
+
+        # OK sample
+        if [[ $((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
+        then
+            ((++oks_in_a_row))
+            failures_in_a_row=0
+            echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
+        # NOK sample
+        else
+            ((++failures_in_a_row))
+            oks_in_a_row=0
+            echo
+            echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
+
+            # Reports failed pods in OpenEBS
+            if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
+            then
+                echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
+                echo "${OPENEBS_NOT_READY}"
+                echo
+            fi
+
+            # Reports failed statefulsets
+            if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
+            then
+                echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
+                echo "${METALLB_NOT_READY}"
+                echo
+            fi
+        fi
+
+        #------------ NEXT SAMPLE
+        sleep ${sampling_period}
+    done
+
+    ####################################################################################
+    # OUTCOME
+    ####################################################################################
+    if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
+    then
+        echo
+        FATAL "K8S CLUSTER IS BROKEN"
+    else
+        echo
+        echo "K8S CLUSTER IS READY"
+    fi
 }
+
 #deploys flannel as daemonsets
 function deploy_cni_provider() {
     CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")"
@@ -911,40 +1010,19 @@ function deploy_osm_pla_service() {
     kubectl apply -n $OSM_STACK_NAME -f $OSM_DOCKER_WORK_DIR/osm_pla
 }
 
-#Install helm and tiller
+#Install Helm v3
 function install_helm() {
     helm > /dev/null 2>&1
     if [ $? != 0 ] ; then
         # Helm is not installed. Install helm
         echo "Helm is not installed, installing ..."
-        curl https://get.helm.sh/helm-v2.15.2-linux-amd64.tar.gz --output helm-v2.15.2.tar.gz
-        tar -zxvf helm-v2.15.2.tar.gz
+        curl https://get.helm.sh/helm-v3.6.3-linux-amd64.tar.gz --output helm-v3.6.3.tar.gz
+        tar -zxvf helm-v3.6.3.tar.gz
         sudo mv linux-amd64/helm /usr/local/bin/helm
         rm -r linux-amd64
-        rm helm-v2.15.2.tar.gz
-    fi
-
-    # Checking if tiller has being configured
-    kubectl --namespace kube-system get serviceaccount tiller > /dev/null 2>&1
-    if [ $? == 1 ] ; then
-        # tiller account for kubernetes
-        kubectl --namespace kube-system create serviceaccount tiller
-        kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
-        # HELM initialization
-        helm init --stable-repo-url https://charts.helm.sh/stable --service-account tiller
-
-        # Wait for Tiller to be up and running. If timeout expires, continue installing
-        tiller_timeout=120;
-        counter=0;
-        tiller_status=""
-        while (( counter < tiller_timeout ))
-        do
-            tiller_status=`kubectl -n kube-system get deployment.apps/tiller-deploy --no-headers |  awk '{print $2'}`
-            ( [ ! -z "$tiller_status" ] && [ $tiller_status == "1/1" ] ) && echo "Tiller ready" && break
-            counter=$((counter + 5))
-            sleep 5
-        done
-        [ "$tiller_status" != "1/1" ] && echo "Tiller is NOT READY YET. Installation will continue"
+        rm helm-v3.6.3.tar.gz
+        helm repo add stable https://charts.helm.sh/stable
+        helm repo update
     fi
 }
 
@@ -1202,10 +1280,13 @@ function install_lightweight() {
         remove_k8s_namespace $OSM_STACK_NAME
         deploy_cni_provider
         taint_master_node
+        install_helm
+        track install_helm
         install_k8s_storageclass
         track k8s_storageclass
-        install_k8s_metallb
+        install_helm_metallb
         track k8s_metallb
+        check_for_readiness
     else
         #install_docker_compose
         [ -n "$INSTALL_NODOCKER" ] || init_docker_swarm
@@ -1331,8 +1412,6 @@ EOF
             track deploy_osm_pla
         fi
         track deploy_osm_services_k8s
-        install_helm
-        track install_helm
         if [ -n "$INSTALL_K8S_MONITOR" ]; then
             # install OSM MONITORING
             install_k8s_monitoring
@@ -1542,8 +1621,9 @@ function parse_docker_registry_url() {
     DOCKER_REGISTRY_URL=$(echo "$DOCKER_REGISTRY_URL" | awk '{split($1,a,"@"); print a[2]}')
 }
 
+LXD_VERSION=4.0
 JUJU_VERSION=2.9
-JUJU_AGENT_VERSION=2.9.5
+JUJU_AGENT_VERSION=2.9.17
 UNINSTALL=""
 DEVELOP=""
 UPDATE=""