aboutsummaryrefslogtreecommitdiff
path: root/prepare-config.py
blob: 397c7f5e6ff1b2ea783699806b976c246da7a432 (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
#!/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 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()