Ejemplo n.º 1
0
def account_user_create():
    username = current_user.username
    _user_account = request.json['user_account']
    print 'account_user_create locals:', dict(locals())
    
    # create user account
    username_ = _user_account['username']
    _user_account['created'] = _user_account['updated'] = datetime.utcnow()
    user_account = UserAccount(**_user_account)
    db.session.add(user_account)
    
    # create user quota
    user_quota = UserQuota(username=username_)
    db.session.add(user_quota)
    
    # create user stat
    user_stat = UserStat(username=username_)
    db.session.add(user_stat)
    
    # commit
    db.session.commit()
    
    _user_account = object_to_dict(user_account)
    
    data = {
        'user_account': _user_account,
    }
    
    return jsonify(data)
Ejemplo n.º 2
0
def volume_update():
    username = current_user.username
    usertype = current_user.usertype
    _volume = request.json['volume']
    print 'volume_update:', locals()
    
    # FIXME:
    if usertype != 'super':
        data = {}
        return jsonify(data)
        
    volume = Volume.query.get(_volume['id'])
    _volume['updated'] = datetime.utcnow()
    update_object_with_dict(volume, _volume)
    db.session.commit()
    _volume = object_to_dict(volume)
    
    # insert host_name
    # insert mount_point_name
    host = Host.query.get(_volume['host_id'])
    assert host is not None
    
    mount_point = MountPoint.query.get(_volume['mount_point_id'])
    assert mount_point is not None
    
    _volume['host_name'] = host.name
    _volume['mount_point_name'] = mount_point.name
    
    data = {
        'volume': _volume,
    }
    
    return jsonify(data)
Ejemplo n.º 3
0
def volume_update():
    username = current_user.username
    usertype = current_user.usertype
    _volume = request.json['volume']
    print 'volume_update:', locals()

    # FIXME:
    if usertype != 'super':
        data = {}
        return jsonify(data)

    volume = Volume.query.get(_volume['id'])
    _volume['updated'] = datetime.utcnow()
    update_object_with_dict(volume, _volume)
    db.session.commit()
    _volume = object_to_dict(volume)

    # insert host_name
    # insert mount_point_name
    host = Host.query.get(_volume['host_id'])
    assert host is not None

    mount_point = MountPoint.query.get(_volume['mount_point_id'])
    assert mount_point is not None

    _volume['host_name'] = host.name
    _volume['mount_point_name'] = mount_point.name

    data = {
        'volume': _volume,
    }

    return jsonify(data)
Ejemplo n.º 4
0
def mount_update():
    username = current_user.username
    usertype = current_user.usertype
    _mount = request.json['mount']
    print 'mount_update:', locals()
    
    # FIXME:
    if usertype != 'super':
        data = {}
        return jsonify(data)
        
    mount = MountPoint.query.get(_mount['id'])
    _mount['updated'] = datetime.utcnow()
    update_object_with_dict(mount, _mount)
    db.session.commit()
    
    _mount = object_to_dict(mount)
    
    # insert host_name
    host = Host.query.get(_mount['host_id'])
    assert host is not None
    _mount['host_name'] = host.name
    
    data = {
        'mount': _mount,
    }
    
    return jsonify(data)
Ejemplo n.º 5
0
def volume_volumes():
    username = current_user.username
    print 'volume_volumes:', locals()

    # get user account properties
    user_account = UserAccount.query.filter_by(username=username).one()
    dct = object_to_dict(user_account)

    return render_template('volume-volumes.html', **dct)
Ejemplo n.º 6
0
def host_hosts():
    username = current_user.username
    print 'host_hosts:', locals()
    
    # get user account properties
    user_account = UserAccount.query.filter_by(username=username).one()
    dct = object_to_dict(user_account)
    
    return render_template(
        'host-hosts.html',
        **dct
    )
Ejemplo n.º 7
0
def network_domains():
    username = current_user.username
    print 'network_domains:', locals()
    
    # get user account properties
    user_account = UserAccount.query.filter_by(username=username).one()
    dct = object_to_dict(user_account)
    
    return render_template(
        'network-domains.html',
        **dct
    )
