def list_hosts(*args, **kwargs): """ List all hosts registered in the system Variables: None Arguments: offset => Offset in the host bucket length => Max number of host returned Data Block: None Result example: { "total": 13, # Total Number of hosts "count": 100, # Number of hosts requested "offset": 0, # Offset in the host bucket "items": [{ # List of host blocks "profile": "Default profile", # Host current profile "machine_info": { # Host Machine info block "uid": "Core-001122334455", # Machine UID "ip": "127.0.0.1", # Machine IP "memory": "23.5", # Machine RAM (GB) "cores": 16, # Machine Num Cores "os": "Linux", # Machine OS "name": "computer1" }, # Machine Name "ip": "127.0.0.1", # Host IP "hostname": "computer1", # Host Name "enabled": true, # Is host enabled? "platform": { # Host platform block "node": "computer1", # Node name "system": "Linux", # Node system "machine": "x86_64", # Node Architecture "version": "#47-Ubuntu SMP", # Node Kernel version "release": "3.13.0-24", # Node Kernel release "proc": "x86_64" }, # Node proc Architecture "mac_address": "001122334455" # Host Mac address }, ... ] } """ hosts = STORAGE.list_node_keys() host_list = sorted([host for host in STORAGE.get_nodes(hosts) if host is not None], key=lambda x: x['hostname']) return make_api_response({'items': host_list, 'count': len(host_list)})
def list_services_workers(**_): """ List number of workers for each services in the system. Variables: None Arguments: None Data Block: None Result example: {"MY SERVICE": 1, ... } # Dictionary of services and number of workers """ services = {s["name"]: 0 for s in STORAGE.list_services() if s['enabled']} profiles = STORAGE.get_profiles_dict( list( set([ p["_yz_rk"] for p in STORAGE.stream_search( "profile", "_yz_rk:*", fl="_yz_rk") ]))) used_profiles = { n['mac_address']: n['profile'] for n in STORAGE.get_nodes(STORAGE.list_node_keys()) if n['profile'] != "" } for _mac, used_p in used_profiles.iteritems(): if used_p in profiles: for srv, cfg in profiles[used_p]["services"].iteritems(): if srv in services: services[srv] += cfg["workers"] for srv, cfg in profiles[used_p]["virtual_machines"].iteritems(): if srv in services: vm = STORAGE.get_virtualmachine(srv) if not vm: continue services[srv] += vm['num_workers'] * cfg['num_instances'] return make_api_response(services, err=[profiles, used_profiles, services])
def load_running_config(**kwargs): """ Loads the currently running cluster configuration Variables: None Arguments: None Data Block: None Result example: { "allocation": { # Resource allocation table "MyService": 3, # Service / VM and the number to allocate ... }, "flex": 3 # Number of flex nodes to reserve } """ profile_map = STORAGE.get_all_profiles() hosts = STORAGE.list_node_keys() host_list = sorted([ host for host in STORAGE.get_nodes(hosts) if host is not None and 'hostagent' in host.get('roles', []) ], key=lambda k: (k.get('machine_info', {}).get('cores', 1), k.get('machine_info', {}).get('name', 1))) flex_count = 0 allocation = {} overrides = {} for host in host_list: profile_name = host.get('profile', None) if profile_name.startswith('flex'): flex_count += 1 else: host_profile = profile_map.get(profile_name, {}) if host_profile: for service in host_profile['services']: alloc_key = 'svc_%s' % service if alloc_key not in allocation: allocation[alloc_key] = 0 allocation[alloc_key] += host_profile['services'][service][ 'workers'] if host_profile['services'][service]["service_overrides"]: if service not in overrides: overrides[service] = [] override_dict = host_profile['services'][service][ "service_overrides"] overrides[service].append({ 'count': host_profile['services'][service]['workers'], 'override': json.dumps(override_dict) }) for vm in host_profile['virtual_machines']: alloc_key = 'vm_%s' % vm if alloc_key not in allocation: allocation[alloc_key] = 0 allocation[alloc_key] += host_profile['virtual_machines'][ vm]['num_instances'] pass return make_api_response({ 'allocation': allocation, 'flex': flex_count, 'overrides': overrides })
def load_system_info(**kwargs): """ Load the full system information Variables: None Arguments: None Data Block: None Result example: { "vms": {}, # Map of vms that are available in the system "services": {}, # Map of service that are available in the system "hosts": [] # List of physical hosts configured } """ temp_service_map = {x['name']: x for x in STORAGE.list_services()} vm_list = STORAGE.list_virtualmachines() hosts = STORAGE.list_node_keys() host_list = sorted( [ host for host in STORAGE.get_nodes(hosts) if host is not None and 'hostagent' in host.get('roles', []) ], key=lambda k: (k.get('machine_info', {}).get('cores', 1), k.get('machine_info', {}). get('memory', '11.7'), k.get('machine_info', {}).get('name', 1))) service_map = copy.copy(temp_service_map) out_vm_map = {} for vm in vm_list: service_name = vm['name'] srv_list = {service_name: vm['num_workers']} cpu_usage = temp_service_map.get(service_name, {}).get( 'cpu_cores', 1) * vm['num_workers'] out_vm_map[service_name] = { "cpu_usage": cpu_usage, "ram_usage": vm['ram'], "services": srv_list, "enabled": vm.get("enabled", False) } try: del service_map[service_name] except KeyError: continue out_service_map = {} for service in service_map.itervalues(): out_service_map[service['name']] = { "cpu_usage": service.get('cpu_cores', 1), "ram_usage": service.get('ram_mb', 1024), "enabled": service.get("enabled", False) } out_host_list = [] for host in host_list: out_host_list.append({ "hostname": host['machine_info']['name'], "profile": host['profile'], "cores": host['machine_info']['cores'], "memory": float(host['machine_info']['memory']) * 1024, "mac": host['mac_address'] }) return make_api_response({ 'vms': out_vm_map, 'services': out_service_map, "hosts": out_host_list })
def get_system_configuration_overview(**_): """ Display a system configuration overview. Variables: None Arguments: None Data Block: None Result example: { "errors": { # Errors in the current config "profiles": [], # Profiles in error "services": [] }, # Services in error "services": { # Services overview "SRV_NAME": { # Single service overview "enabled": True, # is enabled? "profiles" : [], # profiles referencing it "queue": 0, # items in queue "workers": 1 }, # number of workers ...,} } """ errors = {"services": [], "profiles": []} services = { s["name"]: { "workers": 0, "enabled": s["enabled"], "profiles": [], "queue": 0 } for s in STORAGE.list_services() } profiles = STORAGE.get_profiles_dict( list( set([ p["_yz_rk"] for p in STORAGE.stream_search( "profile", "_yz_rk:*", fl="_yz_rk") ]))) used_profiles = { n['mac_address']: n['profile'] for n in STORAGE.get_nodes(STORAGE.list_node_keys()) if n['profile'] != "" } for mac, used_p in used_profiles.iteritems(): if profiles.has_key(used_p): for srv, cfg in profiles[used_p]["services"].iteritems(): if not services.has_key(srv): errors["services"].append({ "service": srv, "profile": used_p }) continue services[srv]["workers"] += cfg["workers"] if used_p not in services[srv]["profiles"]: services[srv]["profiles"].append(used_p) for srv, cfg in profiles[used_p]["virtual_machines"].iteritems(): if not services.has_key(srv): errors["services"].append({ "service": srv, "profile": used_p }) continue vm = STORAGE.get_virtualmachine(srv) if not vm: errors["services"].append({ "service": srv, "profile": used_p }) continue services[srv][ "workers"] += vm['num_workers'] * cfg['num_instances'] if used_p not in services[srv]["profiles"]: services[srv]["profiles"].append(used_p) else: errors["profiles"].append({"profile": used_p, "mac": mac}) for srv in services: services[srv]["queue"] = get_service_queue_length(srv) return make_api_response({"services": services, "errors": errors})