[AETHER]-615 Update the Jenkins job

1. Replace the Rancher CLI with Terraform in TOST related jobs.
2. Still the Rancher CLI in FluentBit but will fix it soon
3. Add new jenkins job to install shared resources

Change-Id: I6e4d5ecf696e7a17a1d954df19093005bf14bf8c
diff --git a/jjb/pipeline/tost-deploy-telegraf.groovy b/jjb/pipeline/tost-deploy-telegraf.groovy
index aa5daa1..ac61496 100644
--- a/jjb/pipeline/tost-deploy-telegraf.groovy
+++ b/jjb/pipeline/tost-deploy-telegraf.groovy
@@ -7,69 +7,64 @@
     }
     environment {
         KUBECONFIG = credentials("${params.k8s_config}")
-        git_password = credentials("${params.git_password_env}")
-        rancher_token = credentials("${params.rancher_api_env}")
+        gcp = credentials("${params.gcp_credential}")
+        rancher_dev = credentials("${params.rancher_api_env}")
     }
     stages {
         stage('Install tools') {
             steps {
-                sh '''
+                sh """
                 set -x
                 apt-get update -y
-                apt-get install -y curl wget jq git
+                apt-get install -y curl wget jq git unzip
 
                 # Install kubectl
                 curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl"
                 chmod +x ./kubectl
                 mv ./kubectl /usr/local/bin/kubectl
 
-                # Install rancher
-                wget https://github.com/rancher/cli/releases/download/v2.4.5/rancher-linux-amd64-v2.4.5.tar.gz
-                tar -xvf rancher-linux-amd64-v2.4.5.tar.gz
-                mv rancher-v2.4.5/rancher /usr/local/bin
-
-                rm rancher-linux-amd64-v2.4.5.tar.gz
-                rm -rf rancher-v2.4.5
-                # Test Kubectl & Rancher
-                KUBE_CONFIG=$KUBECONFIG kubectl get nodes
-                rancher --version
-                '''
+                wget https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_amd64.zip
+                unzip terraform_0.13.2_linux_amd64.zip
+                mv terraform /usr/local/bin
+                terraform version
+                """
             }
         }
-        stage('Clone Config Repo') {
-            options {
-                timeout(time: 10, unit: "SECONDS")
-            }
+        stage('Init Terraform') {
             steps {
-                sh '''
-                git clone https://${git_user}:${git_password}@${git_server}/${git_repo}
+                withCredentials([sshUserPrivateKey(credentialsId: "aether_jenkins", keyFileVariable: 'keyfile')]) {
+                sh """#!/bin/bash
+                set -x
+                mkdir -p ~/.ssh
+                ssh-keyscan -t rsa -p 29418 ${git_server} >> ~/.ssh/known_hosts
+cat <<EOF > ~/.ssh/config
+Host ${git_server}
+  User ${git_user}
+  Hostname ${git_server}
+  Port 29418
+  IdentityFile ${keyfile}
+EOF
+                git clone "ssh://${git_server}:29418/${git_repo}"
+                cd ${workspace}/${git_repo}/${terraform_dir}/tost/telegraf
                 if [ ! -z ${config_review} ] && [ ! -z ${config_patchset} ]; then
-                    cd ${git_repo}
-                    CFG_LAST2=$(echo ${config_review} | tail -c 3)
-                    git fetch "https://${git_user}:${git_password}@${git_server}/a/${git_repo}" refs/changes/${CFG_LAST2}/${config_review}/${config_patchset} && git checkout FETCH_HEAD
+                    CFG_LAST2=\$(echo ${config_review} | tail -c 3)
+                    git fetch "ssh://@${git_server}:29418/${git_repo}" refs/changes/\${CFG_LAST2}/${config_review}/${config_patchset} && git checkout FETCH_HEAD
                     git checkout FETCH_HEAD
-                    cd ..
                 fi
-                '''
-             }
-        }
-        stage('Login Rancher') {
-            steps {
-                sh '''
-                rancher login ${rancher_server} --token ${rancher_token} --context ${rancher_context}:${rancher_project}
-                '''
-             }
+                GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform init
+                """
+                }
+            }
         }
         stage('Uninstall Apps') {
             options {
                 timeout(time: 90, unit: "SECONDS")
             }
             steps {
-                sh '''
-                for app in $(rancher apps ls -q | grep -E '(telegraf)'); do rancher apps delete $app; done
-
-                until [ "$(rancher apps ls -q | grep -E '(telegraf)')" = "" ]; do echo "wait deleted apps"; rancher apps ls ; sleep 1; done
-                '''
+                sh """
+                cd ${workspace}/${git_repo}/${terraform_dir}/tost/telegraf
+                GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform destroy -var-file=${rancher_dev} -var 'cluster_name=${rancher_cluster}' -var 'project_name=tost' -var-file=app_map.tfvars -auto-approve
+                """
              }
         }
        stage('Install apps') {
@@ -77,18 +72,19 @@
                 timeout(time: 600, unit: "SECONDS")
             }
             steps {
-                sh '''
-                cd ${git_repo}/deployment-configs/aether/apps/${config_env}/
-                until rancher apps install --answers telegraf-ans.yml --namespace ${telegraf_ns} cattle-global-data:influxdata-telegraf telegraf; do :; done
-                apps=$(rancher apps -q | grep telegraf)
-                for app in $apps; do until rancher wait $app --timeout 20; do :; done; rancher apps ls; done
-                '''
+                sh """
+                cd ${workspace}/${git_repo}/${terraform_dir}/tost/telegraf
+                GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform apply -var-file=${rancher_dev} -var 'cluster_name=${rancher_cluster}'  -var 'project_name=tost'  -var-file=app_map.tfvars -auto-approve
+                """
              }
         }
 
     }
     post {
         always {
+            sh """
+            rm -rf ${workspace}/${git_repo}
+            """
             cleanWs()
       }
     }