Ejemplo n.º 8
0
def container_containers():
    username = current_user.username
    print 'container_containers:', locals()
    
    # get user account properties
    user_account = UserAccount.query.filter_by(username=username).one()
    dct = object_to_dict(user_account)
    
    return render_template(
        'container-containers.html',
        **dct
    )
Ejemplo n.º 9
0
def account_quota_get():
    username = current_user.username
    usertype = current_user.usertype
    username_ = request.json['username']
    print 'account_quota_get locals:', dict(locals())
    
    # get query
    user_quota = UserQuota.query.filter_by(username=username_).one()
    _user_quota = object_to_dict(user_quota)
    
    data = {
        'user_quota': _user_quota,
    }
    
    return jsonify(data)
Ejemplo n.º 10
0
def account_stat_get():
    username = current_user.username
    usertype = current_user.usertype
    username_ = request.json['username']
    print 'account_stat_get locals:', dict(locals())
    
    # get stat
    user_stat = UserStat.query.filter_by(username=username_).one()
    _user_stat = object_to_dict(user_stat)
    
    data = {
        'user_stat': _user_stat,
    }
    
    return jsonify(data)
Ejemplo n.º 11
0
def account_user_update():
    username = current_user.username
    _user_account = request.json['user_account']
    print 'account_user_update locals:', dict(locals())
    
    # update user account
    username_ = _user_account['username']
    user_account = UserAccount.query.filter_by(username=username_).one()
    _user_account['updated'] = datetime.utcnow()
    update_object_with_dict(user_account, _user_account)
    db.session.commit()
    
    _user_account = object_to_dict(user_account)
    
    data = {
        'user_account': _user_account,
    }
    
    return jsonify(data)
Ejemplo n.º 12
0
def account_quota_update():
    username = current_user.username
    usertype = current_user.usertype
    _user_quota = request.json['user_quota']
    print 'account_quota_update locals:', dict(locals())
    
    # update user quota
    user_quota = UserQuota.query.filter_by(username=_user_quota['username']).one()
    _user_quota['updated'] = datetime.utcnow()
    update_object_with_dict(user_quota, _user_quota)
    db.session.commit()
    
    _user_quota = object_to_dict(user_quota)
    
    data = {
        'user_quota': _user_quota,
    }
    
    return jsonify(data)
Ejemplo n.º 13
0
def network_domain_create():
    username = current_user.username
    usertype = current_user.usertype
    _domain = request.json['domain']
    print 'network_domain_create:', locals()
    
    if usertype != 'super':
        data = {}
        return jsonify(data)
    
    _domain['created'] = _domain['updated'] = datetime.utcnow()
    domain = Domain(**_domain)
    db.session.add(domain)
    db.session.commit()
    
    _domain = object_to_dict(domain)
    
    data = {
        'domain': _domain,
    }
    
    return jsonify(data)
Ejemplo n.º 14
0
def network_domain_update():
    username = current_user.username
    usertype = current_user.usertype
    _domain = request.json['domain']
    print 'network_domain_update:', locals()
    
    if usertype != 'super':
        data = {}
        return jsonify(data)
        
    domain = Domain.query.get(_domain['id'])
    _domain['updated'] = datetime.utcnow()
    update_object_with_dict(domain, _domain)
    db.session.commit()
    
    _domain = object_to_dict(domain)
    
    data = {
        'domain': _domain,
    }
    
    return jsonify(data)
Ejemplo n.º 15
0
def network_route_update():
    username = current_user.username
    usertype = current_user.usertype
    _route = request.json['route']
    print 'network_route_update:', locals()
    
    if usertype != 'super':
        data = {}
        return jsonify(data)
        
    route = Route.query.get(_route['id'])
    _route['updated'] = datetime.utcnow()
    update_object_with_dict(route, _route)
    db.session.commit()
    
    _route = object_to_dict(route)
    
    # insert domain, host, container name
    domain = Domain.query.get(_route['domain_id'])
    assert domain is not None
    
    host = Host.query.get(_route['host_id'])
    assert host is not None
    
    container = Container.query.get(_route['container_id'])
    assert container is not None
    
    _route['domain_domain'] = domain.domain
    _route['host_name'] = host.name
    _route['container_name'] = container.name
    _route['container_container_id'] = container.container_id
    
    data = {
        'route': _route,
    }
    
    return jsonify(data)
