예제 #1
0
파일: metrics.py 프로젝트: cshuo/DRA
class Metric(OpenstackService):

    def __init__(self):

        OpenstackService.__init__(self)
        self.restful = OpenstackRestful(self.tokenId)


    def getAllMeters(self):
        url = "%s/v2/meters" % conf.CEILOMETER_URL
        result = self.restful.getResult(url)
        return result

    def getAllResources(self):
        url = "%s/v2/resources" % conf.CEILOMETER_URL
        result = self.restful.getResult(url)
        return result

    def getMeter(self, meter_name, queryFilter):
        url = "%s/v2/meters/%s" % (conf.CEILOMETER_URL, meter_name)

        #transfer str to list
        queryFilter = eval(queryFilter)
        print queryFilter

        params = ""
        for queryItem in queryFilter:
            param = ""
            for key in queryItem:
                param += "&q.%s=%s" % (key, queryItem[key])
            params += param

        url = url + "?" + params

        result = self.restful.getResult(url)
        return result[0]


    def getMeterStatistics(self, meter_name, queryFilter, groupby = None, period = None, aggregate = None):
        url = "%s/v2/meters/%s/statistics" % (conf.CEILOMETER_URL, meter_name)

        #transfer str to list
        queryFilter = eval(queryFilter)

        params = ""
        for queryItem in queryFilter:
            param = ""
            for key in queryItem:
                param += "&q.%s=%s" % (key, queryItem[key])
            params += param

        url = url + "?" + params

        result = self.restful.get_req(url)
        return result[0]
예제 #2
0
파일: nova.py 프로젝트: cshuo/DRA
class Nova(OpenstackService):

    def __init__(self):
        OpenstackService.__init__(self)
        self.restful = OpenstackRestful(self.tokenId)


    def liveMigrate(self, instance_id, host):
	""" live migrate an instance to dest host """
        url = "{base}/v2/{tenant}/servers/{instance}/action".format(base=conf.NOVA_URL,
		tenant=self.tenantId, instance=instance_id)
        values = {"os-migrateLive":{"block_migration": "true", "host":host, 'disk_over_commit':"false"}}
        self.restful.post_req(url, values)


    def stopInstance(self, instance_id):
	""" stop a specific instance """
        url = "{base}/v2/{tenant}/servers/{instance}/action".format(base=conf.NOVA_URL,
		tenant=self.tenantId, instance=instance_id)
	values = {"os-stop": 'null'}
	self.restful.post_req(url, values)
     

    def startInstance(self, instance_id):
	""" start a specific instance """
        url = "{base}/v2/{tenant}/servers/{instance}/action".format(base=conf.NOVA_URL,
		tenant=self.tenantId, instance=instance_id)
	values = {"os-start": 'null'}
	self.restful.post_req(url, values)


    def getInstances(self):
	""" get all instances belong to a tenant """
        url = "%s/v2/%s/servers" % (conf.NOVA_URL, self.tenantId)

        result = self.restful.getResult(url)
        servers = result['servers']

        instances = []
        for i in range(len(servers)):
            instances.append(servers[i]['id'])

        return instances


    def getInstancesOnHost(self, host):
	""" get all instances on specific host """
        url = "%s/v2/%s/servers?host=%s" % (conf.NOVA_URL, self.tenantId, host)

        result = self.restful.get_req(url)
        servers = result['servers']

        instances = []
        for i in range(len(servers)):
            instances.append(servers[i]['id'])

        return instances


    def getComputeHosts(self):
	""" get all compute hosts of openstack """ 
        url = "%s/v2/%s/os-hosts" % (conf.NOVA_URL, self.tenantId)

        result = self.restful.get_req(url)

        hostsList = result['hosts']
        hosts = []
        for host in hostsList:
            if host['service'] == 'compute':
                hosts.append(host['host_name'])
        return hosts

    def hostDetail(self, host):
	""" show details for a host """
        url = "{base}/v2/{tenant}/os-hosts/{host}".format(
		base=conf.NOVA_URL,tenant=self.tenantId, host=host)
	info = self.restful.get_req(url)
	print info


    def _hypervisorList(self):
	""" get all hypervisors, return name,id pair """
        url = "{base}/v2/{tenant}/os-hypervisors".format(base=conf.NOVA_URL,
							  tenant=self.tenantId)
	info = self.restful.get_req(url)
	hypervisor = {}
	hvsors = info['hypervisors']
	for hvsor in hvsors:
	    hypervisor[hvsor['hypervisor_hostname']] = hvsor['id']
	return hypervisor
	

    def hypervisorDetail(self, host):
	""" get detail info for a specific hypervisor """
	hypervisors = self._hypervisorList()
	host_id = hypervisors[host]
        url = "{base}/v2/{tenant}/os-hypervisors/{hostid}".format(base=conf.NOVA_URL,
							      tenant=self.tenantId,
							      hostid=host_id)
	info = self.restful.get_req(url)['hypervisor']
	sttcs = dict()
	sttcs['vcpus'] = info['vcpus']
	sttcs['vpcus_used'] = info['vcpus_used']
	sttcs['mem_total'] = info['memory_mb']
	sttcs['mem_used'] = info['memory_mb_used']
	sttcs['workload'] = info['current_workload']

	return sttcs
	

    @staticmethod
    def liveMigrateAll(src_host, dest_host):
	""" live migrate all instances on src host to dest """
        ssh_controller = SshTool(conf.CONTROLLER_HOST, 22,
                                  conf.HOST_ROOT_USERNAME,
                                  conf.HOST_ROOT_PASSWORD)

        cmd_migrate = "nova host-evacuate-live --target-host {dest} \
                      --block-migrate {src}".format(src=src_host, dest=dest_host)
        ssh_controller.remote_cmd(cmd_migrate)
        ssh_controller.close()