def _list(cell, traits, partition): """List servers""" admin_srv = context.GLOBAL.admin.server() servers = admin_srv.list({'cell': cell, 'traits': cli.combine(traits), 'partition': partition}) cli.out(formatter(servers))
def configure(server, features, parent, partition, memory, cpu, disk): """Create, get or modify server configuration""" if parent: path = parent.split('/') bucket = None for bucket, bucket_parent in zip(path, [None] + path[:-1]): masterapi.create_bucket(context.GLOBAL.zk.conn, bucket, bucket_parent) assert bucket is not None, 'server topology missing.' masterapi.create_server(context.GLOBAL.zk.conn, server, bucket, partition=partition) features = cli.combine(features) if features: # This is special case - reset features to empty. if features == ['-']: features = [] masterapi.update_server_features(context.GLOBAL.zk.conn, server, features) if memory or cpu or disk: masterapi.update_server_capacity(context.GLOBAL.zk.conn, server, memory=memory, cpu=cpu, disk=disk) server_obj = masterapi.get_server(context.GLOBAL.zk.conn, server) server_obj['name'] = server cli.out(formatter(server_obj))
def reserve(allocation, cell, memory, cpu, disk, rank, max_utilization, traits, label): """Reserve capacity on a given cell""" admin_cell_alloc = admin.CellAllocation(context.GLOBAL.ldap.conn) data = {} if memory: data['memory'] = memory if cpu: data['cpu'] = cpu if disk: data['disk'] = disk if rank is not None: data['rank'] = rank if max_utilization is not None: data['max_utilization'] = max_utilization if traits: data['traits'] = cli.combine(traits) if label: if label == '-': label = None data['label'] = label try: admin_cell_alloc.create([cell, allocation], data) except ldap3.LDAPEntryAlreadyExistsResult: admin_cell_alloc.update([cell, allocation], data) try: admin_alloc = admin.Allocation(context.GLOBAL.ldap.conn) cli.out(formatter(admin_alloc.get(allocation))) except ldap3.LDAPNoSuchObjectResult: click.echo('Allocation does not exist: %s' % allocation, err=True)
def configure(cell, traits, server, partition, data): """Create, get or modify server configuration""" admin_srv = context.GLOBAL.admin.server() attrs = {} if cell: attrs['cell'] = cell if traits: attrs['traits'] = cli.combine(traits) if partition: if partition == '-': partition = None attrs['partition'] = partition if data: with io.open(data, 'r') as fd: attrs['data'] = json.loads(fd.read()) if attrs: try: admin_srv.create(server, attrs) except admin_exceptions.AlreadyExistsResult: admin_srv.update(server, attrs) try: cli.out(formatter(admin_srv.get(server, dirty=bool(attrs)))) except admin_exceptions.NoSuchObjectResult: cli.bad_exit('Server does not exist: %s', server)
def configure(cell, traits, server, partition, data): """Create, get or modify server configuration""" admin_srv = admin.Server(context.GLOBAL.ldap.conn) attrs = {} if cell: attrs['cell'] = cell if traits: attrs['traits'] = cli.combine(traits) if partition: if partition == '-': partition = None attrs['partition'] = partition if data: if data == ['-']: data = None attrs['data'] = data if attrs: try: admin_srv.create(server, attrs) except ldap3.LDAPEntryAlreadyExistsResult: admin_srv.update(server, attrs) try: cli.out(formatter(admin_srv.get(server))) except ldap3.LDAPNoSuchObjectResult: click.echo('Server does not exist: %s' % server, err=True)
def _list(cell, features): """Displays servers list.""" admin_srv = admin.Server(context.GLOBAL.ldap.conn) servers = admin_srv.list({ 'cell': cell, 'features': cli.combine(features) }) cli.out(formatter(servers))
def reserve(allocation, env, cell, partition, rank, rank_adjustment, max_utilization, empty, memory, cpu, disk, traits): """Reserve capacity on the cell for given environment.""" _check_reserve_usage(empty, memory, cpu, disk) restapi = context.GLOBAL.admin_api(ctx.get('api')) _check_tenant_exists(restapi, allocation) _make_allocation(restapi, allocation, env) data = {} if empty: data['memory'] = '0M' data['disk'] = '0M' data['cpu'] = '0%' if memory: data['memory'] = memory if cpu: data['cpu'] = cpu if disk: data['disk'] = disk if rank is not None: data['rank'] = rank if rank_adjustment is not None: data['rank_adjustment'] = rank_adjustment if max_utilization is not None: data['max_utilization'] = max_utilization if partition: data['partition'] = partition if traits: data['traits'] = cli.combine(traits) if data: reservation_url = '/allocation/{}/{}/reservation/{}'.format( allocation, env, cell) try: existing = restclient.get(restapi, reservation_url).json() # TODO: need cleaner way of deleting attributes that are not # valid for update. It is a hack. for attr in list(existing): if (attr not in ['memory', 'cpu', 'disk', 'partition']): del existing[attr] existing.update(data) restclient.put(restapi, reservation_url, payload=existing) except restclient.NotFoundError: # some attributes need default values when creating if not partition: data['partition'] = admin.DEFAULT_PARTITION restclient.post(restapi, reservation_url, payload=data) _display_tenant(restapi, allocation)
def _list(cell, traits, label): """List servers""" admin_srv = admin.Server(context.GLOBAL.ldap.conn) servers = admin_srv.list({ 'cell': cell, 'traits': cli.combine(traits), 'label': label }) cli.out(formatter(servers))
def reserve(allocation, env, cell, partition, rank, rank_adjustment, max_utilization, empty, memory, cpu, disk, traits): """Reserve capacity on the cell for given environment.""" _check_reserve_usage(empty, memory, cpu, disk) restapi = context.GLOBAL.admin_api(ctx.get('api')) _check_tenant_exists(restapi, allocation) _make_allocation(restapi, allocation, env) data = {} if empty: data['memory'] = '0M' data['disk'] = '0M' data['cpu'] = '0%' if memory: data['memory'] = memory if cpu: data['cpu'] = cpu if disk: data['disk'] = disk if partition: data['partition'] = partition reservation_url = '/allocation/{}/{}/reservation/{}'.format( allocation, env, cell) try: existing = restclient.get(restapi, reservation_url).json() if data: # To meet the json schema required in create if 'memory' not in data: data['memory'] = existing['memory'] if 'cpu' not in data: data['cpu'] = existing['cpu'] if 'disk' not in data: data['disk'] = existing['disk'] if 'partition' not in data: data['partition'] = existing['partition'] restclient.put(restapi, reservation_url, payload=data) except restclient.NotFoundError: if rank is not None: data['rank'] = rank if rank_adjustment is not None: data['rank_adjustment'] = rank_adjustment if max_utilization is not None: data['max_utilization'] = max_utilization if traits: data['traits'] = cli.combine(traits) restclient.post(restapi, reservation_url, payload=data) _display_tenant(restapi, allocation)
def reserve(allocation, cell, env, memory, cpu, disk, rank, rank_adjustment, max_utilization, traits, partition, delete): # pylint: disable=R0915 """Reserve capacity on a given cell""" _check_tenant_exists(allocation) admin_cell_alloc = context.GLOBAL.admin.cell_allocation() allocation_env = allocation + '/' + env if delete: admin_cell_alloc.delete([cell, allocation_env]) return _make_allocation(allocation, env) data = {} if memory: data['memory'] = memory if cpu: data['cpu'] = cpu if disk: data['disk'] = disk if partition: if partition == '-': partition = _DEFAULT_PARTITION data['partition'] = partition if rank is not None: data['rank'] = rank if rank_adjustment is not None: data['rank_adjustment'] = rank_adjustment if max_utilization is not None: data['max_utilization'] = max_utilization if traits: data['traits'] = cli.combine(traits) try: existing = admin_cell_alloc.get([cell, allocation_env]) if data: admin_cell_alloc.update([cell, allocation_env], data) except admin_exceptions.NoSuchObjectResult: if memory is None: data['memory'] = '0M' if cpu is None: data['cpu'] = '0%' if disk is None: data['disk'] = '0M' if partition is None: data['partition'] = _DEFAULT_PARTITION if rank is None: data['rank'] = _DEFAULT_RANK admin_cell_alloc.create([cell, allocation_env], data) _display_tenant(allocation)
def configure(features, bucket): """Create, get or modify bucket configuration""" features = cli.combine(features) if features: # This is special case - reset features to empty. if features == ['-']: features = None masterapi.update_bucket_features(context.GLOBAL.zk.conn, bucket, features) data = masterapi.get_bucket(context.GLOBAL.zk.conn, bucket) data['name'] = bucket cli.out(formatter(data))
def reserve(allocation, cell, memory, cpu, disk, rank, rank_adjustment, max_utilization, traits, partition, delete): """Reserve capacity on a given cell""" admin_cell_alloc = context.GLOBAL.admin.cell_allocation() if delete: admin_cell_alloc.delete([cell, allocation]) return data = {} if memory: data['memory'] = memory if cpu: data['cpu'] = cpu if disk: data['disk'] = disk if rank is not None: data['rank'] = rank if rank_adjustment is not None: data['rank_adjustment'] = rank_adjustment if max_utilization is not None: data['max_utilization'] = max_utilization if traits: data['traits'] = cli.combine(traits) if partition: if partition == '-': partition = None data['partition'] = partition try: cell_alloc = admin_cell_alloc.get([cell, allocation], dirty=True) except admin_exceptions.NoSuchObjectResult: cell_alloc = None if not cell_alloc: admin_cell_alloc.create([cell, allocation], data) else: cell_alloc.update(data) admin_cell_alloc.update([cell, allocation], cell_alloc) try: admin_alloc = context.GLOBAL.admin.allocation() cli.out(formatter(admin_alloc.get(allocation, dirty=True))) except admin_exceptions.NoSuchObjectResult: click.echo('Allocation does not exist: %s' % allocation, err=True)
def configure(cell, features, server): """Get or modify server configuration.""" admin_srv = admin.Server(context.GLOBAL.ldap.conn) attrs = {} if cell: attrs['cell'] = cell if features: attrs['features'] = cli.combine(features) if attrs: try: admin_srv.create(server, attrs) except ldap3.LDAPEntryAlreadyExistsResult: admin_srv.update(server, attrs) try: cli.out(formatter(admin_srv.get(server))) except ldap3.LDAPNoSuchObjectResult: click.echo('Server does not exist: %s' % server, err=True)
def reserve(allocation, cell, memory, cpu, disk, rank, rank_adjustment, max_utilization, traits, partition, delete): """Reserve capacity on a given cell""" admin_cell_alloc = admin.CellAllocation(context.GLOBAL.ldap.conn) if delete: return admin_cell_alloc.delete([cell, allocation]) data = {} if memory: data['memory'] = memory if cpu: data['cpu'] = cpu if disk: data['disk'] = disk if rank is not None: data['rank'] = rank if rank_adjustment is not None: data['rank_adjustment'] = rank_adjustment if max_utilization is not None: data['max_utilization'] = max_utilization if traits: data['traits'] = cli.combine(traits) if partition: if partition == '-': partition = None data['partition'] = partition try: admin_cell_alloc.create([cell, allocation], data) except ldap_exceptions.LDAPEntryAlreadyExistsResult: admin_cell_alloc.update([cell, allocation], data) try: admin_alloc = admin.Allocation(context.GLOBAL.ldap.conn) cli.out(formatter(admin_alloc.get(allocation))) except ldap_exceptions.LDAPNoSuchObjectResult: click.echo('Allocation does not exist: %s' % allocation, err=True)
def test_combine(self): """Test combining lists.""" self.assertEqual(None, cli.combine(['-']))