예제 #1
0
def list_device_auths(opts):
    with api_from_opts(opts) as api:
        do_simple_get(
            api,
            admissions_url(opts.service),
            printer=lambda rsp:
            [dump_device_auth(dev, showkey=False) for dev in rsp.json()])
예제 #2
0
def devices_list(opts):
    def devlist_printer(rsp):
        devslist = rsp.json()
        logging.info("Devices:")
        if opts.format == 'plain':
            for dev in devslist:
                attrs = repack_attrs(dev.get('attributes'))
                result = ""
                if opts.attributes:
                    attributes = opts.attributes.split(",")
                    for attribute in attributes:
                        attribute = attribute.strip()
                        if attribute == 'id':
                            result = result + "{}={} ".format(attribute, dev['id'])
                        elif attribute == 'updated':
                            result = result + "{}={} ".format(attribute, dev['updated_ts'])
                        else:
                            result = result + "{}={} ".format(attribute, attrs.get(attribute, '<undefined>'))
                print(result)

        elif opts.format == 'json':
            jsonprinter(rsp)

    # TODO add pagination (go through all pages)
    url = inventory_url(opts.service, '/devices?per_page={}'.format(opts.limit))
    with api_from_opts(opts) as api:
        do_simple_get(api, url, printer=devlist_printer)
예제 #3
0
def do_deployments_logs(opts):
    logging.debug('get log for deployment %s on device %s', opts.id,
                  opts.devid)
    url = deployments_url(opts.service,
                          '{}/devices/{}/log'.format(opts.id, opts.devid))
    with api_from_opts(opts) as api:
        do_simple_get(api, url, printer=simpleprinter)
예제 #4
0
def list_users(opts):
    logging.info('list users')
    with api_from_opts(opts) as api:
        do_simple_get(
            api,
            user_url(opts.service, '/users'),
            printer=lambda rsp: [dump_user(user) for user in rsp.json()])
예제 #5
0
def devices_list(opts):
    def devlist_printer(rsp):
        devslist = rsp.json()
        print('devices:')
        for dev in devslist:
            attrs = repack_attrs(dev.get('attributes'))
            print('  {} (type: {}, updated: {})'.format(
                dev['id'], attrs.get('device_type', '<undefined>'),
                dev['updated_ts']))

    url = inventory_url(opts.service, '/devices')
    with api_from_opts(opts) as api:
        do_simple_get(api, url, printer=devlist_printer)
예제 #6
0
def device_show(opts):
    url = inventory_url(opts.service, '/devices/{}'.format(opts.device))
    with api_from_opts(opts) as api:
        rsp = do_simple_get(api, url)
        logging.debug("%r", rsp.status_code)

        dump_device_attributes(rsp.json())
예제 #7
0
def device_show(opts):
    url = inventory_url(opts.service, '/devices/{}'.format(opts.device))

    with api_from_opts(opts) as api:
        rsp = do_simple_get(api, url)
        logging.debug("%r", rsp.status_code)

        dump_device_attributes(rsp.json())
예제 #8
0
def download_image(url, deployment_id, store=False, **kwargs):
    rsp = do_simple_get(url, stream=True, **kwargs)
    logging.debug('status %s', rsp.status_code)
    if rsp.status_code == 200:
        if store:
            with tempfile.NamedTemporaryFile(prefix=deployment_id[0:8], dir=os.getcwd()) as f:
                for chunk in rsp.iter_content(chunk_size=1024):
                    if chunk:
                        f.write(chunk)
    else:
        logging.error('failed to download image from %s: %s', url, rsp.text)
예제 #9
0
def download_image(url, deployment_id, store=False, **kwargs):
    rsp = do_simple_get(url, stream=True, **kwargs)
    logging.debug('status %s', rsp.status_code)
    if rsp.status_code == 200:
        if store:
            with tempfile.NamedTemporaryFile(prefix=deployment_id[0:8],
                                             dir=os.getcwd()) as f:
                for chunk in rsp.iter_content(chunk_size=1024):
                    if chunk:
                        f.write(chunk)
    else:
        logging.error('failed to download image from %s: %s', url, rsp.text)
예제 #10
0
def device_group(opts):
    url = inventory_url(opts.service, '/devices/{}/group'.format(opts.device))
    if not opts.group_set and not opts.group_delete:
        with api_from_opts(opts) as api:
            do_simple_get(api, url)
    elif opts.group_set:
        group = {
            'group': opts.group_set,
        }
        method = 'PUT'
    elif opts.group_delete:
        url = inventory_url(opts.service, '/devices/{}/group/{}'.format(opts.device,
                                                                        opts.group_delete))
        group = {
            'group': opts.group_delete,
        }
        method = 'DELETE'

    with api_from_opts(opts) as api:
        do_request(api, url, method=method, success=204,
                   json=group)
예제 #11
0
def do_update(opts):
    def updateprinter(rsp):
        if rsp.status_code == 204:
            print('no update available')
        elif rsp.status_code == 200:
            jsonprinter(rsp)
        else:
            errorprinter(rsp)

    url = device_url(opts.service, '/deployments/device/deployments/next')
    with device_api_from_opts(opts) as api:
        return do_simple_get(api, url, printer=updateprinter,
                             success=[200, 204])
예제 #12
0
def do_update(opts):
    def updateprinter(rsp):
        if rsp.status_code == 204:
            print('no update available')
        elif rsp.status_code == 200:
            jsonprinter(rsp)
        else:
            errorprinter(rsp)

    url = device_url(opts.service, '/deployments/device/deployments/next')
    with device_api_from_opts(opts) as api:
        return do_simple_get(api,
                             url,
                             printer=updateprinter,
                             success=[200, 204])
