Esempio n. 1
0
    def post(self, chunk):
        try:
            keys = self.get_key_params(self.request.path)
            if keys[0] == 'create_instance':
                seq = yield sequence.number_seq('CL', '')
                rs = template.create_instance(self.request, keys[1], keys[2],
                                              'CL-%s' % seq)
                self.write(json.dumps(rs))

            if keys[0] == 'delete_instance':
                app_service.do_delete_app(keys[1])
                self.write('{"result":"ok"}')

            if keys[0] == 'delete_stack':
                stack_util.delete_stack(keys[1])
                self.write('{"result":"ok"}')

            if keys[0] == 'send_message':
                self.log.debug('send_message : \n %s' % self.request.body)
                msg = json.loads(self.request.body)
                send_message(self.log, msg)
                self.write('{"result":"ok"}')

            if keys[0] == 'create_vm':
                tmp = json.loads(self.request.body)
                rs = yield _create(tmp)
                self.log.debug(rs)
                self.write(json.dumps(rs))

        except:
            self.log.error(generals.trace())
            self.write('{"result":"error"}')
Esempio n. 2
0
    def execute_template(self, name, action_name, body):
        params = json.loads(self.request.body)

        app_name = params['app_name']

        if 'type' not in params or params['type'] == 'deploy':
            seq = yield sequence.number_seq('CL', '')
            seq = 'CL-%s' % seq
        else:
            rs = yield DBUtil().query(
                ("select * from manor.manor_app_instance "
                 "where app_serial='%s'") % app_name)
            seq = rs[0]['app_id']

        info_serial, serial = create_instance(self.request, name, action_name,
                                              seq)

        self.response(
            generals.gen_response({
                'serial': str(serial),
                'info': info_serial
            }))
