utils.groovy 6.27 KB
Newer Older
1
// INTEL CONFIDENTIAL
2
// Copyright 2018-2019 Intel Corporation All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// The source code contained or described herein and all documents related to the
// source code ("Material") are owned by Intel Corporation or its suppliers or
// licensors. Title to the Material remains with Intel Corporation or its
// suppliers and licensors. The Material may contain trade secrets and proprietary
// and confidential information of Intel Corporation and its suppliers and
// licensors, and is protected by worldwide copyright and trade secret laws and
// treaty provisions. No part of the Material may be used, copied, reproduced,
// modified, published, uploaded, posted, transmitted, distributed, or disclosed
// in any way without Intel's prior express written permission.
// No license under any patent, copyright, trade secret or other intellectual
// property right is granted to or conferred upon you by disclosure or delivery of
// the Materials, either expressly, by implication, inducement, estoppel or
// otherwise. Any license under such intellectual property rights must be express
// and approved by Intel in writing.
STAGES_STATUS_MAP = [:]

19
def getDockerEnvList(String projectName, String dockerContainerNamePrefix, String projectRoot = projectName) {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    /**
    * This method generates configuration map list using dockerfiles available in dockerfiles directory
    *
    * @param projectName name of the project used in paths and configuration map.
    * @param dockerContainerNamePrefix docker container name prefix.
    * @param projectRoot path to project root containing directory with dockerfiles to run
    */

    def rawList = findFiles(glob: "${projectRoot}/dockerfiles/*.dockerfile")
    def envList = []
    for (int i = 0; i < rawList.size(); ++i) {
        def name = rawList[i].name - '.dockerfile'
        def dockerContainerName = "${dockerContainerNamePrefix}_${name}"
        envList.add([name:name, // name is the only obligatory vaiable
                     dockerfilePath:rawList[i].path,
                     projectName:projectName,
                     dockerContainerName:dockerContainerName])
    }
    return envList
}

41
def generateMap(Closure method, configurationMaps) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    /**
    * Generates map for method using configurationMaps.
    *
    * @param method Method that will be executed in each map(configuration).
    * @param configurationMaps Map of configuration that will be parallelized.
    */

    def executionMap = [:]
    for (int i = 0; i < configurationMaps.size(); ++i) {
        configMap = configurationMaps[i]
        executionMap[configMap["name"]] = {
            method(configMap)
        }
    }
    return executionMap
}

59
def createStage(String stageName, Closure method, configurationMaps, force = false) {
60 61
    /**
    * Create pipeline stage.
62
    *
63 64 65 66 67 68 69 70 71 72 73
    * @param stageName Name of stage that will be create.
    * @param method Method that will be executed in each map(configuration).
    * @param configurationMaps Map of configuration that will be parallelized.
    */

    stage(stageName) {
        // Add current stage name to configurationMaps
        for (int i = 0; i < configurationMaps.size(); ++i) {
            configurationMaps[i]["stageName"] = stageName
        }

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        // Fail current stage If earlier stage got aborted or failed
        // unless it's executed with force argument set to true
        Closure genericBodyMethod = {}
        if (!force && ["FAILURE", "ABORTED"].contains(currentBuild.result)) {
            genericBodyMethod = { configMap ->
                println("Skipping stage due to earlier stage ${currentBuild.result}")
                setConfigurationStatus(configMap["stageName"], configMap["name"], currentBuild.result)
                throw new Exception("Skipped due to ${currentBuild.result} in earlier stage")
            }
        }
        else
        {
            genericBodyMethod = { configMap ->
                def status = "SUCCESS"
                try {
                    method(configMap)
                } catch(Exception e) {
                    if (e.toString().contains("FlowInterruptedException")) {
                        status = "ABORTED"
                    } else {
                        status = "FAILURE"
                    }
                    currentBuild.result = status
                    throw e
                } finally {
                    setConfigurationStatus(configMap["stageName"], configMap["name"], status)
                }
101 102 103 104
            }
        }

        try {
105
            def prepareEnvMap = generateMap(genericBodyMethod, configurationMaps)
106
            parallel prepareEnvMap
107 108
        } catch(Exception e) {
            if (e.toString().contains("FlowInterruptedException")) {
109 110 111 112
                currentBuild.result = "ABORTED"
            } else {
                currentBuild.result = "FAILURE"
            }
113 114 115 116
        }
    }
}

117
def setConfigurationStatus(String stageName, String configurationName, String status) {
118 119
    /**
    * Set stage status.
120
    *
121 122 123 124 125 126 127
    * @param stageName The name of the stage in which the configuration is.
    * @param configurationName The name of the configuration whose status will be updated.
    * @param status Configuration status: SUCCESS or FAILURE.
    */
    if (!STAGES_STATUS_MAP.containsKey(stageName)) {
        STAGES_STATUS_MAP[stageName] = [:]
    }
128
    if (["FAILURE", "SUCCESS", "ABORTED"].contains(status.toUpperCase())) {
129 130 131 132 133 134
        STAGES_STATUS_MAP[stageName][configurationName] = status.toUpperCase()
    } else {
        throw new Exception("Not supported status name.")
    }
}

135
def propagateStatus(String parentStageName, String parentConfigurationName) {
136 137 138 139
    /**
    * Popagate status in parent configuration fails.
    * This method will throw exeption "Propagating status of $parentStageName"
    * if parent configuration name status is FAILURE
140
    *
141 142 143 144 145 146 147 148 149 150
    * @param parentStageName The name of the stage in which the configuration is.
    * @param parentConfigurationName The name of the configuration whose status will be propagated.
    */

    parentStageStatus = STAGES_STATUS_MAP[parentStageName][parentConfigurationName]
    if (parentStageStatus == "FAILURE") {
        throw new Exception("Propagating status of ${parentStageName}")
    }
}

151
def showStatusMap() {
152 153 154 155 156 157 158 159
    /**
    * Display status map for every defined stage.
    */

    echo "${STAGES_STATUS_MAP}"
}

return this