aboutsummaryrefslogtreecommitdiffstats
path: root/deploy/config_parse.py
blob: c7546eeb927aa128fb47646095231b496b56ce2d (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

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8
##############################################################################
# Copyright (c) 2016 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

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)