From 7b3f2d4ff0003fb4b7fda94a6227c975df33d470 Mon Sep 17 00:00:00 2001 From: Jay Sharma Date: Thu, 5 Oct 2023 21:56:11 +0530 Subject: [PATCH] Apply validations --- 1 | 113 +++++++++++++++++++++++++++ bitbucket-pipelines.yml | 113 ++++++++++++++++++++------- build/dev/deployment.tpl | 18 ++--- build/prod/configmap.yaml | 4 +- build/prod/deployment.tpl | 18 ++--- build/prod/pvc.yml | 2 +- build/qa/deployment.tpl | 18 ++--- build/stage/deployment.tpl | 28 +++++++ build/stage/web-default-conf-cm.yaml | 9 +++ common-jobs-bitbucket.sh | 106 ++++++++++++++++++------- validation.sh | 83 ++++++++++++++++++++ 11 files changed, 425 insertions(+), 87 deletions(-) create mode 100644 1 create mode 100644 validation.sh diff --git a/1 b/1 new file mode 100644 index 00000000..e8d61ee4 --- /dev/null +++ b/1 @@ -0,0 +1,113 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + deployment.kubernetes.io/revision: "11" + labels: + app: medicalalert-web + name: medicalalert-web + namespace: medicalalert-web +spec: + progressDeadlineSeconds: 600 + replicas: 3 + revisionHistoryLimit: 10 + selector: + matchLabels: + app: medicalalert-web + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + annotations: + kubectl.kubernetes.io/restartedAt: "2023-06-08T17:04:19-04:00" + creationTimestamp: null + labels: + app: medicalalert-web + spec: + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: kubernetes.io/arch + operator: In + values: + - amd64 + - arm64 + containers: + - env: + - name: ENVIRONMENT + value: "$ENV" + image: 716593996126.dkr.ecr.us-east-1.amazonaws.com/medicalalert-web:$VERSION + envFrom: + - secretRef: + name: medicalalert-web-secrets + - configMapRef: + name: medicalalert-web-cm + imagePullPolicy: Always + name: medicalalert-web + ports: + - containerPort: 80 + name: http + protocol: TCP + startupProbe: + exec: + command: + - /root/redinessprobe-wp.sh + initialDelaySeconds: 90 + periodSeconds: 10 + timeoutSeconds: 10 + failureThreshold: 5 + successThreshold: 1 + readinessProbe: + exec: + command: + - /bin/sh + - -c + - "wget -qO- 127.0.0.1/status |grep start && echo redinessprobe-success || exit 9" + initialDelaySeconds: 30 + periodSeconds: 5 + successThreshold: 1 + failureThreshold: 5 + timeoutSeconds: 10 + livenessProbe: + tcpSocket: + port: 80 + initialDelaySeconds: 5 + periodSeconds: 5 + successThreshold: 1 + failureThreshold: 3 + timeoutSeconds: 10 + resources: + limits: + cpu: 500m + memory: 1024Mi + requests: + cpu: 300m + memory: 700Mi + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + volumeMounts: + - mountPath: /usr/share/nginx/subdomain/www/wp-content/uploads + name: persistent-storage + - mountPath: /etc/nginx/conf.d/ + name: default-conf + dnsPolicy: ClusterFirst + imagePullSecrets: + - name: regcred + nodeSelector: + kubernetes.io/os: linux + restartPolicy: Always + schedulerName: default-scheduler + terminationGracePeriodSeconds: 30 + volumes: + - name: persistent-storage + persistentVolumeClaim: + claimName: efs-claim-medicalalert + - name: default-conf + configMap: + defaultMode: 420 + name: medicalalert-web-default-conf-cm diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index f8efd33d..3a268476 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -4,18 +4,44 @@ definitions: - name: VERSION default: "latest" - set-variables-jobs: &set-variables-jobs + set-variables-jobs: &set-variables-jobs-dev - variables: - name: ENV default: "dev" allowed-values: # Optionally restrict variable values - "dev" + - name: JOBNAME + default: "mysql-backup" + allowed-values: # Optionally restrict variable values + - "wp-auto-patch" + - "mysql-backup" + - "wp-data-backup" + - "mysql-restore" + - "wp-data-restore" + - name: SUBDOMAIN + default: dev01 + allowed-values: # Optionally restrict variable values + - "dev01" + - "www" + - "dev02" + - "dev03" + - "dev04" + - "dev05" + - name: RESTORE_VERSION + default: "PASTE_RESTORE_VERSION_FILE_NAME" + + set-variables-jobs: &set-variables-jobs + - variables: + - name: ENV + default: "qa" + allowed-values: # Optionally restrict variable values - "qa" - "stage" - "prod" - name: JOBNAME default: "mysql-backup" allowed-values: # Optionally restrict variable values + - "wp-auto-patch" - "mysql-backup" - "wp-data-backup" - "mysql-restore" @@ -24,14 +50,10 @@ definitions: default: www allowed-values: # Optionally restrict variable values - "www" - - "dev01" - - "dev02" - - "dev03" - - "dev04" - - "dev05" - name: RESTORE_VERSION default: "PASTE_RESTORE_VERSION_FILE_NAME" + set-variables-subdomain: &set-variables-subdomain - variables: - name: ENV @@ -51,7 +73,6 @@ definitions: - name: STATIC_FILE_NAME default: "STATIC_FILE_NAME_TO_DEPLOY" - steps: - step: &deploy name: deploy to $ENV environment @@ -63,8 +84,10 @@ definitions: - source setenv.sh - fi - echo "This is for $ENV environment" + - chmod 755 ./validation.sh && ./validation.sh default && echo validation-done || exit 9 - ls -lrth && cd build/${ENV} - - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install + - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install + - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x ./kubectl && mv ./kubectl /usr/local/bin/kubectl - export DB_PASSWORD=$(aws ssm get-parameters --names $ENV-medicalalert-masterdbpass --query "Parameters[*].{Value:Value}" --output text) - export DB_PASSWORD=`echo -n "$DB_PASSWORD" | base64` && sed -i "/DB_PASSWORD/c\ DB_PASSWORD:\ $DB_PASSWORD" secrets.yaml - envsubst < deployment.tpl > deployment.yaml @@ -80,43 +103,45 @@ definitions: KUBECTL_ARGS: "" KUBECTL_APPLY_ARGS: "-f" DEBUG: "true" + after-script: + - aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 + - sleep 10 && while kubectl get po -A |egrep -i '0/1|crash'; do sleep 10; echo "--- still getting 0/1 or crash pods | fix the issue"; done pipelines: # our custom pipeline, what shows up Bitbucket web app custom: deploy-dev: - - !!merge <<: *set-variables + - <<: *set-variables - step: - !!merge <<: *deploy + <<: *deploy deployment: dev name: "deploy to dev" deploy-qa: - - !!merge <<: *set-variables + - <<: *set-variables - step: - !!merge <<: *deploy + <<: *deploy deployment: qa name: "deploy to qa" release-stg: - - !!merge <<: *set-variables + - <<: *set-variables - step: - !!merge <<: *deploy + <<: *deploy deployment: stage name: "deploy to stage" release-prod: - - !!merge <<: *set-variables + - <<: *set-variables - step: - !!merge <<: *deploy + <<: *deploy deployment: prod name: "deploy to prod" - - common-jobs: - - !!merge <<: *set-variables-jobs + common-jobs-dev: + - <<: *set-variables-jobs-dev - step: - name: vars for common-jobs + name: vars for common-jobs-dev script: - echo "export JOBNAME=$JOBNAME" >> setenv.sh - echo "export ENV=$ENV" >> setenv.sh @@ -124,7 +149,35 @@ pipelines: - echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh - echo "export RESTORE_VERSION=$RESTORE_VERSION" >> setenv.sh - echo "export SUBDOMAIN=$SUBDOMAIN" >> setenv.sh - - echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh + - echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh + - echo "export TOKEN=$TOKEN" >> setenv.sh + artifacts: # define the artifacts to be passed to each future step + - setenv.sh + + - step: + name: common jobs for SQL and WWW backup and restore DEV + image: atlassian/pipelines-awscli + script: + - source setenv.sh && chmod 755 ./common-jobs-bitbucket.sh + - if [ "$JOBNAME" = "mysql-restore" ] && [ "$SUBDOMAIN" = "www" ] ; then ./common-jobs-bitbucket.sh subdomain_deploy; fi + # - [[ "$SUBDOMAIN" != "www" ]] && [[ "$ENV" = "qa" || "$ENV" = "stage" || "$ENV" = "prod" ]] && exit || echo VALIDATION-PASS + - if [[ "$SUBDOMAIN" != "www" ]] && [[ "$ENV" = "qa" || "$ENV" = "stage" || "$ENV" = "prod" ]] ; then echo VALIDATION-FAIL && exit 9 ; else echo VALIDATION-PASS;fi + - ./common-jobs-bitbucket.sh $JOBNAME + + + common-jobs: + - <<: *set-variables-jobs + - step: + name: vars for common-jobs qa/stage/prod + script: + - echo "export JOBNAME=$JOBNAME" >> setenv.sh + - echo "export ENV=$ENV" >> setenv.sh + - echo "export CLUSTER=caresage-eks-cluster-$ENV" >> setenv.sh + - echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh + - echo "export RESTORE_VERSION=$RESTORE_VERSION" >> setenv.sh + - echo "export SUBDOMAIN=$SUBDOMAIN" >> setenv.sh + - echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh + - echo "export TOKEN=$TOKEN" >> setenv.sh artifacts: # define the artifacts to be passed to each future step - setenv.sh @@ -138,9 +191,8 @@ pipelines: - if [[ "$SUBDOMAIN" != "www" ]] && [[ "$ENV" = "qa" || "$ENV" = "stage" || "$ENV" = "prod" ]] ; then echo VALIDATION-FAIL && exit 9 ; else echo VALIDATION-PASS;fi - ./common-jobs-bitbucket.sh $JOBNAME - subdomain-deploy: - - !!merge <<: *set-variables-subdomain + - <<: *set-variables-subdomain - step: name: vars for subdomain script: @@ -152,7 +204,8 @@ pipelines: - echo "export URL_DOMAIN=$URL_DOMAIN" >> setenv.sh - echo "export MYSQL_FILE_NAME=$MYSQL_FILE_NAME" >> setenv.sh - echo "export STATIC_FILE_NAME=$STATIC_FILE_NAME" >> setenv.sh - - echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh + - echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh + - echo "export TOKEN=$TOKEN" >> setenv.sh artifacts: - setenv.sh @@ -187,11 +240,12 @@ pipelines: release: - - !!merge <<: *set-variables + - <<: *set-variables - step: name: "plan for the release and tag the given version." image: atlassian/pipelines-awscli script: + - chmod 755 ./validation.sh && ./validation.sh default - echo "make sure that release version file and plan has been added to the release repo" - source release - export MANIFEST=$(aws ecr batch-get-image --repository-name medicalalert-web --image-ids imageTag=$VERSION --output json | jq --raw-output --join-output '.images[0].imageManifest') @@ -201,7 +255,7 @@ pipelines: artifacts: # define the artifacts to be passed to each future step - setenv.sh - step: - !!merge <<: *deploy + <<: *deploy deployment: stage name: "deploy the release to stage" trigger: manual @@ -214,7 +268,7 @@ pipelines: artifacts: # define the artifacts to be passed to each future step - setenv.sh - step: - !!merge <<: *deploy + <<: *deploy deployment: prod name: "deploy the release to production" trigger: manual @@ -225,6 +279,7 @@ pipelines: name: build the application and push that to dev env. image: atlassian/default-image:3 script: + - chmod 755 ./validation.sh && ./validation.sh default - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install - export BUILD_D=$BITBUCKET_BRANCH_$BITBUCKET_COMMIT_$BITBUCKET_BUILD_NUMBER - export IMAGE="medicalalert-web" && export VERSION=$BITBUCKET_BUILD_NUMBER @@ -251,7 +306,7 @@ pipelines: artifacts: # define the artifacts to be passed to each future step - setenv.sh - step: - !!merge <<: *deploy + <<: *deploy deployment: dev name: "deploy to dev" - step: @@ -262,7 +317,7 @@ pipelines: artifacts: - setenv.sh - step: - !!merge <<: *deploy + <<: *deploy deployment: qa name: "deploy to qa" trigger: manual diff --git a/build/dev/deployment.tpl b/build/dev/deployment.tpl index c3f0b026..016bc1c7 100755 --- a/build/dev/deployment.tpl +++ b/build/dev/deployment.tpl @@ -57,10 +57,10 @@ spec: exec: command: - /root/redinessprobe-wp.sh - initialDelaySeconds: 60 - periodSeconds: 15 - timeoutSeconds: 30 - failureThreshold: 10 + initialDelaySeconds: 90 + periodSeconds: 10 + timeoutSeconds: 10 + failureThreshold: 5 successThreshold: 1 readinessProbe: exec: @@ -68,19 +68,19 @@ spec: - /bin/sh - -c - "wget -qO- 127.0.0.1/status |grep start && echo redinessprobe-success || exit 9" - initialDelaySeconds: 120 + initialDelaySeconds: 30 periodSeconds: 5 successThreshold: 1 failureThreshold: 5 - timeoutSeconds: 30 + timeoutSeconds: 10 livenessProbe: tcpSocket: port: 80 - initialDelaySeconds: 2 - periodSeconds: 2 + initialDelaySeconds: 5 + periodSeconds: 5 successThreshold: 1 failureThreshold: 3 - timeoutSeconds: 30 + timeoutSeconds: 10 resources: limits: cpu: 500m diff --git a/build/prod/configmap.yaml b/build/prod/configmap.yaml index 5cbbc52f..2b9b075d 100644 --- a/build/prod/configmap.yaml +++ b/build/prod/configmap.yaml @@ -6,8 +6,8 @@ metadata: labels: app: medicalalert-web data: - DB_HOST: medicalalert-ecommerce.c5om7w6xopq1.us-east-1.rds.amazonaws.com - ENV: qa + DB_HOST: prod-medicalalert-ecommerce.c5om7w6xopq1.us-east-1.rds.amazonaws.com + ENV: prod WP_DEBUG: "false" DB_CHARSET: utf8 DB_COLLATE: utf8_general_ci diff --git a/build/prod/deployment.tpl b/build/prod/deployment.tpl index cb9b52a3..e8d61ee4 100644 --- a/build/prod/deployment.tpl +++ b/build/prod/deployment.tpl @@ -57,10 +57,10 @@ spec: exec: command: - /root/redinessprobe-wp.sh - initialDelaySeconds: 60 - periodSeconds: 15 - timeoutSeconds: 30 - failureThreshold: 10 + initialDelaySeconds: 90 + periodSeconds: 10 + timeoutSeconds: 10 + failureThreshold: 5 successThreshold: 1 readinessProbe: exec: @@ -68,19 +68,19 @@ spec: - /bin/sh - -c - "wget -qO- 127.0.0.1/status |grep start && echo redinessprobe-success || exit 9" - initialDelaySeconds: 120 + initialDelaySeconds: 30 periodSeconds: 5 successThreshold: 1 failureThreshold: 5 - timeoutSeconds: 30 + timeoutSeconds: 10 livenessProbe: tcpSocket: port: 80 - initialDelaySeconds: 2 - periodSeconds: 2 + initialDelaySeconds: 5 + periodSeconds: 5 successThreshold: 1 failureThreshold: 3 - timeoutSeconds: 30 + timeoutSeconds: 10 resources: limits: cpu: 500m diff --git a/build/prod/pvc.yml b/build/prod/pvc.yml index eebe0e80..a23026e6 100644 --- a/build/prod/pvc.yml +++ b/build/prod/pvc.yml @@ -6,7 +6,7 @@ metadata: spec: accessModes: - ReadWriteMany - storageClassName: efs-sc-ecommerce + storageClassName: efs-sc-prod-ecommerce resources: requests: storage: 5Gi diff --git a/build/qa/deployment.tpl b/build/qa/deployment.tpl index cb9b52a3..e8d61ee4 100644 --- a/build/qa/deployment.tpl +++ b/build/qa/deployment.tpl @@ -57,10 +57,10 @@ spec: exec: command: - /root/redinessprobe-wp.sh - initialDelaySeconds: 60 - periodSeconds: 15 - timeoutSeconds: 30 - failureThreshold: 10 + initialDelaySeconds: 90 + periodSeconds: 10 + timeoutSeconds: 10 + failureThreshold: 5 successThreshold: 1 readinessProbe: exec: @@ -68,19 +68,19 @@ spec: - /bin/sh - -c - "wget -qO- 127.0.0.1/status |grep start && echo redinessprobe-success || exit 9" - initialDelaySeconds: 120 + initialDelaySeconds: 30 periodSeconds: 5 successThreshold: 1 failureThreshold: 5 - timeoutSeconds: 30 + timeoutSeconds: 10 livenessProbe: tcpSocket: port: 80 - initialDelaySeconds: 2 - periodSeconds: 2 + initialDelaySeconds: 5 + periodSeconds: 5 successThreshold: 1 failureThreshold: 3 - timeoutSeconds: 30 + timeoutSeconds: 10 resources: limits: cpu: 500m diff --git a/build/stage/deployment.tpl b/build/stage/deployment.tpl index 5a0516df..e8d61ee4 100644 --- a/build/stage/deployment.tpl +++ b/build/stage/deployment.tpl @@ -53,6 +53,34 @@ spec: - containerPort: 80 name: http protocol: TCP + startupProbe: + exec: + command: + - /root/redinessprobe-wp.sh + initialDelaySeconds: 90 + periodSeconds: 10 + timeoutSeconds: 10 + failureThreshold: 5 + successThreshold: 1 + readinessProbe: + exec: + command: + - /bin/sh + - -c + - "wget -qO- 127.0.0.1/status |grep start && echo redinessprobe-success || exit 9" + initialDelaySeconds: 30 + periodSeconds: 5 + successThreshold: 1 + failureThreshold: 5 + timeoutSeconds: 10 + livenessProbe: + tcpSocket: + port: 80 + initialDelaySeconds: 5 + periodSeconds: 5 + successThreshold: 1 + failureThreshold: 3 + timeoutSeconds: 10 resources: limits: cpu: 500m diff --git a/build/stage/web-default-conf-cm.yaml b/build/stage/web-default-conf-cm.yaml index 422decc8..18aa7519 100644 --- a/build/stage/web-default-conf-cm.yaml +++ b/build/stage/web-default-conf-cm.yaml @@ -282,4 +282,13 @@ data: deny all; } + location ~ ^/(status|ping)$ { + access_log off; + allow 127.0.0.1; + deny all; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; + fastcgi_pass unix:/run/php/php7.4-fpm.sock; + } + } diff --git a/common-jobs-bitbucket.sh b/common-jobs-bitbucket.sh index b248870c..348042fe 100644 --- a/common-jobs-bitbucket.sh +++ b/common-jobs-bitbucket.sh @@ -1,5 +1,7 @@ #!/bin/bash exec > >(tee -a script.log) 2>&1 + + # Install the required packages # # -------------------------------------------------------------- apk upgrade && apk add curl wget zip unzip @@ -10,17 +12,33 @@ curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s htt # ---------------------------------------------------------------------- export NEW_URL=https://$URL_DOMAIN export CLUSTER=caresage-eks-cluster-$ENV + +if [[ "$ENV" = "prod" ]] ; then +export APP=`echo $URL_DOMAIN | awk -F[.-] '{print $2}'` +else export APP=`echo $URL_DOMAIN | awk -F[.-] '{print $3}'` +fi + export NAMESPACE=$APP-web + export REPO=$BITBUCKET_REPO_SLUG export REPLACE=`echo $NEW_URL | awk -F[./] '{print $4"."$5}'` +export MAIN_DOMAIN=`echo $NEW_URL | awk -F[./] '{print $4"."$5}'` export URL_DOMAIN=$URL_DOMAIN # --------------------------- echo "export NEW_URL=https://$URL_DOMAIN" >> setenv.sh + +if [[ "$ENV" = "prod" ]] ; then +echo "export APP=`echo $URL_DOMAIN | awk -F[.-] '{print $2}'`" >> setenv.sh +else echo "export APP=`echo $URL_DOMAIN | awk -F[.-] '{print $3}'`" >> setenv.sh +fi + echo "export NAMESPACE=$APP-web" >> setenv.sh + echo "export REPO=$BITBUCKET_REPO_SLUG" >> setenv.sh echo "export REPLACE=`echo $NEW_URL | awk -F[./] '{print $4"."$5}'`" >> setenv.sh +echo "export MAIN_DOMAIN=`echo $NEW_URL | awk -F[./] '{print $4"."$5}'`" >> setenv.sh echo "export URL_DOMAIN=$URL_DOMAIN" >> setenv.sh echo "export TOKEN=$TOKEN" >> setenv.sh # --------------------------------------------------------------- @@ -42,21 +60,37 @@ cat ./setenv.sh opt=$1 deploy_version=$2 -case $opt in +# Check if it is already running # +# ------------------------------------- +aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 +echo "common-job-pod will be deleted if already there and thus make sure you run single common job for a given repo" +echo "" +if ( kubectl get po -A | grep common-job-pod ) && [[ "$SUBDOMAIN" = "www" ]] ; then +echo "common-job-pod-running and maindomain is $URL_DOMAIN and common-job-pod will not be deleted" +else +kubectl get po -A | grep common-job-pod && kubectl delete po common-job-pod -n $NAMESPACE || echo "----common-job-pod-t0-be-created" +fi + +# validation checks # +#---------------------------------- +#validation () { chmod 755 ./validation.sh && ./validation.sh; } && validation + +case $opt in wp-auto-patch) +# The auto patch to be executed for dev01 to be specific +# ---------------------------------------------------------- if [[ "$SUBDOMAIN" != "dev01" ]] ; then echo VALIDATION-NOPASS-AUTO-PATCH-IS-FOR-DEV01 && exit 9 ; else echo VALIDATION-PASS-AUTO-PATCH-IS-FOR-DEV01;fi aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 -kubectl apply -f build/$ENV/pod.tpl && sleep 15 +kubectl apply -f build/$ENV/pod.tpl && kubectl apply -f build/$ENV/pvc.yml && sleep 15 kubectl cp setenv.sh $NAMESPACE/common-job-pod:/tmp/setenv.sh kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'chmod 755 /tmp/setenv.sh' kubectl exec common-job-pod -n $NAMESPACE -- apt update kubectl exec common-job-pod -n $NAMESPACE -- apt install wget zip unzip curl git rsync -y - # --------------------------------------------------------------------------------------------------------------------------------------------------------- kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; cd /tmp; @@ -70,22 +104,29 @@ git add --all && git commit -m "auto-patch $VERSION-$ENV-$SUBDOMAIN-$(date +%Y- kubectl delete po common-job-pod -n $NAMESPACE -cat script.log | egrep -v -i "warning|SQL|create mode|rewrite" | egrep -i "fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt +cat script.log | egrep -v "SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt ;; subdomain_deploy) -echo "Deploy the wordpress source code fromo the branch to pvc mount of subdomain..." +echo "Deploy the wordpress source code from the branch to pvc mount of subdomain..." + aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 -kubectl apply -f build/$ENV/pod.tpl && sleep 15 && echo pod-created +echo "common-job-pod will be deleted if already there and thus make sure you run single common job for a given repo" +echo "" +kubectl get po -A | grep common-job-pod && kubectl delete po common-job-pod -n $NAMESPACE || echo ----common-job-pod-t0-be-created + + +kubectl apply -f build/$ENV/pod.tpl && kubectl apply -f build/$ENV/pvc.yml && sleep 15 && echo pod-created + kubectl cp wp $NAMESPACE/common-job-pod:/tmp && echo "wp copy to /tmp done" || exit 9 kubectl cp setenv.sh $NAMESPACE/common-job-pod:/tmp/setenv.sh && echo "setenv copy done" || exit 9 kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'chmod 755 /tmp/setenv.sh' kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh ; cp -rf /tmp/wp/* /usr/share/nginx/subdomain/$SUBDOMAIN/' echo $SUBDOMAIN | grep www && echo "maindomain $URL_DOMAIN" || kubectl delete po common-job-pod -n $NAMESPACE -cat script.log | egrep -v -i "warning|SQL|create mode|rewrite" | egrep -i "fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt +cat script.log | egrep -v "SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt ;; @@ -94,7 +135,7 @@ mysql-backup) echo "Running mysql backup with s3..." aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 -kubectl apply -f build/$ENV/pod.tpl && sleep 15 +kubectl apply -f build/$ENV/pod.tpl && kubectl apply -f build/$ENV/pvc.yml && sleep 15 kubectl cp setenv.sh $NAMESPACE/common-job-pod:/tmp/setenv.sh kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'chmod 755 /tmp/setenv.sh' @@ -113,14 +154,14 @@ curl --request POST -F files=@"$FILE" --url "https://api.bitbucket.org/2.0/repo kubectl delete po common-job-pod -n $NAMESPACE -cat script.log | egrep -v -i "warning|SQL|create mode|rewrite" | egrep -i "fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt +cat script.log | egrep -v "SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt ;; wp-data-backup) aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 -kubectl apply -f build/$ENV/pod.tpl && sleep 15 +kubectl apply -f build/$ENV/pod.tpl && kubectl apply -f build/$ENV/pvc.yml && sleep 15 kubectl cp setenv.sh $NAMESPACE/common-job-pod:/tmp/setenv.sh kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'chmod 755 /tmp/setenv.sh' kubectl exec common-job-pod -n $NAMESPACE -- apt update @@ -130,15 +171,13 @@ kubectl cp $NAMESPACE/common-job-pod:/tmp/upload.zip $NAMESPACE-$VERSION-$ENV-$S FILE=`ls $NAMESPACE-*.zip` aws s3 cp $FILE s3://commonjobs/$NAMESPACE/$opt/ kubectl delete po common-job-pod -n $NAMESPACE -cat script.log | egrep -v -i "warning|SQL|create mode|rewrite" | egrep -i "fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt +cat script.log | egrep -v "SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt ;; - - mysql-restore) aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 -kubectl apply -f build/$ENV/pod.tpl && sleep 15 +kubectl apply -f build/$ENV/pod.tpl && kubectl apply -f build/$ENV/pvc.yml && sleep 15 kubectl cp setenv.sh $NAMESPACE/common-job-pod:/tmp/setenv.sh kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'chmod 755 /tmp/setenv.sh' @@ -152,7 +191,6 @@ kubectl exec common-job-pod -n $NAMESPACE -- apt install zip wget unzip curl mar curl -s -L -O -H "Authorization: Bearer $TOKEN" https://api.bitbucket.org/2.0/repositories/connectamerica/$REPO/downloads/$FILE && echo file_downloaded || aws s3 cp s3://commonjobs/$NAMESPACE/mysql-backup/$FILE $FILE - kubectl cp $FILE $NAMESPACE/common-job-pod:/tmp/$FILE #Create schema if not present # @@ -161,29 +199,41 @@ kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; mysql -h $ kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; export FILE=`ls /tmp/*.sql.gz` ; gunzip -c $FILE | mysql -h $DB_HOST --user=$DB_USER --password=$DB_PASSWORD $SUBDOMAIN' kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar ; chmod u+x wp-cli.phar;mv wp-cli.phar /usr/local/bin/wp; php /usr/local/bin/wp --info' - # ---------------------------------------------------------------------------------------------------------------------------------------------------- -kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; OLD_URL=`wp option get siteurl --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN`; echo "export OLD_URL=$OLD_URL" >> /tmp/setenv.sh; echo "export FIND=$(echo $OLD_URL|cut -d. -f2,3)" >> /tmp/setenv.sh; echo "export OLD_URL_DOMAIN=$(echo $OLD_URL| cut -d/ -f3)" >> /tmp/setenv.sh ' - -kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'cat /tmp/setenv.sh ; . /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN "$FIND" "$REPLACE" --all-tables' - -kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; OLD_URL=`wp option get siteurl --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN`; echo "export OLD_URL=$OLD_URL" >> /tmp/setenv.sh; echo "export FIND=$(echo $OLD_URL|cut -d. -f2,3)" >> /tmp/setenv.sh; echo "export OLD_URL_DOMAIN=$(echo $OLD_URL| cut -d/ -f3)" >> /tmp/setenv.sh ' - +kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; OLD_URL=`wp option get siteurl --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN`; echo "export OLD_URL=$OLD_URL" >> /tmp/setenv.sh; echo "export FIND=$(echo $OLD_URL|cut -d. -f2,3)" >> /tmp/setenv.sh; echo "export OLD_MAIN_DOMAIN=$(echo $OLD_URL|cut -d. -f2,3)" >> /tmp/setenv.sh; echo "export OLD_URL_DOMAIN=$(echo $OLD_URL| cut -d/ -f3)" >> /tmp/setenv.sh; echo "export OLD_SUBDOMAIN=$(echo $OLD_URL| cut -d/ -f3|cut -d. -f1)" >> /tmp/setenv.sh' +# ----------------------------------------------------------------------------------------- +kubectl cp $NAMESPACE/common-job-pod:/tmp/setenv.sh setenv.sh && . ./setenv.sh +# ----------------------------------------------------------------------------------------- +if [[ "$OLD_SUBDOMAIN" = "www" ]] && [[ "$SUBDOMAIN" = "www" ]]; then kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'cat /tmp/setenv.sh ; . /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN "$OLD_URL_DOMAIN" "$URL_DOMAIN" --all-tables' -# -------------------------------------------------------------------------------------------- +kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'cat /tmp/setenv.sh ; . /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN ".$OLD_MAIN_DOMAIN" ".$MAIN_DOMAIN" --all-tables' +elif [[ "$OLD_SUBDOMAIN" = "www" ]] && [[ "$SUBDOMAIN" != "www" ]]; then +kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'cat /tmp/setenv.sh ; . /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN "$OLD_URL_DOMAIN" "$URL_DOMAIN" --all-tables' +kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'cat /tmp/setenv.sh ; . /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN ".$OLD_MAIN_DOMAIN" ".$URL_DOMAIN" --all-tables' + +elif [[ "$OLD_SUBDOMAIN" != "www" ]] && [[ "$SUBDOMAIN" = "www" ]]; then +kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'cat /tmp/setenv.sh ; . /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN ".$OLD_URL_DOMAIN" ".$MAIN_DOMAIN" --all-tables' +kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'cat /tmp/setenv.sh ; . /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN "$OLD_URL_DOMAIN" "$URL_DOMAIN" --all-tables' + +elif [[ "$OLD_SUBDOMAIN" != "www" ]] && [[ "$SUBDOMAIN" != "www" ]]; then +kubectl exec common-job-pod -n $NAMESPACE -- sh -c 'cat /tmp/setenv.sh ; . /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN "$OLD_URL_DOMAIN" "$URL_DOMAIN" --all-tables' + +else +echo "======== fix the issues =============" +fi + +# ---------------------------------------------------------------------------------------------------------------------------------------- kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; wp search-replace --allow-root --path=/usr/share/nginx/subdomain/$SUBDOMAIN "http://$URL_DOMAIN" "$NEW_URL" --all-tables' - - kubectl delete po common-job-pod -n $NAMESPACE -cat script.log | egrep -v -i "warning|SQL|create mode|rewrite" | egrep -i "fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt +cat script.log | egrep -v "SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt ;; wp-data-restore) aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 -kubectl apply -f build/$ENV/pod.tpl && sleep 60 || exit 9 +kubectl apply -f build/$ENV/pod.tpl && kubectl apply -f build/$ENV/pvc.yml && sleep 15 kubectl get po common-job-pod -n $NAMESPACE | grep -i pending && exit 9 || echo ----common-pod-NOT-in-pending-state -- kubectl cp setenv.sh $NAMESPACE/common-job-pod:/tmp/setenv.sh @@ -198,7 +248,7 @@ kubectl cp $FILE $NAMESPACE/common-job-pod:/tmp/$FILE kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; mkdir -p /usr/share/nginx/subdomain/$SUBDOMAIN/wp-content/uploads' kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh;export FILE=`ls /tmp/*.zip`;unzip -o $FILE -d /usr/share/nginx/subdomain/$SUBDOMAIN/wp-content/uploads' kubectl delete po common-job-pod -n $NAMESPACE -cat script.log | egrep -v -i "warning|SQL|create mode|rewrite" | egrep -i "fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt +cat script.log | egrep -v "SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt ;; diff --git a/validation.sh b/validation.sh new file mode 100644 index 00000000..54020713 --- /dev/null +++ b/validation.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +export DIR=`pwd` +exec > >(tee -a $DIR/script.log) 2>&1 + +function install { + +# kubectl +# ---------------- +curl -fsSLO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl +chmod +x ./kubectl +mv ./kubectl /usr/local/bin/kubectl +# setenv +# ----------- +. ./setenv.sh + +# krew +# --------- +set -x; cd "$(mktemp -d)" && +OS="$(uname | tr '[:upper:]' '[:lower:]')" && +ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" && +KREW="krew-${OS}_${ARCH}" && +curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" && +tar zxvf "${KREW}.tar.gz" && +./"${KREW}" install krew && +export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" >> ~/.bashrc && . ~/.bashrc + +# kubeval +# ------------------- +curl -fsSLO https://github.com/instrumenta/kubeval/releases/latest/download/kubeval-linux-amd64.tar.gz +tar xf kubeval-linux-amd64.tar.gz && +cp kubeval /usr/local/bin + +# kube-score +# --------------- +kubectl krew install score + +} + +install + +default () { + +cd $DIR/build +find . -type f -exec kubeval '{}' \; | tee -a $DIR/script.log + +find . -type f -exec kubectl score --output-format ci '{}' \; | tee -a $DIR/script.log + +cat $DIR/script.log | egrep -v -i "script.log|warning|SQL|create mode|rewrite" | egrep -i "fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file|ERR" && exit 9 || echo VALIDATION_PASS_$opt + +} + + +opt=$1 + +case $opt in + +default) + +default +;; + +env) + +env +;; + + *) + echo "validation job shell script utility taking variables from env" + echo "Usage: $0 {default|env}" + +esac + + + + + + + +# ------------------------------------------------------------------------------------------------------------------------------------------- +#cat $DIR/script.log | egrep -v -i "script.log|warning|SQL|create mode|rewrite" | egrep -i "fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file|ERR" && exit 9 || echo VALIDATION_PASS_$opt +# -------------------------------------------------------------------------------------------- +