예제 #1
0
def list_users():
    su = request.args.get('su',False)
    _list = []
    if su:
        results = Users.select().where(Users.su == 'Yes')
    else:
        results = Users.select()
    for obj in results:
        _list.append(model_to_dict(obj))
    return jsonify(status="ok", data=_list), 200
예제 #2
0
def update_user(user_id):
    data = request.get_json(force=True)
    user = Users.get(Users.id ==user_id)
    if user:
        user.name = data['name']
        user.su = data['su']
        if 'password' in data:
            user.password = data['password']
        user.save()
    return jsonify(status="ok"), 200
예제 #3
0
def create_user():
    data = request.get_json(force=True)
    if data is None or 'username' not in data:
        return jsonify(status="error", error="Bad request"), 400
    if 'name' not in data:
        data.update(name=data['username'])
    if 'su' not in data:
        data['su'] = False
    q = Users.create(name=data['name'],username=data['username'],password=data['password'],su=data['su'])
    return jsonify(status="ok"), 200
예제 #4
0
def get_projects():
    """
    Returns projects.
        title = CharField()
    description = TextField(null=True)
    admin = ForeignKeyField(Users)
    created_date = DateTimeField(default=datetime.datetime.now)
    active = BooleanField(default=True)
    """
    _list = []
    results = Projects.select()
    if len(results) == 0:
        admin = Users.get(Users.username=='admin')
        Projects.create(title='Default',description='Default project to start with',admin=admin)
        results = Projects.select()
    for obj in results:
        _list.append(model_to_dict(obj))
    return jsonify(status="ok", objects=_list), 200
예제 #5
0
def hydrate_system():
    """
    Checks for unmanaged hosts and containers in db.
    """
    #~ print("hydrating...")
    hosts = Hosts.select()
    # ~ print(hosts[0])
    admin = Users.get(Users.username=='admin')
    containers_in_db = Containers.select()
    containers_in_host = lxc.list_containers()
    print(len(containers_in_db),len(containers_in_host))
    
    if len(containers_in_host) > len(containers_in_db):
        for cih in containers_in_host:
            #~ print(cih)
            search = Containers.select().where(Containers.name==cih)
            if len(search) == 0:
                container = Containers.create(name=cih, host=hosts[0], admin=admin)
    
    return jsonify({})
예제 #6
0
 def authenticate(self, username, password):
     hash_password = hash_passwd(password)
     results = Users.select().where((Users.username == username)).limit(1)
     return results[0] if len(results) > 0 else None
예제 #7
0
def install(path):
    """
    Installer
    """
    exec_path = os.path.abspath(os.path.dirname(sys.argv[0]))
    if len(path) > 0:
        return redirect('/')
    test_file = os.path.join('/etc', id_generator())
    can_we_install = False
    already_installed = is_already_installed()
    #~ print(already_installed)
    context = {
        'can_we_install': can_we_install,
        'already_installed': already_installed,
    }

    try:
        open(test_file, 'w').close()
    except:
        pass
    if os.path.exists(test_file):
        context['can_we_install'] = True
        os.remove(test_file)
    else:
        return render_template('installer.html', **context)
    context['checks'] = {}  #lxc.checkconfig()
    if request.method == 'POST':
        from pantry.utils import hash_passwd
        f = request.form
        datadir = f.get('datadir', '/var/pantry')
        create(datadir)
        conffile = f.get('conffile', '/etc/pantry/pantry.conf')
        create(get_parent_path(conffile))
        config = ConfigParser()
        config['global'] = {}
        config['global']['address'] = f.get('address', '127.0.0.1')
        config['global']['debug'] = f.get('debug', 'False')
        config['global']['port'] = f.get('port', '5000')
        config['global']['auth'] = f.get('auth', 'database')
        config['global']['prefix'] = f.get('prefix', '')
        config['storage_repository'] = {}
        config['storage_repository']['local'] = f.get(
            'local_storage_repository', 'backups')
        create(os.path.join(datadir, config['storage_repository']['local']))
        config['database'] = {}
        config['database']['file'] = f.get('database_uri',
                                           'sqlite:////var/pantry/pantry.db')
        config['session'] = {}
        config['session']['time'] = f.get('time', '10')
        config['api'] = {}
        internal_token = hash_passwd('pantry')
        config['api']['username'] = f.get('api_username', 'admin')
        config['api']['token'] = f.get('api_token', internal_token)
        with open(conffile, 'w') as configfile:
            config.write(configfile)

        from pantry.database.models import get_database, Users, ApiTokens, Projects, Hosts, Containers, ContainerTag, Tags

        database = get_database()
        database.create_tables([
            Users, ApiTokens, Projects, Hosts, Containers, ContainerTag, Tags
        ])
        admin = Users.create(name='Admin',
                             username='******',
                             su='Yes',
                             password=hash_passwd('admin'))
        ApiTokens.create(username='******',
                         description='internal',
                         token=internal_token)
        host = Hosts.create(hostname=socket.gethostname(),
                            admin=admin,
                            api_token=config['api']['token'],
                            api_user=config['api']['username'])
        default_project = Projects.create(
            title='Default',
            description='Default project to start with',
            admin=admin)
        subprocess.check_call('touch {}'.format(exec_path), shell=True)
        context['already_installed'] = is_already_installed()
    return render_template('installer.html', **context)
예제 #8
0
def delete_user(user_id):
    results = Users.delete().where(Users.id ==user_id).limit(1)
    if len(results) > 0:
        results.execute()
    return jsonify(status="ok"), 200