Ejemplo n.º 1
0
def validates_guest_add(obj):
    checker = Checker()
    check = True

    _ = obj._
    checker.errors = []

    if not is_param(obj.input, 'm_name'):
        check = False
        checker.add_error(_('Parameter m_name does not exist.'))
    else:
        check = checker.check_string(
                    _('Machine Name'),
                    obj.input.m_name,
                    CHECK_EMPTY | CHECK_LENGTH | CHECK_ONLYSPACE,
                    None,
                    min = MACHINE_NAME_MIN_LENGTH,
                    max = MACHINE_NAME_MAX_LENGTH,
            ) and check

    if is_param(obj.input, 'note_title'):
        check = checker.check_string(
                    _('Title'),
                    obj.input.note_title,
                    CHECK_LENGTH | CHECK_ONLYSPACE,
                    None,
                    min = NOTE_TITLE_MIN_LENGTH,
                    max = NOTE_TITLE_MAX_LENGTH,
                ) and check

    if is_param(obj.input, 'note_value'):
        check = checker.check_string(
                    _('Note'),
                    obj.input.note_value,
                    CHECK_ONLYSPACE,
                    None,
                    None,
                    None,
                ) and check

    if is_param(obj.input, 'tags'):
        for tag in comma_split(obj.input.tags):
            check = checker.check_string(
                        _('Tag'),
                        tag,
                        CHECK_LENGTH | CHECK_ONLYSPACE,
                        None,
                        min = TAG_MIN_LENGTH,
                        max = TAG_MAX_LENGTH,
                    ) and check


    if not is_param(obj.input, 'm_hypervisor'):
        check = False
        checker.add_error(_('Parameter m_hypervisor does not exist.'))
    else:
        check = checker.check_hypervisor(
                _('Hypervisor'),
                obj.input.m_hypervisor,
                CHECK_EMPTY | CHECK_VALID | CHECK_MIN | CHECK_MAX,
                HYPERVISOR_MIN_SIZE,
                HYPERVISOR_MAX_SIZE,
            ) and check

    if not is_param(obj.input, 'domain_name'):
        check = False
        checker.add_error(_('Parameter domain_name does not exist.'))
    else:
        check = checker.check_string(
                _('Domain Name'),
                obj.input.domain_name,
                CHECK_EMPTY | CHECK_VALID | CHECK_LENGTH | CHECK_ONLYSPACE,
                '[^-a-zA-Z0-9_\.]+',
                DOMAIN_NAME_MIN_LENGTH,
                DOMAIN_NAME_MAX_LENGTH,
            ) and check

        if obj.input.domain_name in get_dom_list():
            dom_type = get_dom_type(obj.input.domain_name)
            checker.add_error(_("The same domain name already exists for hypervisor '%s'.") % dom_type.upper())
            check = False

    if is_param(obj.input, 'vm_mem_size'):
        check = checker.check_number(
                _('Memory Size (MB)'),
                obj.input.vm_mem_size,
                CHECK_VALID | CHECK_MIN | CHECK_EMPTY,
                MEMORY_MIN_SIZE,
                None,
            ) and check

    if is_param(obj.input, 'pool_type'):
        if obj.input.pool_type != "block":
            if is_param(obj.input, 'vm_disk_size'):
                check = checker.check_number(
                        _('Disk Size (MB)'),
                        obj.input.vm_disk_size,
                        CHECK_VALID | CHECK_MIN | CHECK_EMPTY,
                        DISK_MIN_SIZE,
                        None,
                ) and check

    if not is_param(obj.input, 'boot_image'):
        check = False
        checker.add_error(_('Parameter boot_image does not exist.'))
    else:
        if obj.input.boot_image == "kernel":
            if not is_param(obj.input, 'vm_kernel'):
                check = False
                checker.add_error(_('Parameter vm_kernel does not exist.'))
            else:
                check = checker.check_startfile(
                        _('Kernel Image'),
                        obj.input.vm_kernel,
                        CHECK_EMPTY | CHECK_VALID | CHECK_EXIST,
                    ) and check

            if not is_param(obj.input, 'vm_initrd'):
                check = False
                checker.add_error(_('Parameter vm_initrd does not exist.'))
            else:
                check = checker.check_startfile(
                        _('Initrd Image'),
                        obj.input.vm_initrd,
                        CHECK_EMPTY | CHECK_VALID | CHECK_EXIST,
                    ) and check

        if obj.input.boot_image == "iso":
            if not is_param(obj.input, 'vm_iso'):
                check = False
                checker.add_error(_('Parameter vm_iso does not exist.'))
            else:
                check = checker.check_startfile(
                        _('ISO Image'),
                        obj.input.vm_iso,
                        CHECK_EMPTY | CHECK_VALID | CHECK_EXIST,
                    ) and check
                if check:
                    check = is_iso9660_filesystem_format(obj.input.vm_iso)
                    checker.add_error(_('"%s" is not valid ISO 9660 CD-ROM filesystem data.') % obj.input.vm_iso)

    if not is_param(obj.input, 'keymap'):
        check = False
        checker.add_error(_('"%s" is required.') % _('Graphics Keymap'))
    else:
        hypervisor = "KVM"
        if int(obj.input.m_hypervisor) == MACHINE_HYPERVISOR['XEN']:
            hypervisor = "XEN"
        elif int(obj.input.m_hypervisor) == MACHINE_HYPERVISOR['KVM']:
            hypervisor = "KVM"
        check = checker.check_keymap(
                _('Graphics Keymap'),
                obj.input.keymap,
                CHECK_EMPTY | CHECK_EXIST,
                hypervisor
                ) and check

    if not is_param(obj.input, 'vm_graphics_port'):
        check = False
        checker.add_error(_('Parameter vm_graphics_port does not exist.'))
    else:
        check = checker.check_number(
                _('Graphics Port Number'),
                obj.input.vm_graphics_port,
                CHECK_EMPTY | CHECK_VALID | CHECK_MIN | CHECK_MAX,
                GRAPHICS_PORT_MIN_NUMBER,
                GRAPHICS_PORT_MAX_NUMBER,
            ) and check

    if not is_param(obj.input, 'vm_mac'):
        check = False
        checker.add_error(_('Parameter vm_mac does not exist.'))
    else:
        check = checker.check_macaddr(
                _('MAC Address'),
                obj.input.vm_mac,
                CHECK_EMPTY | CHECK_VALID,
            ) and check

    obj.view.alert = checker.errors
    return check