Ejemplo n.º 16
0
def host_update():
    username = current_user.username
    usertype = current_user.usertype
    _host = request.json['host']
    print 'host_update:', locals()
    
    if usertype != 'super':
        data = {}
        return jsonify(data)
        
    host = Host.query.get(_host['id'])
    assert host is not None
    
    _host['updated'] = datetime.utcnow()
    update_object_with_dict(host, _host)
    db.session.commit()
    
    _host = object_to_dict(host)
    
    data = {
        'host': _host,
    }
    
    return jsonify(data)
Ejemplo n.º 17
0
def host_update():
    username = current_user.username
    usertype = current_user.usertype
    _host = request.json['host']
    print 'host_update:', locals()

    if usertype != 'super':
        data = {}
        return jsonify(data)

    host = Host.query.get(_host['id'])
    assert host is not None

    _host['updated'] = datetime.utcnow()
    update_object_with_dict(host, _host)
    db.session.commit()

    _host = object_to_dict(host)

    data = {
        'host': _host,
    }

    return jsonify(data)
Ejemplo n.º 18
0
def mount_create():
    username = current_user.username
    usertype = current_user.usertype
    _mount = request.json['mount']
    print 'mount_add:', locals()
    
    # FIXME:
    if usertype != 'super':
        data = {}
        return jsonify(data)
    
    # try to find range patterns
    if 'host' in _mount:
        name = _mount['name']
        ranges = list(re.findall('\[\w+\-\w+\]', name))
    else:
        ranges = None
    
    if ranges:
        # generate all combinations
        patterns = list(ranges)
        ranges = [r.strip('[]').split('-') for r in ranges]
        
        for i, r in enumerate(ranges):
            s, e = r
            
            if s.isdigit() and e.isdigit():
                s, e = map(int, r)
                r = range(s, e + 1)
                ranges[i] = r
            else:
                # FIXME: support alpha-num ranges for multi-character strings
                #        with same size
                assert len(s) == len(e) == 1
                s, e = map(ord, r)
                r = range(s, e + 1)
                r = map(chr, r)
                r = ''.join(r)
                ranges[i] = r
        
        combs = [map(str, c) for c in product(*ranges)]
        
        # generate mount points
        host = _mount['host']
        device = _mount['device']
        mountpoint = _mount['mountpoint']
        filesystem = _mount['filesystem']
        capacity = _mount['capacity']
        
        mounts = []
        _mounts = []
        
        for comb in combs:
            _host = host
            _name = name
            _device = device
            _mountpoint = mountpoint
            _filesystem = filesystem
            _capacity = capacity
            
            for p, c in zip(patterns, comb):
                _host = _host.replace(p, c)
                _name = _name.replace(p, c)
                _device = _device.replace(p, c)
                _mountpoint = _mountpoint.replace(p, c)
                _filesystem = _filesystem.replace(p, c)
                _capacity = _capacity.replace(p, c)
            
            host_ = Host.query.filter_by(name=_host).one()
            assert host_ is not None
            _host_id = host_.id
            
            __mount = {
                'host_id': _host_id,
                'name': _name,
                'device': _device,
                'mountpoint': _mountpoint,
                'filesystem': _filesystem,
                'capacity': _capacity,
            }
            
            __mount['created'] = __mount['updated'] = datetime.utcnow()
            mount = MountPoint(**__mount)
            db.session.add(mount)
            mounts.append(mount)
        
        db.session.commit()
        
        for mount in mounts:
            __mount = object_to_dict(mount)
            
            # insert host_name
            host_ = Host.query.get(__mount['host_id'])
            assert host_ is not None
            __mount['host_name'] = host_.name
            
            _mounts.append(__mount)
        
        data = {
            'mounts': _mounts,
        }
    else:
        _mount['created'] = _mount['updated'] = datetime.utcnow()
        mount = MountPoint(**_mount)
        db.session.add(mount)
        db.session.commit()
        
        _mount = object_to_dict(mount)
        
        # insert host_name
        host = Host.query.get(_mount['host_id'])
        assert host is not None
        _mount['host_name'] = host.name
        
        data = {
            'mount': _mount,
        }
    
    return jsonify(data)
