Exemple #1
0
Fichier : vm.py Projet : stamhe/apc
 def attach_nic(self, vm, network_id):
     sunstone = Sunstone(vm.cluster)
     network = Network.query.get(network_id)
     params = '{"action":{"perform":"attachnic","params":{"nic_template":{"NIC":{"NETWORK":"%s","NETWORK_UNAME":"oneadmin","MODEL":"virtio"}}}}}' % \
               network.sunstone_name
     resp = sunstone.vm_action(vm.vm_id, params)
     return resp.status==204 and 1 or 0
Exemple #2
0
def refresh():
    ts = int(time.time())
    for x in Cluster.query.all():
        sunstone = Sunstone(x)
        resp = sunstone.host()
        print resp.status
        info = json.loads(resp.data)['HOST_POOL']['HOST']
        if type(info) == dict:
            info = [info]
        for y in info:
            hostinst = Hosts.query.filter(
                and_(Hosts.cluster_id == x.id,
                     Hosts.sunstone_id == y['ID'])).first()
            if hostinst:
                hostinst.vmnum = y['HOST_SHARE']['RUNNING_VMS']
                hostinst.max_cpu = y['HOST_SHARE']['MAX_CPU']
                hostinst.cpu_usage = y['HOST_SHARE']['CPU_USAGE']
                hostinst.used_cpu = y['HOST_SHARE']['USED_CPU']
                hostinst.max_mem = y['HOST_SHARE']['MAX_MEM']
                hostinst.mem_usage = y['HOST_SHARE']['MEM_USAGE']
                hostinst.used_mem = y['HOST_SHARE']['USED_MEM']
            else:
                hostinst = Hosts(
                    x.id, y['ID'], y['NAME'], y['HOST_SHARE']['RUNNING_VMS'],
                    y['HOST_SHARE']['MAX_CPU'], y['HOST_SHARE']['CPU_USAGE'],
                    y['HOST_SHARE']['USED_CPU'], y['HOST_SHARE']['MAX_MEM'],
                    y['HOST_SHARE']['MEM_USAGE'], y['HOST_SHARE']['USED_MEM'],
                    ts)
            db.session.add(hostinst)
            db.session.commit()
    newest_update = Hosts.query.order_by(Hosts.last_update_time.desc()).first()
    Hosts.query.filter(
        Hosts.last_update_time < newest_update.last_update_time).delete()
    return redirect(url_for('.index'))
Exemple #3
0
 def attach_nic(self, vm, network_id):
     sunstone = Sunstone(vm.cluster)
     network = Network.query.get(network_id)
     params = '{"action":{"perform":"attachnic","params":{"nic_template":{"NIC":{"NETWORK":"%s","NETWORK_UNAME":"oneadmin","MODEL":"virtio"}}}}}' % \
               network.sunstone_name
     resp = sunstone.vm_action(vm.vm_id, params)
     return resp.status == 204 and 1 or 0
Exemple #4
0
def refresh():
    ts = int(time.time())
    for x in Cluster.query.all():
        sunstone = Sunstone(x)
        resp = sunstone.host()
        print resp.status
        info = json.loads(resp.data)['HOST_POOL']['HOST']
        if type(info) == dict:
            info = [info]
        for y in info:
            hostinst = Hosts.query.filter(and_(Hosts.cluster_id==x.id, Hosts.sunstone_id==y['ID'])).first()
            if hostinst:
                hostinst.vmnum      = y['HOST_SHARE']['RUNNING_VMS']
                hostinst.max_cpu    = y['HOST_SHARE']['MAX_CPU']
                hostinst.cpu_usage  = y['HOST_SHARE']['CPU_USAGE']
                hostinst.used_cpu   = y['HOST_SHARE']['USED_CPU']
                hostinst.max_mem    = y['HOST_SHARE']['MAX_MEM']
                hostinst.mem_usage  = y['HOST_SHARE']['MEM_USAGE']
                hostinst.used_mem   = y['HOST_SHARE']['USED_MEM']
            else:
                hostinst = Hosts(x.id, y['ID'], y['NAME'], y['HOST_SHARE']['RUNNING_VMS'], 
                                 y['HOST_SHARE']['MAX_CPU'], y['HOST_SHARE']['CPU_USAGE'], y['HOST_SHARE']['USED_CPU'], 
                                 y['HOST_SHARE']['MAX_MEM'], y['HOST_SHARE']['MEM_USAGE'], y['HOST_SHARE']['USED_MEM'], ts)
            db.session.add(hostinst)
            db.session.commit()
    newest_update = Hosts.query.order_by(Hosts.last_update_time.desc()).first()
    Hosts.query.filter(Hosts.last_update_time < newest_update.last_update_time).delete()
    return redirect(url_for('.index'))
