From c1cffc4d6f4d5bf69d32c4e4da30859d4d207a7d Mon Sep 17 00:00:00 2001 From: Benjamin Bohard Date: Thu, 10 Feb 2022 10:26:05 +0100 Subject: [PATCH] =?UTF-8?q?Automatiser=20la=20cr=C3=A9ation=20des=20ressou?= =?UTF-8?q?rces=20en=20fonction=20du=20tag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 19 ++++++----- vars/pulp.groovy | 89 ++++++++++++++++++++++++++++++------------------ 2 files changed, 66 insertions(+), 42 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index c8114ab..cfd3750 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -103,12 +103,15 @@ pipeline { ) // On publie chacun des paquets construits - def repositoriesMapping = ['unstable': 'Cadoles4MSE unstable', - 'dev': 'Cadoles4MSE dev', - 'staging': 'Cadoles4MSE staging', - 'stable': 'Cadoles4MSE stable'] + def splittedTag = env.ref.split('/') + def repositoryName = "${splittedTag[2]} ${splittedTag[1]}" + def distributionName = repositoryName + def basePath = repositoryName.replace(' ', '-') + def product = splittedTag[2].split('-')[0] + def contentGuardMapping = ['mse': 'mse_contentguard'] + def signingServiceMapping = ['mse': 'sign_deb_release'] def credentials = '212d6dc7-f9a2-4d27-94d8-de7fc6cae0a1' - def repositoryHREF = pulp.getRepositoryHREF(credentials, repositoriesMapping[result.env]) + def repositoryHREF = pulp.getRepositoryHREF(credentials, repositoryName) def exportTasks = pulp.exportPackages(credentials, result.packages) def pulpPackages = [] exportTasks.each { @@ -118,10 +121,8 @@ pipeline { } } pulp.addToRepository(credentials, pulpPackages, repositoryHREF) - def publicationHREF = pulp.publishRepository(credentials, repositoryHREF) - def distributionName = repositoriesMapping[result.env] - def base_path = distributionName.replaceAll(' ', '_') - def distributionHREF = pulp.distributePublication(credentials, publicationHREF[0], distributionName, base_path) + def publicationHREF = pulp.publishRepository(credentials, repositoryHREF, signingServiceMapping.get(product)) + def distributionHREF = pulp.distributePublication(credentials, publicationHREF[0], distributionName, base_path, contentGuardMapping.get(product)) def distributionURL = pulp.getDistributionURL(credentials, distributionHREF[0]) // On liste l'ensemble des paquets construits diff --git a/vars/pulp.groovy b/vars/pulp.groovy index d2655e2..571875e 100644 --- a/vars/pulp.groovy +++ b/vars/pulp.groovy @@ -1,5 +1,38 @@ import groovy.json.JsonOutput +def getResourceHREF( + String credentials, + String resourceEndpoint, + String resourceName +) { + def response = httpRequest authentication: credentials, url: "https://${pulpHost}/pulp/api/v3/${resourceEndpoint}", httpMode: 'GET', ignoreSslErrors: true + def jsonResponse = readJSON text: response.content + def resource = jsonResponse.results.find { it -> it.name == resourceName} + if (resource) { + return resource.pulp_href + } + return null +} + +def waitForTaskCompletion( + String credentials, + String taskHREF, + String pulpHost = '192.168.30.3' +) { + def status = '' + def created_resources = [] + while (status != 'completed') { + def response = httpRequest authentication: credentials, url: "https://${pulpHost}${taskHREF}", httpMode: 'GET', ignoreSslErrors: true + def jsonResponse = readJSON text: response.content + status = jsonResponse.state + if (status == 'completed') { + created_resources = jsonResponse.created_resources + } + sleep(10) + } + return created_resources +} + def exportPackages( String credentials, List packages = [], @@ -31,12 +64,9 @@ def getRepositoryHREF( String repository = 'Cadoles4MSE unstable', String pulpHost = '192.168.30.3' ) { - def response = httpRequest authentication: credentials, url: "https://${pulpHost}/pulp/api/v3/repositories/deb/apt/", httpMode: 'GET', ignoreSslErrors: true - def jsonResponse = readJSON text: response.content - def repositories = jsonResponse.results - def repositoryHREF = repositories.find { it -> it['name'] == repository } + def repositoryHREF = getResourceHREF(credentials, 'repositories/deb/apt/', repository) if (repositoryHREF) { - return repositoryHREF.pulp_href + return repositoryHREF } else { return createRepository(credentials, repository) } @@ -58,9 +88,17 @@ def addToRepository( def publishRepository( String credentials, String repositoryHREF, + String signing_service = null String pulpHost = '192.168.30.3' ) { - def postBody = JsonOutput.toJson(["repository": repositoryHREF, "simple": true]) + def postContent = ["repository": repositoryHREF, "simple": true] + if (signing_service) { + def signingServiceHREF = getResourceHREF(credentials, 'signing-services/', signing_service) + if (signingServiceHREF) { + postContent.put("signing-service", "https://${pulpHost}${signingServiceHREF}) + } + } + def postBody = JsonOutput.toJson(postContent) def response = httpRequest authentication: credentials, url: "https://${pulpHost}/pulp/api/v3/publications/deb/apt/", httpMode: 'POST', requestBody: postBody, contentType: 'APPLICATION_JSON', ignoreSslErrors: true def jsonResponse = readJSON text: response.content return waitForTaskCompletion(credentials, jsonResponse.task) @@ -71,23 +109,27 @@ def distributePublication( String publicationHREF, String distributionName, String basePath, - String pulpHost = '192.168.30.3', String contentGuard = null + String pulpHost = '192.168.30.3', ) { - def response = httpRequest authentication: credentials, url: "https://${pulpHost}/pulp/api/v3/distributions/deb/apt/", httpMode: 'GET', ignoreSslErrors: true - def jsonResponse = readJSON text: response.content def httpMode = '' def url = '' - def distribution = jsonResponse.results.find { it -> it.name == distributionName} - if (distribution) { + def distributionHREF = getResourceHREF(credentials, 'distributions/deb/apt/', distributionName) + if (distributionHREF) { httpMode = 'PUT' - url = distribution.pulp_href - + url = distributionHREF } else { httpMode = 'POST' url = '/pulp/api/v3/distributions/deb/apt/' } - def postBody = JsonOutput.toJson(["publication": publicationHREF, "name": distributionName, "base_path": basePath, "content_guard": contentGuard]) + def bodyContent = ["publication": publicationHREF, "name": distributionName, "base_path": basePath] + if (contentGuard) { + def contentGuardHREF = getResourceHREF(credentials, 'contentguards/core/rbac/', contentGuard) + if (contentGuardHREF) { + bodyContent.put('content_guard', "https://${pulpHost}${contentGuardHREF}") + } + } + def postBody = JsonOutput.toJson(bodyContent) response = httpRequest authentication: credentials, url: "https://${pulpHost}${url}", httpMode: httpMode, requestBody: postBody, contentType: 'APPLICATION_JSON', ignoreSslErrors: true, validResponseCodes: "100:399" jsonResponse = readJSON text: response.content if (distribution) { @@ -98,25 +140,6 @@ def distributePublication( } } -def waitForTaskCompletion( - String credentials, - String taskHREF, - String pulpHost = '192.168.30.3' -) { - def status = '' - def created_resources = [] - while (status != 'completed') { - def response = httpRequest authentication: credentials, url: "https://${pulpHost}${taskHREF}", httpMode: 'GET', ignoreSslErrors: true - def jsonResponse = readJSON text: response.content - status = jsonResponse.state - if (status == 'completed') { - created_resources = jsonResponse.created_resources - } - sleep(10) - } - return created_resources -} - def getDistributionURL( String credentials, String resourceHREF,