Ejemplo n.º 19
0
def image_create():
    username = current_user.username
    usertype = current_user.usertype
    _image = request.json['image']
    print 'image_add:', locals()

    # FIXME:
    if usertype != 'super':
        data = {}
        return jsonify(data)

    _image['status'] = 'downloading'
    _image['created'] = _image['updated'] = datetime.utcnow()
    image = Image(**_image)
    db.session.add(image)
    db.session.commit()
    _image = object_to_dict(image)

    ##
    # "async" create/pull image
    def _requests_post(*args, **kwargs):
        try:
            r = requests.post(*args, **kwargs)
            print r
            assert r.status_code == 200
        except requests.exceptions.ChunkedEncodingError as e:
            print e

    def _image_create():
        # get all hosts
        hosts = Host.query.all()
        threads = []

        for host in hosts:
            # create volume at host
            url = 'http://%s:%i/images/create?fromImage=%s' % (
                host.host,
                host.port,
                _image['name'],
            )

            data_ = json.dumps({})
            headers = {'content-type': 'application/json'}
            auth = HTTPBasicAuth(host.auth_username, host.auth_password)

            t = Thread(target=_requests_post,
                       args=(url, ),
                       kwargs=dict(data=data_, headers=headers, auth=auth))

            threads.append(t)
            t.start()

        for t in threads:
            t.join()

        session = db.create_scoped_session()
        image = session.query(Image).get(_image['id'])
        image.status = 'ready'
        session.commit()

    mt = Thread(target=_image_create)
    mt.start()
    ##

    # insert host_name
    host = Host.query.get(_image['host_id'])

    if host:
        _image['host_name'] = host.name
    else:
        _image['host_name'] = 'ALL'

    data = {
        'image': _image,
    }

    return jsonify(data)
