def stop_instance(): res = {} if request.method == 'POST': req_data = json.loads(request.data) db_session = db.Session() instance_query_result = db_session.query(Instance).filter(\ Instance.container_serial == req_data['container_serial']).first() if instance_query_result is not None: policy = {} policy['operate'] = app.config['STOP'] policy['container_serial'] = req_data['container_serial'] policy['container_name'] = instance_query_result.container_name policy['user_name'] = req_data['user_name'] message = json.dumps(policy) ui_mq = UiQueue() worker_res = ui_mq.send(message) worker_res_dict = json.loads(worker_res) res['code'] = worker_res_dict['code'] res['message'] = worker_res_dict['message'] res['container_serial'] = worker_res_dict['container_serial'] eagle_logger.info(res['message']) else: res['code'] = '0x9' res['message'] = 'container not exist' return jsonify(**res)
def sign_in(): res = {} if request.method == 'POST': req_data = json.loads(request.data) db_session = db.Session() result = db_session.query(User).filter(or_(User.username == req_data['username']\ , User.email == req_data['username'])).first() if result is None: res['code'] = '0x7' res['message'] = 'Username not found.' else: passcode = hashlib.md5(req_data['password'] + result.salt).hexdigest() if result.password == passcode: session['is_login'] = True user_profile = {} user_profile['id'] = result.id user_profile['username'] = result.username user_profile['password'] = result.password session['user_profile'] = json.dumps(user_profile) instances = db_session.query(Instance).all() res['code'] = '0x1' res['message'] = 'sign in successful' else: res['code'] = '0x6' res['message'] = 'wrong password.' return jsonify(**res) return render_template('index.html')
def create_instance(): res = {} if request.method == 'POST': req_data = json.loads(request.data) container_name = '-'.join([ req_data['user_name'], req_data['container_name'] ]) db_session = db.Session() instance_query_result = db_session.query(Instance).filter(\ Instance.container_name == container_name).first() if instance_query_result is None: policy = {} policy['operate'] = app.config['CREATE'] policy['image_id'] = req_data['image_id'] policy['container_name'] = container_name policy['user_name'] = req_data['user_name'] message = json.dumps(policy) ui_mq = UiQueue() #blocking max time: 60s worker_res = ui_mq.send(message) worker_res_dict = json.loads(worker_res) res['code'] = worker_res_dict['code'] res['message'] = worker_res_dict['message'] res['instance'] = worker_res_dict['ins'] eagle_logger.info(res['message']) eagle_logger.info('db add instance commit!') else: res['code'] = '0x8' res['message'] = 'container name occupied.' eagle_logger.info('container name occupied.') return jsonify(**res)
def run_container(cli, *args, **kwargs): res = {'code': '0x2', 'message': 'Unknown problem', 'ins': {}} container_serial = kwargs.get('container_serial') response = cli.start(container=container_serial) if response is None: #update db inspect_res = cli.inspect_container(container_serial) host = inspect_res["NetworkSettings"]["IPAddress"] port = 22 update_instance_by_serial(container_serial, status=worker_cfg.RUNNING_INSTANCE,\ host=host, port=port) db_session = db.Session() image_query_res = db_session.query(Image).filter(\ Image.id == kwargs.get('image_id')).first() res['code'] = '0x1' res['message'] = 'running successful' res['ins']['image_id'] = kwargs.get('image_id') res['ins']['image_name'] = image_query_res.image_name res['ins']['container_serial'] = container_serial res['ins']['container_name'] = kwargs.get('container_name') res['ins']['host'] = host res['ins']['port'] = port res['ins']['user_name'] = kwargs.get('user_name') res['ins']['status'] = worker_cfg.RUNNING_INSTANCE res['ins']['jump_server'] = worker_cfg.DEPLOY_HOSTNAME worker_logger.info("succeed to start %s." % kwargs.get('container_name')) else: res['ins']['container_serial'] = kwargs.get('container_serial') worker_logger.error("failed to start %s. %s" % (kwargs.get('container_name'), response)) return json.dumps(res)
def sign_up(): res = {} if request.method == 'POST': eagle_logger.debug(type(request.data)) req_data = json.loads(request.data) db_session = db.Session() result = db_session.query(User).filter( User.username == req_data['username']).first() req_email = req_data.get('email', None) if req_email is not None: result_mail = db_session.query(User).filter( User.email == req_email).first() if result is not None: res['code'] = '0x4' res['message'] = 'Username have been occupied by others' elif req_email is not None and result_mail is not None: res['code'] = '0x5' res['message'] = 'Email has been occupied by others' else: timestamp = str(time.time()) + str(random.randint(10000, 20000)) salt = hashlib.md5(timestamp).hexdigest() passcode = hashlib.md5(req_data['password'] + salt).hexdigest() u = User(req_data['username'], passcode, email=req_data.get('email', ''), \ salt=salt, create_time=datetime.datetime.now(), update_time=datetime.datetime.now()) db_session.add(u) db_session.commit() res['code'] = '0x1' res['message'] = 'sign up successful' return jsonify(**res) return render_template('index.html')
def remove_user_by_id(id): db_session = db.Session() user_query_res = db_session.query(User).filter(User.id == id).first() user_name = user_query_res.username db_session.delete(user_query_res) db_session.commit() return user_name
def update_instance_by_serial(container_serial, **kwargs): db_session = db.Session() instance_query_res = db_session.query(Instance).filter(\ Instance.container_serial == container_serial).first() for key in kwargs: setattr(instance_query_res, key, kwargs.get(key)) db_session.commit()
def remove_instance_by_serial(container_serial): db_session = db.Session() instance_query_res = db_session.query(Instance).filter( Instance.container_serial == container_serial).first() container_name = instance_query_res.container_name db_session.delete(instance_query_res) db_session.commit() return container_name
def create_instance(ins): db_session = db.Session() ins = Instance(ins.get('image_id'),\ ins.get('user_id'), ins.get('container_name'),\ ins.get('container_serial'), ins.get('host'), \ ins.get('port'), ins.get('status'), ins.get('jump_server')) db_session.add(ins) db_session.commit()
def profile(): res = {} res['code'] = '0x1' res['message'] = 'modify profile successful' if request.method == 'POST': eagle_logger.debug(type(request.data)) req_data = json.loads(request.data) db_session = db.Session() testid = req_data['id'] result = db_session.query(User).filter(User.id == testid).first() req_id = result.id new_username = req_data['username'] if new_username is not None: db_session = db.Session() result_User = db_session.query(User).filter( User.username == new_username).first() if result_User is not None and req_id != result_User.id: res['code'] = '0x4' res['message'] = 'Username has been occupied by others' return jsonify(**res) else: update_username_by_id(req_id, new_username) new_email = req_data['email'] if new_email is not None: db_session = db.Session() result_User = db_session.query(User).filter( User.email == new_email).first() if result_User is not None and req_id != result_User.id: res['code'] = '0x5' res['message'] = 'Email has been occupied by others' return jsonify(**res) else: update_email_by_id(req_id, new_email) new_password = req_data['password'] if new_password is not None: timestamp = str(time.time()) + str(random.randint(10000, 20000)) salt = hashlib.md5(timestamp).hexdigest() passcode = hashlib.md5(new_password + salt).hexdigest() update_password_by_id(req_id, passcode) update_salt_by_id(req_id, salt) update_update_time_by_id(req_id, datetime.datetime.now()) return jsonify(**res) return render_template('index.html')
def create_user(user): db_session = db.Session() password = hashlib.md5(user.get('password') + user.get('salt', '')).hexdigest() user_res = User(user.get('username'), password, email=user.get('email'), salt=user.get('salt', ''), create_time=user.get('create_time', datetime.datetime.utcnow()), update_time=user.get('update_time', datetime.datetime.utcnow()), is_deleted=user.get('is_deleted', 0)) db_session.add(user_res) db_session.commit()
def create_container(cli, *args, **kwargs): res = {'code': '0x2', 'message': 'Unknown problem', 'ins': {}} image_id = kwargs.get('image_id') image_name = worker_cfg.IMAGE_DICT.get(image_id) db_session = db.Session() user_query_res = db_session.query(User).filter(\ User.username == kwargs.get('user_name')).first() user_id = user_query_res.id try: container = cli.create_container(image=image_name, detach=True, name=kwargs.get('container_name')) except docker.errors.APIError as e: msg = str(e) res['code'] = '0x3' res['message'] = msg res['ins']['container_serial'] = '' res['ins']['image_id'] = image_id res['ins']['user_id'] = user_id res['ins']['host'] = '' res['ins']['port'] = 0 res['ins']['jump_server'] = '' res['ins']['container_name'] = kwargs.get('container_name') res['ins']['image_name'] = image_name res['ins']['status'] = worker_cfg.FAILED_INSTANCE worker_logger.error("failed to create %s. %s" % (kwargs.get('container_name'), msg)) else: #write db res['code'] = '0x1' res['message'] = 'create successful' res['ins']['container_serial'] = container.get('Id') res['ins']['image_id'] = image_id res['ins']['user_id'] = user_id res['ins']['host'] = '' res['ins']['port'] = 0 res['ins']['container_name'] = kwargs.get('container_name') res['ins']['image_name'] = image_name res['ins']['status'] = worker_cfg.STOP_INSTANCE res['ins']['jump_server'] = worker_cfg.DEPLOY_HOSTNAME worker_logger.info("succeed to write %s in database." % kwargs.get('container_name')) create_instance(res['ins']) return json.dumps(res)
def list_instance(): res = {} instances = [] db_session = db.Session() user_query_result = db_session.query(User).filter(User.username == request.args.get('signin_username')).first() if user_query_result is not None: instances = db_session.query(Instance, Image).\ join(Image, Instance.image_id == Image.id).filter(Instance.user_id == user_query_result.id).all() ins_list = [] for ins, img in instances: ins_item = {} ins_item['image_id'] = ins.image_id ins_item['image_name'] = img.image_name ins_item['container_serial'] = ins.container_serial ins_item['container_name'] = ins.container_name ins_item['host'] = ins.host ins_item['port'] = ins.port ins_item['status'] = ins.status ins_item['jump_server'] = ins.jump_server ins_list.append(ins_item) ui_logger.info('len(instances) == %s' % str(len(instances))) res['code'] = '0x1' res['instances'] = ins_list return jsonify(**res)
def get_user_by_username(username): db_session = db.Session() user_query_res = db_session.query(User).filter( User.username == username).first() return user_query_res
def update_user_by_id(id, *args, **kwargs): db_session = db.Session() user_query_res = db_session.query(User).filter(User.id == id).first() for key in kwargs: setattr(user_query_res, key, kwargs.get(key)) db_session.commit()
def get_instance_by_serial(container_serial): db_session = db.Session() instance_query_res = db_session.query(Instance).filter( Instance.container_serial == container_serial).first() return instance_query_res