Jenkins/vars/pulp.groovy

134 lines
5.4 KiB
Groovy
Raw Normal View History

import groovy.json.JsonOutput
def exportPackages(
String credentials,
List packages = [],
2022-02-08 09:28:59 +01:00
String pulpHost = '192.168.30.3'
) {
def exportTasks = []
packages.each {
2022-02-09 21:03:53 +01:00
def response = httpRequest authentication: credentials, url: "https://${pulpHost}/pulp/api/v3/content/deb/packages/", httpMode: 'POST', ignoreSslErrors: true, multipartName: "file", timeout: 900, uploadFile: "${it}"
2022-02-09 20:43:21 +01:00
def jsonResponse = readJSON text: response.content
exportTasks << jsonResponse['task']
}
return exportTasks
}
2022-02-09 19:05:31 +01:00
def createRepository(
String credentials,
String name,
String pulpHost = '192.168.30.3'
) {
def repositoryName = ["name": name]
def postBody = JsonOutput.toJson(repositoryName)
def response = httpRequest authentication: credentials, url: "https://${pulpHost}/pulp/api/v3/repositories/deb/apt/", httpMode: 'POST', requestBody: postBody, contentType: 'APPLICATION_JSON', ignoreSslErrors: true, validResponseCodes: "100:399"
def jsonResponse = readJSON text: response.content
return jsonResponse.pulp_href
2022-02-09 19:05:31 +01:00
}
def getRepositoryHREF(
String credentials,
String repositoryLevel = 'dev',
2022-02-08 09:28:59 +01:00
String pulpHost = '192.168.30.3'
) {
2022-02-09 19:05:31 +01:00
def repositoriesMapping = ['unstable': 'Cadoles4MSE unstable',
'dev': 'Cadoles4MSE dev',
'staging': 'Cadoles4MSE staging',
'stable': 'Cadoles4MSE stable']
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'] == repositoriesMapping[repositoryLevel] }
2022-02-09 19:05:31 +01:00
if (repositoryHREF) {
return repositoryHREF.pulp_href
} else {
return createRepository(credentials, repositoriesMapping[repositoryLevel])
2022-02-09 19:05:31 +01:00
}
}
def addToRepository(
String credentials,
List packagesHREF,
String repositoryHREF,
2022-02-08 09:28:59 +01:00
String pulpHost = '192.168.30.3'
) {
def packagesHREFURL = ["add_content_units": packagesHREF.collect { "https://$pulpHost$it" }]
def postBody = JsonOutput.toJson(packagesHREFURL)
2022-02-09 19:05:31 +01:00
def response = httpRequest authentication: credentials, url: "https://${pulpHost}${repositoryHREF}modify/", httpMode: 'POST', requestBody: postBody, contentType: 'APPLICATION_JSON', ignoreSslErrors: true, validResponseCodes: "100:399"
def jsonResponse = readJSON text: response.content
return waitForTaskCompletion(credentials, jsonResponse.task)
}
def publishRepository(
String credentials,
String repositoryHREF,
2022-02-08 09:28:59 +01:00
String pulpHost = '192.168.30.3'
) {
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
return waitForTaskCompletion(credentials, jsonResponse.task)
}
def distributePublication(
String credentials,
String publicationHREF,
String distributionName,
String basePath,
2022-02-08 09:28:59 +01:00
String pulpHost = '192.168.30.3',
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])
2022-02-09 19:05:31 +01:00
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) {
waitForTaskCompletion(credentials, jsonResponse.task)
return [url]
} else {
return waitForTaskCompletion(credentials, jsonResponse.task)
}
}
def waitForTaskCompletion(
String credentials,
String taskHREF,
2022-02-08 09:28:59 +01:00
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,
2022-02-08 09:28:59 +01:00
String pulpHost = '192.168.30.3'
) {
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
}