diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 0a565c0e..741d88e8 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -3,7 +3,38 @@ definitions: - variables: - name: VERSION default: "latest" +# -------------------------------------------------------- + set-variables-jobs: &set-variables-schedule-pipelines + - variables: + - name: ACTION + default: "create-schedule-pipelines" + allowed-values: # Optionally restrict variable values + - "create-schedule-pipelines" + - "delete-schedule-pipelines" + - "list-schedule-pipelines" + - name: PIPELINE_NAME + default: "common-jobs-dev" + allowed-values: # Optionally restrict variable values + - "backup-mysql-monthly-prod" + - "backup-static-monthly-prod" + - "cleanup-backup-monthly-prod" + - "common-jobs-dev" # for patch with default values + - name: BRANCH_NAME + default: "feature/backups" + allowed-values: # Optionally restrict variable values + - "develop" + - "feature/backups" + - "feature/backup" + - name: CRONTIME + default: ENTER_CRON_SCHEDULE + # ------------------------------------------------------- +# allowed-values: # Optionally restrict variable values +# - "0 * * * 1 ? *" # On 1st every month +# - "0 * * * * 7 *" # Every sunday +# - "" +# - "ENTER_CRON_SCHEDULE" +# ---------------------------------------------------- set-variables-jobs: &set-variables-jobs-dev - variables: - name: ENV @@ -11,7 +42,7 @@ definitions: allowed-values: # Optionally restrict variable values - "dev" - name: JOBNAME - default: "mysql-backup" + default: "wp-auto-patch" allowed-values: # Optionally restrict variable values - "wp-auto-patch" - "mysql-backup" @@ -41,7 +72,6 @@ definitions: - name: JOBNAME default: "mysql-backup" allowed-values: # Optionally restrict variable values - - "wp-auto-patch" - "mysql-backup" - "wp-data-backup" - "mysql-restore" @@ -111,6 +141,90 @@ definitions: pipelines: # our custom pipeline, what shows up Bitbucket web app custom: + +# ------------------------------------------- + schedule-pipelines: + - <<: *set-variables-schedule-pipelines + - step: + name: vars for schedule-pipelines + script: + - echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> schedule-setenv.sh + - echo "export BITBUCKET_WORKSPACE=$BITBUCKET_WORKSPACE" >> schedule-setenv.sh + - echo "export TOKEN=$TOKEN" >> schedule-setenv.sh + - echo "export ACTION=$ACTION" >> schedule-setenv.sh + - echo "export PIPELINE_NAME=$PIPELINE_NAME" >> schedule-setenv.sh + - echo "export BRANCH_NAME=$BRANCH_NAME" >> schedule-setenv.sh + - echo "export CRONTIME=\"$CRONTIME\"" >> schedule-setenv.sh + artifacts: # define the artifacts to be passed to each future step + - schedule-setenv.sh + + - step: + name: Actions for schedule-pipelines + image: atlassian/pipelines-awscli + script: + - cat schedule-setenv.sh && chmod 755 ./schedule-pipeline.sh && ./schedule-pipeline.sh $ACTION +# ------------------------------------------------- + + backup-mysql-monthly-prod: + - step: + name: vars for common-jobs-prod + script: + - echo "export JOBNAME=mysql-backup" >> setenv.sh + - echo "export ENV=prod" >> setenv.sh + - echo "export CLUSTER=caresage-eks-cluster-prod" >> setenv.sh + - echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh + - echo "export SUBDOMAIN=www" >> 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 + - ./common-jobs-bitbucket.sh $JOBNAME + + backup-static-monthly-prod: + - step: + name: vars for common-jobs-prod + script: + - echo "export JOBNAME=wp-data-backup" >> setenv.sh + - echo "export ENV=prod" >> setenv.sh + - echo "export CLUSTER=caresage-eks-cluster-prod" >> setenv.sh + - echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh + - echo "export SUBDOMAIN=www" >> 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 && ./common-jobs-bitbucket.sh $JOBNAME + + cleanup-backup-monthly-prod: + - step: + name: vars for common-jobs-prod + script: + - echo "export JOBNAME=cleanup-backup" >> setenv.sh + - echo "export ENV=prod" >> setenv.sh + - echo "export CLUSTER=caresage-eks-cluster-prod" >> setenv.sh + - echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh + - echo "export SUBDOMAIN=www" >> 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 && ./common-jobs-bitbucket.sh $JOBNAME deploy-dev: - <<: *set-variables - step: diff --git a/common-jobs-bitbucket.sh b/common-jobs-bitbucket.sh index 681999ae..68c5c045 100644 --- a/common-jobs-bitbucket.sh +++ b/common-jobs-bitbucket.sh @@ -4,7 +4,7 @@ exec > >(tee -a script.log) 2>&1 # Install the required packages # # -------------------------------------------------------------- -apk upgrade && apk add curl wget zip unzip +apk upgrade && apk add curl wget zip unzip coreutils 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 @@ -77,6 +77,15 @@ deploy_version=$2 case $opt in +cleanup-backup) + +aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1 && . ./setenv.sh && chmod 755 ./s3cleanup.sh + +./s3cleanup.sh && echo list-of-files-older-than-thirty-days-done || exit 9 + +cat script.log | egrep -v "Errors:|SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|Permission denied|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt + +;; wp-auto-patch) # The auto patch to be executed for dev01 to be specific @@ -288,9 +297,7 @@ kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; mkdir -p / kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; mkdir /tmp/uploads ;export FILE=`ls /tmp/*.zip`;unzip -o $FILE -d /tmp/uploads' kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; rclone sync /tmp/uploads/ /usr/share/nginx/subdomain/$SUBDOMAIN/wp-content/uploads -q && echo ---sync-done--- ' -# --------------------------------------------------------------------------------------------------------------------------------------------------- -kubectl exec common-job-pod -n $NAMESPACE -- sh -c '. /tmp/setenv.sh; mkdir /usr/share/nginx/subdomain/$SUBDOMAIN/wp-content/uploads/cache && echo ---CacheCreated---' -# -------------------------------------------------------------------------------------------------------------------------------------------- +#------------------------------------------------------------------------------------------------------------------------------------------ kubectl delete po common-job-pod -n $NAMESPACE cat script.log | egrep -v "Errors:|inflating|adding|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/schedule-pipeline.sh b/schedule-pipeline.sh new file mode 100644 index 00000000..b8ef6e64 --- /dev/null +++ b/schedule-pipeline.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +exec > >(tee -a script.log) 2>&1 + +# Install the required packages # +# -------------------------------------------------------------- +apk upgrade && apk add --update curl wget zip unzip coreutils + +cat schedule-setenv.sh && . ./schedule-setenv.sh + +opt=$1 + +case $opt in + +create-schedule-pipelines) + + +# ----------------------------------------------------------------------------------------------------------------------------------------------------- +#curl --request POST --url "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pipelines_config/schedules" --header "Authorization: Bearer $TOKEN" --header 'Accept: application/json' --header "Content-Type: application/json" --data { \"type\": \"pipeline_schedule\", \"enabled\": true, \"target\": { \"ref_type\": \"branch\", \"type\": \"pipeline_ref_target\", \"ref_name\": \"$BRANCH_NAME\", \"selector\": { \"type\": \"custom\", \"pattern\": \"$PIPELINE_NAME\" } }, \"cron_pattern\": \"$CRONTIME\" } +# ----------------------------------------------------------------------------------- + + +curl -X POST https://api.bitbucket.org/2.0/repositories/connectamerica/100plus-web/pipelines_config/schedules -H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization: Bearer $TOKEN" -d "{ + \"type\": \"pipeline_schedule\", + \"enabled\": true, + \"target\": { + \"ref_type\": \"branch\", + \"type\": \"pipeline_ref_target\", + \"ref_name\": \"$BRANCH_NAME\", + \"selector\": { + \"type\": \"custom\", + \"pattern\": \"$PIPELINE_NAME\" + } + }, + \"cron_pattern\": \"$CRONTIME\" + }" + + + +echo "" && echo ----------------------------------- + +cat script.log | egrep -v "Errors:|SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|Permission denied|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt + +;; + +delete-schedule-pipelines) + +for i in `curl -s --request GET --url "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pipelines_config/schedules" --header "Authorization: Bearer $TOKEN" --header 'Accept: application/json'| jq ".values[].target.selector.pattern+.values[].uuid" |awk -F'["{}]' '{print $3}'| uniq` + +do + +echo curl --location -g --request DELETE --url \'https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pipelines_config/schedules/{$i}\' --header \'Authorization: Bearer $TOKEN\' > delete.sh + +chmod 755 delete.sh && ./delete.sh + +done + +cat script.log | egrep -v "Errors:|SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|Permission denied|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt + +;; + + +list-schedule-pipelines) + +echo "" && echo ---------------------------------------------- + +curl -s --request GET --url "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pipelines_config/schedules" --header "Authorization: Bearer $TOKEN" --header 'Accept: application/json' | jq . + +echo "" + +cat script.log | egrep -v "Errors:|SQL|rewrite|Rewrite|warning|Warning|create mode" | egrep -i "not in gzip format|Permission denied|fail|fatal|exception|terminated|command not found|error|unexpected|cannot access|No such file" && exit 9 || echo VALIDATION_PASS_$opt + +;; + + *) + echo "script for schedule-pipelines" + echo "Usage: schedule-pipeline.sh {create-schedule-pipelines|delete-schedule-pipelines}" + echo "create-schedule-pipelines : create-schedule-pipelines" + echo "delete-schedule-pipelines : delete-schedule-pipelines" + echo "list-schedule-pipelines : list-schedule-pipelines" + + ;; +esac