예제 #13
0
def show_device_auth(opts):
    url = admissions_url(opts.service, '/{}'.format(opts.device))
    with api_from_opts(opts) as api:
        rsp = do_simple_get(api, url,
                            printer=lambda rsp: dump_device_auth(rsp.json()))
예제 #14
0
def list_device_auths(opts):
    with api_from_opts(opts) as api:
        do_simple_get(api, admissions_url(opts.service),
                      printer=lambda rsp: [dump_device_auth(dev,
                                                            showkey=False)
                                           for dev in rsp.json()])
예제 #15
0
def do_artifacts_list(opts):
    logging.debug('list artifacts')
    url = artifacts_url(opts.service)
    with api_from_opts(opts) as api:
        do_simple_get(api, url)
예제 #16
0
def do_artifacts_show(opts):
    logging.debug('get artifact %s download', opts.id)
    url = artifacts_url(opts.service, opts.id)
    with api_from_opts(opts) as api:
        do_simple_get(api, url)
예제 #17
0
def do_deployments_status(opts):
    logging.debug('get deployment %s', opts.id)
    url = deployments_url(opts.service, opts.id)
    with api_from_opts(opts) as api:
        do_simple_get(api, url)
예제 #18
0
def group_show(opts):
    url = inventory_url(opts.service, 'groups/{}/devices'.format(opts.group))
    with api_from_opts(opts) as api:
        do_simple_get(api, url)
예제 #19
0
def do_deployments_find(opts):
    logging.debug('lookup deployment %s', opts.name)
    url = deployments_url(opts.service)
    with api_from_opts(opts) as api:
        do_simple_get(api, url, params={'name': opts.name})
예제 #20
0
def do_deployments_logs(opts):
    logging.debug('get log for deployment %s on device %s', opts.id, opts.devid)
    url = deployments_url(opts.service,
                          '{}/devices/{}/log'.format(opts.id, opts.devid))
    with api_from_opts(opts) as api:
        do_simple_get(api, url, printer=simpleprinter)
예제 #21
0
def list_users(opts):
    logging.info('list users')
    with api_from_opts(opts) as api:
        do_simple_get(api, user_url(opts.service, '/users'),
                      printer=lambda rsp: [dump_user(user)
                                           for user in rsp.json()])
예제 #22
0
def do_deployments_devices(opts):
    logging.debug('get deployment %s devices', opts.id)
    url = deployments_url(opts.service,
                          '{}/devices'.format(opts.id))
    with api_from_opts(opts) as api:
        do_simple_get(api, url)
예제 #23
0
def do_deployments_status(opts):
    logging.debug('get deployment %s', opts.id)
    url = deployments_url(opts.service, opts.id)
    with api_from_opts(opts) as api:
        do_simple_get(api, url)
예제 #24
0
def count_devices(opts):
    url = authentication_url(opts.service, '/devices/count?{}'.format(opts.status))
    with api_from_opts(opts) as api:
        rsp = do_simple_get(api, url)
예제 #25
0
def list_devices(opts):
    with api_from_opts(opts) as api:
        do_simple_get(api, authentication_url(opts.service, '/devices'),
                      printer=lambda rsp: [dump_device_brief(dev)
                                           for dev in rsp.json()])
예제 #26
0
def show_device(opts):
    url = authentication_url(opts.service, '/devices/{}'.format(opts.device))
    with api_from_opts(opts) as api:
        rsp = do_simple_get(api, url,
                            printer=lambda rsp: dump_device(rsp.json()))
예제 #27
0
def show_device_auth(opts):
    url = admissions_url(opts.service, '/{}'.format(opts.device))
    with api_from_opts(opts) as api:
        rsp = do_simple_get(api,
                            url,
                            printer=lambda rsp: dump_device_auth(rsp.json()))
예제 #28
0
def count_devices(opts):
    url = authentication_url(opts.service,
                             '/devices/count?{}'.format(opts.status))
    with api_from_opts(opts) as api:
        rsp = do_simple_get(api, url)
예제 #29
0
def show_device(opts):
    url = authentication_url(opts.service, '/devices/{}'.format(opts.device))
    with api_from_opts(opts) as api:
        rsp = do_simple_get(api,
                            url,
                            printer=lambda rsp: dump_device(rsp.json()))
예제 #30
0
def list_devices(opts):
    with api_from_opts(opts) as api:
        do_simple_get(
            api,
            authentication_url(opts.service, '/devices'),
            printer=lambda rsp: [dump_device_brief(dev) for dev in rsp.json()])
예제 #31
0
def group_list(opts):
    url = inventory_url(opts.service, 'groups')
    with api_from_opts(opts) as api:
        do_simple_get(api, url)
예제 #32
0
def do_artifacts_download(opts):
    logging.debug('get artifact %s download', opts.id)
    url = artifacts_url(opts.service, '/{}/download'.format(opts.id))

    with api_from_opts(opts) as api:
        do_simple_get(api, url)
예제 #33
0
def do_deployments_find(opts):
    logging.debug('lookup deployment %s', opts.name)
    url = deployments_url(opts.service)
    with api_from_opts(opts) as api:
        do_simple_get(api, url, params={'name': opts.name})
예제 #34
0
def do_deployments_devices(opts):
    logging.debug('get deployment %s devices', opts.id)
    url = deployments_url(opts.service, '{}/devices'.format(opts.id))
    with api_from_opts(opts) as api:
        do_simple_get(api, url)