summaryrefslogtreecommitdiffstats
path: root/modules/opnfv/installer_adapters/joid
AgeCommit message (Expand)AuthorFilesLines
2016-11-16Move installer adapters to the new modules directoryjose.lausuch2-0/+32
#n58'>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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
import os
import yaml
import sys
from Cheetah.Template import Template


def init(file):
    with open(file) as fd:
        return yaml.load(fd)


def decorator(func):
    def wrapter(s, seq):
        host_list = s.get('hosts', [])
        result = []
        for host in host_list:
            s = func(s, seq, host)
            if not s:
                continue
            result.append(s)
        if len(result) == 0:
            return ""
        else:
            return "\"" + seq.join(result) + "\""
    return wrapter


@decorator
def hostnames(s, seq, host=None):
    return host.get('name', '')


@decorator
def hostroles(s, seq, host=None):
    return "%s=%s" % (host.get('name', ''), ','.join(host.get('roles', [])))


@decorator
def hostmacs(s, seq, host=None):
    return host.get('mac', '')


def export_dha_file(s, dha_file, conf_dir, ofile):
    env = {}
    env.update(s)
    if env.get('hosts', []):
        env.pop('hosts')

    env.update({'TYPE': s.get('TYPE', "virtual")})
    env.update({'FLAVOR': s.get('FLAVOR', "cluster")})
    env.update({'HOSTNAMES': hostnames(s, ',')})
    env.update({'HOST_ROLES': hostroles(s, ';')})
    env.update({'DHA': dha_file})

    value = hostmacs(s, ',')
    if len(value) > 0:
        env.update({'HOST_MACS': value})

    os.system("echo \#config file deployment parameter > %s" % ofile)
    for k, v in env.items():
        os.system("echo 'export %s=${%s:-%s}' >> %s" % (k, k, v, ofile))


def export_reset_file(s, tmpl_dir, output_dir, output_file):
    tmpl_file_name = s.get('POWER_TOOL', '')
    if not tmpl_file_name:
        return

    tmpl = Template(
        file=os.path.join(
            tmpl_dir,
            'power',
            tmpl_file_name +
            '.tmpl'),
        searchList=s)

    reset_file_name = os.path.join(output_dir, tmpl_file_name + '.sh')
    with open(reset_file_name, 'w') as f:
        f.write(tmpl.respond())

    os.system(
        "echo 'export POWER_MANAGE=%s' >> %s" %
        (reset_file_name, output_file))

if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("parameter wrong%d %s" % (len(sys.argv), sys.argv))
        sys.exit(1)

    _, dha_file, conf_dir, tmpl_dir, output_dir, output_file = sys.argv

    if not os.path.exists(dha_file):
        print("%s is not exist" % dha_file)
        sys.exit(1)

    data = init(dha_file)

    export_dha_file(
        data,
        dha_file,
        conf_dir,
        os.path.join(
            output_dir,
            output_file))
    export_reset_file(
        data,
        tmpl_dir,
        output_dir,
        os.path.join(
            output_dir,
            output_file))

    sys.exit(0)