#!/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 # 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[a-zA-Z0-9_\-]+):(?P[a-zA-Z0-9_\-]+)=(?P.*)$") 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()