Ejemplo n.º 2
0
def chkopts(opts):
    reg = re.compile("[^a-zA-Z0-9\./_:-]")

    if opts.name:
        if reg.search(opts.name):
            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-n or --name', opts.name))
    else:
        raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')

    if opts.type:
        if opts.type.lower() != "xen" and opts.type.lower() != "kvm":
            raise KssCommandOptException('ERROR: %s option is require %s or %s.' % ('-t or --type', 'xen', 'kvm'))

    if opts.storage_pool:
        if reg.search(opts.storage_pool):
            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-P or --storage-pool', opts.storage_pool))
    else:
        raise KssCommandOptException('ERROR: %s option is required.' % '-P or --storage-pool')

    if opts.storage_volume:
        if reg.search(opts.storage_volume):
            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-V or --storage-volume', opts.storage_volume))
    else:
        raise KssCommandOptException('ERROR: %s option is required.' % '-V or --storage-volume')

    if opts.bus:
        if reg.search(opts.bus):
            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-B or --bus', opts.bus))

    reg = re.compile("[^0-9]")
    if opts.mem_size:
        if reg.search(str(opts.mem_size)):
            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-m or --mem-size', opts.mem_size))

    if opts.vnc_port:
        if reg.search(str(opts.vnc_port)):
            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-v or --vnc-port', opts.vnc_port))
        elif int(opts.vnc_port) < VNC_PORT_MIN_NUMBER or VNC_PORT_MAX_NUMBER < int(opts.vnc_port):
            raise KssCommandOptException('ERROR: Illigal port number. option=%s value=%s' % ('-v or --vnc-port', opts.vnc_port))

    if opts.vcpus:
        if reg.search(str(opts.vcpus)):
            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-c or --vcpus', opts.vcpus))

    if opts.uuid:
        if not is_uuid(opts.uuid):
            raise KssCommandOptException('ERROR: Illigal UUID. uuid=%s' % (opts.uuid))

    if opts.mac:
        reg = re.compile("^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$")
        if not reg.match(opts.mac):
            raise KssCommandOptException('ERROR: Illigal MAC address. mac=%s' % (opts.mac))

    #if opts.disk and os.path.isfile(opts.disk) is False:
    #    raise KssCommandOptException('ERROR: %s not found.' % opts.disk)

    if opts.iso is not None:
        if opts.kernel is not None or opts.initrd is not None:
            raise KssCommandOptException('ERROR: %s option cannot be specified with %s options.' % ('--iso', '--kernel and --initrd',))

        if os.path.isfile(opts.iso) is False:
            raise KssCommandOptException('ERROR: The specified ISO image path does not exist. - %s' % opts.iso)

        if is_iso9660_filesystem_format(opts.iso) is False:
            raise KssCommandOptException('ERROR: The specified ISO image is not valid ISO 9660 CD-ROM filesystem data. - %s' % opts.iso)

    else:
        _r_get_net = re.compile("^(ftp|http)://")

        if opts.kernel:
            if _r_get_net.match(str(opts.kernel)) is None and os.path.isfile(opts.kernel) is False:
                raise KssCommandOptException('ERROR: The specified kernel image path does not exist. - %s' % opts.kernel)
        else:
            raise KssCommandOptException('ERROR: %s option is required.' % '-k or --kernel')

        if opts.initrd:
            if _r_get_net.match(str(opts.initrd)) is None and os.path.isfile(opts.initrd) is False:
                raise KssCommandOptException('ERROR: The specified initrd image path does not exist. - %s' % opts.initrd)
        else:
            raise KssCommandOptException('ERROR: %s option is required.' % '-i or --initrd')
Ejemplo n.º 3
0
def chkopts(opts):
    reg = re.compile("[^a-zA-Z0-9\./_:-]")

    if opts.name:
        if reg.search(opts.name):
            raise KssCommandOptException(
                'ERROR: Illigal option value. option=%s value=%s' %
                ('-n or --name', opts.name))
    else:
        raise KssCommandOptException('ERROR: %s option is required.' %
                                     '-n or --name')

    if opts.type:
        if opts.type.lower() != "xen" and opts.type.lower() != "kvm":
            raise KssCommandOptException(
                'ERROR: %s option is require %s or %s.' %
                ('-t or --type', 'xen', 'kvm'))

    if opts.storage_pool:
        if reg.search(opts.storage_pool):
            raise KssCommandOptException(
                'ERROR: Illigal option value. option=%s value=%s' %
                ('-P or --storage-pool', opts.storage_pool))
    else:
        raise KssCommandOptException('ERROR: %s option is required.' %
                                     '-P or --storage-pool')

    if opts.storage_volume:
        if reg.search(opts.storage_volume):
            raise KssCommandOptException(
                'ERROR: Illigal option value. option=%s value=%s' %
                ('-V or --storage-volume', opts.storage_volume))
    else:
        raise KssCommandOptException('ERROR: %s option is required.' %
                                     '-V or --storage-volume')

    if opts.bus:
        if reg.search(opts.bus):
            raise KssCommandOptException(
                'ERROR: Illigal option value. option=%s value=%s' %
                ('-B or --bus', opts.bus))

    reg = re.compile("[^0-9]")
    if opts.mem_size:
        if reg.search(str(opts.mem_size)):
            raise KssCommandOptException(
                'ERROR: Illigal option value. option=%s value=%s' %
                ('-m or --mem-size', opts.mem_size))

    if opts.graphics_port:
        if reg.search(str(opts.graphics_port)):
            raise KssCommandOptException(
                'ERROR: Illigal option value. option=%s value=%s' %
                ('-v or --graphics-port', opts.graphics_port))
        elif int(
                opts.graphics_port
        ) < GRAPHICS_PORT_MIN_NUMBER or GRAPHICS_PORT_MAX_NUMBER < int(
                opts.graphics_port):
            raise KssCommandOptException(
                'ERROR: Illigal port number. option=%s value=%s' %
                ('-v or --graphics-port', opts.graphics_port))

    if opts.vcpus:
        if reg.search(str(opts.vcpus)):
            raise KssCommandOptException(
                'ERROR: Illigal option value. option=%s value=%s' %
                ('-c or --vcpus', opts.vcpus))

    if opts.uuid:
        if not is_uuid(opts.uuid):
            raise KssCommandOptException('ERROR: Illigal UUID. uuid=%s' %
                                         (opts.uuid))

    if opts.mac:
        reg = re.compile("^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$")
        if not reg.match(opts.mac):
            raise KssCommandOptException('ERROR: Illigal MAC address. mac=%s' %
                                         (opts.mac))

    #if opts.disk and os.path.isfile(opts.disk) is False:
    #    raise KssCommandOptException('ERROR: %s not found.' % opts.disk)

    if opts.iso is not None:
        if opts.kernel is not None or opts.initrd is not None:
            raise KssCommandOptException(
                'ERROR: %s option cannot be specified with %s options.' % (
                    '--iso',
                    '--kernel and --initrd',
                ))

        if os.path.isfile(opts.iso) is False:
            raise KssCommandOptException(
                'ERROR: The specified ISO image path does not exist. - %s' %
                opts.iso)

        if is_iso9660_filesystem_format(opts.iso) is False:
            raise KssCommandOptException(
                'ERROR: The specified ISO image is not valid ISO 9660 CD-ROM filesystem data. - %s'
                % opts.iso)

    else:
        _r_get_net = re.compile("^(ftp|http)://")

        if opts.kernel:
            if _r_get_net.match(str(opts.kernel)) is None and os.path.isfile(
                    opts.kernel) is False:
                raise KssCommandOptException(
                    'ERROR: The specified kernel image path does not exist. - %s'
                    % opts.kernel)
        else:
            raise KssCommandOptException('ERROR: %s option is required.' %
                                         '-k or --kernel')

        if opts.initrd:
            if _r_get_net.match(str(opts.initrd)) is None and os.path.isfile(
                    opts.initrd) is False:
                raise KssCommandOptException(
                    'ERROR: The specified initrd image path does not exist. - %s'
                    % opts.initrd)
        else:
            raise KssCommandOptException('ERROR: %s option is required.' %
                                         '-i or --initrd')
Ejemplo n.º 4
0
def validates_guest_add(obj):
    checker = Checker()
    check = True

    _ = obj._
    checker.errors = []

    if not is_param(obj.input, 'm_name'):
        check = False
        checker.add_error(_('Parameter m_name does not exist.'))
    else:
        check = checker.check_string(
            _('Machine Name'),
            obj.input.m_name,
            CHECK_EMPTY | CHECK_LENGTH | CHECK_ONLYSPACE,
            None,
            min=MACHINE_NAME_MIN_LENGTH,
            max=MACHINE_NAME_MAX_LENGTH,
        ) and check

    if is_param(obj.input, 'note_title'):
        check = checker.check_string(
            _('Title'),
            obj.input.note_title,
            CHECK_LENGTH | CHECK_ONLYSPACE,
            None,
            min=NOTE_TITLE_MIN_LENGTH,
            max=NOTE_TITLE_MAX_LENGTH,
        ) and check

    if is_param(obj.input, 'note_value'):
        check = checker.check_string(
            _('Note'),
            obj.input.note_value,
            CHECK_ONLYSPACE,
            None,
            None,
            None,
        ) and check

    if is_param(obj.input, 'tags'):
        for tag in comma_split(obj.input.tags):
            check = checker.check_string(
                _('Tag'),
                tag,
                CHECK_LENGTH | CHECK_ONLYSPACE,
                None,
                min=TAG_MIN_LENGTH,
                max=TAG_MAX_LENGTH,
            ) and check

    if not is_param(obj.input, 'm_hypervisor'):
        check = False
        checker.add_error(_('Parameter m_hypervisor does not exist.'))
    else:
        check = checker.check_hypervisor(
            _('Hypervisor'),
            obj.input.m_hypervisor,
            CHECK_EMPTY | CHECK_VALID | CHECK_MIN | CHECK_MAX,
            HYPERVISOR_MIN_SIZE,
            HYPERVISOR_MAX_SIZE,
        ) and check

    if not is_param(obj.input, 'domain_name'):
        check = False
        checker.add_error(_('Parameter domain_name does not exist.'))
    else:
        check = checker.check_string(
            _('Domain Name'),
            obj.input.domain_name,
            CHECK_EMPTY | CHECK_VALID | CHECK_LENGTH | CHECK_ONLYSPACE,
            '[^-a-zA-Z0-9_\.]+',
            DOMAIN_NAME_MIN_LENGTH,
            DOMAIN_NAME_MAX_LENGTH,
        ) and check

        if obj.input.domain_name in get_dom_list():
            dom_type = get_dom_type(obj.input.domain_name)
            checker.add_error(
                _("The same domain name already exists for hypervisor '%s'.") %
                dom_type.upper())
            check = False

    if is_param(obj.input, 'vm_mem_size'):
        check = checker.check_number(
            _('Memory Size (MB)'),
            obj.input.vm_mem_size,
            CHECK_VALID | CHECK_MIN | CHECK_EMPTY,
            MEMORY_MIN_SIZE,
            None,
        ) and check

    if is_param(obj.input, 'pool_type'):
        if obj.input.pool_type != "block":
            if is_param(obj.input, 'vm_disk_size'):
                check = checker.check_number(
                    _('Disk Size (MB)'),
                    obj.input.vm_disk_size,
                    CHECK_VALID | CHECK_MIN | CHECK_EMPTY,
                    DISK_MIN_SIZE,
                    None,
                ) and check

    if not is_param(obj.input, 'boot_image'):
        check = False
        checker.add_error(_('Parameter boot_image does not exist.'))
    else:
        if obj.input.boot_image == "kernel":
            if not is_param(obj.input, 'vm_kernel'):
                check = False
                checker.add_error(_('Parameter vm_kernel does not exist.'))
            else:
                check = checker.check_startfile(
                    _('Kernel Image'),
                    obj.input.vm_kernel,
                    CHECK_EMPTY | CHECK_VALID | CHECK_EXIST,
                ) and check

            if not is_param(obj.input, 'vm_initrd'):
                check = False
                checker.add_error(_('Parameter vm_initrd does not exist.'))
            else:
                check = checker.check_startfile(
                    _('Initrd Image'),
                    obj.input.vm_initrd,
                    CHECK_EMPTY | CHECK_VALID | CHECK_EXIST,
                ) and check

        if obj.input.boot_image == "iso":
            if not is_param(obj.input, 'vm_iso'):
                check = False
                checker.add_error(_('Parameter vm_iso does not exist.'))
            else:
                check = checker.check_startfile(
                    _('ISO Image'),
                    obj.input.vm_iso,
                    CHECK_EMPTY | CHECK_VALID | CHECK_EXIST,
                ) and check
                if check:
                    check = is_iso9660_filesystem_format(obj.input.vm_iso)
                    checker.add_error(
                        _('"%s" is not valid ISO 9660 CD-ROM filesystem data.')
                        % obj.input.vm_iso)

    if not is_param(obj.input, 'keymap'):
        check = False
        checker.add_error(_('"%s" is required.') % _('Graphics Keymap'))
    else:
        hypervisor = "KVM"
        if int(obj.input.m_hypervisor) == MACHINE_HYPERVISOR['XEN']:
            hypervisor = "XEN"
        elif int(obj.input.m_hypervisor) == MACHINE_HYPERVISOR['KVM']:
            hypervisor = "KVM"
        check = checker.check_keymap(_('Graphics Keymap'), obj.input.keymap,
                                     CHECK_EMPTY | CHECK_EXIST,
                                     hypervisor) and check

    if not is_param(obj.input, 'vm_graphics_port'):
        check = False
        checker.add_error(_('Parameter vm_graphics_port does not exist.'))
    else:
        check = checker.check_number(
            _('Graphics Port Number'),
            obj.input.vm_graphics_port,
            CHECK_EMPTY | CHECK_VALID | CHECK_MIN | CHECK_MAX,
            GRAPHICS_PORT_MIN_NUMBER,
            GRAPHICS_PORT_MAX_NUMBER,
        ) and check

    if not is_param(obj.input, 'vm_mac'):
        check = False
        checker.add_error(_('Parameter vm_mac does not exist.'))
    else:
        check = checker.check_macaddr(
            _('MAC Address'),
            obj.input.vm_mac,
            CHECK_EMPTY | CHECK_VALID,
        ) and check

    obj.view.alert = checker.errors
    return check