def _get_ids(**kwargs): resc_id = kwargs.get('id') if resc_id: return [resc_id] name = kwargs.get('name') if not name: raise utils.InnerException('Neither resource id nor name is passed.') ids = _get_ids_by_name(**kwargs) if not ids: raise utils.InnerException('No resource found with give name') return ids
def list_listener(**kwargs): base = "neutron lbaas-listener-list" if kwargs['lb'] is None: raise utils.InnerException( 'ERROR: arguments missed, try use -h to find them') lb_id = kwargs['lb'] and '--loadbalancer_id=%s' % kwargs['lb'] or '' return ' '.join([base, lb_id])
def list_sg_rule(**kwargs): base = "neutron security-group-rule-list" if kwargs['sg'] is None: raise utils.InnerException( 'ERROR: arguments missed, try use -h to find them') sg_id = kwargs['sg'] and '--security_group_id=%s' % kwargs['sg'] or '' return ' '.join([base, sg_id])
def list_portv2(**kwargs): if kwargs['id']: pass else: filters = {} if kwargs['owner']: if kwargs['owner'] in const.DEVICE_OWNERS: filters['device_owner'] = const.DEVICE_OWNERS[kwargs['owner']] else: raise utils.InnerException( 'Unknown device_owner. Support list: %r' % (const.DEVICE_OWNERS)) if kwargs['host']: filters['binding:host_id'] = kwargs['host'] if kwargs['net']: filters['network_id'] = kwargs['net'] if kwargs['snet']: filters['fixed_ips=subnet_id'] = kwargs['snet'] if kwargs['ip']: filters['fixed_ips=ip_address'] = kwargs['ip'] if kwargs['device']: filters['device_id'] = kwargs['device'] data = api_cmd.get_resources('ports', filters) for d in data: for k in d: fmt = "{:<25} | {:<15}" print fmt.format(k, d[k])
def api(**kwargs): token = kwargs.get('token') or kwargs['env_token'] if not token: raise utils.InnerException( 'ERROR: The api command need X-Auth-Token, use -t to assign or ' 'set OCEANA_OS_TOKEN to use it.\n' 'We cannot get token automatically by openstackclient, since it ' 'is not installed, or we have no permission to run it.') api_ip = kwargs.get('server_ip') or kwargs['env_api_ip'] if not api_ip: raise utils.InnerException( 'ERROR: The api command need API service IP, use -s to assign or ' ' set OCEANA_OS_API_IP to use it') kwargs.update({'token': token, 'api_ip': api_ip}) action = kwargs['action'] return getattr(os.sys.modules[__name__], action + '_api')(**kwargs)
def get_br_node_ex_intf_info(): pi = inner_call( **{'cmd': 'ovsdb-client dump Open_vSwitch Interface name ofport'})[0] intf_dict = {} inner_ofp = re.search(re.compile('"2-br-ex"\s+(\w+)'), pi) if is_net_node(): if not inner_ofp: raise utils.InnerException( 'Interface 2-br-ex not exists, try to rerun with -i and -p first.' ) intf_dict['inner'] = inner_ofp.groups()[0] for intf, ofp in re.findall(re.compile('"(\d+-\d+-\d+-\d+)"\s+(\d+)'), pi): intf_dict[intf.replace('-', '.')] = ofp if len(intf_dict) == 1: raise utils.InnerException( 'No formatted interface found, try to rerun with -i and -p first.') return intf_dict
def ping_fip(**kwargs): fip_ip = kwargs['sub_resource'] if not netaddr.valid_ipv4(fip_ip): raise utils.InnerException('%s is not a valid IPv4 address' % fip_ip) filters = {'floating_ip_address': fip_ip} fip_data = api_cmd.get_resources('floatingips', filters)[0] router = api_cmd.get_resource('routers', fip_data['router_id']) router_gw_ip = router['external_gateway_info'].get( 'external_fixed_ips')[0]['ip_address'] router_host = api_cmd.get_resource('router_host', router['id']) print router_host
def delete_api(**kwargs): kwargs.update({'action': 'DELETE'}) resc_ids = _get_ids(**kwargs) if len(resc_ids) > 1 and not kwargs.get('force', False): raise utils.InnerException( 'Multiple resources found by given name, use -f to force delete') curl = curl_base + '/%(resc_id)s.json' ret = [] for id in resc_ids: kwargs.update({'resc_id': id}) ret.append(curl % kwargs) return ret
def build_fakenet(**kwargs): if kwargs['o']: set_flows(**kwargs) elif kwargs['i']: set_local(**kwargs) if kwargs['p']: for peer_ip in kwargs['p']: if not na.valid_ipv4(peer_ip): raise utils.InnerException('Invalid peer IP %s' % peer_ip) reach = subprocess.Popen('ping -c1 -w1 %s -I %s ' % (peer_ip, kwargs['i']), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).wait() == 0 if not reach: raise utils.InnerException( 'Peer IP %s is unreachable from %s' % (peer_ip, kwargs['i'])) set_peer(**kwargs) else: raise utils.InnerException( 'ERROR: arguments missed, try use -h to find them')
def list_port(**kwargs): debug = kwargs['debug'] and ' --debug ' or ' ' base = "neutron" + debug + "port-list" host = kwargs['host'] and '--binding:host_id=%s' % kwargs['host'] or '' net = kwargs['net'] and '--network_id=%s' % kwargs['net'] or '' snet = kwargs['snet'] and 'subnet_id=%s' % kwargs['snet'] or '' ip = kwargs['ip'] and 'ip_address=%s' % kwargs['ip'] or '' fixed_ip = ' '.join([i for i in (snet, ip) if i]) fixed_ip = '--fixed_ips %s' % fixed_ip if fixed_ip else '' device = kwargs['device'] and '--device_id=%s' % kwargs['device'] or '' owner = kwargs['owner'] if owner: if owner in const.DEVICE_OWNERS: owner = '--device_owner=%s' % const.DEVICE_OWNERS[owner] else: raise utils.InnerException( 'Unknown device_owner. Support list: %r' % (const.DEVICE_OWNERS)) ret = ' '.join( [i for i in (base, host, net, fixed_ip, device, owner) if i]) if base == ret: raise utils.InnerException( 'ERROR: arguments missed, try use -h to find them') return ret
def hosting_mq_queue(sub_resource, **kwargs): base = ( 'rabbitmqctl list_queues --online pid name synchronised_slave_pids ' '| egrep ') neutron = '"(lbaas|l3|dhcp|metering|q-agent|q-plugin|q-reports|ipsec)"' queues = { "nova": base + '"(cert|conductor|console|consoleauth| scheduler)"', "neutron": base + neutron, "cinder": base + 'cinder', "glance": base + 'notifications', } queue = queues.get(sub_resource) if not queue: raise utils.InnerException('Unknown resource, or not supported yet...') return queue
def api_examples_help(**kwargs): action = kwargs['action'] if action not in ('create', 'update'): raise utils.InnerException( 'Show example (-e) and list attributes (-l) ' 'only work for create and update action') action = {'create': 'post', 'update': 'put'}[action] resource = kwargs['resource'].replace('-', '_') if kwargs['list']: for k, v in api_examples.get_attrs(resource, action).items(): print "%s\t\t%s" % (k, v) if kwargs['example']: data = str(api_examples.get_example(resource, action)).replace("'", '"') kwargs.update({ 'action': action.upper(), 'api_ip': 'localhost', 'token': 'TOKEN', 'data': data }) curl = (curl_base + ".json -d '%(data)s'") % kwargs print curl