Ejemplo n.º 1
0
def backup_description():

    heap_file = open("./heap.yaml")
    hosts_file = open("./hosts.yaml")
    heap_cfg = list(yaml.load_all(heap_file))
    hosts_cfg = list(yaml.load_all(hosts_file))

    backup_file = open("./backup_description.yaml", 'w')

    yml = YAML()
    yml.explicit_start = True
    yml.Loader = ruamel.yaml.RoundTripLoader
    yml.Dumper = ruamel.yaml.RoundTripDumper

    for j in heap_cfg:
        for i in hosts_cfg:
            if j['HOST'] == i['HOST']:
                yaml_str = "VM: " + j['VM'] + "\nHOST: " + j['HOST']\
                           + "\nDESC: " + api_get_vm_desc(
                            j['HOST'], i['USER'], i['PASSWORD'], j['VM']
                            ) + "\n"
                data = yml.load(yaml_str)
                yml.dump(data, backup_file)
                break

    heap_file.close()
    hosts_file.close()
    backup_file.close()
Ejemplo n.º 2
0
def get_yaml_loader(typ):
    if typ == "rt":
        loader = YAML(typ=typ)
        loader.preserve_quotes = True
        loader.default_flow_style = False
        loader.indent(sequence=4, offset=2)
        loader.width = 1000
        loader.Representer = MyRepresenter
        loader.Loader = ruamel.yaml.RoundTripLoader
    elif typ == "safe":
        loader = YAML(typ=typ)
    return loader
