Enhance config scripts 05/11505/2
authordemx8as6 <martin.skorupski@highstreet-technologies.com>
Thu, 6 Jul 2023 14:09:11 +0000 (14:09 +0000)
committerMartin Skorupski <martin.skorupski@highstreet-technologies.com>
Thu, 6 Jul 2023 14:13:34 +0000 (14:13 +0000)
- method 'get_environment_variable(name)' is now recursive

Issue-ID: OAM-347
Change-Id: I49b311ebfd26940a52c7b38221e7457790efe1cf
Signed-off-by: demx8as6 <martin.skorupski@highstreet-technologies.com>
solution/adopt_to_environment.py [new file with mode: 0755]
solution/network/config.py
solution/smo/common/identity/config.py

diff --git a/solution/adopt_to_environment.py b/solution/adopt_to_environment.py
new file mode 100755 (executable)
index 0000000..0993659
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+################################################################################
+# Copyright 2023 highstreet technologies GmbH
+#
+# Licensed under the Apache License, Version 2.0 (the 'License');
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an 'AS IS' BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import os
+import argparse
+
+default_ip_address = 'aaa.bbb.ccc.ddd'
+default_http_domain = 'smo.o-ran-sc.org'
+
+script_name = os.path.basename(__file__)
+directory_path = os.path.dirname(__file__)
+file_extensions = ['.env', '.yaml', '.json']
+
+parser = argparse.ArgumentParser(script_name)
+required = parser.add_argument_group('required named arguments')
+required.add_argument("-i", "--ip_address", help="The remote accessable IP address of this system.", type=str, required=True)
+parser.add_argument("-d", "--http_domain", help="The http domain. Default is " + default_http_domain + ".", type=str, default=default_http_domain)
+parser.add_argument("-r", "--revert", help="Reverts the previous made changes.", action='store_true')
+args = parser.parse_args()
+
+def find_replace(directory, find_text, replace_text, extensions):
+    for root, dirs, files in os.walk(directory):
+        for file_name in files:
+            file_path = os.path.join(root, file_name)
+            file_ext = os.path.splitext(file_name)[1]
+            if file_ext in extensions or file_name in extensions:
+                with open(file_path, 'r') as file:
+                    content = file.read()
+                    if find_text in content:
+                        updated_content = content.replace(find_text, replace_text)
+                        with open(file_path, 'w') as file:
+                            file.write(updated_content)
+                        print(f"Replaced '{find_text}' with '{replace_text}' in '{file_path}'")
+
+if args.revert == False:
+    # replace ip
+    find_replace(directory_path, default_ip_address, args.ip_address, file_extensions)
+
+    # replace domain
+    if not args.http_domain == default_http_domain:
+      find_replace(directory_path, default_http_domain, args.http_domain, file_extensions)
+else:
+    # revert back ip
+    find_replace(directory_path, args.ip_address, default_ip_address, file_extensions)
+
+    # revert back domain
+    if not args.http_domain == default_http_domain:
+      find_replace(directory_path, args.http_domain, default_http_domain, file_extensions)
index 704954d..3bee9be 100644 (file)
@@ -19,6 +19,7 @@
 import os
 import sys
 import json
+import re
 import requests
 import subprocess
 import pathlib
@@ -26,11 +27,21 @@ from jproperties import Properties
 
 def get_environment_variable(name):
     configs = Properties()
-    path = pathlib.Path( os.path.dirname(os.path.abspath(__file__)) )
+    path = pathlib.Path(os.path.dirname(os.path.abspath(__file__)))
     env_file = str(path.absolute()) + '/.env'
     with open(env_file, "rb") as read_prop:
         configs.load(read_prop)
-    return configs.get(name).data
+    value = configs.get(name).data
+
+    regex = r"\$\{([^\}]+)\}"
+    matches = re.finditer(regex, value)
+    while True:
+        match = next(matches, None)
+        if match is None:
+            break
+        inner = get_environment_variable(match.group(1))
+        value = value.replace("${" + match.group(1) + "}", inner )
+    return value
 
 dockerFilter = subprocess.check_output("docker ps --format '{{.Names}}'", shell=True)
 containers = dockerFilter.splitlines()
index 22ef641..5a8bf44 100644 (file)
@@ -23,6 +23,7 @@ import json
 import time
 import getpass
 import requests
+import re
 import warnings
 from jproperties import Properties
 from typing import List
@@ -36,7 +37,17 @@ def get_environment_variable(name):
     env_file = str(path.parent.absolute()) + '/.env'
     with open(env_file, "rb") as read_prop:
         configs.load(read_prop)
-    return configs.get(name).data
+    value = configs.get(name).data
+
+    regex = r"\$\{([^\}]+)\}"
+    matches = re.finditer(regex, value)
+    while True:
+        match = next(matches, None)
+        if match is None:
+            break
+        inner = get_environment_variable(match.group(1))
+        value = value.replace("${" + match.group(1) + "}", inner )
+    return value
 
 
 def load_arguments(args: List[str]) -> tuple:
@@ -63,6 +74,7 @@ def load_arguments(args: List[str]) -> tuple:
 
 def isReady(timeoutSeconds=180):
     url = getBaseUrl()
+    print(f'url={url}')
     while timeoutSeconds > 0:
         try:
             response = requests.get(url, verify=False, headers={})