aboutsummaryrefslogtreecommitdiff
path: root/prepare-config.py
blob: 3678ee4d5c9edad5e4560e7554040c93320c2c62 (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
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
#!/usr/bin/env python
#
# Workload Automation v2 for LAVA
#
# Copyright (C) 2014, Linaro Limited.
#
# 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 2
# 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.
#
# Author: Milosz Wasilewski <milosz.wasilewski@linaro.org>
#

import os
import re
import sys
from string import Template
from optparse import OptionParser

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

def isgoodipv4(s):
    pieces = s.split('.')
    if len(pieces) != 4: return False
    try: return all(0<=int(p)<256 for p in pieces)
    except ValueError: return False

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")
    parser.add_option("-c", "--config", dest="config",
                  help="Name of config.py template. Default is config.py",
                  default="config.py")
    (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")
                if ipaddr == "_MISSING_":
                    ipaddr = None
                else:
                    with open(IPADDR_FILE, "w") as ipaddr_file:
                        ipaddr_file.write(ipaddr)
    lava_cache_file.close()
    if not ipaddr:
        sys.exit(1)
    config = open(os.path.join(CONFIG_PATH, options.config), "r")
    config_template = Template(config.read())
    config.seek(0)
    linux = False
    for line in config:
        if line.startswith('device = ') and '_linux' in line:
            linux = True
    config.close()
    dest_config = open(CONFIG_NAME, "w")
    if isgoodipv4(ipaddr) and not linux:
        dest_config.write(config_template.safe_substitute(ipaddr=ipaddr + ":5555"))
    else:
        dest_config.write(config_template.safe_substitute(ipaddr=ipaddr))
    dest_config.close()