Esempio n. 1
0
def _normalize_cidr(raw_cidr):
    try:
        network = IPNetwork(raw_cidr)
        cidr = str(network)
    except AddrFormatError:
        raise exception.Conflict(reason="Invalid network address format")

    if not network.is_private():
        raise exception.Conflict(reason="Invalid network address. Must be RFC1918 compliant")

    return cidr
Esempio n. 2
0
def create_user():
    data = utils.parse_json_body(request)

    if 'username' not in data:
        raise exception.ParameterMissing(name="username")

    if not data['username'] or not data['username'].strip():
        raise exception.ParameterMissing(name="username")

    if users.exists(username=data['username']):
        raise exception.Conflict(
            reason="User with username '%s' already exists" % data['username'])

    if 'firstname' not in data:
        raise exception.ParameterMissing(name="firstname")

    if 'lastname' not in data:
        raise exception.ParameterMissing(name="lastname")

    user = User(username=data['username'],
                firstname=data['firstname'],
                lastname=data['lastname'])

    if 'password' in data:
        user.password = data['password']

    id = users.save(user)
    return get_user(id)
Esempio n. 3
0
def exec_command(id, data):

    if 'method' not in data:
        raise exception.BadRequest(reason="Attribute 'method' is missing")

    if data['method'] != 'ssh':
        raise exception.Conflict(reason="Method '%s' is not supported" %
                                 data['method'])

    if 'command' not in data:
        raise exception.BadRequest(reason="Attribute 'command' is missing")

    if 'username' not in data:
        raise exception.BadRequest(reason="Attribute 'username' is missing")

    if 'private_key' not in data:
        raise exception.BadRequest(reason="Attribute 'private_key' is missing")

    username = data['username']
    command = data['command']
    private_key = data['private_key']

    session = db.session()
    with session.begin():
        blade = _get_blade(id, session)
        ip = network.read_ip_address(blade.mac_address)

        if not ip:
            raise exception.Conflict(
                reason="Did not manage to resolve IP address for blade %s" %
                id)

        exec_command = ExecCommand(blade.id, ip, command, private_key,
                                   username)
        session.add(exec_command)

        commandObject = _command_to_dict(exec_command, include_result=False)

    session = db.session()
    with session.begin():
        session.add(exec_command)
        exec_command.start()

    return commandObject
Esempio n. 4
0
def update_config(config_dict):
    session = db.session()
    with session.begin():
        config = {}
        for c in session.query(Config).filter(Config.category == 'dhcp'):
            config[c.key] = c
        for k in config_dict:
            if k in config:
                config[k].value = config_dict[k]

        if config['dhcp_mode'].value not in ['proxy', 'dhcp']:
            raise exception.Conflict(reason="Invalid DHCP mode. Must be either 'proxy' or 'dhcp'")

        config['dhcp_network'].value = _normalize_cidr(config['dhcp_network'].value)

        if config['dhcp_mode'].value == 'dhcp':
            try:
                start = int(IPAddress(config['dhcp_range_start'].value))
            except AddrFormatError:
                raise exception.Conflict(reason="Invalid format for DHCP start range IP address")
            try:
                end = int(IPAddress(config['dhcp_range_end'].value))
            except AddrFormatError:
                raise exception.Conflict(reason="Invalid format for DHCP end range IP address")

            network = IPNetwork(config['dhcp_network'].value)
            network_ip = int(network.ip)
            netmask = int(network.netmask)
            network_checksum = network_ip & netmask

            start_checksum = start & netmask
            if start_checksum != network_checksum:
                raise exception.Conflict(
                        reason="Invalid DHCP start range IP address. Must be in network %s" % str(network))

            end_checksum = end & netmask
            if end_checksum != network_checksum:
                raise exception.Conflict(
                        reason="Invalid DHCP end range IP address. Must be in network %s" % str(network))

            if start > end:
                raise exception.Conflict(reason="DHCP end range must be greater than start range")

            if config['dhcp_lease_duration'].value != 'infinite':
                try:
                    time = int(config['dhcp_lease_duration'].value)
                    if time < 300:
                        raise exception.Conflict(reason="DHCP lease duration must be at least 300 seconds")
                except ValueError:
                    raise exception.Conflict(
                            reason="DHCP lease duration must be either a count in seconds or the word 'infinite'")

    refresh()
Esempio n. 5
0
def find(id=None, username=None):
    if id is None and username is None:
        return find_all()

    if id is not None and username is not None:
        raise exception.Conflict()

    if id is not None:
        return findOne(User.id == id)

    if username is not None:
        return findOne(User.username == username)
