/**
 *
 * Promote MCP
 *
 * Expected parameters:
 *   SOURCE_REVISION
 *   TARGET_REVISION
 *   RELEASE_APTLY
 *   RELEASE_DEB_MIRRORS
 *   RELEASE_DOCKER
 *   RELEASE_GIT
 *   APTLY_URL
 *   APTLY_STORAGES
 *   DOCKER_CREDENTIALS
 *   DOCKER_URL
 *   DOCKER_IMAGES
 *   GIT_CREDENTIALS
 *   GIT_REPO_LIST
 *   VCP_IMAGE_LIST - list of images
 *   SYNC_VCP_IMAGE_TO_S3 - boolean
 *   RELEASE_VCP_IMAGES - boolean
 *   EMAIL_NOTIFY
 *   NOTIFY_RECIPIENTS
 *
 */

common = new com.mirantis.mk.Common()

syncVcpImagesToS3 = env.SYNC_VCP_IMAGE_TO_S3 ?: false
emailNotify = env.EMAIL_NOTIFY ?: false

def triggerAptlyPromoteJob(aptlyUrl, components, diffOnly, dumpPublish, packages, recreate, source, storages, target) {
    build job: "aptly-promote-all-testing-stable", parameters: [
        [$class: 'StringParameterValue', name: 'APTLY_URL', value: aptlyUrl],
        [$class: 'StringParameterValue', name: 'COMPONENTS', value: components],
        [$class: 'BooleanParameterValue', name: 'DIFF_ONLY', value: diffOnly],
        [$class: 'BooleanParameterValue', name: 'DUMP_PUBLISH', value: dumpPublish],
        [$class: 'StringParameterValue', name: 'PACKAGES', value: packages],
        [$class: 'BooleanParameterValue', name: 'RECREATE', value: recreate],
        [$class: 'StringParameterValue', name: 'SOURCE', value: source],
        [$class: 'StringParameterValue', name: 'STORAGES', value: storages],
        [$class: 'StringParameterValue', name: 'TARGET', value: target],
    ]
}

def triggerDockerMirrorJob(dockerCredentials, dockerRegistryUrl, targetTag, imageList, sourceImageTag) {
    build job: "docker-images-mirror", parameters: [
        [$class: 'StringParameterValue', name: 'TARGET_REGISTRY_CREDENTIALS_ID', value: dockerCredentials],
        [$class: 'StringParameterValue', name: 'REGISTRY_URL', value: dockerRegistryUrl],
        [$class: 'StringParameterValue', name: 'IMAGE_TAG', value: targetTag],
        [$class: 'TextParameterValue', name: 'IMAGE_LIST', value: imageList],
        [$class: 'StringParameterValue', name: 'SOURCE_IMAGE_TAG', value: sourceImageTag],
    ]
}

def triggerMirrorRepoJob(snapshotId, snapshotName) {
    build job: "mirror-snapshot-name-all", parameters: [
        [$class: 'StringParameterValue', name: 'SNAPSHOT_NAME', value: snapshotName],
        [$class: 'StringParameterValue', name: 'SNAPSHOT_ID', value: snapshotId],
    ]
}

def triggerEbfRepoJob(snapshotId, snapshotName) {
    build job: "ebf-snapshot-name-all", parameters: [
        [$class: 'StringParameterValue', name: 'SNAPSHOT_NAME', value: snapshotName],
        [$class: 'StringParameterValue', name: 'SNAPSHOT_ID', value: snapshotId],
    ]
}

def triggerGitTagJob(gitRepoList, gitCredentials, tag, sourceTag) {
    // There is no `nightly` and `testing` build-IDs` in release process
    // for git repos
    if (sourceTag in ['nightly', 'testing']) {
        sourceTag = 'master'
    }
    build job: "tag-git-repos-all", parameters: [
        [$class: 'TextParameterValue', name: 'GIT_REPO_LIST', value: gitRepoList],
        [$class: 'StringParameterValue', name: 'GIT_CREDENTIALS', value: gitCredentials],
        [$class: 'StringParameterValue', name: 'TAG', value: tag],
        [$class: 'StringParameterValue', name: 'SOURCE_TAG', value: sourceTag],
    ]
}

def triggerPromoteVCPJob(VcpImageList, tag, sourceTag) {
    build job: "promote-vcp-images-all", parameters: [
        [$class: 'TextParameterValue', name: 'VCP_IMAGE_LIST', value: VcpImageList],
        [$class: 'StringParameterValue', name: 'TAG', value: tag],
        [$class: 'StringParameterValue', name: 'SOURCE_TAG', value: sourceTag],
        [$class: 'BooleanParameterValue', name: 'FORCE_OVERWRITE', value: true],
    ]
}

