Esempio n. 1
0
def r_delete(ids):
    rows, _ = SSHKeyGuestMapping.get_by_filter(
        filter_str=':'.join(['ssh_key_id', 'in', ids]))
    SSHKeyGuestMapping.delete_by_filter(
        filter_str=':'.join(['ssh_key_id', 'in', ids]))

    for row in rows:
        update_ssh_key(uuid=row['guest_uuid'])

    return ssh_key_base.delete(ids=ids,
                               ids_rule=Rules.IDS.value,
                               by_field='id')
Esempio n. 2
0
def r_delete(uuids):

    args_rules = [
        Rules.UUIDS.value
    ]

    # TODO: 加入是否删除使用的数据磁盘开关,如果为True,则顺便删除使用的磁盘。否则解除该磁盘被使用的状态。
    try:
        ji.Check.previewing(args_rules, {'uuids': uuids})

        guest = Guest()
        # 检测所指定的 UUDIs 实例都存在
        for uuid in uuids.split(','):
            guest.uuid = uuid
            guest.get_by('uuid')

        config = Config()
        config.id = 1
        config.get()

        # 执行删除操作
        for uuid in uuids.split(','):
            guest.uuid = uuid
            guest.get_by('uuid')

            message = {
                '_object': 'guest',
                'action': 'delete',
                'uuid': uuid,
                'storage_mode': config.storage_mode,
                'dfs_volume': config.dfs_volume,
                'node_id': guest.node_id
            }

            Utils.emit_instruction(message=json.dumps(message))

            # 删除创建失败的 Guest
            if guest.status == status.GuestState.dirty.value:
                disk = Disk()
                disk.uuid = guest.uuid
                disk.get_by('uuid')

                if disk.state == status.DiskState.pending.value:
                    disk.delete()
                    guest.delete()
                    SSHKeyGuestMapping.delete_by_filter(filter_str=':'.join(['guest_uuid', 'eq', guest.uuid]))

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)
        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Esempio n. 3
0
def r_unbound(ssh_key_id):

    args_rules = [
        Rules.SSH_KEY_ID_EXT.value,
    ]

    try:
        ji.Check.previewing(args_rules, {'ssh_key_id': ssh_key_id})

        rows, _ = SSHKeyGuestMapping.get_by_filter(
            filter_str=':'.join(['ssh_key_id', 'eq', ssh_key_id]))
        guests_uuid = list()
        for row in rows:
            guests_uuid.append(row['guest_uuid'])

        if guests_uuid.__len__() == 0:
            guests_uuid.append('_')

        request.__setattr__(
            'args',
            ImmutableMultiDict([
                ('filter', ':'.join(['uuid', 'notin', ','.join(guests_uuid)])),
                ('page_size', 10000)
            ]))

        return guest_base.get_by_filter()
    except ji.PreviewingError, e:
        return json.loads(e.message)
Esempio n. 4
0
def update_ssh_key(uuid):

    guest = Guest()
    guest.uuid = uuid
    guest.get_by('uuid')

    # 不支持更新离线虚拟机的 SSH-KEY
    if guest.status != GuestState.running.value:
        return

    os_template_image = OSTemplateImage()
    os_template_profile = OSTemplateProfile()

    os_template_image.id = guest.os_template_image_id
    os_template_image.get()

    os_template_profile.id = os_template_image.os_template_profile_id
    os_template_profile.get()

    # 不支持更新 Windows 虚拟机的 SSH-KEY
    if os_template_profile.os_type == 'windows':
        return

    rows, _ = SSHKeyGuestMapping.get_by_filter(
        filter_str=':'.join(['guest_uuid', 'eq', uuid]))

    ssh_keys_id = list()
    for row in rows:
        ssh_keys_id.append(row['ssh_key_id'].__str__())

    ssh_keys = list()

    if ssh_keys_id.__len__() > 0:
        rows, _ = SSHKey.get_by_filter(
            filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))
        for row in rows:
            ssh_keys.append(row['public_key'])

    else:
        ssh_keys.append('')

    message = {
        '_object': 'guest',
        'uuid': uuid,
        'node_id': guest.node_id,
        'action': 'update_ssh_key',
        'ssh_keys': ssh_keys,
        'os_type': os_template_profile.os_type,
        'passback_parameters': {
            'uuid': uuid,
            'ssh_keys': ssh_keys,
            'os_type': os_template_profile.os_type
        }
    }

    Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
Esempio n. 5
0
def r_get(uuids):
    ret = guest_base.get(ids=uuids, ids_rule=Rules.UUIDS.value, by_field='uuid')

    if '200' != ret['state']['code']:
        return ret

    rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', uuids]))

    guest_uuid_ssh_key_id_mapping = dict()
    ssh_keys_id = list()

    for row in rows:
        if row['ssh_key_id'] not in ssh_keys_id:
            ssh_keys_id.append(row['ssh_key_id'].__str__())

        if row['guest_uuid'] not in guest_uuid_ssh_key_id_mapping:
            guest_uuid_ssh_key_id_mapping[row['guest_uuid']] = list()

        guest_uuid_ssh_key_id_mapping[row['guest_uuid']].append(row['ssh_key_id'])

    rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))

    ssh_key_id_mapping = dict()

    for row in rows:
        row['url'] = url_for('v_ssh_keys.show')
        ssh_key_id_mapping[row['id']] = row

    if -1 == uuids.find(','):
        if 'ssh_keys' not in ret['data']:
            ret['data']['ssh_keys'] = list()

        if ret['data']['uuid'] in guest_uuid_ssh_key_id_mapping:
            for ssh_key_id in guest_uuid_ssh_key_id_mapping[ret['data']['uuid']]:

                if ssh_key_id not in ssh_key_id_mapping:
                    continue

                ret['data']['ssh_keys'].append(ssh_key_id_mapping[ssh_key_id])

    else:
        for i, guest in enumerate(ret['data']):
            if 'ssh_keys' not in ret['data'][i]:
                ret['data'][i]['ssh_keys'] = list()

            if ret['data'][i]['uuid'] in guest_uuid_ssh_key_id_mapping:
                for ssh_key_id in guest_uuid_ssh_key_id_mapping[ret['data'][i]['uuid']]:

                    if ssh_key_id not in ssh_key_id_mapping:
                        continue

                    ret['data'][i]['ssh_keys'].append(ssh_key_id_mapping[ssh_key_id])

    return ret
Esempio n. 6
0
def r_unbind(ssh_key_id, uuids):

    args_rules = [Rules.SSH_KEY_ID_EXT.value, Rules.UUIDS.value]

    try:
        ji.Check.previewing(args_rules, {
            'ssh_key_id': ssh_key_id,
            'uuids': uuids
        })

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)

        # 获取已经和该 ssh_key 绑定过的映射集合,从中获取映射 id,用于解绑操作
        rows, _ = SSHKeyGuestMapping.get_by_filter(
            filter_str=':'.join(['ssh_key_id', 'eq', ssh_key_id]))

        guests_uuid = uuids.split(',')

        ssh_key_guest_mapping = SSHKeyGuestMapping()
        for row in rows:
            # 解除已经绑定过的 guest
            if row['guest_uuid'] in guests_uuid:
                ssh_key_guest_mapping.id = row['id']
                ssh_key_guest_mapping.delete()

                update_ssh_key(uuid=row['guest_uuid'])

        # 返回执行结果
        rows, _ = SSHKeyGuestMapping.get_by_filter(
            filter_str=':'.join(['ssh_key_id', 'eq', ssh_key_id]))
        guests_uuid = list()
        for row in rows:
            guests_uuid.append(row['guest_uuid'])

        if guests_uuid.__len__() == 0:
            guests_uuid.append('_')

        request.__setattr__(
            'args',
            ImmutableMultiDict([
                ('filter', ':'.join(['uuid', 'in', ','.join(guests_uuid)])),
                ('page_size', 10000)
            ]))

        return guest_base.get_by_filter()
    except ji.PreviewingError, e:
        return json.loads(e.message)
Esempio n. 7
0
    def response_processor(cls):
        _object = cls.message['message']['_object']
        action = cls.message['message']['action']
        uuid = cls.message['message']['uuid']
        state = cls.message['type']
        data = cls.message['message']['data']
        node_id = cls.message['node_id']

        if _object == 'guest':
            if action == 'create':
                if state == ResponseState.success.value:
                    # 系统盘的 UUID 与其 Guest 的 UUID 相同
                    cls.disk.uuid = uuid
                    cls.disk.get_by('uuid')
                    cls.disk.guest_uuid = uuid
                    cls.disk.state = DiskState.mounted.value
                    # disk_info['virtual-size'] 的单位为Byte,需要除以 1024 的 3 次方,换算成单位为 GB 的值
                    cls.disk.size = data['disk_info']['virtual-size'] / (1024**
                                                                         3)
                    cls.disk.update()

                else:
                    cls.guest.uuid = uuid
                    cls.guest.get_by('uuid')
                    cls.guest.status = GuestState.dirty.value
                    cls.guest.update()

            elif action == 'migrate':
                pass

            elif action == 'delete':
                if state == ResponseState.success.value:
                    cls.config.get()
                    cls.guest.uuid = uuid
                    cls.guest.get_by('uuid')

                    if IP(cls.config.start_ip).int() <= IP(
                            cls.guest.ip).int() <= IP(cls.config.end_ip).int():
                        if db.r.srem(app.config['ip_used_set'], cls.guest.ip):
                            db.r.sadd(app.config['ip_available_set'],
                                      cls.guest.ip)

                    if (cls.guest.vnc_port - cls.config.start_vnc_port) <= \
                            (IP(cls.config.end_ip).int() - IP(cls.config.start_ip).int()):
                        if db.r.srem(app.config['vnc_port_used_set'],
                                     cls.guest.vnc_port):
                            db.r.sadd(app.config['vnc_port_available_set'],
                                      cls.guest.vnc_port)

                    cls.guest.delete()

                    # TODO: 加入是否删除使用的数据磁盘开关,如果为True,则顺便删除使用的磁盘。否则解除该磁盘被使用的状态。
                    cls.disk.uuid = uuid
                    cls.disk.get_by('uuid')
                    cls.disk.delete()
                    cls.disk.update_by_filter(
                        {
                            'guest_uuid': '',
                            'sequence': -1,
                            'state': DiskState.idle.value
                        },
                        filter_str='guest_uuid:eq:' + cls.guest.uuid)

                    SSHKeyGuestMapping.delete_by_filter(filter_str=':'.join(
                        ['guest_uuid', 'eq', cls.guest.uuid]))

            elif action == 'reset_password':
                if state == ResponseState.success.value:
                    cls.guest.uuid = uuid
                    cls.guest.get_by('uuid')
                    cls.guest.password = cls.message['message'][
                        'passback_parameters']['password']
                    cls.guest.update()

            elif action == 'attach_disk':
                cls.disk.uuid = cls.message['message']['passback_parameters'][
                    'disk_uuid']
                cls.disk.get_by('uuid')
                if state == ResponseState.success.value:
                    cls.disk.guest_uuid = uuid
                    cls.disk.sequence = cls.message['message'][
                        'passback_parameters']['sequence']
                    cls.disk.state = DiskState.mounted.value
                    cls.disk.update()

            elif action == 'detach_disk':
                cls.disk.uuid = cls.message['message']['passback_parameters'][
                    'disk_uuid']
                cls.disk.get_by('uuid')
                if state == ResponseState.success.value:
                    cls.disk.guest_uuid = ''
                    cls.disk.sequence = -1
                    cls.disk.state = DiskState.idle.value
                    cls.disk.update()

            elif action == 'boot':

                if state == ResponseState.success.value:
                    pass

        elif _object == 'disk':
            if action == 'create':
                cls.disk.uuid = uuid
                cls.disk.get_by('uuid')
                cls.disk.node_id = node_id
                if state == ResponseState.success.value:
                    cls.disk.state = DiskState.idle.value

                else:
                    cls.disk.state = DiskState.dirty.value

                cls.disk.update()

            elif action == 'resize':
                if state == ResponseState.success.value:
                    cls.config.get()
                    cls.disk.uuid = uuid
                    cls.disk.get_by('uuid')
                    cls.disk.size = cls.message['message'][
                        'passback_parameters']['size']
                    cls.disk.quota(config=cls.config)
                    cls.disk.update()

            elif action == 'delete':
                cls.disk.uuid = uuid
                cls.disk.get_by('uuid')
                cls.disk.delete()

        elif _object == 'snapshot':
            if action == 'create':
                cls.snapshot.id = cls.message['message'][
                    'passback_parameters']['id']
                cls.snapshot.get()

                if state == ResponseState.success.value:
                    cls.snapshot.snapshot_id = data['snapshot_id']
                    cls.snapshot.parent_id = data['parent_id']
                    cls.snapshot.xml = data['xml']
                    cls.snapshot.progress = 100
                    cls.snapshot.update()

                    disks, _ = Disk.get_by_filter(filter_str='guest_uuid:eq:' +
                                                  cls.snapshot.guest_uuid)

                    for disk in disks:
                        cls.snapshot_disk_mapping.snapshot_id = cls.snapshot.snapshot_id
                        cls.snapshot_disk_mapping.disk_uuid = disk['uuid']
                        cls.snapshot_disk_mapping.create()

                else:
                    cls.snapshot.progress = 255
                    cls.snapshot.update()

            if action == 'delete':
                if state == ResponseState.success.value:
                    cls.snapshot.id = cls.message['message'][
                        'passback_parameters']['id']
                    cls.snapshot.get()

                    # 更新子快照的 parent_id 为,当前快照的 parent_id。因为当前快照已被删除。
                    Snapshot.update_by_filter(
                        {'parent_id': cls.snapshot.parent_id},
                        filter_str='parent_id:eq:' + cls.snapshot.snapshot_id)

                    SnapshotDiskMapping.delete_by_filter(filter_str=':'.join(
                        ['snapshot_id', 'eq', cls.snapshot.snapshot_id]))

                    cls.snapshot.delete()

                else:
                    pass

            if action == 'revert':
                # 不论恢复成功与否,都使快照恢复至正常状态。
                cls.snapshot.id = cls.message['message'][
                    'passback_parameters']['id']
                cls.snapshot.get()
                cls.snapshot.progress = 100
                cls.snapshot.update()

            if action == 'convert':
                cls.snapshot.snapshot_id = cls.message['message'][
                    'passback_parameters']['id']
                cls.snapshot.get_by('snapshot_id')
                cls.snapshot.progress = 100
                cls.snapshot.update()

                cls.os_template_image.id = cls.message['message'][
                    'passback_parameters']['os_template_image_id']
                cls.os_template_image.get()

                if state == ResponseState.success.value:
                    cls.os_template_image.progress = 100

                else:
                    cls.os_template_image.progress = 255

                cls.os_template_image.update()

        elif _object == 'os_template_image':
            if action == 'delete':
                cls.os_template_image.id = cls.message['message'][
                    'passback_parameters']['id']
                cls.os_template_image.get()

                if state == ResponseState.success.value:
                    cls.os_template_image.delete()

                else:
                    pass

        else:
            pass
Esempio n. 8
0
def r_content_search():
    ret = guest_base.content_search()

    uuids = list()
    for guest in ret['data']:
        uuids.append(guest['uuid'])

    rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', ','.join(uuids)]))

    guest_uuid_ssh_key_id_mapping = dict()
    ssh_keys_id = list()

    for row in rows:
        if row['ssh_key_id'] not in ssh_keys_id:
            ssh_keys_id.append(row['ssh_key_id'].__str__())

        if row['guest_uuid'] not in guest_uuid_ssh_key_id_mapping:
            guest_uuid_ssh_key_id_mapping[row['guest_uuid']] = list()

        guest_uuid_ssh_key_id_mapping[row['guest_uuid']].append(row['ssh_key_id'])

    rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))

    ssh_key_id_mapping = dict()

    for row in rows:
        row['url'] = url_for('v_ssh_keys.show')
        ssh_key_id_mapping[row['id']] = row

    rows, _ = Snapshot.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', ','.join(uuids)]))

    snapshots_guest_uuid_mapping = dict()

    for row in rows:
        guest_uuid = row['guest_uuid']
        if guest_uuid not in snapshots_guest_uuid_mapping:
            snapshots_guest_uuid_mapping[guest_uuid] = list()

        snapshots_guest_uuid_mapping[guest_uuid].append(row)

    for i, guest in enumerate(ret['data']):

        guest_uuid = ret['data'][i]['uuid']

        if 'ssh_keys' not in ret['data'][i]:
            ret['data'][i]['ssh_keys'] = list()

        if guest_uuid in guest_uuid_ssh_key_id_mapping:
            for ssh_key_id in guest_uuid_ssh_key_id_mapping[guest_uuid]:

                if ssh_key_id not in ssh_key_id_mapping:
                    continue

                ret['data'][i]['ssh_keys'].append(ssh_key_id_mapping[ssh_key_id])

        if 'snapshot' not in ret['data'][i]:
            ret['data'][i]['snapshot'] = {
                'creatable': True,
                'mapping': list()
            }

        if guest_uuid in snapshots_guest_uuid_mapping:
            ret['data'][i]['snapshot']['mapping'] = snapshots_guest_uuid_mapping[guest_uuid]

            for snapshot in snapshots_guest_uuid_mapping[guest_uuid]:
                if snapshot['progress'] == 100:
                    continue

                else:
                    ret['data'][i]['snapshot']['creatable'] = False

    return ret
Esempio n. 9
0
def r_create():

    args_rules = [
        Rules.CPU.value,
        Rules.MEMORY.value,
        Rules.BANDWIDTH.value,
        Rules.BANDWIDTH_UNIT.value,
        Rules.OS_TEMPLATE_IMAGE_ID.value,
        Rules.QUANTITY.value,
        Rules.REMARK.value,
        Rules.PASSWORD.value,
        Rules.LEASE_TERM.value
    ]

    if 'node_id' in request.json:
        args_rules.append(
            Rules.NODE_ID.value
        )

    if 'ssh_keys_id' in request.json:
        args_rules.append(
            Rules.SSH_KEYS_ID.value
        )

    try:
        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)

        ji.Check.previewing(args_rules, request.json)

        config = Config()
        config.id = 1
        config.get()

        os_template_image = OSTemplateImage()
        os_template_profile = OSTemplateProfile()

        os_template_image.id = request.json.get('os_template_image_id')
        if not os_template_image.exist():
            ret['state'] = ji.Common.exchange_state(40450)
            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_template_image.id.__str__()])
            return ret

        os_template_image.get()
        os_template_profile.id = os_template_image.os_template_profile_id
        os_template_profile.get()

        os_template_initialize_operates, os_template_initialize_operates_count = \
            OSTemplateInitializeOperate.get_by_filter(
                filter_str='os_template_initialize_operate_set_id:eq:' +
                           os_template_profile.os_template_initialize_operate_set_id.__str__())

        if db.r.scard(app.config['ip_available_set']) < 1:
            ret['state'] = ji.Common.exchange_state(50350)
            return ret

        node_id = request.json.get('node_id', None)

        # 默认只取可随机分配虚拟机的 hosts
        available_hosts = Host.get_available_hosts(nonrandom=False)

        # 当指定了 host 时,取全部活着的 hosts
        if node_id is not None:
            available_hosts = Host.get_available_hosts(nonrandom=None)

        if available_hosts.__len__() == 0:
            ret['state'] = ji.Common.exchange_state(50351)
            return ret

        available_hosts_mapping_by_node_id = dict()

        for host in available_hosts:
            if host['node_id'] not in available_hosts_mapping_by_node_id:
                available_hosts_mapping_by_node_id[host['node_id']] = host

        if node_id is not None and node_id not in available_hosts_mapping_by_node_id:
            ret['state'] = ji.Common.exchange_state(50351)
            return ret

        ssh_keys_id = request.json.get('ssh_keys_id', list())
        ssh_keys = list()
        ssh_key_guest_mapping = SSHKeyGuestMapping()

        if ssh_keys_id.__len__() > 0:
            rows, _ = SSHKey.get_by_filter(
                filter_str=':'.join(['id', 'in', ','.join(_id.__str__() for _id in ssh_keys_id)]))

            for row in rows:
                ssh_keys.append(row['public_key'])

        bandwidth = request.json.get('bandwidth')
        bandwidth_unit = request.json.get('bandwidth_unit')

        if bandwidth_unit == 'k':
            bandwidth = bandwidth * 1000

        elif bandwidth_unit == 'm':
            bandwidth = bandwidth * 1000 ** 2

        elif bandwidth_unit == 'g':
            bandwidth = bandwidth * 1000 ** 3

        else:
            ret = dict()
            ret['state'] = ji.Common.exchange_state(41203)
            raise ji.PreviewingError(json.dumps(ret, ensure_ascii=False))

        # http://man7.org/linux/man-pages/man8/tc.8.html
        # 如果带宽大于 tc 所控最大速率,则置其为无限带宽
        # 34359738360 等于 tc 最大可控字节速率,换算出的比特位
        if bandwidth > 34359738360:
            bandwidth = 0

        quantity = request.json.get('quantity')

        while quantity:
            quantity -= 1
            guest = Guest()
            guest.uuid = uuid4().__str__()
            guest.cpu = request.json.get('cpu')
            # 虚拟机内存单位,模板生成方法中已置其为GiB
            guest.memory = request.json.get('memory')
            guest.bandwidth = bandwidth
            guest.os_template_image_id = request.json.get('os_template_image_id')
            guest.label = ji.Common.generate_random_code(length=8)
            guest.remark = request.json.get('remark', '')

            guest.password = request.json.get('password')
            if guest.password is None or guest.password.__len__() < 1:
                guest.password = ji.Common.generate_random_code(length=16)

            guest.ip = db.r.spop(app.config['ip_available_set'])
            db.r.sadd(app.config['ip_used_set'], guest.ip)

            guest.network = config.vm_network
            guest.manage_network = config.vm_manage_network

            guest.vnc_port = db.r.spop(app.config['vnc_port_available_set'])
            db.r.sadd(app.config['vnc_port_used_set'], guest.vnc_port)

            guest.vnc_password = ji.Common.generate_random_code(length=16)

            disk = Disk()
            disk.uuid = guest.uuid
            disk.remark = guest.label.__str__() + '_SystemImage'
            disk.format = 'qcow2'
            disk.sequence = 0
            disk.size = 0
            disk.path = config.storage_path + '/' + disk.uuid + '.' + disk.format
            disk.guest_uuid = ''
            # disk.node_id 由 guest 事件处理机更新。涉及迁移时,其所属 node_id 会变更。参见 @models/event_processory.py:111 附近。
            disk.node_id = 0
            disk.quota(config=config)
            disk.create()

            if node_id is None:
                # 在可用计算节点中平均分配任务
                chosen_host = available_hosts[quantity % available_hosts.__len__()]

            else:
                chosen_host = available_hosts_mapping_by_node_id[node_id]

            guest.node_id = chosen_host['node_id']

            guest_xml = GuestXML(host=chosen_host, guest=guest, disk=disk, config=config,
                                 os_type=os_template_profile.os_type)
            guest.xml = guest_xml.get_domain()

            guest.node_id = int(guest.node_id)
            guest.create()

            ssh_key_guest_mapping.guest_uuid = guest.uuid

            if ssh_keys_id.__len__() > 0:
                for ssh_key_id in ssh_keys_id:
                    ssh_key_guest_mapping.ssh_key_id = ssh_key_id
                    ssh_key_guest_mapping.create()

            # 替换占位符为有效内容
            _os_template_initialize_operates = copy.deepcopy(os_template_initialize_operates)
            for k, v in enumerate(_os_template_initialize_operates):
                _os_template_initialize_operates[k]['content'] = v['content'].replace('{IP}', guest.ip).\
                    replace('{HOSTNAME}', guest.label). \
                    replace('{PASSWORD}', guest.password). \
                    replace('{NETMASK}', config.netmask).\
                    replace('{GATEWAY}', config.gateway).\
                    replace('{DNS1}', config.dns1).\
                    replace('{DNS2}', config.dns2). \
                    replace('{SSH-KEY}', '\n'.join(ssh_keys))

                _os_template_initialize_operates[k]['command'] = v['command'].replace('{IP}', guest.ip). \
                    replace('{HOSTNAME}', guest.label). \
                    replace('{PASSWORD}', guest.password). \
                    replace('{NETMASK}', config.netmask). \
                    replace('{GATEWAY}', config.gateway). \
                    replace('{DNS1}', config.dns1). \
                    replace('{DNS2}', config.dns2). \
                    replace('{SSH-KEY}', '\n'.join(ssh_keys))

            message = {
                '_object': 'guest',
                'action': 'create',
                'uuid': guest.uuid,
                'storage_mode': config.storage_mode,
                'dfs_volume': config.dfs_volume,
                'node_id': guest.node_id,
                'name': guest.label,
                'template_path': os_template_image.path,
                'os_type': os_template_profile.os_type,
                'disks': [disk.__dict__],
                'xml': guest_xml.get_domain(),
                'os_template_initialize_operates': _os_template_initialize_operates,
                'passback_parameters': {}
            }

            Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Esempio n. 10
0
def r_bind(ssh_key_id, uuids):

    args_rules = [Rules.SSH_KEY_ID_EXT.value, Rules.UUIDS.value]

    try:
        ji.Check.previewing(args_rules, {
            'ssh_key_id': ssh_key_id,
            'uuids': uuids
        })

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)

        ssh_key = SSHKey()
        ssh_key.id = ssh_key_id

        # 判断 ssh_key id 为 ssh_key_id 的对象是否存在
        if not ssh_key.exist():
            ret['state'] = ji.Common.exchange_state(40401)
            ret['state']['sub']['zh-cn'] = ''.join(
                [ret['state']['sub']['zh-cn'], ': ssh_key_id: ', ssh_key_id])
            return ret

        # 获取已经和该 ssh_key 绑定过的 guest uuid 集合,用于判断是否已经绑定过该 ssh_key,避免重复绑定
        rows, _ = SSHKeyGuestMapping.get_by_filter(
            filter_str=':'.join(['ssh_key_id', 'eq', ssh_key_id]))

        guests_uuid = list()
        for row in rows:
            guests_uuid.append(row['guest_uuid'])

        ssh_key_guest_mapping = SSHKeyGuestMapping()
        for uuid in uuids.split(','):
            # 如果已经绑定过,则忽略
            if uuid in guests_uuid:
                continue

            ssh_key_guest_mapping.ssh_key_id = ssh_key_id
            ssh_key_guest_mapping.guest_uuid = uuid
            ssh_key_guest_mapping.create()

            update_ssh_key(uuid=uuid)

        # 返回执行结果
        rows, _ = SSHKeyGuestMapping.get_by_filter(
            filter_str=':'.join(['ssh_key_id', 'eq', ssh_key_id]))
        guests_uuid = list()
        for row in rows:
            guests_uuid.append(row['guest_uuid'])

        if guests_uuid.__len__() == 0:
            guests_uuid.append('_')

        request.__setattr__(
            'args',
            ImmutableMultiDict([
                ('filter', ':'.join(['uuid', 'in', ','.join(guests_uuid)])),
                ('page_size', 10000)
            ]))

        return guest_base.get_by_filter()
    except ji.PreviewingError, e:
        return json.loads(e.message)