<commit_before>ssh_options = '-o StrictHostKeyChecking=no -o ForwardAgent=yes'

def getHostname(String inventory) {
    bool section = false
    String hostname

    for (line in inventory.readLines()) {
        if (line == '[management]') {
           section = true
           continue
        }
        if (section) {
           matcher = (line =~ /^\w+/)
           hostname = matcher[0]
           break
        }
    }
    return hostname
}

def deploy() {
    playbooks = [
        'resolution.yml',
        'cluster.yml',
        'validate.yml',
        'examples.yml'
    ]

    def inventory = new File('cluster.status').text
    def hostname = getHostname(inventory)
    echo "Start deploy stage on ${hostname}"

    for (String playbook : playbooks) {
    	echo "playbook ${playbook}"
        sh "ssh ${ssh_options} ${hostname} ansible-playbook -i src/contrib/ansible/inventory ${playbook}"
    }
}

def guestbook_status() {
    sh "ssh %{ssh_options} curl http://172.16.0.252:3000/info > guestbook.status"
}

test_ec2_k8s_basic = {
    node {
        // git url: 'https://github.com/Juniper/container-networking-ansible.git'
        dir('test/ec2-k8s') {
            withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'k8s-provisioner', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY']]) {
                // create cluster
                sh "ansible-playbook -i localhost playbook.yml --tags=create -e job_id=${env.BUILD_NUMBER}"
            }

            try {
                sshagent(credentials: ["k8s"]) {
                    sh 'ansible-playbook -i cluster.status playbook.yml --tags=deployer-install'
                    sh 'ansible-playbook -i cluster.status playbook.yml --tags=workspace'
                    // ssh client steps
                    deploy()

                    // verify
                    guestbook_status()
                }
            } finally {
                withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'k8s-provisioner', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY']]) {
                    // delete cluster
                    sh 'ansible-playbook -i cluster.status clean.yml'
                }
            }
        }
    }
}

test_noop = {
    node {
        // jenkins-<JOB_NAME>-<BUILD_NUMBER>
        echo env.BUILD_TAG
    }
}

def getTestMatrix() {
    tests = [
        ec2_k8s_basic: test_ec2_k8s_basic,
        noop: test_noop,
    ]<commit_msg>Deployer is required for application validation stage also.<commit_after>ssh_options = '-o StrictHostKeyChecking=no -o ForwardAgent=yes'

def getDeployerHostname() {
    def inventory = new File('cluster.status').text

    bool section = false
    String hostname

    for (line in inventory.readLines()) {
        if (line == '[management]') {
           section = true
           continue
        }
        if (section) {
           matcher = (line =~ /^\w+/)
           hostname = matcher[0]
           break
        }
    }
    return hostname
}

def deploy(deployer) {
    playbooks = [
        'resolution.yml',
        'cluster.yml',
        'validate.yml',
        'examples.yml'
    ]

    echo "Start deploy stage on ${deployer}"

    for (playbook in playbooks) {
    	echo "playbook ${playbook}"
        sh "ssh ${ssh_options} ${deployer} ansible-playbook -i src/contrib/ansible/inventory ${playbook}"
    }
}

def guestbook_status(deployer) {
    sh "ssh ${ssh_options} ${deployer} curl http://172.16.0.252:3000/info > guestbook.status"
}

test_ec2_k8s_basic = {
    node {
        // git url: 'https://github.com/Juniper/container-networking-ansible.git'
        dir('test/ec2-k8s') {
            withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'k8s-provisioner', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY']]) {
                // create cluster
                sh "ansible-playbook -i localhost playbook.yml --tags=create -e job_id=${env.BUILD_NUMBER}"
            }

	    def deployer = getDeployerHostname()

            try {
                sshagent(credentials: ["k8s"]) {
                    sh 'ansible-playbook -i cluster.status playbook.yml --tags=deployer-install'
                    sh 'ansible-playbook -i cluster.status playbook.yml --tags=workspace'
                    // ssh client steps
                    deploy(deployer)

                    // verify
                    guestbook_status(deployer)
                }
            } finally {
                withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'k8s-provisioner', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY']]) {
                    // delete cluster
                    sh 'ansible-playbook -i cluster.status clean.yml'
                }
            }
        }
    }
}

test_noop = {
    node {
        // jenkins-<JOB_NAME>-<BUILD_NUMBER>
        echo env.BUILD_TAG
    }
}

def getTestMatrix() {
    tests = [
        ec2_k8s_basic: test_ec2_k8s_basic,
        noop: test_noop,
    ]
{'fraction_non_alphanumeric': 0.11619307123394317, 'fraction_numerical': 0.00973141300116777, 'mean_word_length': 1.9215463331438318, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '725889', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1527, 'n_words': 305}