def triggerPkgPromoteJob(PkgRepoList, PromoteFrom, PromoteTo) {
    def repos = PkgRepoList.trim().tokenize()
    def RepoName, RepoDist, PackagesToPromote
    for (repo in repos) {
        if (repo.startsWith('#')) {
            common.warningMsg("Skipping repo ${repo}")
            continue
        }
        if (repo.trim().indexOf(' ') == -1) {
            throw new IllegalArgumentException("Wrong format of repository and commit input")
        }
        repoArray = repo.trim().tokenize(' ')
        RepoName = repoArray[0]
        RepoDist = repoArray[1]
        PackagesToPromote = repoArray[2]
        build job: "pkg-promote", parameters: [
            [$class: 'ChoiceParameterValue', name: 'repoName', value: RepoName],
            [$class: 'ChoiceParameterValue', name: 'repoDist', value: RepoDist],
            [$class: 'ChoiceParameterValue', name: 'promoteFrom', value: PromoteFrom],
            [$class: 'ChoiceParameterValue', name: 'promoteTo', value: PromoteTo],
            [$class: 'TextParameterValue', name: 'packagesToPromote', value: PackagesToPromote],
        ]
    }
}

def triggerSyncVCPJob(VcpImageList, targetTag) {
    // Operation must be synced with triggerPromoteVCPJob procedure!
    def images = VcpImageList.trim().tokenize()
    TargetVcpImageList = ''
    for (image in images) {
        if (image.startsWith('#')) {
            common.warningMsg("Skipping image ${image}")
            continue
        }
        common.infoMsg("Replacing SUBS_SOURCE_VCP_IMAGE_TAG => ${targetTag}")
        TargetVcpImageList += image.replace('SUBS_SOURCE_VCP_IMAGE_TAG', targetTag) + '\n' +
            image.replace('SUBS_SOURCE_VCP_IMAGE_TAG', targetTag).trim() + '.md5' + '\n'

    }
    build job: "upload_images_to_s3", parameters: [
        [$class: 'TextParameterValue', name: 'FILENAMES',
         value : TargetVcpImageList]
    ]
}

timeout(time: 12, unit: 'HOURS') {
    node() {
        try {
            stage("Promote") {
                if (RELEASE_APTLY.toBoolean()) {
                    common.infoMsg("Promoting Aptly")
                    triggerAptlyPromoteJob(APTLY_URL, 'all', false, true, 'all', false, "(.*)/${SOURCE_REVISION}", APTLY_STORAGES, "{0}/${TARGET_REVISION}")
                }

                if (PKG_PROMOTE.toBoolean() && SOURCE_REVISION == 'testing') {
                    common.infoMsg("Promoting Extra and Ceph packages")
                    triggerPkgPromoteJob(PKG_REPO_LIST, SOURCE_REVISION, TARGET_REVISION)
                }

                if (RELEASE_DEB_MIRRORS.toBoolean()) {
                    common.infoMsg("Promoting Debmirrors")
                    triggerMirrorRepoJob(SOURCE_REVISION, TARGET_REVISION)
                }

                if (RELEASE_EBF_MIRRORS.toBoolean()) {
                    common.infoMsg("Promoting Emergency Bug Fix Debmirrors")
                    triggerEbfRepoJob(SOURCE_REVISION, TARGET_REVISION)
                }

                if (RELEASE_DOCKER.toBoolean()) {
                    common.infoMsg("Promoting Docker images")
                    triggerDockerMirrorJob(DOCKER_CREDENTIALS, DOCKER_URL, TARGET_REVISION, DOCKER_IMAGES, SOURCE_REVISION)
                }

                if (RELEASE_GIT.toBoolean()) {
                    common.infoMsg("Promoting Git repositories")
                    triggerGitTagJob(GIT_REPO_LIST, GIT_CREDENTIALS, TARGET_REVISION, SOURCE_REVISION)

                }
                if (RELEASE_VCP_IMAGES.toBoolean()) {
                    common.infoMsg("Promoting VCP images")
                    triggerPromoteVCPJob(VCP_IMAGE_LIST, TARGET_REVISION, SOURCE_REVISION)

                }
                if (syncVcpImagesToS3.toBoolean()) {
                    common.infoMsg("Syncing VCP images from internal: http://images.mcp.mirantis.net/ to s3: images.mirantis.com")
                    triggerSyncVCPJob(VCP_IMAGE_LIST, TARGET_REVISION)
                }
                if (emailNotify) {
                    notify_text = "MCP Promotion  ${env.SOURCE_REVISION} => ${env.TARGET_REVISION} has been done"
                    emailext(to: NOTIFY_RECIPIENTS,
                        body: notify_text,
                        subject: "MCP Promotion has been done")
                }
            }
        } catch (Throwable e) {
            // If there was an error or exception thrown, the build failed
            currentBuild.result = "FAILURE"
            throw e
        }
    }
}
