Last active
November 29, 2017 17:49
-
-
Save elijahsgh/3a1e3f0b04f3924bf9c43c64e67f41df to your computer and use it in GitHub Desktop.
Upload packages from CI to Katello, basic PoC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# Based on https://gist.github.com/jlsherrill/4fa35b787147e6f087fd302dfae2af25 | |
# If Foreman reports an invalid package then reduce the chunk size. | |
import requests | |
import argparse | |
import sys | |
def getorganization(foremansettings, org_name): | |
request_url = foremansettings.url + '/katello/api/organizations' | |
r = requests.get(request_url, verify=foremansettings.verifyssl, | |
headers={'Content-Type': 'application/json'}, | |
auth=foremansettings.auth) | |
response = r.json() | |
for result in response['results']: | |
if result['name'].lower() == org_name.lower(): | |
return result['id'] | |
print "Organization not found." | |
sys.exit(-1) | |
def getrepositories(foremansettings, org_id, repo_name): | |
request_url = foremansettings.url + '/katello/api/repositories' | |
r = requests.get(request_url, verify=foremansettings.verifyssl, | |
headers={'Content-Type': 'application/json'}, | |
auth=foremansettings.auth, | |
params={'organization_id': org_id}) | |
response = r.json() | |
for result in response['results']: | |
if result['name'].lower() == repo_name.lower(): | |
return result['id'] | |
print "Repository not found" | |
sys.exit(-1) | |
def getupload_id(foremansettings, repository_id): | |
request_url = foremansettings.url + \ | |
'/katello/api/repositories/{0}/content_uploads'.format(repository_id) | |
r = requests.post(request_url, | |
verify=foremansettings.verifyssl, | |
headers={'Content-Type': 'application/json'}, | |
auth=foremansettings.auth) | |
return r.json()['upload_id'] | |
def upload_package(foremansettings, repository_id, upload_id, package): | |
request_url = foremansettings.url + \ | |
'/katello/api/repositories/{0}/content_uploads/{1}'.format( | |
repository_id, upload_id) | |
offset = 0 | |
chunksize = 2 * 1024 * 1024 | |
print "Uploading with upload_id {0}".format(upload_id) | |
with open(package, 'r') as f: | |
chunk = f.read(chunksize) | |
while len(chunk) > 0: | |
r = requests.put(request_url, | |
verify=foremansettings.verifyssl, | |
headers={'Accept': 'application/json;version=2', | |
'Content-Type': 'multipart/form-data'}, | |
data={'offset': offset, | |
'content': chunk}, | |
auth=foremansettings.auth) | |
offset += len(chunk) | |
chunk = f.read(chunksize) | |
def import_package(foremansettings, repository_id, upload_id): | |
request_url = foremansettings.url + \ | |
'/katello/api/repositories/{0}/import_uploads'.format(repository_id) | |
r = requests.put(request_url, | |
headers={'Content-Type': 'application/json'}, | |
json={'upload_ids': [upload_id]}, | |
verify=foremansettings.verifyssl, | |
auth=foremansettings.auth) | |
print "Importing {0} returned {1}".format(upload_id, r.status_code) | |
def delete_upload_id(foremansettings, repository_id, upload_id=True): | |
request_url = foremansettings.url + \ | |
'/katello/api/repositories/{0}/content_uploads/{1}'.format( | |
repository_id, upload_id) | |
r = requests.delete(request_url, | |
verify=foremansettings.verifyssl, | |
headers={'Content-Type': 'application/json'}, | |
auth=foremansettings.auth) | |
print "Deleting {0} returned {1}.".format(upload_id, r.status_code) | |
def getargs(): | |
parser = argparse.ArgumentParser( | |
description="Upload large packages to Katello repository.") | |
parser.add_argument('--noverifyssl', dest='verifyssl', action='store_false', | |
default=True, help='Disable certificate check') | |
parser.add_argument('--org', dest='orgname', required=True, | |
help='Organization name') | |
parser.add_argument('--repo', dest='reponame', required=True, | |
help='Repository name') | |
parser.add_argument('url', | |
help='Foreman host ex: https://myforemanserver.internal') | |
parser.add_argument('-u', dest='user', required=True, | |
help='User for Foreman user') | |
parser.add_argument('-p', dest='password', required=True, | |
help='Password for Foreman user') | |
parser.add_argument('pkgs', nargs='*', | |
help='Package(s) to upload') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
foremansettings = getargs() | |
if foremansettings.verifyssl == False: | |
requests.packages.urllib3.disable_warnings() | |
orgname = foremansettings.orgname | |
reponame = foremansettings.reponame | |
pkgs = foremansettings.pkgs | |
foremansettings.auth = (foremansettings.user, foremansettings.password) | |
org_id = getorganization(foremansettings, orgname) | |
repository_id = getrepositories(foremansettings, org_id, reponame) | |
upload_id = getupload_id(foremansettings, repository_id) | |
for pkg in pkgs: | |
print 'Package: {}'.format(pkg) | |
upload_package(foremansettings, repository_id, upload_id, pkg) | |
import_package(foremansettings, repository_id, upload_id) | |
delete_upload_id(foremansettings, repository_id, upload_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment