Ejemplo n.º 1
0
def upload_rpm(ctx, ip_address, pulp_repo, filepath, username, password,
               interactive):
    """
    Upload the rpm.
    """
    slab_logger.info('Uploading rpm to pulp repo')
    if not username:
        username = ctx.get_username()
    if not password:
        password = ctx.get_password(interactive)
    if not password or not username:
        slab_logger.error("Username is %s and password is %s. "
                          "Please, set the correct value for both and retry." %
                          (username, password))
        sys.exit(1)
    slab_logger.log(25, "Starting upload of {0}".format(filepath))
    url = "/pulp/api/v2/content/uploads/"
    val = pulp_utils.post(url, ip_address, ctx, username, password, "")
    res_json = json.loads(val)
    slab_logger.log(25, "Got upload id : %s " % (res_json['upload_id']))
    rpm_file = open(filepath, 'rb')
    offset = 0
    while True:
        chunk = rpm_file.read(100000)
        if chunk:
            put_url = "%s%s/%s/" % (url, res_json['upload_id'], offset)
            offset = offset + 100000
            slab_logger.log(25, ".", nl=False)
            val = pulp_utils.put(put_url, ip_address, ctx, username, password,
                                 chunk)
            slab_logger.log(25, ".", nl=False)
        else:
            break
    rpm_file.close()
    payload = '{"override_config": {}, "unit_type_id": "rpm", "upload_id": '\
              '"%s", "unit_key": {}, "unit_metadata": '\
              '{"checksum_type": null}}' % (res_json['upload_id'])
    post_url = '/pulp/api/v2/repositories/%s/actions'\
               '/import_upload/' % (pulp_repo)
    slab_logger.log(
        25, "\nImporting rpm to server for upload id: %s " %
        (res_json['upload_id']))
    time.sleep(2)
    val = pulp_utils.post(post_url, ip_address, ctx, username, password,
                          payload)
    payload = '{"override_config": {},  "id" : "yum_distributor"}'
    post_url = "/pulp/api/v2/repositories/%s/actions/publish/" % (pulp_repo)
    slab_logger.log(
        25, "Publishing rpm to server for upload id: %s " %
        (res_json['upload_id']))
    val = pulp_utils.post(post_url, ip_address, ctx, username, password,
                          payload)
    slab_logger.log(
        25, "Upload process completed for rpm {0}."
        "\nGot response from server : ".format(filepath))
    slab_logger.log(25, val)
Ejemplo n.º 2
0
def download_rpm(ctx, username, password, ip_address, pulp_repo, rpm,
                 interactive):
    """
    Download the artifact.
    """
    slab_logger.info('Downloading rpm from pulp repo')
    if not username:
        username = ctx.get_username()
    if not password:
        password = ctx.get_password(interactive)
    if not password or not username:
        slab_logger.error("Username is %s and password is %s. "
                          "Please, set the correct value for both and retry." %
                          (username, password))
        sys.exit(1)
    url = "/pulp/api/v2/distributors/search/"
    payload = '{"criteria":{"filters":{"repo_id":{"$eq": "%s"}}}}' % (
        pulp_repo)
    val = pulp_utils.post(url, ip_address, ctx, username, password, payload)
    res_json = json.loads(val)
    repo_json = filter(lambda x: x['repo_id'] == pulp_repo, res_json)

    if len(repo_json) > 0:
        url = "/pulp/api/v2/repositories/%s/search/units/" % (pulp_repo)
        payload = '{ "criteria": { "filters" : { "unit" : { "name" : "%s"}},'\
                  ' "fields": { "unit": [ "name", "filename" ] },'\
                  ' "type_ids": [ "rpm" ] } }' % (rpm)
        val = pulp_utils.post(url, ip_address, ctx, username, password,
                              payload)
        rpm_json = json.loads(val)
        if len(rpm_json) > 0:
            download_url = '%s/pulp/repos/%s/%s' % (
                ip_address, repo_json[0]['config']['relative_url'],
                rpm_json[0]['metadata']['filename'])
            slab_logger.log(25,
                            "Starting download from {0}".format(download_url))
            req = requests.get(download_url, verify=False)
            with open(rpm_json[0]['metadata']['filename'], 'wb') as rpm_file:
                for chunk in req.iter_content(chunk_size=1024):
                    if chunk:
                        rpm_file.write(chunk)
                        slab_logger.log(25, ".", nl=False)
            slab_logger.log(25, "\nDownload complete.")
            return
        else:
            slab_logger.error("Rpm %s could not be download since it was"
                              " not found in repo : %s" % (rpm, pulp_repo))
    else:
        slab_logger.error(
            "Repo with id %s does not exist. Unable to download the rpm." %
            (pulp_repo))
Ejemplo n.º 3
0
def list_rpms(ctx, ip_address, username, password, pulp_repo, interactive):
    """
    Lists rpms using Pulp Server API.
    """
    slab_logger.info('Listing rpms from pulp server')
    if not username:
        username = ctx.get_username()
    if not password:
        password = ctx.get_password(interactive)
    if not password or not username:
        slab_logger.error("Username is %s and password is %s. "
                          "Please, set the correct value for both and retry." %
                          (username, password))
        sys.exit(1)
    url = "/pulp/api/v2/repositories/%s/search/units/" % (pulp_repo)
    payload = '{ "criteria": { "fields": { "unit": [ "name",'\
              '"version", "filename", "relative_url" ] },'\
              '"type_ids": [ "rpm" ] } }'
    val = pulp_utils.post(url, ip_address, ctx, username, password, payload)
    rpms = json.loads(val)

    if rpms is not None and len(rpms) > 0:
        for rpm in rpms:
            slab_logger.log(25, "Id      : %s" % rpm["id"])
            slab_logger.log(25, "Filename: %s" % rpm["metadata"]["filename"])
            slab_logger.log(25, "Name    : %s" % rpm["metadata"]["name"])
            slab_logger.log(25, "Version : %s" % rpm["metadata"]["version"] + "\n")
    else:
        slab_logger.error("No rpms found in this repository.")
Ejemplo n.º 4
0
def find_pulp_repos(ctx, repo_name, ip_address, username, password,
                    interactive):
    """
    Lists rpms using Pulp Server API.
    """
    slab_logger.info('Searching for rps on Pulp server')
    if not username:
        username = ctx.get_username()
    if not password:
        password = ctx.get_password(interactive)
    if not password or not username:
        slab_logger.error("Username is %s and password is %s. "
                          "Please, set the correct value for both and retry." %
                          (username, password))
        sys.exit(1)
    url = "/pulp/api/v2/repositories/search/"
    payload = '{ "criteria": { "filters" : { "display_name" : {"$regex" : "%s"}}\
                             } }' % (repo_name)

    val = pulp_utils.post(url, ip_address, ctx, username, password, payload)
    repos = json.loads(val)

    if repos is not None and len(repos) > 0:
        for repo in repos:
            slab_logger.log(25, "Repo Id      : %s" % repo["id"])
            slab_logger.log(25, "Repo Name    : %s" % repo["display_name"])
            slab_logger.log(25, "Repo path    : %s" % repo["_href"] + "\n")
    else:
        slab_logger.error(
            "No matching repositories found on this pulp server.")
Ejemplo n.º 5
0
def display_rpm_status(ctx, rpm, pulp_repo, ip_address, username, password,
                       interactive):
    """
    Displays rpm stats.
    """
    slab_logger.info('Displaying rpm status')
    if not username:
        username = ctx.get_username()
    if not password:
        password = ctx.get_password(interactive)
    if not password or not username:
        slab_logger.error("Username is %s and password is %s. "
                          "Please, set the correct value for both and retry." %
                          (username, password))
        sys.exit(1)
    url = "/pulp/api/v2/repositories/%s/search/units/" % (pulp_repo)
    payload = '{ "criteria": { "filters" : { "unit" : { "name" : "%s"}},'\
              ' "fields": { "unit": [ "name", "version" ] }, "type_ids":'\
              ' [ "rpm" ] } }' % (rpm)
    val = pulp_utils.post(url, ip_address, ctx, username, password, payload)
    slab_logger.log(25, 'Listing rpm stats for rpm : %s' % (rpm))
    slab_logger.log(25, json.dumps(json.loads(val), indent=4, sort_keys=True))