Use environment variables for the lab metadata as jinja2 variables
- All environment variables from instance_boot.sh can be used
in the "hardware_metadata" like:
hardware_metadata: |
'0c:c4:7a:33:26:7c':
write_files:
- path: '/etc/netplan/control_network.yaml'
content: |
network:
version: 2
ethernets:
eno1:
dhcp4: false
addresses:
- {{ PUBLIC_INTERFACE_IP }}/{{ PUBLIC_INTERFACE_NETMASK }}
https: //mirantis.jira.com/browse/PRODX-2630
Change-Id: I18389aa8c83cc8fea43acf022535baf17d321b3f
diff --git a/de/heat-templates/scripts/instance_boot.sh b/de/heat-templates/scripts/instance_boot.sh
index 09d916b..7a6d8da 100644
--- a/de/heat-templates/scripts/instance_boot.sh
+++ b/de/heat-templates/scripts/instance_boot.sh
@@ -1,5 +1,7 @@
#!/bin/bash
set -x
+# allow access to the local variables from prepare-metadata.py
+set -a
#
# Variables in this block are passed from heat template
@@ -402,9 +404,6 @@
download_bundles
workaround_default_forward_policy
;;
- os_only)
- echo "OS is installed"
- ;;
*)
echo "Usage: $0 {ucp|master|worker}"
exit 1
diff --git a/de/heat-templates/scripts/prepare-metadata.py b/de/heat-templates/scripts/prepare-metadata.py
index cd592c4..70924ca 100644
--- a/de/heat-templates/scripts/prepare-metadata.py
+++ b/de/heat-templates/scripts/prepare-metadata.py
@@ -17,8 +17,9 @@
"""Prepare metadata python module
-The module is aimed to prepare system files files (networking configs etc)
+The module is aimed to prepare system files (networking configs etc)
based on lab metadata.
+Shell environment variables can be used in the metadata as Jinja2 variables.
Example:
python prepare-metadata --metadata-file '/etc/lab-metadata.yaml'
@@ -30,6 +31,7 @@
- path: '/tmp/123.yaml'
content: |
foo: bar
+ bee: {{ PUBLIC_INTERFACE_IP }}
Attributes:
metadata-file - The file with metadata
@@ -39,20 +41,14 @@
__version__ = '1.0'
import argparse
+import jinja2
import os
import yaml
-import logging
import netifaces
import sys
-LOG = logging.getLogger(__name__)
-
def main():
- logging.basicConfig(
- format='%(asctime)s - %(levelname)s - %(message)s'
- )
- LOG.setLevel(logging.INFO)
parser = argparse.ArgumentParser(
description=('Render system files based on metadata')
@@ -66,17 +62,17 @@
)
args = parser.parse_args()
- with open(args.metadata_file) as f:
- metadata = yaml.safe_load(f)
+ metadata = yaml.safe_load(render_template(args.metadata_file))
+
if not metadata:
- LOG.info("The metadata is empty")
+ print("The metadata is empty")
return
node_meta = get_node_metadata(metadata)
if node_meta is not None:
- LOG.info(f"Processing node_metadata: {node_meta}")
+ print(f"Processing node_metadata: {node_meta}")
create_files(node_meta.get('write_files', []))
else:
- LOG.error("No matches to MACs for node_metadata found")
+ print("No matches to MACs for node_metadata found")
def get_interface_mac(iface_name):
mac = None
@@ -104,9 +100,27 @@
f.write(content)
os.chmod(path, permissions)
+def render_template(file_path):
+ """Render a Jinja2 template file
+
+ In the template:
+ {{ SOME_ENV_NAME }} : Insert an environment variable into the template
+
+ :param file_path: str, path to the jinja2 template
+ """
+ print("Reading template {0}".format(file_path))
+
+ path, filename = os.path.split(file_path)
+ environment = jinja2.Environment(
+ loader=jinja2.FileSystemLoader([path, os.path.dirname(path)],
+ followlinks=True))
+ template = environment.get_template(filename).render(os.environ)
+
+ return template
+
if __name__ == '__main__':
try:
main()
except Exception as e:
- LOG.exception(f"Failed to apply image layout: {e}")
+ print(f"Failed to apply image layout: {e}")
sys.exit(1)