def disconnect(user): instance_id = redis.get(user) if instance_id is not None: instance = Instance.get(instance_id) instance.users.remove(user) redis.delete(user) return 'disconnected'
def request_reset(id): user = request.json['user'] if redis.get(user) != id: return abort(403) instance = Instance.get(id) instance.users.remove(user) instance.save() redis.sadd(f'{user}_avoid', instance.container.id) new_instance = get_instance_for(user, instance.challenge) redis.set(user, new_instance.container_id) return jsonify(new_instance.to_json())
def get_instance_for(user, challenge): for instance_id in redis.smembers(challenge): instance = Instance.get(instance_id) if len(instance.users) < instance.user_limit and not redis.sismember( f'{user}_avoid', instance.container.id): if len(instance.users) + 2 > instance.user_limit: redis.sadd('new_instance_queue', challenge) instance.users.append(user) instance.save() redis.set(user, str(instance)) return instance instance = start_instance(challenge) instance.users.append(user) instance.save() redis.set(user, str(instance)) return instance
def cleanup(): print("Stopping old instances...") for challenge in challenge_data: empty_containers = [] has_free_instance = False for instance_id in redis.smembers(challenge): instance = Instance.get(instance_id) if len(instance.users) + 2 <= instance.user_limit: has_free_instance = True elif len(instance.users) == 0: empty_containers.append(instance) empty_containers.sort(key=lambda x: x.started) for instance in empty_containers[:-1]: instance.stop() if not has_free_instance and not redis.sismember('new_instance_queue', challenge): redis.sadd('new_instance_queue', challenge)
def user_instance(user): instance = redis.get(user) if instance is not None: return jsonify(Instance.from_string(instance)) return abort(404)
def docker_instance(id): instance = Instance.get(id) if instance is not None: return jsonify(instance.container.stats) return abort(404)
def detail_instance(id): instance = Instance.get(id) if instance is not None: return jsonify(instance.to_json()) return abort(404)
def get_logs(id): instance = Instance.get(id) if instance is None: return abort(404) return instance.container.logs