Ejemplo n.º 3
0
def parse_xls(XLS_FILE, VM_NAME_SHEET, VM_NAME_COLUMN, VM_DESCRIPTION_SHEET,
              VM_DESCRIPTION_COLUMN, ENCODE, HOST_REPRESENT):

    # get VM and description from XLS
    tbl = []
    book = xlrd.open_workbook(XLS_FILE)
    vm_sheet = book.sheet_by_index(VM_NAME_SHEET)  # Switching to sheet
    for rownum in range(vm_sheet.nrows)[1:]:  # [1:] - skipping 1-st title row
        row = vm_sheet.row_values(rownum)
        tbl.append(row[VM_NAME_COLUMN])
    for j in range(len(tbl)):
        vm_description_sheet = book.sheet_by_index(VM_DESCRIPTION_SHEET)
        # (j+1) - skipping 1-st title row
        row = vm_description_sheet.row_values(j+1)
        tbl[j] = tbl[j], row[VM_DESCRIPTION_COLUMN]
    # ------------------------------------------------------------------------
    # get info from MariaDB
    mariadb_connection = mysql.connector.connect(user='******',
                                                 database='DBNAME')
    cursor = mariadb_connection .cursor()
    if HOST_REPRESENT == "hostname":
        query = ("select name, parent_title from vEnvironments where
                 parent_title != 'None'")
    if HOST_REPRESENT == "ip":
        query = ("select E.name, H.ip_address from vEnvironments E inner join
                 vHosts H on E.parent_title=H.hostname")
    cursor.execute(query)
    dbresult = cursor.fetchall()

    cursor.close()
    mariadb_connection.close()
    # ------------------------------------------------------------------------
    # convert tuple of tuples to list of lists
    dbresult = list(list(x) for x in dbresult)
    # decode every element to "utf-8"
    for i in range(len(dbresult)):
        dbresult[i][0] = dbresult[i][0].decode(ENCODE)
        dbresult[i][1] = dbresult[i][1].decode(ENCODE)
    # ------------------------------------------------------------------------
    # heap - is array VM, host, description
    heap = []
    for j in range(len(tbl)):
        for i in range(len(dbresult)):
            if tbl[j][0] == dbresult[i][0]:
                heap.append((tbl[j][0], dbresult[i][1], tbl[j][1]))
                break
            if i == (len(dbresult)-1):
                heap.append((tbl[j][0], "NULL", tbl[j][1]))
    # ------------------------------------------------------------------------
    # get longest length of VM column (need for formatting output)
    longest_first = 0
    for j in range(len(heap)):
        if len(heap[j][0]) > longest_first:
            longest_first = len(heap[j][0])

    # get longest lengtn of host column (need for formatting output)
    longest_second = 0
    for j in range(len(heap)):
        if len(heap[j][1]) > longest_second:
            longest_second = len(heap[j][1])

    # print VM (from XLS), host (from MariaDB; if hos not found -
    # then print 'NULL'), description (from XLS)
    print "This table shows VMs, hosts and description. If the host is shown\
          as NULL, then for this VM we can not change the description."
    for j in range(len(heap)):
        if heap[j][1] == "NULL":
            print "{0:{1}} {2} {3}".format(heap[j][0], longest_first, (
                   "\x1b[0;32;41m" + "NULL" + (" "*(longest_second-4)) +
                   "\x1b[0;32;40m"), heap[j][2].encode(ENCODE))
        else:
            print "{0:{1}} {2:{3}} {4}".format(
                   heap[j][0], longest_first, heap[j][1], longest_second,
                   heap[j][2].encode(ENCODE))
    # ------------------------------------------------------------------------
    # get unique hosts from heap (for which we need passwords)
    hosts = []
    for j in range(len(heap)):
        if heap[j][1] == "NULL":
            continue
        if heap[j][1] not in hosts:
            hosts.append(heap[j][1])

    hosts = sorted(hosts)  # sort hosts alpabetically

    # print hosts
    print "For these hosts you need to specify the credentials in the\
          configuration file hosts.yaml"
    for j in hosts:
        print j
    # ------------------------------------------------------------------------
    # write config for hosts: hosts.yaml
    config_file = open("./hosts.yaml", 'w')

    yaml = YAML()
    yaml.explicit_start = True
    yaml.Loader = ruamel.yaml.RoundTripLoader
    yaml.Dumper = ruamel.yaml.RoundTripDumper

    for j in hosts:
        yaml_str = "HOST: "+j+"\nUSER: USERNAME\nPASSWORD: \n"
        data = yaml.load(yaml_str)
        yaml.dump(data, config_file)
    config_file.close()
    # ------------------------------------------------------------------------
    # write heap to heap.yaml (for using in key -set)
    config_file = open("./heap.yaml", 'w')

    yaml = YAML()
    yaml.explicit_start = True
    yaml.Loader = ruamel.yaml.RoundTripLoader
    yaml.Dumper = ruamel.yaml.RoundTripDumper

    for j in range(len(heap)):
        if heap[j][1] == "NULL":
            continue
        yaml_str = "VM: " + heap[j][0] + "\nHOST: " + heap[j][1] + "\nDESC: "\
                   + heap[j][2] + "\n"
        data = yaml.load(yaml_str)
        yaml.dump(data, config_file)
    config_file.close()
    # ------------------------------------------------------------------------
    return heap
Ejemplo n.º 4
0
def main(docname):

    with open(docname, "r") as fi:
        lines = fi.readlines()
    context = {}
    rest_lines = []
    for line in lines:
        # print(line)
        if "{%" in line:
            set_expr = re.search("{%(.*)%}", line)
            set_expr = set_expr.group(1)
            set_expr = set_expr.replace("set", "", 1).strip()
            exec(set_expr, globals(), context)
        else:
            rest_lines.append(line)

    yaml = YAML(typ="rt")
    yaml.preserve_quotes = True
    yaml.default_flow_style = False
    yaml.indent(sequence=4, offset=2)
    yaml.width = 1000
    yaml.Representer = MyRepresenter
    yaml.Loader = ruamel.yaml.RoundTripLoader

    result_yaml = CommentedMap()
    result_yaml["context"] = context

    def has_selector(s):
        return s.strip().endswith("]")

    quoted_lines = []
    for line in rest_lines:
        if has_selector(line):
            selector_start = line.rfind("[")
            selector_end = line.rfind("]")
            selector_content = line[selector_start + 1 : selector_end]

            if line.strip().startswith("-"):
                line = (
                    line[: line.find("-") + 1]
                    + f" sel({selector_content}): "
                    + line[
                        line.find("-") + 1 : min(line.rfind("#"), line.rfind("["))
                    ].strip()
                    + "\n"
                )
        quoted_lines.append(line)
    rest_lines = quoted_lines

    def check_if_quoted(s):
        s = s.strip()
        return s.startswith('"') or s.startswith("'")

    quoted_lines = []
    for line in rest_lines:
        if "{{" in line:
            # make sure that jinja stuff is quoted
            if line.find(":") != -1:
                idx = line.find(":")
            elif line.strip().startswith("-"):
                idx = line.find("-")
            rest = line[idx + 1 :]

            if not check_if_quoted(rest):
                if "'" in rest:
                    rest = rest.replace("'", '"')

                line = line[: idx + 1] + f" '{rest.strip()}'\n"
        quoted_lines.append(line)
    rest_lines = quoted_lines

    skips, wo_skip_lines = [], []
    for line in rest_lines:
        if line.strip().startswith("skip"):
            parts = line.split(":")
            rhs = parts[1].strip()
            if rhs.startswith("true"):
                selector_start = line.rfind("[")
                selector_end = line.rfind("]")
                selector_content = line[selector_start + 1 : selector_end]
                skips.append(selector_content)
            else:
                print("ATTENTION skip: false not handled!")
        else:
            wo_skip_lines.append(line)

    rest_lines = wo_skip_lines
    result_yaml.update(
        ruamel.yaml.load("".join(rest_lines), ruamel.yaml.RoundTripLoader)
    )

    if len(skips) != 0:
        result_yaml["build"]["skip"] = skips

    if result_yaml.get("outputs"):
        for o in result_yaml["outputs"]:
            name = o["name"]
            package = {"name": name}
            del o["name"]
            if o.get("version"):
                package["version"] = o["version"]
                del o["version"]

            build = {}
            if o.get("script"):
                build["script"] = o["script"]
                del o["script"]

            o["package"] = package
            o["build"] = build

        for d in result_yaml["outputs"]:
            print(order_output_dict(d))
        result_yaml["outputs"] = [order_output_dict(d) for d in result_yaml["outputs"]]

    from io import StringIO

    output = StringIO()
    yaml.dump(result_yaml, output)

    # Hacky way to insert an empty line after the context-key-object
    context_output = StringIO()
    yaml.dump(context, context_output)
    context_output = context_output.getvalue()
    context_output_len = len(context_output.split("\n"))

    final_result = output.getvalue()
    final_result_lines = final_result.split("\n")
    final_result_lines.insert(context_output_len, "")

    print("\n".join(final_result_lines))