Remove ci.mcp.Tools package
Instead one must use packages from com.mirantis.mcp.* namespace
Change-Id: I43808b56642fc84deac933323b33172ce433775c
diff --git a/src/ci/mcp/Tools.groovy b/src/ci/mcp/Tools.groovy
deleted file mode 100644
index 565232f..0000000
--- a/src/ci/mcp/Tools.groovy
+++ /dev/null
@@ -1,329 +0,0 @@
-package ci.mcp
-
-/**
-* Please do not add new functions to this file. Add them to corresponding
-* files in com/mirantis/mcp instead.
-**/
-
-/**
- * https://issues.jenkins-ci.org/browse/JENKINS-26481
- * fix groovy List.collect()
-**/
-@NonCPS
-def constructString(ArrayList options, String keyOption, String separator = " ") {
- return options.collect{ keyOption + it }.join(separator).replaceAll("\n", "")
-}
-
-/**
- * Build command line options, e.g:
- * cmd_opts=["a=b", "c=d", "e=f"]
- * key = "--build-arg "
- * separator = " "
- * def options = getCommandBuilder(cmd_opts, key, separator)
- * println options
- * > --build-arg a=b --build-arg c=d --build-arg e=f
- *
- * @param options List of Strings (options that should be populated)
- * @param keyOption key that should be added before each option
- * @param separator Separator between key+Option pairs
- */
-def getCommandBuilder(ArrayList options, String keyOption, String separator = " ") {
- return constructString(options, keyOption)
-}
-
-/**
-* Add LABEL to the end of the Dockerfile
-* User can also add some custom properties
-*
-* @param dockerfilePath is the path to Dockerfile, the default is ./Dockerfile
-* @param customProperties a Array of Strings that should be added to mandatory props
-* in format ["prop1=value1", "prop2=value2"]
-**/
-def setDockerfileLabels(String dockerfilePath = "./Dockerfile", ArrayList customProperties = null){
-
- if (!fileExists(dockerfilePath)){
- throw new RuntimeException("Unable to add LABEL to Dockerfile, ${dockerfilePath} doesn't exists")
- }
- echo "Updating ${dockerfilePath}"
-
- def namespace = "com.mirantis.image-specs."
- def properties = [
- "gerritProject=${env.GERRIT_PROJECT}",
- "gerritChangeNumber=${env.GERRIT_CHANGE_NUMBER}",
- "gerritPatchsetNumber=${env.GERRIT_PATCHSET_NUMBER}",
- "gerritChangeId=${env.GERRIT_CHANGE_ID}",
- "gerritPatchsetRevision=${env.GERRIT_PATCHSET_REVISION}"
- ]
-
- if (customProperties != null){
- properties.addAll(customProperties)
- }
-
- def metadata = constructString(properties, namespace, " ")
- sh """
- cat <<EOF>> ${dockerfilePath}
- # Apply additional build metadata
- LABEL ${metadata}
- """
- return metadata
-}
-
-/**
-* Return string of mandatory build properties for binaries
-* User can also add some custom properties
-*
-* @param customProperties a Array of Strings that should be added to mandatory props
-* in format ["prop1=value1", "prop2=value2"]
-**/
-def getBinaryBuildProperties(ArrayList customProperties) {
-
- def namespace = "com.mirantis."
- def properties = [
- "gerritProject=${env.GERRIT_PROJECT}",
- "gerritChangeNumber=${env.GERRIT_CHANGE_NUMBER}",
- "gerritPatchsetNumber=${env.GERRIT_PATCHSET_NUMBER}",
- "gerritChangeId=${env.GERRIT_CHANGE_ID}",
- "gerritPatchsetRevision=${env.GERRIT_PATCHSET_REVISION}"
- ]
-
- if (customProperties){
- properties.addAll(customProperties)
- }
-
- return constructString(properties, namespace, ";")
-}
-
-/**
- * Parse HEAD of current directory and return commit hash
- */
-def getGitCommit() {
- git_commit = sh (
- script: 'git rev-parse HEAD',
- returnStdout: true
- ).trim()
- return git_commit
-}
-
-/**
- * Describe a commit using the most recent tag reachable from it
- */
-def getGitDescribe() {
- git_commit = sh (
- script: 'git describe --tags',
- returnStdout: true
- ).trim()
- return git_commit
-}
-
-/**
- * Generate current timestamp
- *
- * @param format Defaults to yyyyMMddHHmmss
- */
-def getDatetime(format="yyyyMMddHHmmss") {
- def now = new Date();
- return now.format(format, TimeZone.getTimeZone('UTC'));
-}
-
-/**
-* Get URL to artifact by properties
-* Returns String with URL to found artifact or null if nothing
-*
-* @param artifactoryURL String, an URL to Artifactory
-* @param properties LinkedHashMap, a Hash of properties (key-value) which
-* which should determine artifact in Artifactory
-*/
-def uriByProperties(String artifactoryURL, LinkedHashMap properties) {
- def key, value
- def properties_str = ''
- for ( int i = 0; i < properties.size(); i++ ) {
- // avoid serialization errors
- key = properties.entrySet().toArray()[i].key
- value = properties.entrySet().toArray()[i].value
- properties_str += "${key}=${value}&"
- }
- def search_url = "${artifactoryURL}/api/search/prop?${properties_str}"
-
- def result = sh(script: "bash -c \"curl -X GET \'${search_url}\'\"",
- returnStdout: true).trim()
- def content = new groovy.json.JsonSlurperClassic().parseText(result)
- def uri = content.get("results")
- if ( uri ) {
- return uri.last().get("uri")
- } else {
- return null
- }
-}
-
-/**
-* Get URL to artifact by properties
-* Returns String with URL to found artifact or null if nothing
-*
-* @param artifactoryURL String, an URL to Artifactory
-* @param properties String, URI in format prop1=val1&prop2=val2&prop3val3
-* which should determine artifact in Artifactory
-*/
-def uriByProperties(String artifactoryURL, String properties) {
-
- def search_url = "${artifactoryURL}/api/search/prop?${properties}"
-
- def result = sh(script: "bash -c \"curl -X GET \'${search_url}\'\"",
- returnStdout: true).trim()
- def content = new groovy.json.JsonSlurperClassic().parseText(result)
- def uri = content.get("results")
- if ( uri ) {
- return uri.last().get("uri")
- } else {
- return null
- }
-}
-
-/**
-* Set properties for artifact in Artifactory repo
-*
-* @param artifactUrl String, an URL to artifact in Artifactory repo
-* @param properties LinkedHashMap, a Hash of properties (key-value) which
-* should be assigned for choosen artifact
-* @param recursive Boolean, if artifact_url is a directory, whether to set
-* properties recursively or not
-*/
-def setProperties (String artifactUrl, LinkedHashMap properties, Boolean recursive=false) {
- def properties_str = 'properties='
- def key,value
- if (recursive) {
- recursive = 'recursive=1'
- } else {
- recursive = 'recursive=0'
- }
- for ( int i = 0; i < properties.size(); i++ ) {
- // avoid serialization errors
- key = properties.entrySet().toArray()[i].key
- value = properties.entrySet().toArray()[i].value
- properties_str += "${key}=${value}|"
- }
- def url = "${artifactUrl}?${properties_str}&${recursive}"
- withCredentials([
- [$class: 'UsernamePasswordMultiBinding',
- credentialsId: 'artifactory',
- passwordVariable: 'ARTIFACTORY_PASSWORD',
- usernameVariable: 'ARTIFACTORY_LOGIN']
- ]) {
- sh "bash -c \"curl -X PUT -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\""
- }
-}
-
-/**
-* Get properties for specified artifact in Artifactory
-* Returns LinkedHashMap of properties
-*
-* @param artifactUrl String, an URL to artifact in Artifactory repo
-*/
-def getPropertiesForArtifact(String artifactUrl) {
- def url = "${artifactUrl}?properties"
- def result
- withCredentials([
- [$class: 'UsernamePasswordMultiBinding',
- credentialsId: 'artifactory',
- passwordVariable: 'ARTIFACTORY_PASSWORD',
- usernameVariable: 'ARTIFACTORY_LOGIN']
- ]) {
- result = sh(script: "bash -c \"curl -X GET -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"",
- returnStdout: true).trim()
- }
- def properties = new groovy.json.JsonSlurperClassic().parseText(result)
- return properties.get("properties")
-}
-
-/**
-* Upload docker image to Artifactory
-*
-* @param artifactoryURL String, an URL to Artifactory
-* @param registry String, the name of Docker registry
-* @param image String, Docker image name
-* @param version String, Docker image version
-* @param repository String, The name of Artifactory Docker repository
-*/
-def uploadImageToArtifactory (String artifactoryURL, String registry, String image,
- String version, String repository) {
- // TODO Switch to Artifactoy image' pushing mechanism once we will
- // prepare automatical way for enabling artifactory build-proxy
- //def artDocker
- withCredentials([
- [$class: 'UsernamePasswordMultiBinding',
- credentialsId: 'artifactory',
- passwordVariable: 'ARTIFACTORY_PASSWORD',
- usernameVariable: 'ARTIFACTORY_LOGIN']
- ]) {
- sh ("docker login -u ${ARTIFACTORY_LOGIN} -p ${ARTIFACTORY_PASSWORD} ${registry}")
- //artDocker = Artifactory.docker("${env.ARTIFACTORY_LOGIN}", "${env.ARTIFACTORY_PASSWORD}")
- }
-
- sh ("docker push ${registry}/${image}:${version}")
- //artDocker.push("${registry}/${image}:${version}", "${repository}")
- def image_url = "${artifactoryURL}/api/storage/${repository}/${image}/${version}"
-
- def properties = ['com.mirantis.build_name':"${env.JOB_NAME}",
- 'com.mirantis.build_id': "${env.BUILD_NUMBER}",
- 'com.mirantis.changeid': "${env.GERRIT_CHANGE_ID}",
- 'com.mirantis.patchset_number': "${env.GERRIT_PATCHSET_NUMBER}",
- 'com.mirantis.target_tag': "${version}"]
- setProperties(image_url, properties)
-}
-
-/**
-* Upload binaries to Artifactory
-*
-* @param server ArtifactoryServer, the instance of Artifactory server
-* @param buildInfo BuildInfo, the instance of a build-info object which can be published
-* @param uploadSpec String, a spec which is a JSON file that specifies which files should be
-* uploaded or downloaded and the target path
-* @param publishInfo Boolean, whether publish a build-info object to Artifactory
-*/
-def uploadBinariesToArtifactory (server, buildInfo, String uploadSpec,
- Boolean publishInfo=false) {
- buildInfo.append(server.upload(uploadSpec))
-
- if ( publishInfo ) {
- buildInfo.env.capture = true
- buildInfo.env.filter.addInclude("*")
- buildInfo.env.filter.addExclude("*PASSWORD*")
- buildInfo.env.filter.addExclude("*password*")
- buildInfo.env.collect()
- server.publishBuildInfo(buildInfo)
- }
-}
-
-/**
-* Promote Docker image artifact to release repo
-*
-* @param artifactoryURL String, an URL to Artifactory
-* @param artifactoryDevRepo String, the source dev repository name
-* @param artifactoryProdRepo String, the target repository for the move or copy
-* @param dockerRepo String, the docker repository name to promote
-* @param artifactTag String, an image tag name to promote
-* @param targetTag String, target tag to assign the image after promotion
-* @param copy Boolean, an optional value to set whether to copy instead of move
-* Default: false
-*/
-def promoteDockerArtifact(String artifactoryURL, String artifactoryDevRepo,
- String artifactoryProdRepo, String dockerRepo,
- String artifactTag, String targetTag, Boolean copy=false) {
- def url = "${artifactoryURL}/api/docker/${artifactoryDevRepo}/v2/promote"
- writeFile file: "query.json",
- text: """{
- \"targetRepo\": \"${artifactoryProdRepo}\",
- \"dockerRepository\": \"${dockerRepo}\",
- \"tag\": \"${artifactTag}\",
- \"targetTag\" : \"${targetTag}\",
- \"copy\": \"${copy}\"
- }""".stripIndent()
- sh "cat query.json"
- withCredentials([
- [$class: 'UsernamePasswordMultiBinding',
- credentialsId: 'artifactory',
- passwordVariable: 'ARTIFACTORY_PASSWORD',
- usernameVariable: 'ARTIFACTORY_LOGIN']
- ]) {
- sh "bash -c \"curl -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -H \"Content-Type:application/json\" -X POST -d @query.json ${url}\""
- }
-}
diff --git a/src/ci/mcp/Tools.txt b/src/ci/mcp/Tools.txt
deleted file mode 100644
index 8229970..0000000
--- a/src/ci/mcp/Tools.txt
+++ /dev/null
@@ -1,218 +0,0 @@
-getBinaryBuildProperties
-------------------------
-
-1. getBinaryBuildProperties() should be used to define mandatory
-properties for binary artifact, which are:
-
- "gerritProject=${env.GERRIT_PROJECT}",
- "gerritChangeNumber=${env.GERRIT_CHANGE_NUMBER}",
- "gerritPatchsetNumber=${env.GERRIT_PATCHSET_NUMBER}",
- "gerritChangeId=${env.GERRIT_CHANGE_ID}",
- "gitSha=${env.GERRIT_PATCHSET_REVISION}"
-
-2. User can add some custom properties, e.g:
-
-def properties = tools.getBinaryBuildProperties(
- ["test=123",
- "prop1=val1",
- prop2=val2"
- ])
-
-3. The resulting values will be the string in props format with
-namespace com.mirantis, e.g:
-
- com.mirantis.gerritProject=asd;com.mirantis.gerritChangeNumber=123;com.mirantis.gerritChangeId=ddd
-
-4. How to use:
-
- def tools = new ci.mcp.Tools()
- ...
- def buildInfo = Artifactory.newBuildInfo()
- ...
- def properties = tools.getBinaryBuildProperties()
- ...
- // Create the upload spec.
- def uploadSpec = """{
- "files": [
- {
- "pattern": "**",
- "target": "some/target",
- "props": "${properties}"
- }
- ]
- }"""
- server.upload(uploadSpec, buildInfo)
- server.publishBuildInfo buildInfo
-
-
-setDockerfileLabels
--------------------
-// simple usage, which adds mandatory properties to Dockerfile which is
-// located in current directory
-tools.setDockerfileLabels()
-
-// extended usage
-// add Labels to calico ctl and node images
- def properties = [
- "CALICO_NODE_IMAGE_REPO=${calicoNodeImageRepo}",
- "CALICOCTL_IMAGE_REPO=${calicoCtlImageRepo}",
- "CALICO_VERSION=${calicoVersion}"
- ]
- tools.setDockerfileLabels("./calicoctl/Dockerfile", properties)
- tools.setDockerfileLabels("./calico_node/Dockerfile", properties)
-
-
-
-uriByProperties
----------------
-
-1. uriByProperties() should be used to get URL for some artifact
-with specified properties, like:
-
- "gerritChangeId=${env.GERRIT_CHANGE_ID}",
- "gerritPatchsetNumber=${env.GERRIT_PATCHSET_NUMBER}"
-
-2. Also can be used custom user' properties.
-
-3. The resulting value will be the string in URL format, e.g:
-
- "https://ci.mcp-ci.local/artifactory/mcp-k8s-ci/images-info/conformance_image_v1.4.1-5_1476965708283.yaml"
-
-4. How to use:
-
- def tools = new ci.mcp.Tools()
- ...
- def properties = ['com.mirantis.changeid': "${env.GERRIT_CHANGE_ID}",
- 'com.mirantis.patchset_number': "${env.GERRIT_PATCHSET_NUMBER}" ]
- def artifact_uri = tools.uriByProperties(properties)
-
-5. Important notes:
-
- - if specified properties are set for few artifacts will be taken last.
-
-
-setProperties
--------------
-
-1. setProperties() should be used to set some properties to specified artifact in repo, like:
-
- "gerritChangeId=${env.GERRIT_CHANGE_ID}",
- "gerritPatchsetNumber=${env.GERRIT_PATCHSET_NUMBER}"
-
-2. How to use:
-
- def tools = new ci.mcp.Tools()
- ...
- def artifact_url = "${env.ARTIFACTORY_URL}/api/storage/${repository}/${tag}/${version}"
- // for example: https://ci.mcp-ci.local/artifactory/api/storage/mcp-k8s-local/hyperkube-amd64/v1.4.1-5
- def properties = ['com.mirantis.build_name':"${env.JOB_NAME}",
- 'com.mirantis.build_id': "${env.BUILD_NUMBER}",
- 'com.mirantis.changeid': "${env.GERRIT_CHANGE_ID}",
- 'com.mirantis.patchset_number': "${env.GERRIT_PATCHSET_NUMBER}"]
-
- tools.setProperties(artifact_url, properties)
-
-3. As result provided artifact in repo will be tagged by specified properties.
-
-
-getPropertiesForArtifact
-------------------------
-
-1. getPropertiesForArtifact() should be used to get properties for specified artifact in repo, like:
-
- "https://ci.mcp-ci.local:443/artifactory/api/storage/mcp-k8s-ci/hyperkube-amd64/v1.4.1-5"
-
-2. As a result will be returned LinkedHashMap of properties for specified artifact URL.
-
-3. How to use:
-
- def tools = new ci.mcp.Tools()
- ...
- //
- def artifact_url = "${env.ARTIFACTORY_URL}/api/storage/${repository}/${tag}"
- def properties = tools.getPropertiesForArtifact(artifact_url)
-
-4. Important notes:
-
- - will be returned LinkedHashMap of properties key-value pairs. Each value is an Array by default,
- because each key can have few values.
- - each key is available by get() method.
-
-
-uploadImageToArtifactory
-------------------------
-
-1. uploadImageToArtifactory() should be used to upload Docker image to Artifactory.
-
-2. How to use:
-
- def tools = new ci.mcp.Tools()
- ...
- // Docker registry for binaries and images
- def docker_registry = 'ci.mcp-ci.local:5001'
- def docker_image_name = 'hyperkube-amd64'
- def docker_image_version = 'some_version'
- def docker_repo = 'mcp-ci-local'
- ...
- <docker build steps>
- ...
- tools.uploadImageToArtifactory(docker_registry, docker_image_name, docker_image_version, docker_repo)
-
-3. As a result specified image will be pushed to artifactory docker repo.
-
-
-uploadBinariesToArtifactory
----------------------------
-
-1. uploadBinariesToArtifactory() should be used to upload binaries to Artifactory, like:
-
- - some executive binaries, which were built during job execution;
- - *.yml files, etc.
-
-2. How to use:
-
- def tools = new ci.mcp.Tools()
- ...
- def server = Artifactory.server('mcp-ci')
- def buildInfo = Artifactory.BuildInfo()
- ...
- def uploadSpec = """{
- "files": [
- {
- "pattern": "hyperkube*.yaml",
- "target": "${artifactory_dev_repo}/images-info/"
- },
- {
- "pattern": "artifacts/hyperkube**",
- "target": "${artifactory_dev_repo}/hyperkube-binaries/"
- }
- ]
- }"""
- tools.uploadBinariesToArtifactory(server, buildInfo, uploadSpec)
-
-
-promoteDockerArtifact
----------------------
-
-1. promoteDockerArtifact() should be used to promote docker image to production repo,
-because native promotion mechanism is not allow to do it due to Artifactory bug.
-
-2. How to use:
-
- def tools = new ci.mcp.Tools()
- ...
- def artifactory_dev_repo = 'docker-local'
- def artifactory_prod_repo = 'docker-prod'
- def docker_repo = 'hyperkube-amd64'
- ...
- // functions above shall be used
- <getting all tag's and artifact's information>
- ...
- def artifactImageTag = 'v1.4.0-XXXX'
- def targetArtifactImageTag = 'v1.4.0'
- ...
- tools.promoteDockerArtifact(artifactory_dev_repo,
- artifactory_prod_repo,
- docker_repo,
- artifactImageTag,
- targetArtifactImageTag)
\ No newline at end of file