Esempio n. 6
0
def cancel_build_blade(id):
    session = db.session()
    with session.begin():
        blade = _get_blade(id, session)
        if not blade.building:
            raise exception.Conflict(
                reason="The server is not in the building state")

        pxe_tftp_dir = "/tftp/pxe/pxelinux.cfg"
        pwe_www_dir = "/var/www/pxe"
        pxe_mac_address = network.format_mac_address_pxe(blade.mac_address)

        os.remove("%s/%s" % (pxe_tftp_dir, pxe_mac_address))
        os.remove("%s/%s.seed" % (pwe_www_dir, pxe_mac_address))
        os.remove("%s/%s.sh" % (pwe_www_dir, pxe_mac_address))

        blade.building = False
Esempio n. 7
0
def update_blade(id, data):
    session = db.session()
    with session.begin():
        blade = _get_blade(id, session)

        if 'id' in data and int(data['id']) != int(id):
            raise exception.Conflict(reason="Id mismatch")

        if 'name' in data:
            blade.name = data['name']

        if 'description' in data:
            blade.description = data['description']

        if 'mac_address' in data:
            blade.mac_address = network.normalize_mac_address(
                data['mac_address'])

    return get_blade(id)
Esempio n. 8
0
def update_user(id):
    data = utils.parse_json_body(request)

    if 'id' in data and int(data['id']) != int(id):
        raise exception.Conflict(reason="Id mismatch")

    if 'firstname' not in data:
        raise exception.ParameterMissing(name="firstname")

    if 'lastname' not in data:
        raise exception.ParameterMissing(name="lastname")

    user = User(firstname=data['firstname'], lastname=data['lastname'])
    user.id = id

    if 'password' in data:
        user.password = data['password']

    users.update(user)
    return get_user(id)
Esempio n. 9
0
def build_blade(id, data):
    session = db.session()
    with session.begin():
        blade = _get_blade(id, session)
        if blade.building:
            raise exception.Conflict(
                reason="A building operation is already in progress")
        if not blade.mac_address:
            raise exception.Conflict(
                reason="Blade must have is MAC address set in order to be built"
            )

        pxe_tftp_dir = "/tftp/pxe/pxelinux.cfg"
        pwe_www_dir = "/var/www/pxe"
        pxe_mac_address = network.format_mac_address_pxe(blade.mac_address)

        ip = network.get_iface_ip_address('eth0')

        # Default values
        os = 'ubuntu1404'
        hostname = 'blade' + str(blade.id)
        username = '******'
        ssh_pub_key = ''
        password = ''

        if 'os' in data and data['os']:
            os = data['os']

        if 'username' in data and data['username']:
            username = data['username']

        if 'hostname' in data and data['hostname']:
            hostname = data['hostname']

        if 'ssh_pub_key' not in data or not data['ssh_pub_key']:
            if 'password' not in data:
                raise exception.BadRequest(
                    reason=
                    "A password or a SSH public key must be provided for user '%s'"
                    % username)

            if not data['password']:
                raise exception.BadRequest(
                    reason=
                    "A password or a SSH public key must be provided for user '%s'"
                    % username)

            password = data['password']
        else:
            ssh_pub_key = data['ssh_pub_key']
            if 'password' in data and data['password']:
                password = data['password']

        _write_template(
            "ubuntu-1404/pxemenu", "%s/%s" % (pxe_tftp_dir, pxe_mac_address), {
                'pxe_server': ip,
                'preseed_filename': "%s.seed" % pxe_mac_address,
                'serial_baudrate': 115200
            })

        _write_template(
            "ubuntu-1404/preseed",
            "%s/%s.seed" % (pwe_www_dir, pxe_mac_address), {
                'http_proxy': "http://%s:3128" % ip,
                'ubuntu_mirror': "archive.ubuntu.com",
                'root_password': "******",
                'ntp_server_host': ip,
                'pxe_server': ip,
                'post_install_script_name': "%s.sh" % pxe_mac_address
            })

        post_install_model = {
            'blade_number': blade.id,
            'serial_baudrate': 115200,
            'api_base_url': "http://%s:5000" % ip,
            'username': username,
            'hostname': hostname
        }

        if password:
            post_install_model['password'] = password

        if ssh_pub_key:
            post_install_model['ssh_pub_key'] = ssh_pub_key

        _write_template("ubuntu-1404/post-install.sh",
                        "%s/%s.sh" % (pwe_www_dir, pxe_mac_address),
                        post_install_model)

        blade.building = True