Ejemplo n.º 20
0
def container_create():
    username = current_user.username
    usertype = current_user.usertype
    _container = request.json['container']
    print 'container_add:', locals()
    
    # FIXME:
    if usertype != 'super':
        data = {}
        return jsonify(data)
    
    # unpack
    _host_id = _container.get('host_id', None)
    _name = _container['name']
    _image_id = _container['image_id']
    _command = _container['command']
    _volumes = _container.get('volumes', [])
    _volumes_from = _container.get('volumes_from', [])
    _env_vars = _container['env_vars']
    _expose_ports = _container['expose_ports']
    _publish_ports = _container['publish_ports']
    _link_containers = _container.get('link_containers', [])
    _ram_limit = _container['ram_limit']
    _n_cpu_cores = _container['n_cpu_cores']
    
    # find suitable volume according to host
    if not _host_id and not _volumes:
        hosts = Host.query.all()
        host = random.choice(hosts)
        _host_id = host.id
        _host_host = host.host
        _volume_names = []
    elif not _host_id and _volumes:
        assert len(_volumes) == 1
        _volume_id = _volumes[0]
        volume = Volume.query.get(_volume_id)
        _volume_names = [volume.perm_name]
        
        host = Host.query.get(volume.host_id)
        _host_id = host.id
        _host_host = host.host
    else:
        host = Host.query.get(_host_id)
        _host_host = host.host
        _volume_names = []
    
    # find image name
    image = Image.query.get(_image_id)
    _image_name = image.name
    
    _volumes = json.dumps(_volumes)
    _volumes_from = json.dumps(_volumes_from)
    _link_containers = json.dumps(_link_containers)
    
    ##
    # create docker container
    url = 'http://%s:%i/containers/create' % (host.host, host.port)
    
    data_ = json.dumps({
        "Hostname": _host_host,
        "User": "",
        "Memory": _ram_limit,
        "MemorySwap": 0,
        "AttachStdin": True,
        "AttachStdout": True,
        "AttachStderr": True,
        "PortSpecs": None,
        "Tty": False,
        "OpenStdin": False,
        "StdinOnce": False,
        "Env": _env_vars,
        "Cmd": [_command],
        "Image": _image_name,
        "Volumes": {v: {} for v in _volume_names},
        "WorkingDir": "",
        "DisableNetwork": False,
        "ExposedPorts": {p: {} for p in _publish_ports.strip('\t ').split(',')},
    })
    
    print data_
    
    headers = {
        'content-type': 'application/json',
    }
    
    auth = HTTPBasicAuth(host.auth_username, host.auth_password)
    r = requests.post(url, data=data_, headers=headers, auth=auth)
    print r
    assert r.status_code == 200
    container_id = r.json()['Id']
    ##
    
    # insert container into database
    _status = 'ready'
    
    __container = {
        'host_id': _host_id,
        'name': _name,
        'image_id': _image_id,
        'command': _command,
        'volumes': _volumes,
        'volumes_from': _volumes_from,
        'env_vars': _env_vars,
        'expose_ports': _expose_ports,
        'publish_ports': _publish_ports,
        'link_containers': _link_containers,
        'ram_limit': _ram_limit,
        'n_cpu_cores': _n_cpu_cores,
        'cpu_share': _cpu_share,
        'status': _status,
    }
    
    __container['username'] = username
    __container['container_id'] = container_id
    __container['created'] = __container['updated'] = datetime.utcnow()
    
    __container['perm_name'] = '%s_%s' % (
        __container['username'],
        __container['name'],
    )
    
    container = Container(**__container)
    db.session.add(container)
    db.session.commit()
    
    __container = object_to_dict(container)
    
    # insert host_name
    host = Host.query.get(__container['host_id'])
    __container['host_name'] = host.name if host else 'ALL'
    
    # insert image_name
    image = Image.query.get(__container['image_id'])
    __container['image_name'] = image.name
    
    data = {
        'container': __container,
    }
    
    return jsonify(data)
Ejemplo n.º 21
0
def volume_create():
    username = current_user.username
    usertype = current_user.usertype
    _volume = request.json['volume']
    print 'volume_add:', locals()

    # FIXME:
    if usertype != 'super':
        data = {}
        return jsonify(data)

    host_id = _volume.get('host_id', None)
    mount_point_id = _volume.get('mount_point_id', None)
    name = _volume['name']
    capacity = _volume['capacity']
    username_ = _volume['username']

    # find available host and/or mount point
    if host_id is None or mount_point_id is None:
        query = MountPoint.query

        if host_id is not None and mount_point_id is None:
            query = query.filter_by(host_id=host_id)

        mount_points = query.all()

        mount_points = [
            m for m in mount_points if m.capacity - m.reserved >= capacity
        ]

        # no mount_points available
        if not mount_points:
            data = {
                'error': 'The is no available space.'\
                         'Try smaller volume capacity than %s GB.' % capacity,
            }

            return jsonify(data)

        # take first available slice
        mount_points.sort(key=lambda m: m.capacity - m.reserved)
        mount_point = random.choice(mount_points)

        # host, mount_point
        host_id = mount_point.host_id
        mount_point_id = mount_point.id

    # host_name
    host = Host.query.get(host_id)
    assert host is not None

    # mount_point_name
    mount_point = MountPoint.query.get(mount_point_id)
    assert mount_point is not None

    if mount_point.capacity - mount_point.reserved < capacity:
        data = {
            'error': 'The is no available space.'\
                     'Try smaller volume capacity than %s GB.' % capacity,
        }

        return jsonify(data)

    # increase reserved storage at mount point
    mount_point.reserved = mount_point.reserved + capacity

    # insert volume into database
    __volume = {
        'host_id': host_id,
        'mount_point_id': mount_point_id,
        'name': name,
        'capacity': capacity,
        'username': username_,
    }

    __volume['created'] = __volume['updated'] = datetime.utcnow()
    __volume['perm_name'] = perm_name = '%s_%s' % (username_, uuid.uuid4().hex)

    volume = Volume(**__volume)
    db.session.add(volume)

    ##
    # create volume at host
    url = 'http://%s:%i/dockyard/volume/create' % (host.host, host.port)

    data_ = json.dumps({
        'mountpoint': mount_point.mountpoint,
        'name': perm_name,
        'size': capacity,
    })

    headers = {
        'content-type': 'application/json',
    }

    auth = HTTPBasicAuth(host.auth_username, host.auth_password)
    r = requests.post(url, data=data_, headers=headers, auth=auth)
    assert r.status_code == 200
    ##

    db.session.commit()

    # return response
    __volume = object_to_dict(volume)

    # insert host_name
    # insert mount_point_name
    __volume['host_name'] = host.name
    __volume['mount_point_name'] = mount_point.name

    data = {
        'volume': __volume,
    }

    return jsonify(data)
