Statistics
| Branch: | Revision:

iof-tools / gen-rspec.py @ 63130429

History | View | Annotate | Download (5.39 KB)

1
#!/usr/bin/env python
2
import xml.etree.ElementTree as ET
3

    
4
try:
5
    from StringIO import StringIO
6
except ImportError:
7
    from io import StringIO
8
from subprocess import check_output
9
from argparse import ArgumentParser
10
import sys
11

    
12
from const import AVAILABLE, COMPONENT_ID, COMPONENT_NAME, FOOTER_TEMPLATE, \
13
    HARDWARE_TYPE, HEADER_TEMPLATE, LOCATION, NAME, NODE, NODE_TEMPLATE, NOW, \
14
    TRUE, X, Y, Z, NETWORK_HEADER, NETWORK_FOOTER, NETWORK_TEMPLATE
15

    
16

    
17
def matches(name, filters, nodes):
18
    if len(nodes) > 0:
19
        return name in nodes
20
    if len(filters) == 0:
21
        return True
22
    for f in filters:
23
        if name.startswith(f):
24
            return True
25
    return False
26

    
27

    
28
def matches_hardware(hardware_filter, hardware_types):
29
    if len(hardware_filter) == 0:
30
        return True, ""
31
    for f in hardware_filter:
32
        for h in hardware_types:
33
            if h.startswith(f):
34
                return True, h
35
    return False, ""
36

    
37

    
38
def fetch_nodes(testbed, n, use_hardware, filters, nodes, hardware_types,
39
                dump_file, df):
40
    omni = ["omni", "-V3", "listresources", "-a", testbed, "--error",
41
            "--tostdout"]
42

    
43
    xml = check_output(omni)
44

    
45
    # parse the xml file removing the namespaces from tag names
46
    xml_file = StringIO(xml.decode('utf-8'))
47
    it = ET.iterparse(xml_file)
48
    for _, el in it:
49
        el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
50
    e = it.root
51

    
52
    t = __import__('%stemplates' % testbed, globals(), locals(),
53
                   [NODE_TEMPLATE, HEADER_TEMPLATE, FOOTER_TEMPLATE, NETWORK_HEADER, NETWORK_FOOTER, NETWORK_TEMPLATE])
54
    h = 40
55
    w = 120
56
    xs = 100
57
    ys = 100
58

    
59
    xml = ""
60
    xmlnetwork = ""
61

    
62
    for node in e.findall(NODE):
63
        name = node.get(COMPONENT_NAME)
64
        available = node.find(AVAILABLE).get(NOW).lower() == TRUE
65
        has_position = X in node.find(LOCATION).attrib
66
        component_id = node.get(COMPONENT_ID)
67
        if has_position:
68
            x = float(node.find(LOCATION).get(X))
69
            y = float(node.find(LOCATION).get(Y))
70
            z = float(node.find(LOCATION).get(Z))
71
        else:
72
            x = 0
73
            y = 0
74
            z = 0
75
        row = (n - 1) / 8
76
        col = (n - 1) % 8
77
        good_node = False
78
        hw = ""
79
        if use_hardware:
80
            hardware_type_nodes = node.findall(HARDWARE_TYPE)
81
            hardware_types = [ht.get(NAME) for ht in hardware_type_nodes]
82
            hw_match, hw = matches_hardware(hardware, hardware_types)
83
            if available and hw_match:
84
                good_node = True
85
        else:
86
            if available and matches(name, filters, nodes):
87
                good_node = True
88

    
89
        if good_node:
90
            domain = component_id.split("+")[1]
91
            hostname = "{}.{}".format(name, domain)
92
            xml += t.node_template % \
93
                (name, component_id, xs + col * w, ys + row * h, name, hw)
94
            xmlnetwork += t.network_template % (name)
95
            if dump_file != "":
96
                df.write("%s\n" % name)
97
            n += 1
98
    return n, xml, xmlnetwork
99

    
100

    
101
parser = ArgumentParser()
102
parser.add_argument("-t", "--testbed", dest="testbed",
103
                    default="wall1", action="store", metavar="TESTBED",
104
                    help="Comma separated list of testbeds to use [default: "
105
                         "%(default)s]")
106
parser.add_argument("-f", "--filter", dest="filter",
107
                    default="", action="store", metavar="List of filters",
108
                    help="Comma separated list of node prefixes [default: %("
109
                         "default)s]")
110
parser.add_argument("-w", "--hardware", dest="hardware",
111
                    default="", action="store", metavar="List of hardware",
112
                    help="Comma separated list of hardware types [default: %("
113
                         "default)s]")
114
parser.add_argument("-n", "--nodes", dest="nodes",
115
                    default="", action="store", metavar="List of nodes",
116
                    help="Comma separated list of nodes [default: %("
117
                         "default)s] This argument has precedence over "
118
                         "--filter")
119
parser.add_argument("-d", "--dump", dest="dump",
120
                    default="", action="store", metavar="FILENAME",
121
                    help="Output file where to store the list of available "
122
                         "nodes. If not specified, no file will be written")
123
args = parser.parse_args()
124

    
125
if args.filter and args.hardware:
126
    print("Cannot use the --filter and the --hardware options together")
127
    sys.exit(1)
128

    
129
testbeds = args.testbed.split(",")
130
use_hardware = False
131
if args.filter:
132
    filters = args.filter.split(",")
133
else:
134
    filters = []
135

    
136
if args.nodes:
137
    nodes = args.nodes.split(",")
138
else:
139
    nodes = []
140

    
141
if args.hardware:
142
    use_hardware = True
143
    hardware = args.hardware.split(",")
144
else:
145
    hardware = []
146

    
147
dump_file = args.dump
148

    
149
t = __import__('%stemplates' % testbeds[0], globals(), locals(),
150
               [NODE_TEMPLATE, HEADER_TEMPLATE, FOOTER_TEMPLATE, NETWORK_HEADER, NETWORK_FOOTER, NETWORK_TEMPLATE])
151

    
152
df = None
153
if dump_file != "":
154
    df = open(dump_file, "w")
155

    
156
print(t.header_template)
157

    
158
n = 0
159
for testbed in testbeds:
160
    n, xmlnode,xmlnetwork = fetch_nodes(testbed, n, use_hardware, filters, nodes, hardware,
161
                         dump_file, df)
162
    print(xmlnode)
163
    print(t.network_header)
164
    print(xmlnetwork)
165
    print(t.network_footer)
166

    
167
print(t.footer_template)
168

    
169
if dump_file != "":
170
    df.close()