def main_lopp(): with app.app_context(): while True: apps = get_all_apps() find_problem_apps(apps) try_deploy_problem_apps() sleep(60)
def remove_docker(container_id): with app.app_context(): client = docker.from_env() try: container = client.containers.get(container_id) container.stop() container.remove() except docker.errors.APIError: pass
def start_new_docker(app_name): with app.app_context(): client = docker.from_env() str_env = "APP_NAME=" + app_name server_key_env = "SERVER_KEY=" + get_server_key() port = get_port() container = client.containers.run( 'vktestapp', ports={'8080/tcp': port}, detach=True, environment=[str_env, server_key_env]) return port, container.id
def restart_docker(app_name, container_id, port): with app.app_context(): client = docker.from_env() try: container = client.containers.get(container_id) container.stop() container.remove() #if container remove, but exist in database except docker.errors.APIError: port = get_port() str_env = "APP_NAME=" + app_name server_key_env = "SERVER_KEY=" + get_server_key() container = client.containers.run( 'vktestapp', ports={'8080/tcp': port}, detach=True, environment=[str_env, server_key_env]) return port, container.id
def delete_app(app_name): with app.app_context(): cursor = get_db().cursor() sql = "DELETE FROM Apps WHERE appName=?" cursor.execute(sql, [app_name]) get_db().commit()
def get_info_app(app_name): with app.app_context(): cursor = get_db().cursor() sql = "SELECT * FROM Apps WHERE appName=?" cursor.execute(sql, [app_name]) return cursor.fetchone()
def get_all_apps(): with app.app_context(): cursor = get_db().cursor() sql = "SELECT * FROM Apps " cursor.execute(sql) return cursor.fetchall()
def update_info_app(app_name, url, port, container): with app.app_context(): cursor = get_db().cursor() sql = "UPDATE Apps SET url = ? , container = ? , port = ? WHERE appName=?" cursor.execute(sql, [url, container, port, app_name]) get_db().commit()
def set_info_app(app_name, url, port, container): with app.app_context(): cursor = get_db().cursor() sql = "INSERT INTO Apps VALUES (?, ?, ?, ?)" cursor.execute(sql, [app_name, url, port, container]) get_db().commit()
def init_db(): with app.app_context(): db = get_db() with app.open_resource('Database/schema.sql', mode='r') as f: db.cursor().executescript(f.read()) db.commit()