Project

General

Profile

Statistics
| Branch: | Revision:

root / web-visualization / utils.py @ master

History | View | Annotate | Download (1.51 KB)

1
#!/usr/bin/env python
2

    
3
import MySQLdb
4
import base64, string
5

    
6
#mysql database information that will be derived via ingest.sh
7
HOST =
8
PORT =
9
USER =
10
PASS =
11
DB =
12

    
13
db = None
14
cursor = None
15

    
16
#executes the query and returns the result
17
def execute(query):
18
    global db
19
    global cursor
20
    if db is None:
21
        db = MySQLdb.connect(host=HOST, port=PORT, user=USER, passwd=PASS, db=DB)
22
        cursor = db.cursor()
23
    try:
24
        cursor.execute(query)
25
        db.commit()
26
        ret = cursor.fetchall()
27
    except:
28
        db.rollback()
29
        ret = None
30
        print "Failed query"
31

    
32
    return ret
33

    
34
#from hex output (that we don't use elsewhere), get a base64 version
35
def gdp_printable_name(hexVal):
36
    binary = base64.b16decode(hexVal.upper())
37
    stdb64 = base64.b64encode(binary)
38
    # convert to filesystem safe version
39
    return stdb64.translate(string.maketrans("/+","_-"), "=")
40

    
41
#takes in a single 'item' from the list of primary nodes as input.
42
#parse the dictionary to get the public IP of primary
43
def parse_primary(d):
44
    s = d["NodeID (IP:port)"]
45
    return s.split(":")[0]
46

    
47
#takes in a single 'item' from the list of secondary nodes as input
48
#ideally, instead of 'Secondary Nodes', it should be called:
49
#">bunch of secondary nodes that are behind same public IP<"
50
#parses the maze, and return a list of pub/priv pairs
51
def parse_secondary(d):
52
    pubIP = d["NATIP"]
53
    nodes = d["Nodes"]
54
    ret = []
55
    for n in nodes:
56
        tmp = n["NodeID (IP:port)"]
57
        privIP = tmp.split(":")[0]
58
        ret.append((pubIP, privIP))
59
    return ret