예제 #1
0
파일: vm.py 프로젝트: 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
예제 #2
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
예제 #3
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