Ejemplo n.º 22
0
def volume_create():
    username = current_user.username
    usertype = current_user.usertype
    _volume = request.json['volume']
    print 'volume_add:', locals()
    
    # FIXME:
    if usertype != 'super':
        data = {}
        return jsonify(data)
    
    host_id = _volume.get('host_id', None)
    mount_point_id = _volume.get('mount_point_id', None)
    name = _volume['name']
    capacity = _volume['capacity']
    username_ = _volume['username']
    
    # find available host and/or mount point
    if host_id is None or mount_point_id is None:
        query = MountPoint.query
        
        if host_id is not None and mount_point_id is None:
            query = query.filter_by(host_id=host_id)
        
        mount_points = query.all()
        
        mount_points = [
            m for m in mount_points
            if m.capacity - m.reserved >= capacity
        ]
        
        # no mount_points available
        if not mount_points:
            data = {
                'error': 'The is no available space.'\
                         'Try smaller volume capacity than %s GB.' % capacity,
            }
            
            return jsonify(data)
        
        # take first available slice
        mount_points.sort(key=lambda m: m.capacity - m.reserved)
        mount_point = random.choice(mount_points)
        
        # host, mount_point
        host_id = mount_point.host_id
        mount_point_id = mount_point.id
    
    # host_name
    host = Host.query.get(host_id)
    assert host is not None
    
    # mount_point_name
    mount_point = MountPoint.query.get(mount_point_id)
    assert mount_point is not None
    
    if mount_point.capacity - mount_point.reserved < capacity:
        data = {
            'error': 'The is no available space.'\
                     'Try smaller volume capacity than %s GB.' % capacity,
        }
        
        return jsonify(data)
    
    # increase reserved storage at mount point
    mount_point.reserved = mount_point.reserved + capacity
    
    # insert volume into database
    __volume = {
        'host_id': host_id,
        'mount_point_id': mount_point_id,
        'name': name,
        'capacity': capacity,
        'username': username_,
    }
    
    __volume['created'] = __volume['updated'] = datetime.utcnow()
    __volume['perm_name'] = perm_name = '%s_%s' % (username_, uuid.uuid4().hex)
    
    volume = Volume(**__volume)
    db.session.add(volume)
    
    ##
    # create volume at host
    url = 'http://%s:%i/dockyard/volume/create' % (host.host, host.port)
    
    data_ = json.dumps({
        'mountpoint': mount_point.mountpoint,
        'name': perm_name,
        'size': capacity,
    })
    
    headers = {
        'content-type': 'application/json',
    }
    
    auth = HTTPBasicAuth(host.auth_username, host.auth_password)
    r = requests.post(url, data=data_, headers=headers, auth=auth)
    assert r.status_code == 200
    ##
    
    db.session.commit()
    
    # return response
    __volume = object_to_dict(volume)
    
    # insert host_name
    # insert mount_point_name
    __volume['host_name'] = host.name
    __volume['mount_point_name'] = mount_point.name
    
    data = {
        'volume': __volume,
    }
    
    return jsonify(data)