Exemple #5
0
Fichier : vm.py Projet : stamhe/apc
    def refresh(self):
        cluster = Cluster.query.all()
        for x in cluster:
            sunstone = Sunstone(x)
            resp = sunstone.vm()
            if resp.status != 200:
                return 0
            try:
                info = json.loads(resp.data)['VM_POOL']['VM']
            except:
                return 1
            info = type(info)==dict and [info] or info
            for y in info:
                vm = VM.query.filter(and_(VM.cluster_id==x.id, VM.vm_id==int(y['ID']))).first()
                if not vm:
                    continue
                #IP
                nics = y['TEMPLATE'].has_key('NIC') and y['TEMPLATE']['NIC'] or []
                nics = type(nics)==dict and [nics] or nics
                ip = [z['IP'] for z in nics]
                vm.ip = ' '.join(ip)
                #Status
                lcm_state_code = int(y['LCM_STATE'])
                vm_state_code = int(y['STATE'])
                if lcm_state_code == 0:
                    vm.status = app.config['VM_STATE'][vm_state_code]
                else:
                    vm.status = app.config['LCM_STATE'][lcm_state_code]

                db.session.add(vm)
                db.session.commit()
        return 1
Exemple #6
0
    def refresh(self):
        cluster = Cluster.query.all()
        for x in cluster:
            sunstone = Sunstone(x)
            resp = sunstone.vm()
            if resp.status != 200:
                return 0
            try:
                info = json.loads(resp.data)['VM_POOL']['VM']
            except:
                return 1
            info = type(info) == dict and [info] or info
            for y in info:
                vm = VM.query.filter(
                    and_(VM.cluster_id == x.id,
                         VM.vm_id == int(y['ID']))).first()
                if not vm:
                    continue
                #IP
                nics = y['TEMPLATE'].has_key(
                    'NIC') and y['TEMPLATE']['NIC'] or []
                nics = type(nics) == dict and [nics] or nics
                ip = [z['IP'] for z in nics]
                vm.ip = ' '.join(ip)
                #Status
                lcm_state_code = int(y['LCM_STATE'])
                vm_state_code = int(y['STATE'])
                if lcm_state_code == 0:
                    vm.status = app.config['VM_STATE'][vm_state_code]
                else:
                    vm.status = app.config['LCM_STATE'][lcm_state_code]

                db.session.add(vm)
                db.session.commit()
        return 1
Exemple #7
0
    def create_vm(self):
        '''
        return
          0: created failed
          1: created succeed
          2: aleady created
        '''
        network_dict = network2dict()
        occi = OCCI(self.order.cluster)
        sunstone = Sunstone(self.order.cluster)
        vminst = VMC()

        # if aleady created
        if self.order.cnt <= self.order.vm.__len__():
            return 2

        ds_info = sunstone.datastore_id(self.order.cluster.ds_id).data
        ds_mad = json.loads(ds_info)['DATASTORE']['DS_MAD']

        # create vm
        ret = 1
        for i in range(0, self.order.cnt - self.order.vm.__len__()):
            if not vminst.create(self.order, ds_mad):
                ret = 0

        if ret:
            self.order.status = 1
        else:
            self.order.status = 2

        db.session.add(self.order)
        db.session.commit()

        return ret
