Example #1
0
File: status.py Project: WZoe/Harp
def index():
    # db = get_db()
    # orgs = db.execute(
    #     'SELECT o.id, orgname, u.id, username'
    #     ' FROM organization o JOIN user u ON o.id = u.org_id'
    #     ' ORDER BY o.id ASC'
    # ).fetchall()
    org_info = None
    ip = None
    docker_list = []

    # user界面
    if g.user['id'] and g.user['usertype'] == 1:
        org_info = get_db().execute(
            'SELECT o.id, orgname'
            ' FROM organization o JOIN user u on o.id = u.org_id'
            ' WHERE u.id = ?', (g.user['id'], )).fetchone()
        ip = get_db().execute(
            'SELECT ip'
            ' FROM organization o JOIN host h ON o.host_id = h.id'
            ' WHERE o.id = ?', (org_info[0], )).fetchone()

    # admin界面:master机中docker machine中正在运行docker列表
    # 默认master级为admin所在机,如果需要多机登录admin,ssh到master host
    else:
        cmd = shlex.split('docker-machine ls')
        child = subprocess.Popen(cmd,
                                 shell=False,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        child.wait()
        response = child.communicate()
        response = str(response[0])[2:-3].split('\\n')
        for item in response[1:]:
            if item:
                docker_list.append(item.split())
                if len(docker_list[-1]) == 6:
                    docker_list[-1].append(' ')

                docker_name = docker_list[-1][0]
                if docker_name != 'orderer':
                    id = get_db().execute(
                        'SELECT id'
                        '   FROM organization'
                        '   WHERE orgname=?', (docker_name, )).fetchone()
                    docker_list[-1].append(id[0])
                else:
                    docker_list[-1].append(None)
    return render_template('status/index.html',
                           org_info=org_info,
                           ip=ip,
                           docker_list=docker_list)
Example #2
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute('SELECT * FROM user WHERE id = ?',
                                  (user_id, )).fetchone()
Example #3
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        # usertype:
        # 0 - admin
        # 1 - normal user
        if g.user['usertype'] == 0:
            usertype = request.form['usertype']
            org_id = request.form['org_id']
        else:
            org_id = g.user['org_id']
            usertype = 1

        if int(usertype) == 0:
            org_id = None
        db = get_db()
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        elif not org_id:
            if int(usertype) == 1:
                error = 'Organization ID is required.'
        elif (int(org_id) < 1) or (int(org_id) > max(
                db.execute('SELECT id FROM organization').fetchall()[0])):
            error = 'Invalid organization ID.'
        elif db.execute('SELECT id FROM user WHERE username = ?',
                        (username, )).fetchone() is not None:
            error = 'User {} is already registered.'.format(username)

        if error is None:
            # 如果没有填写org_id:
            if not org_id:
                db.execute(
                    'INSERT INTO user (username, password, usertype) VALUES (?, ?, ?)',
                    (username, generate_password_hash(password),
                     int(usertype)))
            # 如果填写了org_id:
            else:
                db.execute(
                    'INSERT INTO user (username, password, usertype, org_id) VALUES (?, ?, ?, ?)',
                    (username, generate_password_hash(password), int(usertype),
                     int(org_id)))
            db.commit()
            flash('Successfully added!')
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/register.html')
Example #4
0
File: status.py Project: WZoe/Harp
def get_org(id, check_user=True):
    org_info = get_db().execute(
        'SELECT orgname, ip, api_port'
        ' FROM organization o JOIN host h ON o.host_id = h.id'
        ' WHERE o.id = ?', (id, )).fetchone()

    if org_info is None:
        abort(404, "organization id {0} doesn't exist.".format(id))

    if check_user and id != g.user['org_id'] and g.user['usertype'] == 1:
        abort(403)

    return org_info
Example #5
0
File: status.py Project: WZoe/Harp
def get_jwt(id, api_url):
    # 这里的id是user id
    username = get_db().execute('SELECT username FROM user WHERE id=?',
                                (id, )).fetchone()

    if username is None:
        abort(404, "User id {0} doesn't exist.".format(id))

    # response = requests.post(api_url+'/users', headers={'Content-Type': 'application/json'}, data='{"username":"******","password":"******"}')
    response = requests.post(api_url + '/users',
                             headers={'Content-Type': 'application/json'},
                             json={
                                 "username": username[0] + '_' + str(id),
                                 "password": '******'
                             })

    # return str(jwt).replace('"', '')
    if response.status_code == 200:
        return json.loads(response.content.decode('utf-8'))
    else:
        # abort(404, "JWT Failed.")
        return None
Example #6
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None
        user = db.execute('SELECT * FROM user WHERE username = ?',
                          (username, )).fetchone()

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/index.html')
Example #7
0
File: status.py Project: WZoe/Harp
def new():
    if request.method == 'POST':
        name = request.form['name']
        ip_id = request.form['id']
        api_port = request.form['api_port']
        docker_type = request.form['type']
        d_port = request.form['d_port']

        db = get_db()
        error = None

        if not name:
            error = 'Name is required.'
        elif not ip_id:
            error = 'Host ID is required.'
        elif not api_port:
            error = 'API Port is required.'
        elif not d_port:
            error = 'Docker Daemon Port is required.'
        elif (int(ip_id) < 1) or (int(ip_id) > max(
                db.execute('SELECT id FROM host').fetchall()[0])):
            error = 'Invalid Host ID.'
        elif db.execute('SELECT id FROM organization WHERE orgname = ?',
                        (name, )).fetchone() is not None:
            error = 'Name {} already exists.'.format(name)

        if error is None:
            if int(docker_type) == 1:
                # 配置新org

                # 查询host信息
                host = db.execute(
                    'SELECT ip, user, port'
                    ' FROM host'
                    ' WHERE h.id = ?', (ip_id, )).fetchone()

                # 新建docker
                cmd = shlex.split(
                    os.path.dirname(os.path.abspath(__file__)) +
                    '/create-org-docker.sh -docker_name ' + name +
                    ' -host_ip ' + host[0] + ' -host_user ' + host[1] +
                    ' ssh_port ' + host[2] + ' api_port ' + api_port +
                    ' daemon_port ' + d_port)
                child = subprocess.Popen(cmd,
                                         shell=False,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE)
                child.wait()
                logs = child.communicate()

                db.execute(
                    'INSERT INTO organization (orgname, host_id, api_port, daemon_port) VALUES (?, ?, ?,?)',
                    (name, ip_id, api_port, d_port))
                db.commit()
                flash('Successfully added and deployed! Deployment logs: \n' +
                      str(logs))
                # flash(logs)
                return redirect(url_for('status.index'))
            else:
                pass

        flash(error)

    return render_template('status/new.html')