Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # |
| 3 | # Copyright 2018 Mirantis, Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | |
| 18 | """Prepare metadata python module |
| 19 | |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 20 | The module is aimed to prepare system files (networking configs etc) |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 21 | based on lab metadata. |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 22 | Shell environment variables can be used in the metadata as Jinja2 variables. |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 23 | |
| 24 | Example: |
| 25 | python prepare-metadata --metadata-file '/etc/lab-metadata.yaml' |
| 26 | |
| 27 | Example of lab-metadata.yaml |
| 28 | |
| 29 | '52:54:00:10:94:78': |
| 30 | write_files: |
| 31 | - path: '/tmp/123.yaml' |
| 32 | content: | |
| 33 | foo: bar |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 34 | bee: {{ PUBLIC_INTERFACE_IP }} |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 35 | |
| 36 | Attributes: |
| 37 | metadata-file - The file with metadata |
| 38 | """ |
| 39 | |
| 40 | |
| 41 | __version__ = '1.0' |
| 42 | |
| 43 | import argparse |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 44 | import jinja2 |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 45 | import os |
| 46 | import yaml |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 47 | import netifaces |
| 48 | import sys |
| 49 | |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 50 | |
| 51 | def main(): |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 52 | |
| 53 | parser = argparse.ArgumentParser( |
| 54 | description=('Render system files based on metadata') |
| 55 | ) |
| 56 | |
| 57 | group = parser.add_argument_group() |
| 58 | group.add_argument( |
| 59 | '--metadata-file', |
| 60 | help='The path to metadata file.', |
| 61 | required=True |
| 62 | ) |
| 63 | args = parser.parse_args() |
| 64 | |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 65 | metadata = yaml.safe_load(render_template(args.metadata_file)) |
| 66 | |
Vasyl Saienko | c719b5c | 2020-03-05 19:14:26 +0200 | [diff] [blame] | 67 | if not metadata: |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 68 | print("The metadata is empty") |
Vasyl Saienko | c719b5c | 2020-03-05 19:14:26 +0200 | [diff] [blame] | 69 | return |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 70 | node_meta = get_node_metadata(metadata) |
| 71 | if node_meta is not None: |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 72 | print(f"Processing node_metadata: {node_meta}") |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 73 | create_files(node_meta.get('write_files', [])) |
| 74 | else: |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 75 | print("No matches to MACs for node_metadata found") |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 76 | |
| 77 | def get_interface_mac(iface_name): |
| 78 | mac = None |
| 79 | ifaddresses = netifaces.ifaddresses(iface_name) |
| 80 | link = ifaddresses.get(netifaces.AF_LINK, []) |
| 81 | if link: |
| 82 | return link[0]['addr'] |
| 83 | |
| 84 | def get_node_macs(): |
| 85 | ifaces = netifaces.interfaces() |
| 86 | macs = [get_interface_mac(iface_name) for iface_name in ifaces] |
| 87 | return [mac for mac in macs if mac is not None] |
| 88 | |
| 89 | def get_node_metadata(metadata): |
| 90 | for mac in get_node_macs(): |
| 91 | if mac in metadata: |
| 92 | return metadata[mac] |
| 93 | |
| 94 | def create_files(files_meta): |
| 95 | for file_meta in files_meta: |
| 96 | path = file_meta['path'] |
| 97 | content = file_meta['content'] |
| 98 | permissions = int(str(file_meta.get('permissions', '644')), base=8) |
| 99 | with open(path, "w") as f: |
| 100 | f.write(content) |
| 101 | os.chmod(path, permissions) |
| 102 | |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 103 | def render_template(file_path): |
| 104 | """Render a Jinja2 template file |
| 105 | |
| 106 | In the template: |
| 107 | {{ SOME_ENV_NAME }} : Insert an environment variable into the template |
| 108 | |
| 109 | :param file_path: str, path to the jinja2 template |
| 110 | """ |
| 111 | print("Reading template {0}".format(file_path)) |
| 112 | |
| 113 | path, filename = os.path.split(file_path) |
| 114 | environment = jinja2.Environment( |
| 115 | loader=jinja2.FileSystemLoader([path, os.path.dirname(path)], |
| 116 | followlinks=True)) |
| 117 | template = environment.get_template(filename).render(os.environ) |
| 118 | |
| 119 | return template |
| 120 | |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 121 | if __name__ == '__main__': |
| 122 | try: |
| 123 | main() |
| 124 | except Exception as e: |
Dennis Dmitriev | b12d234 | 2020-03-12 19:36:24 +0200 | [diff] [blame^] | 125 | print(f"Failed to apply image layout: {e}") |
Vasyl Saienko | f9ee158 | 2020-03-02 16:53:41 +0200 | [diff] [blame] | 126 | sys.exit(1) |