Added ability to run containers in web browser

This commit is contained in:
Lukasdotcom
2022-01-22 15:04:24 -05:00
parent 9d8f052140
commit 2e3ec08397
8 changed files with 212 additions and 7 deletions

View File

@@ -179,7 +179,9 @@ def repair(): # Repairs all tables
"space3likes" : [["id", 1], ["account", 0]],
"golfGamePlayers" : [["gameID", 1], ["multiplier", 1], ["user", 0], ["points", 1], ["orderID", 1], ["lastMode", 0], ["upToDate", 6]],
"golfGameCards" : [["gameID", 1], ["user", 0], ["card", 1], ["cardPlacement", 1], ["faceUp", 6]],
"golfGame" : [["ID", 5], ["deck", 4], ["discard", 4], ["cardNumber", 1], ["flipNumber", 1], ["multiplierForFlip", 1], ["pointsToEnd", 1], ["name", 0], ["password", 0], ["players", 1], ["playersToStart", 1], ["currentPlayer", 1], ["turnStartTime", 1], ["locked", 6]]
"golfGame" : [["ID", 5], ["deck", 4], ["discard", 4], ["cardNumber", 1], ["flipNumber", 1], ["multiplierForFlip", 1], ["pointsToEnd", 1], ["name", 0], ["password", 0], ["players", 1], ["playersToStart", 1], ["currentPlayer", 1], ["turnStartTime", 1], ["locked", 6]],
"docker" : [["link", 0], ["action", 0], ["image", 0], ["password", 0], ["owner", 0], ["port", 1], ["ID", 0]],
"dockerImages" : [["realName", 0], ["shortName", 0], ["description", 0]]
}
changedTables = []
for x in databaseDict:
@@ -245,9 +247,14 @@ def repair(): # Repairs all tables
command("ALTER TABLE cookies ADD lastIP varchar(255) NULL")
version = "v1.1"
updatedVersions.append("v1.1")
if versionNumber == "v1.1":
createTable("docker", [["link", 0], ["action", 0], ["image", 0], ["password", 0], ["owner", 0], ["port", 1], ["ID", 0]])
createTable("dockerImage", [["realName", 0], ["shortName", 0], ["description", 0]])
version = "v2.0"
updatedVersions.append("v2.0")
# Fixes the version if it is invalid to the latest version
if version != "v1.1":
version = "v1.1"
if version != "v2.0":
version = "v2.0"
except:
1
command("DELETE FROM information WHERE pointer='version'")

View File

@@ -15,7 +15,12 @@ import os
import datetime
import sys
import glob
try:
import docker
dockerClient = docker.from_env()
skipDocker = False
except:
skipDocker = True
def error(e):
return "".join(traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__))
@@ -230,9 +235,11 @@ try:
database.command("DELETE FROM golfGameCards WHERE NOT EXISTS (SELECT * FROM golfGame WHERE golfGameCards.gameID = golfGame.ID)") # Removes players from games that do not exist
writeLog("Server maintenance ran succesfully.", 12)
# Will add to log if the GPIO library exists
# Will add to log if a library could not be connected to
if skipGPIO:
writeLog("Could not import GPIO library", 9)
if skipDocker:
writeLog("Could not connect to docker", 9)
while True: # will wait until connected to internet
try:
urllib.request.urlopen("https://google.com")
@@ -341,6 +348,28 @@ try:
# Will check every 2 seconds if the button is pressed and when it is show it on the led and then wait another second to verify that it is an actual press
while True:
time.sleep(2)
# Will check if a docker container should be started or stopped.
if not skipDocker:
dockerList = database.trueSearch("SELECT * FROM docker")
for x in dockerList:
print(x)
# Will check if the container has already been stopped
id = x[6]
if x[1] == "started":
try:
dockerClient.containers.get(id)
except Exception:
database.command(f"UPDATE docker SET action='stopped' WHERE ID='{id}'")
writeLog(f"Container with id of {id} was stopped", 24)
elif x[1] == "starting": # Will start all containers that are neccessary
password = x[3]
newID = dockerClient.containers.run(x[2], detach=True, ports={'80/tcp':x[5]}, remove=True, environment=[f"VNC_PASSWD={password}"]).attrs["Id"]
database.command(f"UPDATE docker SET action='started', id='{newID}' WHERE ID='{id}'")
writeLog(f"Container with id of {id} which changed to {newID} was started", 23)
elif x[1] == "stopping": # Stops all containers that need to be stopped.
dockerClient.containers.get(id).stop()
database.command(f"UPDATE docker SET action='stopped' WHERE ID='{id}'")
writeLog(f"Container with id of {id} was stopped", 24)
if os.path.isfile(location + "restart.json"): # Used to restart the server
writeLog("Server is being restarted", 12)
os.remove(location + "restart.json")