From ce2c30003e63359c5b18ba13c04103cd4f0a390f Mon Sep 17 00:00:00 2001 From: Benjamin Bohard Date: Tue, 25 Jan 2022 15:11:54 +0100 Subject: [PATCH] =?UTF-8?q?Fonction=20pour=20l=E2=80=99envoi=20de=20paquet?= =?UTF-8?q?s=20sur=20pulp=20(domaine=20par=20d=C3=A9faut=20=C3=A0=20modifi?= =?UTF-8?q?er)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vars/pulp.groovy | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 vars/pulp.groovy diff --git a/vars/pulp.groovy b/vars/pulp.groovy new file mode 100644 index 0000000..1778c38 --- /dev/null +++ b/vars/pulp.groovy @@ -0,0 +1,117 @@ +import groovy.json.JsonOutput + +def exportPackages( + String credentials, + List packages = [], + String pulpHost = 'pulp.bbohard.lan' +) { + def exportTasks = [] + packages.each { + def response = httpRequest authentication: credentials, url: "https://${pulpHost}/pulp/api/v3/content/deb/packages/", httpMode: 'POST', ignoreSslErrors: true, multipartName: "file", timeout: 900, responseHandle: 'NONE', uploadFile: "${it}" + jsonResponse = readJSON text: response.content + println(jsonResponse) + exportTasks << jsonResponse['task'] + } + return exportTasks +} + +def getRepositoryHREF( + String credentials, + String repositoryLevel = 'dev', + String pulpHost = 'pulp.bbohard.lan' + ) { + def repositoriesMapping = ['dev': 'Cadoles4MSE'] + def response = httpRequest authentication: credentials, url: "https://${pulpHost}/pulp/api/v3/repositories/deb/apt/", httpMode: 'GET', ignoreSslErrors: true + def jsonResponse = readJSON text: response.content + println(jsonResponse) + def repositories = jsonResponse.results + def repositoryHREF = repositories.find { it -> it['name'] == repositoriesMapping[repositoryLevel] } + return repositoryHREF.pulp_href +} + +def addToRepository( + String credentials, + List packagesHREF, + String repositoryHREF, + String pulpHost = 'pulp.bbohard.lan' +) { + def packagesHREFURL = ["add_content_units": packagesHREF.collect { "https://$pulpHost$it" }] + def postBody = JsonOutput.toJson(packagesHREFURL) + def response = httpRequest authentication: credentials, url: "https://${pulpHost}${repositoryHREF}modify/", httpMode: 'POST', requestBody: postBody, contentType: 'APPLICATION_JSON', ignoreSslErrors: true, validResponseCodes: "100:599" + def jsonResponse = readJSON text: response.content + return waitForTaskCompletion(credentials, jsonResponse.task) +} + +def publishRepository( + String credentials, + String repositoryHREF, + String pulpHost = 'pulp.bbohard.lan' +) { + def postBody = JsonOutput.toJson(["repository": repositoryHREF, "simple": true]) + 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 + println(jsonResponse) + return waitForTaskCompletion(credentials, jsonResponse.task) +} + +def distributePublication( + String credentials, + String publicationHREF, + String distributionName, + String basePath, + String pulpHost = 'pulp.bbohard.lan', + String contentGuard = null +) { + 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) { + httpMode = 'PUT' + url = distribution.pulp_href + + } else { + httpMode = 'POST' + url = '/pulp/api/v3/distributions/deb/apt/' + } + def postBody = JsonOutput.toJson(["publication": publicationHREF, "name": distributionName, "base_path": basePath, "content_guard": contentGuard]) + response = httpRequest authentication: credentials, url: "https://${pulpHost}${url}", httpMode: httpMode, requestBody: postBody, contentType: 'APPLICATION_JSON', ignoreSslErrors: true, validResponseCodes: "100:599" + jsonResponse = readJSON text: response.content + if (distribution) { + waitForTaskCompletion(credentials, jsonResponse.task) + return [url] + } else { + return waitForTaskCompletion(credentials, jsonResponse.task) + } +} + +def waitForTaskCompletion( + String credentials, + String taskHREF, + String pulpHost = 'pulp.bbohard.lan' +) { + 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, + String pulpHost = 'pulp.bbohard.lan' +) { + def response = httpRequest authentication: credentials, url: "https://${pulpHost}${resourceHREF}", httpMode: 'GET', ignoreSslErrors: true + def jsonResponse = readJSON text: response.content + println(jsonResponse) + return jsonResponse.base_url +}