def test_get(): client = ResourceManagerClient() resource = 'test_machine' response_body = '{"_id": "my_id"}' httpretty.register_uri(httpretty.GET, client.endpoint + "/" + resource, body=response_body) assert client.get_resource(resource) == json.loads(response_body)
def test_defaults(): host_client = ResourceManagerClient() pub_ip_client = ResourceManagerClient(resource_type='public-addresses') priv_ip_client = ResourceManagerClient(resource_type='private-addresses') cobbler_url = "http://cobbler.example.com/cobbler_api" cobbler_user = "******" cobbler_password = "******" response_body = '''<?xml version="1.0"?> <methodResponse> <params> <param> <value><string>Some Value</string></value> </param> </params> </methodResponse> ''' distro_map = { 'centos': 'centos6-x86_64-raid0', 'rhel': 'rhel6u6-x86_64-raid0' } httpretty.register_uri(httpretty.POST, cobbler_url, body=response_body) pxe_manager = PxeManager(cobbler_url, cobbler_user, cobbler_password, host_client, pub_ip_client, priv_ip_client) for key, value in distro_map.iteritems(): assert pxe_manager.distro[key] == value
def index(): client = ResourceManagerClient(endpoint=RM_ENDPOINT) machines = client.get_all_resources() available_machines = [] used_machines = [] pxe_failed = [] error_state = [] for machine in machines: try: addr = socket.getaddrinfo(machine['hostname'], 22, socket.AF_INET, socket.IPPROTO_IP, socket.IPPROTO_TCP)[0] machine.update({'ip': str(addr[4][0])}) except: machine.update({'ip': "IP NOT FOUND"}) if machine['state'] == "in_use": used_machines.append(machine) elif machine['state'] == "idle": available_machines.append(machine) elif machine['state'] == "pxe_failed": pxe_failed.append(machine) else: error_state.append(machine) total_machines = len(machines) return render_template('index.html', machines=machines, total_machines=total_machines, available_machines=len(available_machines), used_machines=len(used_machines), pxe_failed=len(pxe_failed))
def test_find_resources(): client = ResourceManagerClient() field = 'some_field' value = 'some_value' url = client.endpoint response_body = "{\"_items\":[]}" httpretty.register_uri(httpretty.GET, url, body=response_body) client.find_resources(field, value)
def test_find_resources(): client = ResourceManagerClient() field = 'some_field' value = 'some_value' url = client.endpoint + "?where=" + field + "==" + urllib.quote("\"" + value + "\"") response_body = "{}" httpretty.register_uri(httpretty.GET, url, body=response_body) client.find_resources(field, value)
def get_current_jobs(): client = ResourceManagerClient(endpoint=RM_ENDPOINT, resource_type='machines') jobs = [] machines = client.get_all_resources() for machine in machines: if machine['job_id'] and machine['job_id'] not in jobs: jobs.append(machine['job_id']) return jobs
def test_update(): client = ResourceManagerClient() resource = "{\"hostname\":\"resource\"}" response_body = '{"_id": "my_id", "_etag":"2341341234"}' httpretty.register_uri(httpretty.GET, client.endpoint + "/resource", body=response_body) response_body = '{"_id": "my_id"}' httpretty.register_uri(httpretty.PATCH, client.endpoint + "/my_id", body=response_body) client.update_resource(resource)
def test_defaults(resource='machines'): client = ResourceManagerClient(resource) assert client.resource_type == resource defaults = dict(endpoint='http://127.0.0.1:5000/machines', resource_type='machines', key='hostname', auth=('admin', 'admin'), headers={'content-type': 'application/json'}, fields=["hostname", "owner", "state", "job_id", "tags", "_updated", "_id"]) for attribute, value in defaults.iteritems(): try: assert value == client.__getattribute__(attribute) except AssertionError: raise AssertionError("Default for {0} should be:{1}\n" "Received: {2}".format(attribute, client.__getattribute__(attribute), value))
def test_defaults(resource='machines'): client = ResourceManagerClient(resource) assert client.resource_type == resource defaults = dict( endpoint='http://127.0.0.1:5000/machines', resource_type='machines', key='hostname', auth=('admin', 'admin'), headers={'content-type': 'application/json'}, fields=["hostname", "owner", "state", "job_id", "_updated", "_id"]) for attribute, value in defaults.iteritems(): try: assert value == client.__getattribute__(attribute) except AssertionError: raise AssertionError("Default for {0} should be:{1}\n" "Received: {2}".format( attribute, client.__getattribute__(attribute), value))
def machines(): client = ResourceManagerClient(endpoint=RM_ENDPOINT) machines = client.get_all_resources() machine_list = [] state = request.args.get('state') if not state: state = "all" for machine in machines: try: addr = socket.getaddrinfo(machine['hostname'], 22, socket.AF_INET, socket.IPPROTO_IP, socket.IPPROTO_TCP)[0] machine.update({'ip': str(addr[4][0])}) except: machine.update({'ip': "IP NOT FOUND"}) if state == "all": machine_list.append(machine) elif state == machine['state']: machine_list.append(machine) return render_template("general/machines.html", machines=machine_list)
def addresses(): state = request.args.get('state') ip_type = request.args.get('type') if not ip_type: ip_type = "public-addresses" if ip_type not in ['public-addresses', 'private-addresses']: ip_type = "public-addresses" if not state: state = "all" client = ResourceManagerClient(endpoint=RM_ENDPOINT, resource_type=ip_type) all_ips = client.get_all_resources() available_ips = client.find_resources(field="owner", value="") jobs = get_current_jobs() zombie_ips = [] for ip in all_ips: if ip['owner'] and ip['owner'] not in jobs: ip.update({'temp_state': 'zombie'}) zombie_ips.append(ip) if state == "zombie": ips = zombie_ips else: ips = all_ips zombie_query_string = "type=" + ip_type + "&state=zombie" return render_template("general/ipaddresses.html", total_pub_ips=len(all_ips), available_ips=len(available_ips), zombie_ips=len(zombie_ips), zombie_query_string=zombie_query_string, ips=ips, ip_type=ip_type)
'cpucores': 4}, 'g-xx-xx': {'regex': r'g-\d\d-\d\d.qa1.eucalyptus-systems.com', 'mem': 8589934592, 'cpucores': 2, 'cpumhz': 2200, 'interfaces': [{'ratembps': 1000}, {'ratembps': 1000}]}, 'h-xx': {'regex': r'h-\d\d.qa1.eucalyptus-systems.com', 'mem': 68719476736, 'cpucores': 8, 'cpumhz': 2400}, 'a-xx-x': {'regex': r'a-\d\d-\w.qa1.eucalyptus-systems.com', 'mem': 137438953472, 'cpucores': 8, 'cpumhz': 2600, 'interfaces': [{'ratembps': 10000}]} } with open(newhosts, "r") as file: collected = json.loads(file.read()) # client = ResourceManagerClient(endpoint="http://10.111.4.100:5000") # PRODUCTION client = ResourceManagerClient(endpoint="http://127.0.0.1:5000") # USE "http://10.111.4.100:5000" for production ourlist = client.get_all_resources() for item in ourlist: machine = client.get_resource(item['hostname']) # PRODUCTION for mtype in memtable: match = re.search(memtable[mtype]['regex'], item['hostname']) if match: if 'tags' not in machine.keys(): machine[u'tags'] = {} if item['hostname'] in collected['hosts'].keys(): print "{}: updating to {} (collected) (vs {} table)".format( item['hostname'], int(collected['hosts'][item['hostname']])*1024, memtable[mtype]['mem']) machine[u'tags'][u'memory'] = int(collected['hosts'][item['hostname']])*1024 # PRODUCTION else:
def test_get_all_resources(): client = ResourceManagerClient() url = client.endpoint response_body = "{\"_items\":[]}" httpretty.register_uri(httpretty.GET, url, body=response_body) assert isinstance(client.get_all_resources(), list)
def test_init(): ResourceManagerClient()
def test_create(): client = ResourceManagerClient() resource = "{\"hostname\":\"resource\"}" httpretty.register_uri(httpretty.POST, client.endpoint, status=201) client.create_resource(resource)
'regex': r'a-\d\d-\w.qa1.eucalyptus-systems.com', 'mem': 137438953472, 'cpucores': 8, 'cpumhz': 2600, 'interfaces': [{ 'ratembps': 10000 }] } } with open(newhosts, "r") as file: collected = json.loads(file.read()) # client = ResourceManagerClient(endpoint="http://10.111.4.100:5000") # PRODUCTION client = ResourceManagerClient( endpoint="http://127.0.0.1:5000" ) # USE "http://10.111.4.100:5000" for production ourlist = client.get_all_resources() for item in ourlist: machine = client.get_resource(item['hostname']) # PRODUCTION for mtype in memtable: match = re.search(memtable[mtype]['regex'], item['hostname']) if match: if 'tags' not in machine.keys(): machine[u'tags'] = {} if item['hostname'] in collected['hosts'].keys(): print "{}: updating to {} (collected) (vs {} table)".format( item['hostname'], int(collected['hosts'][item['hostname']]) * 1024, memtable[mtype]['mem']) machine[u'tags'][u'memory'] = int(collected['hosts'][