Exemple #8
0
Fichier : vm.py Projet : stamhe/apc
    def create(self, order, ds_mad):
        disk_arr = []
        network_dict = network2dict()
        occi = OCCI(order.cluster)
        sunstone = Sunstone(order.cluster)

        # if ceph ds, create datablock first
        if ds_mad == "ceph":
            ready_tag = False
            posts = '{"image":{"NAME":"%s","TYPE":"DATABLOCK","PERSISTENT":"YES","SIZE":"%s","FSTYPE":"raw"},"ds_id":"%s"}'
            for x in order.disk:
                params = posts % (time.time(), x*1024, order.cluster.ds_id)
                resp = sunstone.image(method='POST', params=params)
                resp = json.loads(resp.data)
                disk_arr.append(resp['IMAGE']['ID'])

            while not ready_tag:
                ready_tag = True
                for x in disk_arr:
                    resp = sunstone.image_id(x)
                    data = json.loads(resp.data)
                    if data['IMAGE']['STATE'] != "1":
                        ready_tag = False

        # create vm
        template = render_template('vm/occi_template.html', order=order, network_dict=network_dict, disk_arr=disk_arr, ds_mad=ds_mad)
        resp = occi.compute_create(template)
        if resp.status != 201:
            return 0

        xmldoc = minidom.parseString(resp.data)
        vm_id = xmldoc.getElementsByTagName('ID')[0].firstChild.nodeValue
        hostname = xmldoc.getElementsByTagName('NAME')[0].firstChild.nodeValue
        state = xmldoc.getElementsByTagName('STATE')[0].firstChild.nodeValue
        ip = [a.firstChild.nodeValue for a in xmldoc.getElementsByTagName('IP')]

        # rename ceph disk
        if ds_mad == "ceph":
            posts = '{"action":{"perform":"rename","params":{"name":"%s"}}}'
            for index,d in enumerate(disk_arr):
                resp = sunstone.image_action(d, (posts % ("%s-%s" % (vm_id, index+1))))

        # write vm info to db
        try:
            vm = VM(hostname, ip, order.cpu, order.mem, order.network, order.disk, order.desc, order.id, 
                    order.cluster_id, vm_id, state, -1, order.user.id, 0, order.cluster.if_test, order.days)
            db.session.add(vm)
            db.session.commit()
            vminst = VMC()
            vminst.gen_price(vm)
            return 1
        except:
            return 0
Exemple #9
0
Fichier : vm.py Projet : stamhe/apc
 def resize(self, vm, cpu, mem):
     (cpu, mem) = (int(cpu), int(mem))
     sunstone = Sunstone(vm.cluster)
     params = '{"action":{"perform":"resize","params":{"vm_template":{"CPU":"%s","MEMORY":"%s"},"enforce":false}}}' % (cpu, mem*1024)
     resp = sunstone.vm_action(vm.vm_id, params)
     if resp.status == 204:
         vm.cpu = cpu
         vm.mem = mem
         db.session.add(vm)
         db.session.commit()
         vminst = VMC()
         vminst.gen_price(vm)
         return 1
     else:
         return 0
Exemple #10
0
 def resize(self, vm, cpu, mem):
     (cpu, mem) = (int(cpu), int(mem))
     sunstone = Sunstone(vm.cluster)
     params = '{"action":{"perform":"resize","params":{"vm_template":{"CPU":"%s","MEMORY":"%s"},"enforce":false}}}' % (
         cpu, mem * 1024)
     resp = sunstone.vm_action(vm.vm_id, params)
     if resp.status == 204:
         vm.cpu = cpu
         vm.mem = mem
         db.session.add(vm)
         db.session.commit()
         vminst = VMC()
         vminst.gen_price(vm)
         return 1
     else:
         return 0
Exemple #11
0
def detach_nic(vm, **kvargs):
    try:
        vminst = VMC()
        nic_id = request.form['nic_id']
        vminst.detach_nic(vm, nic_id)
        flag = True
        sunstone = Sunstone(vm.cluster)
        while flag:
            resp = sunstone.vm_id(vm.vm_id)
            info = json.loads(resp.data)['VM']
            if int(info['STATE']) == 3:
                flag = False
        status = 1
    except:
        status = 0

    return jsonify(status=status)
Exemple #12
0
    def image_sunstone(self):
        ret = []

        sunstone = Sunstone(self.cluster)
        data = json.loads(sunstone.image().data)
        images = data['IMAGE_POOL']['IMAGE']

        if type(images) == dict:
            images = [images]

        for image in images:
            ele = {}
            ele['sunstone_id'] = int(image['ID'])
            ele['sunstone_name'] = image['NAME']
            ret.append(ele)

        return ret
Exemple #13
0
    def datastore_sunstone(self):
        ret = []

        sunstone = Sunstone(self.cluster)
        data = json.loads(sunstone.datastore().data)
        datastores = data['DATASTORE_POOL']['DATASTORE']

        if type(datastores) == dict:
            datastores = [datastores]

        for datastore in datastores:
            ele = {}
            ele['sunstone_id'] = int(datastore['ID'])
            ele['sunstone_name'] = datastore['NAME']
            ret.append(ele)

        return ret