Esempio n. 3
0
def create_server(vm):
    """ create vm
    :param request: the request of function
    :param vm: VirtualMachine = {"network": [{"vlan": "uuid", "ip": "ip"}],
    "tenant": "uuid", "host": "10.10.3.11", "image": "uuid", "cores": 4,
    "memory": 8,"metadata": {"sys_vloume": {"type": "ssd"}, "user": "******",
    "order": "uuid", "extend": {"des": "desc", "keepalive ": 0, "displayname": "sys"}},
    "super_user_pass": "******"}

    1.自动创建系统盘模板
    2.镜像信息改动
    3.双网卡
    """
    try:
        """check image"""
        task_id = task.gen_task_id()
        nics = []
        image = yield image_module.get_image(vm.image)
        sys_volume_size = vm.size if vm.size and int(vm.size) > int(
            image['min_disk']) else int(image['min_disk'])
        volume_type = vm.metadata.sys_volume.get(
            'type') or CONF.storage.default_type
        vm.metadata.sys_volume["type"] = volume_type
        is_lvm = volume_type.lower() == "lvm"
        is_iso = 'iso' == image.get("disk_format")
        is_windows = 'windows' == image.get("os").lower()
        message = {"sys_volume_id": "", "drive_volume_id": ""}
        """lvm num must be one"""
        if is_lvm:
            vm.num = 1
        """check quota"""
        total_cores = vm.num * vm.cores
        total_memory = vm.num * vm.memory
        quota = yield check_tenant_quota(vm.tenant, total_cores, total_memory)
        yield update_tenant_vm_quotas(vm.tenant, quota.get("used_cores"),
                                      quota.get("used_memorys"))
        """check flavor"""
        flavor = yield find_or_create_flavor(vm.cores, vm.memory,
                                             sys_volume_size)

        vm_name = yield sequence.number_seq(VM_SEQUENCE_NAME,
                                            VM_SEQUENCE_PREFIX)
        """check available host"""
        host_id = ""
        if vm.host:
            subnet_ids = [network.get('subnet') for network in vm.network]
            avilable_host = yield get_avilable_host(vm.tenant, subnet_ids,
                                                    total_cores, total_memory,
                                                    volume_type)
            for host in avilable_host:
                if vm.host == host['name']:
                    host_id = host['id']
            if not host_id:
                raise HostUnAvailable
        """check network unused ip"""
        vlan_name = {}
        for network in vm.network:
            vlan_id = network.get('vlan')
            network_obj = yield network_module.get_network(vlan_id)
            vlan_name[vlan_id] = network_obj.get("name") if network_obj else ""
            subnet_id = network.get('subnet')
            ips = yield network_module.get_tenant_ips(subnet_id,
                                                      tenant_id=vm.tenant)
            unused = []
            for ip in ips.get("ipavailable"):
                available = yield network_module.gen_ips_list(
                    ip["start"], ip["end"])
                unused.extend(available)
            if len(unused) < vm.num:
                raise IpsInUsedError
        """create vm templates"""
        name_ips = []
        vm_templates = []
        vm_host_used = {}
        availability_zone = None
        for i in xrange(1, vm.num + 1):
            if vm.num > 1:
                name = str(vm_name) + "-" + str(i)
            else:
                name = vm_name
            """select host"""

            host_name = vm.host
            if not vm.host:
                subnet_ids = [network.get('subnet') for network in vm.network]
                avilable_hosts = yield get_avilable_host(
                    vm.tenant, subnet_ids, vm.cores, vm.memory, volume_type,
                    vm_host_used)
                select_host = yield get_host_by_stategy(
                    avilable_hosts, vm.create_policy)
                host_name = select_host['name']
                host_id = select_host['id']
                if host_name in vm_host_used:
                    vm_host_used[host_name]['used_cores'] += vm.cores
                    vm_host_used[host_name]['used_memorys'] += vm.memory
                else:
                    vm_host_used[host_name] = {
                        "used_cores": vm.cores,
                        "used_memorys": vm.memory
                    }
            """check network"""
            nics = yield check_network(name, vm.network, vm.tenant,
                                       vm.metadata.user, host_id)
            new_nics = []
            ips = []
            for nic in nics:
                ips.append(nic.get('v4-fixed-ip'))
                new_nics.append({
                    "name": vlan_name[nic.get('net-id')],
                    "id": nic.get('net-id'),
                    "ip": nic.get('v4-fixed-ip')
                })
            name_ips.append({"name": name, "ips": ips})
            block_device_mapping_v2 = {
                "vda":
                "%s:volume:volume:1:0:disk:" + image.get("disk_bus", "virtio")
            }
            vm.metadata.sys_volume['size'] = sys_volume_size
            metadata = {
                "user": vm.metadata.user,
                "nics": str(new_nics),
                "sys_volume": str(vm.metadata.sys_volume),
                "extend": str(vm.metadata.extend)
            }
            if is_lvm:
                availability_zone = host_name

            if is_iso:
                block_device_mapping_v2[
                    "vda"] = "%s:volume:volume:0:0:cdrom:" + image.get(
                        "disk_bus", "virtio")
                block_device_mapping_v2[
                    "vdb"] = "%s:volume:volume:1:1:disk:virtio"

                if is_windows:
                    """check drive volume image"""
                    drive_image_id = yield get_drive_image_id()
                    if not drive_image_id:
                        raise WindowsDriveImageNotExist()
                    image_volume = yield check_drive_image_up_down(
                        drive_image_id, availability_zone, volume_type)
                    drive_image_volume_id = image_volume.get("drive_volume")
                    if drive_image_volume_id:
                        """drive volume exist"""
                        message["drive_volume_id"] = drive_image_volume_id
                    else:
                        """create drive volume image"""
                        admin_tenant = yield get_admin_tenant_id()
                        yield volume_create(
                            size=image['min_disk'],
                            tenant_id=admin_tenant,
                            snapshot_id=None,
                            source_volid=None,
                            name=image_volume.get("name"),
                            description=3,
                            volume_type=volume_type,
                            source_replica=None,
                            metadata={
                                "task_id": task_id,
                                "displayname": image.get("disk_format")
                            },
                            project_id=admin_tenant,
                            image_ref=drive_image_id,
                            availability_zone=availability_zone)

                    block_device_mapping_v2[
                        "vdc"] = "%s:volume:volume:0:2:cdrom:" + image.get(
                            "disk_bus", "virtio")

            template = create_vm_template(
                name=name,
                image="",
                host=host_name,
                tenant=vm.tenant,
                userdata=vm.userdata,
                flavor=flavor['id'],
                meta=metadata,
                block_device_mapping_v2=block_device_mapping_v2,
                nics=nics,
                disk_config='AUTO',
                admin_pass=vm.super_user_pass,
                availability_zone="nova:%s:%s" % (host_name, host_name))

            LOG.debug("boot vm task id is %s resource is %s", task_id, name)

            volume = {
                "name": SYS_VOLUME % name,
                "size": str(sys_volume_size),
                "volume_type": volume_type,
                "tenant_id": vm.tenant,
                "availability_zone": availability_zone
            }
            yield insert_task_flow(task_id,
                                   "wait creat",
                                   name,
                                   message,
                                   type=task.VM_CREATE_TYPE,
                                   body=template['body'],
                                   resource_url=template['resource_url'],
                                   response_key=template['response_key'],
                                   tenant=vm.tenant,
                                   image=vm.image,
                                   is_iso=is_iso,
                                   cores=vm.cores,
                                   memory=vm.memory,
                                   host=template['host'],
                                   volume=volume,
                                   is_windows=is_windows)

            vm_templates.append(template)
        """check sys volume image"""
        image_volume = yield check_image_up_down(vm.image, availability_zone,
                                                 volume_type)
        image_sys_volume = image_volume.get("sys_volume")

        if not image_sys_volume:
            """create sys volume image"""
            admin_tenant = yield get_admin_tenant_id()
            yield volume_create(size=image['min_disk'],
                                tenant_id=admin_tenant,
                                snapshot_id=None,
                                source_volid=None,
                                name=image_volume.get("name"),
                                description=3,
                                volume_type=volume_type,
                                source_replica=None,
                                metadata={
                                    "task_id": task_id,
                                    "displayname": image.get("disk_format")
                                },
                                project_id=admin_tenant,
                                image_ref=vm.image,
                                availability_zone=availability_zone)
        else:
            """clone sys volume"""
            for vm_template in vm_templates:
                vm_n = vm_template['body']["server"]["name"]
                volume = {
                    "name": SYS_VOLUME % vm_n,
                    "size": str(sys_volume_size),
                    "volume_type": volume_type,
                    "tenant_id": vm.tenant,
                }
                LOG.debug("create_server create vm sys volume is %s", volume)
                if is_iso:
                    image_sys_volume = None
                yield volume_create(size=volume.get("size"),
                                    tenant_id=volume.get("tenant_id"),
                                    snapshot_id=None,
                                    source_volid=image_sys_volume,
                                    name=volume["name"],
                                    description=str(1),
                                    volume_type=volume['volume_type'],
                                    source_replica=None,
                                    metadata=None,
                                    project_id=volume['tenant_id'],
                                    image_ref=None,
                                    availability_zone=availability_zone)

    except Exception, e:
        LOG.error("create instance error: %s" % e)
        used_quota = yield get_tenant_quota(tenant_id=vm.tenant)
        yield update_tenant_vm_quotas(vm.tenant, used_quota.get("used_cores"),
                                      used_quota.get("used_memorys"))
        for nic in nics:
            yield network_module.request_delete_ports(vm.tenant,
                                                      nic.get("port-id"))
        raise e