Ejemplo n.º 23
0
def host_create():
    username = current_user.username
    usertype = current_user.usertype
    _host = request.json['host']
    print 'host_add:', locals()

    if usertype != 'super':
        data = {}
        return jsonify(data)

    name = _host['name']
    host = _host['host']
    port = _host['port']
    auth_username = _host['auth_username']
    auth_password = _host['auth_password']
    ram_capacity = _host['ram_capacity']
    ram_reserved = _host['ram_reserved']

    if '[' in name and '-' in name and ']' in name and \
       '[' in host and '-' in host and ']' in host:
        _hosts = []
        hosts = []

        # name base/range
        s = name.find('[')
        e = name.find(']')
        name_base = name[:s]
        name_range = name[s + 1:e]
        name_range = name_range.strip(' ').strip()
        name_range = map(int, name_range.split('-'))
        name_range[1] += 1

        # host base/range
        s = host.find('[')
        e = host.find(']')
        host_base = host[:s]
        host_range = host[s + 1:e]
        host_range = host_range.strip(' ').strip()
        host_range = map(int, host_range.split('-'))
        host_range[1] += 1

        for i, j in zip(range(*name_range), range(*host_range)):
            __host = {
                'name': '%s%i' % (name_base, i),
                'host': '%s%i' % (host_base, j),
                'port': port,
                'auth_username': auth_username,
                'auth_password': auth_password,
                'ram_capacity': ram_capacity,
                'ram_reserved': ram_reserved,
            }

            __host['created'] = __host['updated'] = datetime.utcnow()
            host = Host(**__host)
            db.session.add(host)
            hosts.append(host)

        db.session.commit()

        for host in hosts:
            __host = object_to_dict(host)
            _hosts.append(__host)

        data = {
            'hosts': _hosts,
        }
    else:
        _host['created'] = _host['updated'] = datetime.utcnow()
        host = Host(**_host)
        db.session.add(host)
        db.session.commit()
        _host = object_to_dict(host)

        data = {
            'host': _host,
        }

    return jsonify(data)
Ejemplo n.º 24
0
def host_create():
    username = current_user.username
    usertype = current_user.usertype
    _host = request.json['host']
    print 'host_add:', locals()
    
    if usertype != 'super':
        data = {}
        return jsonify(data)
    
    name = _host['name']
    host = _host['host']
    port = _host['port']
    auth_username = _host['auth_username']
    auth_password = _host['auth_password']
    ram_capacity = _host['ram_capacity']
    ram_reserved = _host['ram_reserved']
    
    if '[' in name and '-' in name and ']' in name and \
       '[' in host and '-' in host and ']' in host:
        _hosts = []
        hosts = []
        
        # name base/range
        s = name.find('[')
        e = name.find(']')
        name_base = name[:s]
        name_range = name[s + 1:e]
        name_range = name_range.strip(' ').strip()
        name_range = map(int, name_range.split('-'))
        name_range[1] += 1
        
        # host base/range
        s = host.find('[')
        e = host.find(']')
        host_base = host[:s]
        host_range = host[s + 1:e]
        host_range = host_range.strip(' ').strip()
        host_range = map(int, host_range.split('-'))
        host_range[1] += 1
        
        for i, j in zip(range(*name_range), range(*host_range)):
            __host = {
                'name': '%s%i' % (name_base, i),
                'host': '%s%i' % (host_base, j),
                'port': port,
                'auth_username': auth_username,
                'auth_password': auth_password,
                'ram_capacity': ram_capacity,
                'ram_reserved': ram_reserved,
            }
            
            __host['created'] = __host['updated'] = datetime.utcnow()
            host = Host(**__host)
            db.session.add(host)
            hosts.append(host)
        
        db.session.commit()
        
        for host in hosts:
            __host = object_to_dict(host)
            _hosts.append(__host)
        
        data = {
            'hosts': _hosts,
        }
    else:
        _host['created'] = _host['updated'] = datetime.utcnow()
        host = Host(**_host)
        db.session.add(host)
        db.session.commit()
        _host = object_to_dict(host)
        
        data = {
            'host': _host,
        }
    
    return jsonify(data)