-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
142 lines (129 loc) · 5.68 KB
/
test.py
File metadata and controls
142 lines (129 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3
import pymysql
import threading
import sys
from sys import argv
def runDDL(argv):
# Define variables
hostname = ""
username = ""
passwd = ""
url = ""
port = ""
db = ""
num = 0
# List of Nodes
nodes = []
# Takes in Command Line Arguments for files
fname, clustercfg, ddlfile = argv
# Reads clustercfg file line by line
# Parses contents and creates a Cluster object for every catalog or node read in
k = open(clustercfg, "r")
with open(clustercfg) as fin:
for line in fin:
if line.strip():
temp = line.strip().split("=")
if temp[0].split(".")[0].find("catalog") > -1:
if temp[0].split(".")[1].find("driver") > -1:
pass
elif temp[0].split(".")[1].find("hostname") > -1:
url = temp[1]
hostname = temp[1].split("/", 2)[2].split(":")[0]
port = temp[1].split("/", 2)[2].split(":")[1].split("/")[0]
db = temp[1].split("/", 2)[2].split(":")[1].split("/")[1]
elif temp[0].split(".")[1].find("username") > -1:
username = temp[1]
elif temp[0].split(".")[1].find("passwd") > -1:
passwd = temp[1]
catalog = Catalog(hostname, username, passwd, db, url)
catalog.createCatalog()
elif temp[0].split(".")[0].find("numnodes") > -1:
pass
elif temp[0].split(".")[0].find("node") > -1:
if temp[0].split(".")[1].find("driver") > -1:
pass
elif temp[0].split(".")[1].find("hostname") > -1:
url = temp[1]
hostname = temp[1].split("/", 2)[2].split(":")[0]
port = temp[1].split("/", 2)[2].split(":")[1].split("/")[0]
db = temp[1].split("/", 2)[2].split(":")[1].split("/")[1]
elif temp[0].split(".")[1].find("username") > -1:
username = temp[1]
elif temp[0].split(".")[1].find("passwd") > -1:
num = num + 1
passwd = temp[1]
nodes.append(Node(hostname, username, passwd, db, num, url, port))
# Reads ddlfile, Remove Whitespace, Remove new lines, Parse contents on ';'
k = open(ddlfile, "r")
sqlcmds = list(filter(None, k.read().strip().replace("\n","").split(';')))
# Run Commands from ddlfile on each Thread
threads = []
table = ""
for cmd in sqlcmds:
# Find table name
table = cmd.split("TABLE ")[1].split("(")[0]
for n in nodes:
threads.append(NodeThread(n, cmd, ddlfile).start())
for n in nodes:
catalog.updateCatalog(table, n)
print("[", catalog.url, "]: catalog updated.")
k.close()
def runCommands(node, cmd, ddlfile):
try:
connect = pymysql.connect(node.hostname, node.username, node.passwd, node.db)
cur = connect.cursor()
cur.execute(cmd)
connect.close()
print("[", node.url, "]:", ddlfile, " success.")
except pymysql.InternalError:
print("[", node.url, "]:", ddlfile, " failed.")
except pymysql.OperationalError:
print("[", node.url, "]:", ddlfile, " failed to connect to server.")
class NodeThread(threading.Thread):
def __init__(self, node, cmd, ddlfile):
threading.Thread.__init__(self)
self.node = node
self.cmd = cmd
self.ddlfile = ddlfile
def run(self):
runCommands(self.node, self.cmd, self.ddlfile)
class Catalog:
'Base Class for Catalog'
def __init__(self, hostname, username, passwd, db, url):
self.hostname = hostname.replace(" ", "")
self.username = username.replace(" ", "")
self.passwd = passwd.replace(" ", "")
self.db = db.replace(" ", "")
self.url = url
def displayCatalog(self):
print("Hostname: ", self.hostname, " Username: ", self.username, " Passwd: ", self.passwd, " DB: ", self.db)
def updateCatalog(self, table, n):
try:
connect = pymysql.connect(self.hostname, self.username, self.passwd, self.db)
cur = connect.cursor()
cur.execute("""INSERT INTO dtables VALUES (%s, NULL, %s, %s, %s, NULL, %s, NULL, NULL, NULL)""", (table, n.url, n.username, n.passwd, n.num))
connect.commit()
connect.close()
except pymysql.InternalError:
print("Error")
def createCatalog(self):
try:
connect = pymysql.connect(self.hostname, self.username, self.passwd, self.db)
cur = connect.cursor()
cur.execute("""CREATE TABLE dtables (tname VARCHAR(32), nodedriver VARCHAR(64), nodeurl VARCHAR(128), nodeuser VARCHAR(16), nodepasswd VARCHAR(16), partmtd INT, nodeid INT, partcol VARCHAR(32), partparam1 VARCHAR(32), partparam2 VARCHAR(32))""")
connect.close()
except pymysql.InternalError:
pass
class Node:
'Base Class for Nodes'
def __init__(self, hostname, username, passwd, db, num, url, port):
self.hostname = hostname.replace(" ", "")
self.username = username.replace(" ", "")
self.passwd = passwd.replace(" ", "")
self.db = db.replace(" ", "")
self.num = num
self.url = url.replace(" ", "")
self.port = port.replace(" ", "")
def displayNode(self):
print("Hostname: ", self.hostname, " Username: ", self.username, " Passwd: ", self.passwd, " DB: ", self.db, " Num: ", self.num, " Url: ", self.url, " Port: ", self.port)
runDDL(argv)