Esempio n. 4
0
def create_volume(**volume):
    """ create volume
    :param volume: A dict of volume
    :return:
    """
    if is_none_or_empty(volume['volume_type']):
        volume['volume_type'] = CONF.storage.default_type
    volume_project = volume['tenant_id']
    volume_size = calc_size(volume['size'])
    volume_num = volume['num']
    if volume['volume_type'] == "lvm":
        volume_num = 1

    disk_capacity = yield get_quota(volume_project, QUOTA_NAMES.disk_capacity)
    disks = yield get_quota(volume_project, QUOTA_NAMES.disks)

    used_disks = disks.get("quota_used")
    used_disk_capacity = disk_capacity.get("quota_used")

    used_disks += volume_num
    used_disk_capacity += volume_size * volume_num

    yield check_quota(volume_project,
                      QUOTA_NAMES.disks,
                      used_disks)
    yield check_quota(volume_project,
                      QUOTA_NAMES.disk_capacity,
                      used_disk_capacity)

    yield update_quota_used(volume_project, QUOTA_NAMES.disks,
                            used_disks)
    yield update_quota_used(volume_project, QUOTA_NAMES.disk_capacity,
                            used_disk_capacity)

    try:
        volume_name = yield number_seq("volume-sequence", "vd-")
        names = []
        for i in range(1, volume_num + 1):
            if volume_num > 1:
                name = str(volume_name) + "-" + str(i)
            else:
                name = volume_name
            names.append(name)
            metadata = {"des": "" if is_none_or_empty(volume['des']) else volume['des'],
                        "user": "" if is_none_or_empty(volume['user_id']) else volume['user_id'],
                        "displayname": "" if is_none_or_empty(volume['displayname']) else volume['displayname']}

            yield volume_create(size=volume_size,
                                tenant_id=volume_project,
                                snapshot_id=None,
                                source_volid=volume['source_volid'],
                                name=name,
                                description=str(0),
                                volume_type=volume['volume_type'],
                                source_replica=None,
                                metadata=metadata,
                                project_id=volume['tenant_id'],
                                image_ref=volume['image_ref'],
                                availability_zone=volume["host"])
    except Exception, e:
        LOG.error("create volume error: %s" % e)
        raise VolumeOperationFailed()
