pipeline {
    agent any
        environment {
            setupResultsOut = "setup-results.xml"
            testResultsOut = "results.xml"

            AWS_ACCESS_KEY_ID     = credentials('AWS_ACCESS_KEY_ID')
            AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
            AWS_SSH_PEM_KEY = credentials('AWS_SSH_PEM_KEY')
        }

        stages {
            stage('Git Checkout') {
                steps {
                    git branch: 'master', url: 'https://github.com/k3s-io/k3s.git'
                    script {
                        dir("${WORKSPACE}/tests/terraform") {
                            if (env.AWS_SSH_PEM_KEY && env.AWS_SSH_KEY_NAME) {
                                    def decoded = new String(AWS_SSH_PEM_KEY.decodeBase64())
                                    writeFile file: AWS_SSH_KEY_NAME, text: decoded
                            }
                        }
                    }
                }
            }

            stage('Configure') {
                steps {
                    sh """
                    set -e -x
                    echo 'aws_ami="${env.AWS_AMI}"
                        aws_user="${env.AWS_USER}"
                        region="${env.REGION}"
                        vpc_id="${env.VPC_ID}"
                        subnets="${env.SUBNETS}"
                        qa_space="${env.QA_SPACE}"
                        ec2_instance_class="${env.AWS_INSTANCE_TYPE}"
                        access_key="/config/$AWS_SSH_KEY_NAME"
                        no_of_worker_nodes="${env.NO_OF_WORKER_NODES}"
                        key_name="jenkins-rke-validation"
                        server_flags="${env.K3S_SERVER_FLAGS}"
                        worker_flags="${env.K3S_WORKER_FLAGS}"
                        k3s_version="${env.K3S_VERSION}"
                        availability_zone="${env.AVAILABILITY_ZONE}"
                        sg_id="${env.SG_ID}"
                        install_mode="${env.K3S_INSTALL_MODE}"
                        resource_name="${env.RESOURCE_NAME}"
                        no_of_server_nodes="${env.NO_OF_SERVER_NODES}"
                        username="${env.RHEL_USERNAME}"
                        password="${env.RHEL_PASSWORD}"
                        db_username="${env.DB_USERNAME}"
                        db_password="${env.DB_PASSWORD}"
                        node_os="${env.NODE_OS}"
                        environment="${env.DB_ENVIRONMENT}"
                        engine_mode="${env.DB_ENGINE_MODE}"
                        external_db="${env.EXTERNAL_DB}"
                        external_db_version="${env.EXTERNAL_DB_VERSION}"
                        instance_class="${env.DB_INSTANCE_CLASS}"
                        db_group_name="${env.DB_GROUP_NAME}"
                        cluster_type="${env.CLUSTER_TYPE}"
                        create_lb=${env.CREATE_LB}
                        ' >${WORKSPACE}/tests/terraform/${env.NODE_OS}${env.EXTERNAL_DB}".tfvars"
                    """
                }
            }
            stage('Build Cluster') {
                steps {
                    sh """

                        /usr/bin/docker build -f tests/terraform/Dockerfile.build -t k3s_create_cluster .

                        /usr/bin/docker run -d --name ${RESOURCE_NAME}_${BUILD_NUMBER} -v ${WORKSPACE}/tests/terraform:/config \
                        -t -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
                        k3s_create_cluster

                        /usr/bin/docker cp "${WORKSPACE}/tests/terraform/${NODE_OS}${EXTERNAL_DB}".tfvars "${RESOURCE_NAME}_${BUILD_NUMBER}":/config

                        /usr/bin/docker cp "${WORKSPACE}/tests/terraform/$AWS_SSH_KEY_NAME" "${RESOURCE_NAME}_${BUILD_NUMBER}":/config

                        /usr/bin/docker exec ${RESOURCE_NAME}_${BUILD_NUMBER} /usr/local/go/bin/go test -v tests/terraform/createcluster.go \
                        tests/terraform/createcluster_test.go tests/terraform/testutils.go \
                        -timeout=1h -node_os=${NODE_OS} \
                        -cluster_type=${CLUSTER_TYPE} -external_db=${EXTERNAL_DB} -resource_name=${RESOURCE_NAME} \
                        -sshuser=${AWS_USER} -sshkey="/config/${AWS_SSH_KEY_NAME}" -destroy=false -arch=${ARCH}

                    """
                }
            }

            stage('Test Report') {
                steps {
                    sh """
                        /usr/bin/docker rm -f ${RESOURCE_NAME}_${BUILD_NUMBER}
                        /usr/bin/docker rmi -f k3s_create_cluster
                    """
                }
            }
        }
    }