2020-01-13 17:02:11 +00:00
|
|
|
#! /usr/bin/python3
|
2020-01-16 01:27:33 +00:00
|
|
|
# Copyright (C) 2018-2020 Merspieler, merspieler _at_ airmail.cc
|
2020-01-13 17:02:11 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
2020-03-13 13:07:44 +00:00
|
|
|
import pymysql
|
2020-01-13 17:02:11 +00:00
|
|
|
|
2020-03-05 19:04:43 +00:00
|
|
|
from common import norm
|
|
|
|
|
2020-01-13 17:02:11 +00:00
|
|
|
host = socket.gethostname()
|
|
|
|
port = 12345
|
|
|
|
verbose = False
|
2020-03-13 13:07:44 +00:00
|
|
|
dbuser = ""
|
|
|
|
dbpw = ""
|
2020-01-13 17:02:11 +00:00
|
|
|
|
|
|
|
argc = len(sys.argv)
|
|
|
|
i = 1
|
|
|
|
while i < argc:
|
|
|
|
if sys.argv[i] == "--port":
|
|
|
|
i += 1
|
|
|
|
port = int(sys.argv[i])
|
|
|
|
elif sys.argv[i] == "--host":
|
|
|
|
i += 1
|
|
|
|
host = sys.argv[i]
|
2020-03-13 13:07:44 +00:00
|
|
|
elif sys.argv[i] == "-u" or sys.argv[i] == "--user":
|
|
|
|
i += 1
|
|
|
|
dbuser = sys.argv[i]
|
|
|
|
elif sys.argv[i] == "-p" or sys.argv[i] == "--password":
|
|
|
|
i += 1
|
|
|
|
dbpw = sys.argv[i]
|
2020-01-13 17:02:11 +00:00
|
|
|
elif sys.argv[i] == "-v":
|
|
|
|
verbose = True
|
|
|
|
elif sys.argv[i] == "-h" or sys.argv[i] == "--help":
|
2020-01-15 11:33:31 +00:00
|
|
|
print("usage: worldbuild-manager.py [OPTIONS]")
|
|
|
|
print("Handles job asignments and keeps track of the status")
|
2020-01-13 17:02:11 +00:00
|
|
|
print("")
|
|
|
|
print("OPTIONS")
|
2020-03-13 13:07:44 +00:00
|
|
|
print(" -u, --user Database user")
|
|
|
|
print(" -p, --password Database password")
|
2020-01-13 17:02:11 +00:00
|
|
|
print(" -v, --verbose Verbose printouts")
|
|
|
|
print(" , --host hostname or interface to bind to")
|
|
|
|
print(" , --port Port to bind to")
|
|
|
|
print(" -h, --help Shows this help and exit")
|
2020-01-15 11:33:31 +00:00
|
|
|
sys.exit(0)
|
2020-01-13 17:02:11 +00:00
|
|
|
else:
|
|
|
|
print("Unknown option " + sys.argv[i])
|
|
|
|
sys.exit(1)
|
|
|
|
i += 1
|
|
|
|
|
2020-03-13 13:07:44 +00:00
|
|
|
if dbuser == "":
|
|
|
|
print("ERROR: No database user given")
|
|
|
|
sys.exit(1)
|
2020-01-13 17:02:11 +00:00
|
|
|
|
2020-03-13 13:07:44 +00:00
|
|
|
if dbpw == "":
|
|
|
|
print("ERROR: No database password given")
|
|
|
|
sys.exit(1)
|
2020-01-18 06:00:31 +00:00
|
|
|
|
2020-03-13 13:07:44 +00:00
|
|
|
db = pymysql.connect("localhost", dbuser, dbpw, "worldbuild")
|
2020-01-18 06:00:31 +00:00
|
|
|
|
2020-03-13 13:07:44 +00:00
|
|
|
cursor = db.cursor()
|
2020-01-18 06:00:31 +00:00
|
|
|
|
2020-03-13 13:07:44 +00:00
|
|
|
def set_state(name, status):
|
|
|
|
if verbose:
|
|
|
|
print(name + " set status to " + status)
|
2020-01-18 06:00:31 +00:00
|
|
|
|
2020-03-13 13:07:44 +00:00
|
|
|
match = re.match(r"^[ew]\d{3}[ns]\d{2}|[0-9]{1,7}$", name)
|
|
|
|
if match == None:
|
|
|
|
print("WARNING: Recived status " + status + " from invalid tile '" + name + "'")
|
2020-01-18 06:01:39 +00:00
|
|
|
else:
|
2020-03-13 13:07:44 +00:00
|
|
|
if status != "started" and status != "done" and status != "rebuild" and status != "skip" and status != "pending" and status != "packaged":
|
|
|
|
print("WARNING: Invalid status '" + status + "' recived from " + name)
|
|
|
|
else:
|
|
|
|
match = re.match(r"[ew]\d{3}[ns]\d{2}", name)
|
|
|
|
if match != None:
|
|
|
|
sql ="SELECT id FROM secondLevel WHERE name = '" + name + "'"
|
|
|
|
cursor.execute(sql)
|
|
|
|
pid = cursor.fetchone()
|
|
|
|
sql = "SELECT id FROM status WHERE name = '" + status + "'"
|
|
|
|
cursor.execute(sql)
|
|
|
|
sid = cursor.fetchone()
|
|
|
|
sql = "UPDATE tile SET status_id = " + str(sid[0]) + " WHERE parent_id = " + str(pid[0])
|
|
|
|
cursor.execute(sql)
|
|
|
|
else:
|
2020-08-27 11:08:20 +00:00
|
|
|
sql = "UPDATE tile SET status_id = (SELECT id FROM status WHERE name = '" + status + "') WHERE id = " + name
|
2020-03-13 13:07:44 +00:00
|
|
|
cursor.execute(sql)
|
|
|
|
db.commit()
|
2020-01-13 17:02:11 +00:00
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.bind((host, port))
|
|
|
|
|
|
|
|
sock.listen(5)
|
|
|
|
print("Up and running...")
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
c, addr = sock.accept()
|
|
|
|
msg = c.recv(128)
|
|
|
|
msg = msg.decode()
|
|
|
|
|
2020-08-27 11:08:20 +00:00
|
|
|
db.ping(reconnect=True)
|
|
|
|
|
2020-03-13 13:07:44 +00:00
|
|
|
match = re.match(r"(set) ([ew]\d{3}[ns]\d{2}|[0-9]{1,7}) (done|started|rebuild|skip|pending|packaged)", msg)
|
2020-01-13 17:02:11 +00:00
|
|
|
if match != None:
|
|
|
|
action = "set"
|
|
|
|
name = match.group(2)
|
|
|
|
status = match.group(3)
|
2020-06-24 15:22:47 +00:00
|
|
|
set_state(name, status)
|
2020-01-13 17:02:11 +00:00
|
|
|
else:
|
2020-01-23 14:02:27 +00:00
|
|
|
match = re.match(r"(get) (started|rebuild|skip|pending|done)", msg)
|
2020-01-13 17:02:11 +00:00
|
|
|
if match != None:
|
2020-01-23 14:02:27 +00:00
|
|
|
if match.group(2) == "done":
|
|
|
|
action = "get-done"
|
|
|
|
else:
|
|
|
|
action = "get-job"
|
|
|
|
get = match.group(2)
|
2020-01-13 17:02:11 +00:00
|
|
|
else:
|
2020-03-13 13:07:44 +00:00
|
|
|
match = re.match(r"(status) ([ew]\d{3}[ns]\d{2}|[0-9]{1,7})", msg)
|
2020-01-16 02:23:47 +00:00
|
|
|
if match != None:
|
|
|
|
action = "status"
|
|
|
|
name = match.group(2)
|
|
|
|
else:
|
|
|
|
print("WARNING: Recived invalid package")
|
2020-01-13 17:02:11 +00:00
|
|
|
|
2020-06-24 23:16:17 +00:00
|
|
|
if action == "get-job":
|
2020-03-17 17:44:28 +00:00
|
|
|
additional_type = ""
|
|
|
|
if get != "pending":
|
|
|
|
additional_type = "OR status_id = (SELECT id FROM status WHERE name = '" + get + "')"
|
2020-08-27 11:08:20 +00:00
|
|
|
sql = "SELECT id FROM tile WHERE status_id = (SELECT id FROM status WHERE name = 'pending') " + additional_type + " ORDER BY parent_id LIMIT 1"
|
|
|
|
cursor.execute(sql)
|
2020-03-13 13:07:44 +00:00
|
|
|
result = cursor.fetchone()
|
2020-03-17 17:44:28 +00:00
|
|
|
if result == None:
|
2020-01-13 17:02:11 +00:00
|
|
|
tile = "None"
|
2020-03-17 17:44:28 +00:00
|
|
|
else:
|
|
|
|
tile = result[0]
|
2020-08-27 11:08:20 +00:00
|
|
|
sql = "UPDATE tile SET status_id = (SELECT id FROM status WHERE name = 'started') WHERE id = " + str(tile)
|
|
|
|
cursor.execute(sql)
|
2020-03-17 17:44:28 +00:00
|
|
|
db.commit()
|
|
|
|
|
2020-01-13 17:02:11 +00:00
|
|
|
|
|
|
|
if verbose:
|
2020-08-27 11:08:20 +00:00
|
|
|
print("Asigning job on tile " + str(tile))
|
2020-01-13 17:02:11 +00:00
|
|
|
|
2020-08-27 11:08:20 +00:00
|
|
|
c.send(str(tile).encode())
|
2020-01-23 14:02:27 +00:00
|
|
|
elif action == "get-done":
|
2020-12-07 09:04:16 +00:00
|
|
|
sql = "SELECT name FROM secondLevel WHERE status_id = (SELECT id FROM status WHERE name = 'done') LIMIT 1"
|
2020-03-17 17:44:28 +00:00
|
|
|
cursor.execute(sql)
|
2020-03-13 13:07:44 +00:00
|
|
|
result = cursor.fetchone()
|
2020-03-17 17:44:28 +00:00
|
|
|
if result == None:
|
2020-01-23 14:02:27 +00:00
|
|
|
tile = "None"
|
2020-03-17 17:44:28 +00:00
|
|
|
else:
|
|
|
|
tile = result[0]
|
|
|
|
|
2020-08-27 11:08:20 +00:00
|
|
|
c.send(str(tile).encode())
|
2020-01-16 02:23:47 +00:00
|
|
|
elif action == "status":
|
2020-03-13 13:07:44 +00:00
|
|
|
match = re.match(r"([ew])(\d{3})([ns])(\d{2})", name)
|
|
|
|
if match != None:
|
2020-06-24 15:22:47 +00:00
|
|
|
sql = "SELECT name FROM status WHERE id = (SELECT status_id FROM secondLevel WHERE name = '" + name + "')"
|
2020-08-27 11:08:20 +00:00
|
|
|
cursor.execute(sql)
|
2020-03-13 13:07:44 +00:00
|
|
|
result = cursor.fetchone()
|
2020-01-16 02:23:47 +00:00
|
|
|
|
2020-03-13 13:07:44 +00:00
|
|
|
c.send(str(result[0]).encode())
|
|
|
|
else:
|
2020-06-24 15:22:47 +00:00
|
|
|
sql = "SELECT name FROM status WHERE id = (SELECT status_id FROM tile WHERE id = '" + name + "')"
|
2020-08-27 11:08:20 +00:00
|
|
|
cursor.execute(sql)
|
2020-03-13 13:07:44 +00:00
|
|
|
result = cursor.fetchone()
|
|
|
|
|
|
|
|
c.send(str(result[0]).encode())
|
2020-01-13 17:02:11 +00:00
|
|
|
c.close()
|
|
|
|
except IOError:
|
|
|
|
print("WARNING: Recived invalid package")
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sock.close()
|
2020-03-13 13:07:44 +00:00
|
|
|
db.close()
|
2020-01-13 17:02:11 +00:00
|
|
|
print("Exiting...")
|
|
|
|
sys.exit(0)
|