// Pipeline script for: backup-cloudsearch job
String SERVICE_NAME = "backup-cloudsearch"

String QA_ACCOUNT_ID = "437795906767"
String RUN_BACKUP_CLOUDSEARCH_TASK_ROLE = "backup-cloudsearch-task-run-role"
String SESSION_NAME = "backup-cloudsearch-jenkins-scheduler"
String AWS_REGION = "us-east-1"
String SLACK_NOTIFICATIONS_CHANNEL = "#kafka-data-highway-alerts"

pipeline {
    agent any
    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
    }
    parameters {
        string(name: 'GLOBAL_LIBRARIES_VERSION', defaultValue: 'master', description: 'The version of the Jenkins global libraries to use. Can be a branch, tag or Git revision.')
        choice(name: 'Environment', choices: ['qa', 'prod'], description: 'Environment for the script.')
        choice(name: 'DOMAIN_NAME', choices: ['prod-etl-releases', 'prod-etl-projects','prod-etl-labels', 'prod-etl-tracks'], description: 'Choose the CS Domain that you want to copy.')
        string(name: 'MAX_ROWS', defaultValue: '', description: 'Set this to limit number of rows processed.')
        string(name: 'RANGE_FILTER_FIELD', defaultValue: '', description: 'CS Field name eg: release_id that you want to filter by.')
        string(name: 'RANGE_FILTER_START', defaultValue: '', description: 'Start value for the range filter. This id/value is included.')
        string(name: 'RANGE_FILTER_END', defaultValue: '', description: 'End value for the range filter. This id/value is excluded. If left blank it will take all values from start.')
    }
    stages {
        stage('Load Global Libraries') {
            steps {
                library "jenkins-global-libraries@${params.GLOBAL_LIBRARIES_VERSION}"
            }
        }
        stage('Run script') {
            environment {
                ACCOUNT_ID = "${QA_ACCOUNT_ID}"
                RUN_TASK_ROLE = "${params.Environment}-${RUN_BACKUP_CLOUDSEARCH_TASK_ROLE}"
                SERVICE_NAME = "${SERVICE_NAME}"
                CLUSTER_NAME = "${params.Environment}-${SERVICE_NAME}"
                VERIFY_MODE = "EXIT_CODE"
            }
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']],
                    doGenerateSubmoduleConfigurations: false, extensions: [],
                    submoduleCfg: [], userRemoteConfigs: [
                        [credentialsId: '7c29b204-9e5b-45e0-bd0a-6535917c604f', url: "git@github.com:theorchard/python-deployment-utils.git"]
                    ]])

                withEnv(["AWS_DEFAULT_REGION=${AWS_REGION}",]) {
                    withAWS(
                        role:  "${params.Environment}-${RUN_BACKUP_CLOUDSEARCH_TASK_ROLE}",
                        roleAccount: QA_ACCOUNT_ID,
                        roleSessionName: "${params.Environment}-${SESSION_NAME}",
                        useNode: true
                    ) {
                        run_task()
                    }
                }
            }
        }
    }

    post {
        regression {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        fixed {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
    }
}

def run_task() {
    dir('fargate') {
        sh """
        set +x

        echo "ENVIRONMENT is \${Environment}"
        echo "VERIFY_MODE is \${VERIFY_MODE}"
        echo "ACCOUNT_ID is \${ACCOUNT_ID}"
        echo "RUN_TASK_ROLE is \${RUN_TASK_ROLE}"
        echo "CLUSTER_NAME is \${CLUSTER_NAME}"
        echo "SERVICE_NAME is \${SERVICE_NAME}"

        python3 -m venv venv
        . ./venv/bin/activate

        pip install -r requirements.txt

        set -x

        python -u run_fargate_task.py --environment "Environment=${params.Environment},DOMAIN_NAME=${DOMAIN_NAME},MAX_ROWS=${MAX_ROWS},RANGE_FILTER_FIELD=${RANGE_FILTER_FIELD},RANGE_FILTER_START=${RANGE_FILTER_START},RANGE_FILTER_END=${RANGE_FILTER_END}"
        """
    }
}