Exemple #14
0
    def vnet_sunstone(self):
        ret = []

        sunstone = Sunstone(self.cluster)
        data = json.loads(sunstone.vnet().data)
        vnets = data['VNET_POOL']['VNET']

        if type(vnets) == dict:
            vnets = [vnets]

        for vnet in vnets:
            ele = {}
            ele['sunstone_id'] = int(vnet['ID'])
            ele['sunstone_name'] = vnet['NAME']
            ret.append(ele)

        return ret
Exemple #15
0
Fichier : vm.py Projet : stamhe/apc
    def detail(self, vm):
        ret = {}
        sunstone = Sunstone(vm.cluster)
        resp = sunstone.vm_id(vm.vm_id)
        info = json.loads(resp.data)['VM']

        nic      = info['TEMPLATE'].has_key('NIC') and info['TEMPLATE']['NIC'] or []
        disk     = info['TEMPLATE'].has_key('DISK') and info['TEMPLATE']['DISK'] or []
        snapshot = info['TEMPLATE'].has_key('SNAPSHOT') and info['TEMPLATE']['SNAPSHOT'] or []

        nic      = type(nic)==dict and [nic] or nic
        disk     = type(disk)==dict and [disk] or disk
        snapshot = type(snapshot)==dict and [snapshot] or snapshot

        ret['NIC'] = nic
        ret['DISK'] = disk
        ret['SNAPSHOT'] = snapshot

        return ret
Exemple #16
0
    def detail(self, vm):
        ret = {}
        sunstone = Sunstone(vm.cluster)
        resp = sunstone.vm_id(vm.vm_id)
        info = json.loads(resp.data)['VM']

        nic = info['TEMPLATE'].has_key('NIC') and info['TEMPLATE']['NIC'] or []
        disk = info['TEMPLATE'].has_key(
            'DISK') and info['TEMPLATE']['DISK'] or []
        snapshot = info['TEMPLATE'].has_key(
            'SNAPSHOT') and info['TEMPLATE']['SNAPSHOT'] or []

        nic = type(nic) == dict and [nic] or nic
        disk = type(disk) == dict and [disk] or disk
        snapshot = type(snapshot) == dict and [snapshot] or snapshot

        ret['NIC'] = nic
        ret['DISK'] = disk
        ret['SNAPSHOT'] = snapshot

        return ret
Exemple #17
0
Fichier : vm.py Projet : stamhe/apc
    def delete(self, vm):
        occi = OCCI(vm.cluster)
        sunstone = Sunstone(vm.cluster)
        vm_info = json.loads(sunstone.vm_id(vm.vm_id).data)

        resp = occi.compute_delete(vm.vm_id)
        if resp.status != 204:
            return 0

        disk = 'DISK' in vm_info['VM']['TEMPLATE'] and vm_info['VM']['TEMPLATE']['DISK'] or []
        if type(disk) == dict:
            disk = [disk]
        for x in disk:
            if "CLONE" in x and x['CLONE'] == 'NO':
                sunstone.image_id(image_id=x['IMAGE_ID'], method='DELETE')

        try:
            db.session.delete(vm)
            db.session.commit()
            return 1
        except:
            return 0
Exemple #18
0
    def delete(self, vm):
        occi = OCCI(vm.cluster)
        sunstone = Sunstone(vm.cluster)
        vm_info = json.loads(sunstone.vm_id(vm.vm_id).data)

        resp = occi.compute_delete(vm.vm_id)
        if resp.status != 204:
            return 0

        disk = 'DISK' in vm_info['VM']['TEMPLATE'] and vm_info['VM'][
            'TEMPLATE']['DISK'] or []
        if type(disk) == dict:
            disk = [disk]
        for x in disk:
            if "CLONE" in x and x['CLONE'] == 'NO':
                sunstone.image_id(image_id=x['IMAGE_ID'], method='DELETE')

        try:
            db.session.delete(vm)
            db.session.commit()
            return 1
        except:
            return 0
Exemple #19
0
Fichier : vm.py Projet : stamhe/apc
 def detach_nic(self, vm, nic_id):
     sunstone = Sunstone(vm.cluster)
     params = '{"action":{"perform":"detachnic","params":{"nic_id":"%s"}}}' % nic_id
     resp = sunstone.vm_action(vm.vm_id, params)
     return resp.status==204 and 1 or 0
Exemple #20
0
 def detach_nic(self, vm, nic_id):
     sunstone = Sunstone(vm.cluster)
     params = '{"action":{"perform":"detachnic","params":{"nic_id":"%s"}}}' % nic_id
     resp = sunstone.vm_action(vm.vm_id, params)
     return resp.status == 204 and 1 or 0
