aboutsummaryrefslogtreecommitdiff
path: root/prepare-config.py
blob: ab0c70a66d9b286c7185a449897518c1add43b3f (plain)
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
#!/usr/bin/env python
import re
import sys
from string import Template
from optparse import OptionParser

TEMPLATE_PATH = "templates/"
CONFIG_NAME = "config.py"
CONFIG_PATH = TEMPLATE_PATH + CONFIG_NAME
LAVA_CACHE_PATH = "/tmp/lava_multi_node_cache.txt"
IPADDR_FILE = "IPADDR"
IPADDR = "-ipaddr"

if __name__ == '__main__':
    usage = "usage: %prog [OPTIONS]"
    parser = OptionParser(usage=usage)
    parser.add_option("-p", "--prefix", dest="prefix",
                  help="Signal prefix used in the multinode job to retrieve IP address")
    (options, args) = parser.parse_args()

    if not options.prefix:
        parser.error("Prefix missing")

    lava_cache_file = open(LAVA_CACHE_PATH, 'r')
    lava_cache_regexp = re.compile("^(?P<device_id>[a-zA-Z0-9_\-]+):(?P<key>[a-zA-Z0-9_\-]+)=(?P<value>.*)$")
    ipaddr = None
    for line in lava_cache_file.readlines()
        print line
        lava_cache_match = lava_cache_regexp.search(line)
        if lava_cache_match:
            if lava_cache_match.group("key") == options.prefix + IPADDR
                ipaddr = lava_cache_match.group("value")
                ipaddr_file = open(IPADDR_FILE, "w")
                ipaddr_file.write(ipaddr)
                ipaddr_file.close()
    lava_cache_file.close()
    if not ipaddr:
        sys.exit(1)
    config = open(CONFIG_PATH, "r")
    config_template = Template(config.read())
    config.close()
    dest_config = open(CONFIG_NAME, "w")
    dest_config.write(config_template.safe_substitute(ipaddr=ipaddr + ":5555"))
    dest_config.close()