Ruslan Gustomiasov | 5d131b6 | 2019-08-21 11:51:26 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Functions to work with Helm |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Build index file for helm chart |
Sergey Otpuschennikov | 50b248c | 2019-08-28 17:21:18 +0400 | [diff] [blame] | 11 | * @param extra_params additional params, e.g. --url repository_URL |
| 12 | * @param charts_dir path to a directory |
Ruslan Gustomiasov | 5d131b6 | 2019-08-21 11:51:26 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Sergey Otpuschennikov | 50b248c | 2019-08-28 17:21:18 +0400 | [diff] [blame] | 15 | def helmRepoIndex(extra_params='', charts_dir='.'){ |
| 16 | sh("helm repo index ${extra_params} ${charts_dir}") |
| 17 | } |
Mykyta Karpin | 3c78c0f | 2019-09-11 18:11:06 +0300 | [diff] [blame] | 18 | |
| 19 | /** |
Sergey Otpuschennikov | 958f287 | 2019-10-16 17:04:33 +0400 | [diff] [blame] | 20 | * Rebuild index file for helm chart repo |
| 21 | * @param helmRepoUrl repository with helm charts |
| 22 | * @param md5Remote md5 sum of index.yaml for check |
| 23 | */ |
| 24 | |
| 25 | def helmMergeRepoIndex(helmRepoUrl, md5Remote='') { |
| 26 | def common = new com.mirantis.mk.Common() |
| 27 | |
| 28 | def helmRepoDir = '.' |
| 29 | def helmExtraParams = "--url ${helmRepoUrl}" |
| 30 | |
| 31 | def indexRes = common.shCmdStatus("wget -O index-upstream.yaml ${helmRepoUrl}/index.yaml") |
| 32 | if (indexRes['status']){ |
| 33 | if (indexRes['status'] == 8 && indexRes['stderr'].contains('ERROR 404') && !md5Remote) { |
| 34 | common.warningMsg("Index.yaml not found in ${helmRepoUrl} and will be fully regenerated") |
| 35 | } else { |
| 36 | error("Something went wrong during index.yaml download: ${indexRes['stderr']}") |
| 37 | } |
| 38 | } else { |
| 39 | if (md5Remote) { |
| 40 | def md5Local = sh(script: "md5sum index-upstream.yaml | cut -d ' ' -f 1", returnStdout: true).readLines()[0] |
| 41 | if (md5Local != md5Remote) { |
| 42 | error 'Target repository already exist, but upstream index.yaml broken or not found' |
| 43 | } |
| 44 | } |
| 45 | helmExtraParams += " --merge index-upstream.yaml" |
| 46 | } |
Sergey Otpuschennikov | 892b4e7 | 2019-10-29 14:54:08 +0400 | [diff] [blame^] | 47 | helmRepoIndex(helmExtraParams, helmRepoDir) |
Sergey Otpuschennikov | 958f287 | 2019-10-16 17:04:33 +0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
Mykyta Karpin | 3c78c0f | 2019-09-11 18:11:06 +0300 | [diff] [blame] | 51 | * Generates version for helm chart based on information from git repository. Tries to search |
| 52 | * first parent git tag using pattern '[0-9]*-{tagSuffix}', if found that tag will be used |
| 53 | * in final version, if not found - version will be formed as '{defaultVersion}-{tagSuffix}'. Number |
| 54 | * of commits since last tag or sha of current commit can be added to version. |
| 55 | * |
| 56 | * @param repoDir string, path to a directory with git repository of helm charts |
| 57 | * @param devVersion Boolean, if set to true development version will be calculated e.g 0.1.0-mcp-{sha of current commit} |
| 58 | * @param increment Boolean, if set to true patch version will be incremented (e.g 0.1.0 -> 0.1.1) |
| 59 | * @param defaultVersion string, value of version which will be used in case no tags found. should be semver2 compatible |
| 60 | * @param tagSuffix string, suffix which will be used for finding tags in git repository, also if tag not found, it |
| 61 | * it will be added to {defaultVersion} e.g {defaultVersion}-{tagSuffix} |
| 62 | */ |
| 63 | |
| 64 | def generateChartVersionFromGit(repoDir, devVersion = true, increment = false, defaultVersion = '0.1.0', tagSuffix = 'mcp') { |
| 65 | def common = new com.mirantis.mk.Common() |
| 66 | def git = new com.mirantis.mk.Git() |
| 67 | String initialVersion = "${defaultVersion}-${tagSuffix}" |
| 68 | String countRange |
| 69 | String versionData |
| 70 | String tagPattern = "[0-9]*-${tagSuffix}" |
| 71 | dir(repoDir){ |
| 72 | Map cmd = common.shCmdStatus("git describe --tags --first-parent --abbrev=0 --match ${tagPattern}") |
| 73 | String lastTag = cmd['stdout'].trim() |
| 74 | |
| 75 | if (cmd['status'] != 0){ |
| 76 | if (cmd['stderr'].contains('fatal: No names found, cannot describe anything')){ |
| 77 | common.warningMsg("No parent git tag found, using initial version ${initialVersion}") |
| 78 | versionData = initialVersion |
| 79 | countRange = 'HEAD' |
| 80 | } else { |
| 81 | error("Something went wrong, cannot find git information ${cmd['stderr']}") |
| 82 | } |
| 83 | } else { |
| 84 | versionData = lastTag |
| 85 | countRange = "${lastTag}..HEAD" |
| 86 | } |
| 87 | List versionParts = versionData.tokenize('-') |
| 88 | |
| 89 | if (versionParts.size() == 2 && common.isSemVer(versionData) && versionParts[1] == tagSuffix){ |
| 90 | String commitsSinceTag = sh(script: "git rev-list --count ${countRange}", returnStdout: true).trim() |
| 91 | String commitSha = sh(script: 'git rev-parse --short=7 HEAD', returnStdout: true).trim() |
| 92 | |
| 93 | if (commitsSinceTag == '0'){ |
| 94 | return versionData |
| 95 | } |
| 96 | |
| 97 | if (devVersion){ |
| 98 | versionParts.add(commitSha) |
| 99 | } else { |
| 100 | versionParts.add(commitsSinceTag) |
| 101 | } |
| 102 | // Patch version will be incremented e.g. 0.1.0 -> 0.1.1 |
| 103 | if (increment) { |
| 104 | versionParts[0] = git.incrementVersion(versionParts[0]) |
| 105 | } |
| 106 | return versionParts.join('-') |
| 107 | } |
| 108 | error "Version ${versionData} doesn't contain required suffix ${tagSuffix} or not in semver2 format" |
| 109 | } |
Mykyta Karpin | 6f050b2 | 2019-09-24 13:57:20 +0300 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Takes a list of dependencies and a version, and sets a version for each dependency in requirements.yaml. If dependency isn't |
| 114 | * found in requirements.yaml or requirements.yaml does not exist - does nothing. |
| 115 | * |
| 116 | * @param chartPath string, path to a directory with helm chart |
| 117 | * @param dependencies list of hashes with names and versions of dependencies in format: |
| 118 | * [['name': 'chart-name1', 'version': '0.1.0-myversion'], ['name': 'chart-name2', 'version': '0.2.0-myversion']] |
| 119 | */ |
| 120 | |
| 121 | def setChartDependenciesVersion(chartPath, List dependencies){ |
| 122 | def common = new com.mirantis.mk.Common() |
| 123 | if (!dependencies){ |
| 124 | error 'No list of target dependencies is specified' |
| 125 | } |
| 126 | def reqsFilePath = "${chartPath}/requirements.yaml" |
| 127 | def chartYaml = readYaml file: "${chartPath}/Chart.yaml" |
| 128 | def reqsUpdateNeeded = false |
| 129 | def reqsMap = [:] |
| 130 | if (fileExists(reqsFilePath)){ |
| 131 | reqsMap = readYaml file: reqsFilePath |
| 132 | for (i in dependencies) { |
| 133 | for (item in reqsMap.get('dependencies', [])){ |
| 134 | if (item['name'] == i['name']){ |
Mykyta Karpin | 882dd36 | 2019-09-25 11:27:55 +0300 | [diff] [blame] | 135 | common.infoMsg("Set version ${i['version']} for dependency ${i['name']} in chart ${chartYaml['name']}") |
Mykyta Karpin | 6f050b2 | 2019-09-24 13:57:20 +0300 | [diff] [blame] | 136 | item['version'] = i['version'] |
| 137 | reqsUpdateNeeded = true |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | if (reqsUpdateNeeded){ |
| 143 | sh "rm ${reqsFilePath}" |
| 144 | writeYaml file: reqsFilePath, data: reqsMap |
| 145 | } else { |
| 146 | common.warningMsg("requirements.yaml doesn't exist at path ${reqsFilePath} or chart doesn't contain ${dependencies}, nothing to set") |
| 147 | } |
Sergey Otpuschennikov | 958f287 | 2019-10-16 17:04:33 +0400 | [diff] [blame] | 148 | } |