9 from jinja2 import Environment
10 from jinja2 import FileSystemLoader
11 from jinja2 import Template
15 "acumos-highcpu-4-avx": "c720c1f8-62e9-4695-823d-f7f54db46c86",
16 "lf-highcpu-2": "1051d06a-61ea-45e3-b9b4-93de92880b27",
17 "lf-highcpu-4": "35eb8e11-490f-4d1a-9f19-76091fc04547",
18 "lf-highcpu-8": "68af673f-54ee-4255-871c-158c18e4f643",
19 "lf-standard-1": "7d76cbb0-f547-4c2c-beaf-554f33832721",
20 "lf-standard-2": "ef454088-7839-42a0-bf23-5e0ab6386a27",
21 "lf-standard-4": "bd74e1e6-c2ed-475b-ab3f-2ce13936a215",
22 "lf-standard-8": "32d74024-8418-41b6-9675-b77816748148",
23 "odl-highcpu-2": "def1b86f-b7f8-4943-b430-4a0599170006",
24 "odl-highcpu-4": "0c8ec795-2ff8-4623-98cf-b4c1d92bb37c",
25 "odl-highcpu-8": "458d6499-e2c8-4580-aa88-a4a04a33ee25",
26 "odl-standard-1": "35800a3f-0c69-428d-b5cb-136d17d46c48",
27 "odl-standard-2": "8ead227a-acfe-4290-be70-fbab92e6dd2f",
28 "odl-standard-4": "f76fb18d-d5fb-4175-95c1-b29d8039d102",
29 "odl-standard-8": "ba38b1af-4f87-4e4e-860e-94e8329d0d78",
30 "v1-standard-1": "bbcb7eb5-5c8d-498f-9d7e-307c575d3566",
31 "v1-standard-2": "ca2a6e9c-2236-4107-8905-7ae9427132ff",
32 "v1-standard-4": "5cf64088-893b-46b5-9bb1-ee020277635d",
33 "v1-standard-8": "6eec77b4-2286-4e3b-b3f0-cac67aa2c727",
34 "v1-standard-16": "2f8730dd-7688-4b72-a512-99fb9a482414",
35 "v1-standard-32": "0da688af-bb0c-4116-a158-cbf37240a8b1",
36 "v1-standard-48": "69471d69-61fb-40dd-bdf3-e6b7f4e6daa3",
37 "v1-standard-64": "0c1d9008-f546-4608-9e8f-f8bdaec8dddd",
38 "v1-standard-96": "5741c775-92a4-4488-bd77-dd7b08e2be81",
39 "v1-standard-128": "e82d0a5b-8031-4526-9a5d-a15f7b4d48ff",
40 "v2-highcpu-1": "c04abb7a-2b61-4ed3-8ce8-6c40ad9df750",
41 "v2-highcpu-2": "03bdf34e-8905-46bc-a4b9-8dbf94b6e06d",
42 "v2-highcpu-4": "3b72e578-7875-4e0e-91b7-71ed292f3ca2",
43 "v2-highcpu-8": "221de281-95ec-414f-8e42-c86c9e0b318d",
44 "v2-highcpu-16": "ddd6863a-ef4f-475c-9aee-61d46898651d",
45 "v2-highcpu-32": "21dfb8a3-c472-4a2c-a8e1-4da8de415ff8",
46 "v2-standard-1": "52a01f6b-e660-48b5-8c06-5fb2a0fab0ec",
47 "v2-standard-2": "ac2c4d17-8d6f-4e3c-a9eb-57c155f0a949",
48 "v2-standard-4": "d9115351-defe-4fac-986b-1a1187e2c31c",
49 "v2-standard-8": "e6fe2e37-0e38-438c-8fa5-fc2d79d0a7bb",
50 "v2-standard-16": "9e4b01cd-6744-4120-aafe-1b5e17584919",
51 "v2-standard-360": "f0d27f44-a410-4f0f-9781-d722f5b5489e"
58 credentialsId: {{ cloud_credential_id }}
59 endPointUrl: {{ cloud_url }}
60 ignoreSsl: {{ cloud_ignore_ssl }}
63 availabilityZone: {{ availability_zone }}
66 name: {{ image_name }}
67 volumeSize: {{ volume_size }}
69 hardwareId: {{ hardware_id }}
70 {%- if is_sandbox is defined %}
71 instanceCap: {{ sandbox_cap }}
73 instanceCap: {{ instance_cap }}{% endif %}
74 keyPairName: {{ key_pair_name }}
77 credentialsId: {{ key_pair_name }}
78 networkId: {{ network_id }}
79 retentionTime: {{ retention_time }}
80 userDataId: {{ user_data_id }}
83 machinetemplate = """\
84 - labels: {{ labels }}
85 name: {{ name_prefix }}-{{ labels }}
89 name: {{ image_name }}
90 volumeSize: {{ volume_size }}
92 hardwareId: {{ hardware_id }}{% endif %}
93 {%- if instance_cap %}
94 instanceCap: {{ instance_cap }}{% endif %}
95 {%- if num_executors %}
96 numExectorts: {{ num_executors }}{% endif %}
97 {%- if retention_time %}
98 retentionTime: {{ retention_time }}
100 retentionTime: 0{% endif %}
102 footertemplate = """\
103 zone: {{ cloud_zone}}
106 #Command line args section
108 if os.path.isdir(path):
111 raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
113 def parse_arguments():
114 parser = argparse.ArgumentParser(description='Create jcasc yaml from path to jenkins config dir.')
115 parser.add_argument('--path', type=dir_path)
116 parser.add_argument('--sandbox', type=bool, default=False, help="Set to True for sandbox yaml generation")
117 return parser.parse_args()
119 parsed_args = parse_arguments()
120 path = (parsed_args.path)
121 path = ("{}**/*.cfg".format(path))
124 #sandbox switch section
127 if parsed_args.sandbox:
129 section_cloud.update(is_sandbox=True)
131 #Config parser from merged files section
133 def __init__(self, filename):
134 self.filename = filename
137 shortname = os.path.basename(filename)
138 shortname1 = os.path.splitext(shortname)[0]
139 return (self.filename, shortname1)
142 def __init__(self, filename, shortname1):
143 self.filename = filename
144 self.shortname1 = shortname1
146 file = open(self.filename, encoding="utf_8")
147 config.read_file(add_section_header(file, self.shortname1), source=self.filename)
150 #cfg files are not real ini files, need to add section headers.
151 def add_section_header(properties_file, header_name):
152 yield '[{}]\n'.format(header_name)
153 for line in properties_file:
156 config = configparser.ConfigParser()
157 for filename in glob.iglob(path, recursive=True):
158 #This just returns the filename you sent it and a shortname
159 # dumb but just messing around with classes.
160 r1 = Iterfiles(filename)
162 #now i send the file name and the shortname to Openfile class
163 #which builds up a big configparser with sections divided by shortname
164 r5 = Openfile(r3[0], r3[1])
165 #config_parser_merged is the configparser object with all the configs from *.cfg
166 config_parser_merged = r5.openf()
169 # Global cloud config section
170 for section in config_parser_merged.sections():
171 if section == "cloud":
172 final = (config.items(section))
175 for index, _ in enumerate(final):
178 for x, y in lookuptable.items():
183 j2_template = Template(maintemplate)
184 print(j2_template.render(section_cloud))
187 # All machines section
188 for section in config_parser_merged.sections():
189 if section != "cloud":
190 machine = (config.items(section))
191 section_all_machines = {}
192 for index, _ in enumerate(machine):
193 a = machine[index][0]
194 b = machine[index][1]
195 for x, y in lookuptable.items():
199 section_all_machines[a] = b
202 #Default volume size of 10
203 if not "volume_size" in section_all_machines:
204 section_all_machines.update(volume_size="10")
205 if not "labels" in section_all_machines:
206 print("LABELS not Set in builder config")
207 print(section_all_machines)
211 j2_template = Template(machinetemplate)
212 section_all_machines.update(name_prefix=name_prefix)
213 print(j2_template.render(section_all_machines))
218 j2_template = Template(footertemplate)
219 print(j2_template.render(section_cloud))