ea92aa7cdcef6e221162e204b39a77175925cf11
[releng/global-jjb.git] / jenkins-admin / create_jenkins_clouds_openstack_yaml.py
1 #!/usr/bin/env python
2
3 import argparse
4 import configparser
5 import glob
6 import os
7 import sys
8
9 from jinja2 import Environment
10 from jinja2 import FileSystemLoader
11 from jinja2 import Template
12
13 #Template section
14 lookuptable = {
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"
52 }
53 maintemplate = """\
54 ---
55 jenkins:
56   clouds:
57     - openstack:
58         credentialsId: {{ cloud_credential_id }}
59         endPointUrl: {{ cloud_url }}
60         ignoreSsl: {{ cloud_ignore_ssl }}
61         name: {{ cloud_name }}
62         slaveOptions:
63           availabilityZone: {{ availability_zone }}
64           bootSource:
65             volumeFromImage:
66               name: {{ image_name }}
67               volumeSize: {{ volume_size }}
68           fsRoot: {{ fs_root }}
69           hardwareId: {{ hardware_id }}
70 {%- if is_sandbox is defined %}
71           instanceCap: {{ sandbox_cap }}
72 {%- else %}
73           instanceCap: {{ instance_cap }}{% endif %}
74           keyPairName: {{ key_pair_name }}
75           launcherFactory:
76             ssh:
77               credentialsId: {{ key_pair_name }}
78           networkId: {{ network_id }}
79           retentionTime: {{ retention_time }}
80           userDataId: {{ user_data_id }}
81         templates:
82 """
83 machinetemplate = """\
84           - labels: {{ labels }}
85             name: {{ name_prefix }}-{{ labels }}
86             slaveOptions:
87               bootSource:
88                 volumeFromImage:
89                   name: {{ image_name }}
90                   volumeSize: {{ volume_size }}
91 {%- if hardware_id  %}
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 }}
99 {%- else %}
100               retentionTime: 0{% endif %}
101 """
102 footertemplate = """\
103         zone: {{ cloud_zone}}
104 """
105
106 #Command line args section
107 def dir_path(path):
108     if os.path.isdir(path):
109         return path
110     else:
111         raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
112
113 def parse_arguments():
114     parser = argparse.ArgumentParser(
115         description='Create jcasc yaml from path to jenkins config dir.')
116
117
118     parser.add_argument('--path', type=dir_path,
119                         help="Path to jenkins-admin directory")
120     parser.add_argument('--name', type=str,
121                         help="cloud name IE: cattle")
122
123     parser.add_argument(
124         "-s", "--sandbox",
125         help = "Is configuration being created for a sandbox",
126         dest = "sandbox", action = "store_true"
127     )
128
129     return parser.parse_args()
130
131 parsed_args = parse_arguments()
132 path = (parsed_args.path)
133 path = ("{}**/*.cfg".format(path))
134
135 #sandbox switch section
136 section_cloud = {}
137 name_prefix = "prd"
138 if parsed_args.sandbox:
139     name_prefix = "snd"
140     section_cloud.update(is_sandbox=True)
141
142 #Config parser from merged files section
143 class Iterfiles:
144     def __init__(self, filename):
145         self.filename = filename
146
147     def iterf(self):
148         shortname = os.path.basename(filename)
149         shortname1 = os.path.splitext(shortname)[0]
150         return (self.filename, shortname1)
151
152 class Openfile:
153     def __init__(self, filename, shortname1):
154         self.filename = filename
155         self.shortname1 = shortname1
156     def openf(self):
157         file = open(self.filename, encoding="utf_8")
158         config.read_file(add_section_header(file, self.shortname1), source=self.filename)
159         return config
160
161 #cfg files are not real ini files, need to add section headers.
162 def add_section_header(properties_file, header_name):
163     yield '[{}]\n'.format(header_name)
164     for line in properties_file:
165         yield line
166
167 config = configparser.ConfigParser()
168 for filename in glob.iglob(path, recursive=True):
169     #This just returns the filename you sent it and a shortname
170     # dumb but just messing around with classes.
171     r1 = Iterfiles(filename)
172     r3 = r1.iterf()
173     #now i send the file name and the shortname to Openfile class
174     #which builds up a big configparser with sections divided by shortname
175     r5 = Openfile(r3[0], r3[1])
176     #config_parser_merged is the configparser object with all the configs from *.cfg
177     config_parser_merged = r5.openf()
178
179
180 # Global cloud config section
181 for section in config_parser_merged.sections():
182     if section == "cloud":
183         afinal = (config.items(section))
184         name = parsed_args.name
185         final = (*afinal, ("cloud_name", name))
186
187
188 for index, _ in enumerate(final):
189     a = final[index][0]
190     b = final[index][1]
191     for x, y in lookuptable.items():
192         if b == x:
193             b = y
194     section_cloud[a] = b
195
196 j2_template = Template(maintemplate)
197 print(j2_template.render(section_cloud))
198
199
200 # All machines section
201 for section in config_parser_merged.sections():
202     if section != "cloud":
203         machine = (config.items(section))
204         section_all_machines = {}
205         for index, _ in enumerate(machine):
206             a = machine[index][0]
207             b = machine[index][1]
208             for x, y in lookuptable.items():
209                 if b == x:
210                     b = y
211             #a = format(a)
212             section_all_machines[a] = b
213
214
215         #Default volume size of 10
216         if not "volume_size" in section_all_machines:
217             section_all_machines.update(volume_size="10")
218         if not "labels" in section_all_machines:
219             print("LABELS not Set in builder config")
220             print(section_all_machines)
221             exit(1)
222
223
224         j2_template = Template(machinetemplate)
225         section_all_machines.update(name_prefix=name_prefix)
226         print(j2_template.render(section_all_machines))
227
228
229 #Footer section
230 j2_template = Template(footertemplate)
231 print(j2_template.render(section_cloud))