Exemple #21
0
    def create(self, order, ds_mad):
        disk_arr = []
        network_dict = network2dict()
        occi = OCCI(order.cluster)
        sunstone = Sunstone(order.cluster)

        # if ceph ds, create datablock first
        if ds_mad == "ceph":
            ready_tag = False
            posts = '{"image":{"NAME":"%s","TYPE":"DATABLOCK","PERSISTENT":"YES","SIZE":"%s","FSTYPE":"raw"},"ds_id":"%s"}'
            for x in order.disk:
                params = posts % (time.time(), x * 1024, order.cluster.ds_id)
                resp = sunstone.image(method='POST', params=params)
                resp = json.loads(resp.data)
                disk_arr.append(resp['IMAGE']['ID'])

            while not ready_tag:
                ready_tag = True
                for x in disk_arr:
                    resp = sunstone.image_id(x)
                    data = json.loads(resp.data)
                    if data['IMAGE']['STATE'] != "1":
                        ready_tag = False

        # create vm
        template = render_template('vm/occi_template.html',
                                   order=order,
                                   network_dict=network_dict,
                                   disk_arr=disk_arr,
                                   ds_mad=ds_mad)
        resp = occi.compute_create(template)
        if resp.status != 201:
            return 0

        xmldoc = minidom.parseString(resp.data)
        vm_id = xmldoc.getElementsByTagName('ID')[0].firstChild.nodeValue
        hostname = xmldoc.getElementsByTagName('NAME')[0].firstChild.nodeValue
        state = xmldoc.getElementsByTagName('STATE')[0].firstChild.nodeValue
        ip = [
            a.firstChild.nodeValue for a in xmldoc.getElementsByTagName('IP')
        ]

        # rename ceph disk
        if ds_mad == "ceph":
            posts = '{"action":{"perform":"rename","params":{"name":"%s"}}}'
            for index, d in enumerate(disk_arr):
                resp = sunstone.image_action(d, (posts % ("%s-%s" %
                                                          (vm_id, index + 1))))

        # write vm info to db
        try:
            vm = VM(hostname, ip, order.cpu, order.mem, order.network,
                    order.disk, order.desc, order.id, order.cluster_id, vm_id,
                    state, -1, order.user.id, 0, order.cluster.if_test,
                    order.days)
            db.session.add(vm)
            db.session.commit()
            vminst = VMC()
            vminst.gen_price(vm)
            return 1
        except:
            return 0
Exemple #22
0
Fichier : vm.py Projet : stamhe/apc
 def recreate(self, vm):
     sunstone = Sunstone(vm.cluster)
     post = '{"action":{"perform":"resubmit"}}'
     resp = sunstone.vm_action(vm.vm_id, post)
     return resp.status==204 and 1 or 0
Exemple #23
0
 def poweroff_hard(self, vm):
     sunstone = Sunstone(vm.cluster)
     post = '{"action":{"perform":"poweroff","params":{"hard":true}}}'
     resp = sunstone.vm_action(vm.vm_id, post)
     return resp.status == 204 and 1 or 0
Exemple #24
0
Fichier : vm.py Projet : stamhe/apc
 def poweroff_hard(self, vm):
     sunstone = Sunstone(vm.cluster)
     post = '{"action":{"perform":"poweroff","params":{"hard":true}}}'
     resp = sunstone.vm_action(vm.vm_id, post)
     return resp.status==204 and 1 or 0
Exemple #25
0
 def recreate(self, vm):
     sunstone = Sunstone(vm.cluster)
     post = '{"action":{"perform":"resubmit"}}'
     resp = sunstone.vm_action(vm.vm_id, post)
     return resp.status == 204 and 1 or 0
Exemple #26
0
 def rename(self, vm, newname):
     sunstone = Sunstone(vm.cluster)
     params = '{"action":{"perform":"rename","params":{"name":"%s"}}}' % newname
     resp = sunstone.vm_action(vm.vm_id, params)
     return resp.status == 204 and 1 or 0
Exemple #27
0
Fichier : vm.py Projet : stamhe/apc
 def rename(self, vm, newname):
     sunstone = Sunstone(vm.cluster)
     params = '{"action":{"perform":"rename","params":{"name":"%s"}}}' % newname
     resp = sunstone.vm_action(vm.vm_id, params)
     return resp.status==204 and 1 or 0