configure global env vars 75/66075/9
authorAric Gardner <agardner@linuxfoundation.org>
Mon, 16 Nov 2020 19:59:20 +0000 (14:59 -0500)
committerAric Gardner <agardner@linuxfoundation.org>
Tue, 17 Nov 2020 18:26:28 +0000 (13:26 -0500)
create_jenkins_global_env_vars.py
thakes jenkins_global_envvars.yaml from
global-vars-production|sandbox.sh
and puts them in the casc.d directory
(or a custom one)

Signed-off-by: Aric Gardner <agardner@linuxfoundation.org>
Change-Id: I95c3f7f268ba7146198c791bba5378e233f9b913

jenkins-admin/create_jenkins_clouds_openstack_yaml.py
jenkins-admin/create_jenkins_global_env_vars.py [new file with mode: 0755]

index bd94a9e..2d81a25 100755 (executable)
@@ -111,16 +111,20 @@ def dir_path(path):
         raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
 
 def parse_arguments():
-    parser = argparse.ArgumentParser(description='Create jcasc yaml from path to jenkins config dir.')
-    parser.add_argument('--path', type=dir_path)
-    parser.add_argument('--sandbox', type=bool, default=False, help="Set to True for sandbox yaml generation")
+    parser = argparse.ArgumentParser(
+        description='Create jcasc yaml from path to jenkins config dir.')
+
+
+    parser.add_argument('--path', type=dir_path,
+                        help="Path to jenkins-admin directory")
+    parser.add_argument('--sandbox', type=bool, default=False,
+                        help="Set to True for sandbox yaml generation")
     return parser.parse_args()
 
 parsed_args = parse_arguments()
 path = (parsed_args.path)
 path = ("{}**/*.cfg".format(path))
 
-
 #sandbox switch section
 section_cloud = {}
 name_prefix = "prd"
@@ -213,7 +217,6 @@ for section in config_parser_merged.sections():
         print(j2_template.render(section_all_machines))
 
 
-
 #Footer section
 j2_template = Template(footertemplate)
 print(j2_template.render(section_cloud))
diff --git a/jenkins-admin/create_jenkins_global_env_vars.py b/jenkins-admin/create_jenkins_global_env_vars.py
new file mode 100755 (executable)
index 0000000..927a665
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+
+import argparse
+import configparser
+import sys
+import os
+import ruamel.yaml
+yaml = ruamel.yaml.YAML()
+
+kvlist = []
+output = {}
+envdict = {"env":kvlist}
+
+def dir_path(path):
+    if os.path.isdir(path):
+        return path
+    else:
+        raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
+
+def parse_arguments():
+    casc_d_dir = "/var/lib/jenkins/casc.d/community.d"
+    parser = argparse.ArgumentParser(
+        description='Create jcasc yaml from path to jenkins config dir.')
+    parser.add_argument('--path', type=dir_path,
+                        help="Path to jenkins-admin directory")
+    parser.add_argument('--sandbox', type=bool, default=False,
+                        help="Set to True for sandbox yaml generation")
+    parser.add_argument(
+        "--outputvars",
+        type=argparse.FileType("w"),
+        default="{}/jenkins_global_envvars.yaml".format(casc_d_dir),
+        help="Optional custom location for jenkins_global_envvars.yaml",
+    )
+    return parser.parse_args()
+
+def global_vars(global_var_file):
+    with open(global_var_file) as myfile:
+        for line in myfile:
+            name, var = line.partition("=")[::2]
+            varstripped = var.strip()
+            kvlist.append({"key": name, "value": varstripped})
+    output.update({'jenkins':
+                   {'globalNodeProperties':
+                       [{'envVars':envdict}]
+                   }
+                  })
+    yaml.dump(output, parsed_args.outputvars)
+
+
+parsed_args = parse_arguments()
+path = (parsed_args.path)
+global_var_file = ("{}/global-vars-production.sh".format(parsed_args.path))
+if parsed_args.sandbox:
+    global_var_file = ("{}/global-vars-sandbox.sh".format(parsed_args.path))
+
+global_vars(global_var_file)