Working with hard resets
This commit is contained in:
@@ -2,6 +2,8 @@ import docker
|
|||||||
from game_tables import GameTable
|
from game_tables import GameTable
|
||||||
from key_tables import KeyTable
|
from key_tables import KeyTable
|
||||||
import os
|
import os
|
||||||
|
import socket
|
||||||
|
import inspect
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
|
||||||
|
|
||||||
@@ -26,8 +28,19 @@ def build_container_routing_labels(prefix: str, strip_prefix=True):
|
|||||||
|
|
||||||
return labels
|
return labels
|
||||||
|
|
||||||
|
def delete_container(docker_id) -> bool:
|
||||||
|
if not docker_id:
|
||||||
|
return False
|
||||||
|
|
||||||
def stop_container(docker_id):
|
try:
|
||||||
|
client = docker.from_env()
|
||||||
|
container = client.containers.get(docker_id)
|
||||||
|
container.remove()
|
||||||
|
return True
|
||||||
|
except docker.errors.NotFound, docker.errors.APIError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def stop_container(docker_id) -> bool:
|
||||||
try:
|
try:
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container = client.containers.get(docker_id)
|
container = client.containers.get(docker_id)
|
||||||
@@ -40,6 +53,7 @@ def stop_container(docker_id):
|
|||||||
def start_foundry_container(table: GameTable, key: KeyTable):
|
def start_foundry_container(table: GameTable, key: KeyTable):
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container = None
|
container = None
|
||||||
|
networkName = client.containers.get(socket.gethostname()).attrs["HostConfig"]["NetworkMode"]
|
||||||
|
|
||||||
if table.docker_id != None and table.docker_id != 0:
|
if table.docker_id != None and table.docker_id != 0:
|
||||||
try:
|
try:
|
||||||
@@ -60,7 +74,7 @@ def start_foundry_container(table: GameTable, key: KeyTable):
|
|||||||
): {"bind": "/data", "mode": "rw"},
|
): {"bind": "/data", "mode": "rw"},
|
||||||
"{data_bind}/container_cache".format(
|
"{data_bind}/container_cache".format(
|
||||||
data_bind=os.environ.get("FOUNDRY_DATA_BIND")
|
data_bind=os.environ.get("FOUNDRY_DATA_BIND")
|
||||||
): {"bind": "/data/container_cache", "mode": "ro"},
|
): {"bind": "/data/container_cache", "mode": "rw"},
|
||||||
}
|
}
|
||||||
environment = [
|
environment = [
|
||||||
"FOUNDRY_ROUTE_PREFIX={prefix}".format(prefix=table.game_table_link),
|
"FOUNDRY_ROUTE_PREFIX={prefix}".format(prefix=table.game_table_link),
|
||||||
@@ -77,6 +91,7 @@ def start_foundry_container(table: GameTable, key: KeyTable):
|
|||||||
environment=environment,
|
environment=environment,
|
||||||
ports=ports,
|
ports=ports,
|
||||||
detach=True,
|
detach=True,
|
||||||
|
network=networkName
|
||||||
)
|
)
|
||||||
|
|
||||||
return container.id
|
return container.id
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ CREATE TABLE IF NOT EXISTS "game_tables" (
|
|||||||
"game_table_id" INTEGER NOT NULL UNIQUE,
|
"game_table_id" INTEGER NOT NULL UNIQUE,
|
||||||
"game_table_name" TEXT UNIQUE,
|
"game_table_name" TEXT UNIQUE,
|
||||||
"game_table_link" TEXT UNIQUE,
|
"game_table_link" TEXT UNIQUE,
|
||||||
"version" INTEGER
|
"version" INTEGER,
|
||||||
"active" INTEGER DEFAULT 0,
|
"active" INTEGER,
|
||||||
"game_key" TEXT,
|
"game_key" TEXT,
|
||||||
"docker_id" TEXT,
|
"docker_id" TEXT,
|
||||||
PRIMARY KEY("game_table_id")
|
PRIMARY KEY("game_table_id")
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import sqlite3
|
|||||||
import container_managment
|
import container_managment
|
||||||
from database import SmartCursor, get_db, init_db
|
from database import SmartCursor, get_db, init_db
|
||||||
import tables
|
import tables
|
||||||
|
import os
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ def delete_table(table_id):
|
|||||||
if table == None:
|
if table == None:
|
||||||
return jsonify({}), 404
|
return jsonify({}), 404
|
||||||
|
|
||||||
|
container_managment.delete_container(table.docker_id)
|
||||||
|
|
||||||
table.delete()
|
table.delete()
|
||||||
with SmartCursor(connection=db) as smartCursor:
|
with SmartCursor(connection=db) as smartCursor:
|
||||||
table.commit(smartCursor)
|
table.commit(smartCursor)
|
||||||
@@ -87,6 +89,8 @@ def activate_table(table_id):
|
|||||||
keyService = KeyService(db)
|
keyService = KeyService(db)
|
||||||
gameService = GameService(db)
|
gameService = GameService(db)
|
||||||
|
|
||||||
|
hard = bool(request.get_json()["hard"])
|
||||||
|
|
||||||
keys = keyService.get_free_keys(cursor)
|
keys = keyService.get_free_keys(cursor)
|
||||||
table = gameService.read_by_id(table_id, cursor)
|
table = gameService.read_by_id(table_id, cursor)
|
||||||
|
|
||||||
@@ -104,6 +108,8 @@ def activate_table(table_id):
|
|||||||
current_app.logger.info(table.docker_id)
|
current_app.logger.info(table.docker_id)
|
||||||
key.reserve(table.game_table_id)
|
key.reserve(table.game_table_id)
|
||||||
table.active = True
|
table.active = True
|
||||||
|
if hard:
|
||||||
|
container_managment.delete_container(table.docker_id)
|
||||||
table.docker_id = container_managment.start_foundry_container(table, key)
|
table.docker_id = container_managment.start_foundry_container(table, key)
|
||||||
key.commit(cursor)
|
key.commit(cursor)
|
||||||
table.commit(cursor)
|
table.commit(cursor)
|
||||||
|
|||||||
Reference in New Issue
Block a user