Exemple #1
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 #2
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 #3
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 #4
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