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()])
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)
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)
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()])
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)
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())
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)
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)
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])
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()))
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()])
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)
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)
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)
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)
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})
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()])
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)
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)
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()])
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()))
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()])
def group_list(opts): url = inventory_url(opts.service, 'groups') with api_from_opts(opts) as api: do_simple_get(api, url)
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)