Esempio n. 5
0
    def save_template(self, action):
        app_path = cfgutils.getval('app', 'template_path')
        body = json.loads(self.request.body)
        try:
            check_template(body)
            # 为流程生成ID
            for a in body['action']:
                if 'name' not in a or a['name'] == '':
                    seq = yield sequence.number_seq('SA', '')
                    a['name'] = 'SA-%s' % seq
                    logging.getLogger('manor').debug(
                        'generate action name: %s' % a['name'])
                else:
                    logging.getLogger('manor').debug('action name: %s' %
                                                     a['name'])

            if action == 'create':
                operation_log_type = Operator.CREATE
                seq = yield sequence.number_seq('TM', '')
                body['name'] = 'TM-%s' % seq
                body['status'] = 0
            else:
                operation_log_type = Operator.UPDATE
                if 'name' not in body:
                    raise Exception('error.manor.templates.no.name')
                if body['name'] == '':
                    raise Exception('error.manor.templates.name.is.empty')
                if not os.path.isfile('%s/%s.yaml' % (app_path, body['name'])):
                    raise Exception('error.manor.templates.update.not.exist')
                for a in body['action']:
                    if 'deploy' == a['type']:
                        a['label'] = 'install_stream'
                    if 'label' not in a:
                        raise Exception(
                            'error.manor.templates.action.no.label')
                    if a['label'] == '':
                        raise Exception(
                            'error.manor.templates.action.label.is.empty')

            script_path = '%s/_%s' % (app_path, body['name'])
            if not os.path.isdir(script_path):
                os.mkdir(script_path)

            for a in body['action']:
                for k in a['streamlet'].keys():
                    if k.split('$')[0] == 'execute_script':
                        node_params = a['streamlet'][k]['params']
                        for p in node_params:
                            if 'execute_script_content' in p:
                                content = p['execute_script_content']
                                self.log.debug('get script content:\n%s' %
                                               content)
                                e_name = get_script_ex_name(p)
                                with codecs.open(
                                        '%s/%s%s' % (script_path, k, e_name),
                                        'w+', 'utf-8') as f:
                                    f.write(content)
                                del p['execute_script_content']

            tmp_f = str(uuid.uuid1())
            with codecs.open('%s/%s' % (app_path, tmp_f), "w+",
                             'utf-8') as text_file:
                text_file.write(yaml.safe_dump(body))
            os.rename('%s/%s' % (app_path, tmp_f),
                      '%s/%s.yaml' % (app_path, body['name']))

            optLog.write(self.request, optLog.Type.APP_TEMPLATE, body['name'],
                         operation_log_type, '%s' % body['label'])

            self.response(generals.gen_response("ok"))
        except Exception as e:
            logging.getLogger('manor').error(generals.trace())
            raise e