-
Notifications
You must be signed in to change notification settings - Fork 9
/
sclorg_sanity_tests.py
executable file
·358 lines (325 loc) · 15 KB
/
sclorg_sanity_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
import os
import sys
import requests
import yaml
import urllib3
import smtplib
import argparse
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from tempfile import TemporaryDirectory
from pathlib import Path
from typing import Dict, List
from shutil import rmtree
from git import Repo
from git.exc import GitCommandError
SCLORG_REPOS = {
# Format is 'repo_name' and 'package_name'
"s2i-perl-container": "perl",
"s2i-nodejs-container": "nodejs",
"s2i-php-container": "php",
"s2i-ruby-container": "ruby",
"s2i-python-container": "python",
"postgresql-container": "postgresql",
"redis-container": "redis",
"varnish-container": "varnish",
"mysql-container": "mysql",
"mariadb-container": "mariadb",
"nginx-container": "nginx"
}
OS_HOSTS = [
"fedora",
"c9s",
"c8s",
"rhel7",
"rhel8",
"rhel9"
]
# This is reference link to internal repository
EOL_GA_URL = "https://url.corp.redhat.com/application-streams-yaml"
DEVEL_REPO_URL = "https://url.corp.redhat.com/devel-images"
SANITY_LOG = "sanity_log.txt"
class SclOrgSanityChecker(object):
def __init__(self):
self.tmp_path_dir: Path
self.repo = ""
self.pkg_name = ""
self.app_stream: Dict = {}
self.cwd = os.getcwd()
self.tmp_path_repo: Path
self.report_text_filename: str = ""
self.args = self.parse_args()
self.data_dict: Dict = {}
self.failed_repos: List[str] = []
self.success_repos: List[str] = []
self.global_result_flag: bool = True
self.log_dir = os.getcwd()
self.debug_log = Path(self.log_dir) / SANITY_LOG
self.message = ""
def log_to_file(self, msg):
print(msg)
with open(self.debug_log, "a") as fw:
if isinstance(msg, Dict):
for key, value in msg.items():
fw.write(f"{key}:{value}")
fw.write("\n")
return
fw.write(msg)
fw.write("\n")
def update_message(self, msg):
self.message += f"{msg}" + "\n"
def write_to_textfile(self, msg, report_file):
self.log_to_file(msg)
with open(report_file, "a") as fw:
fw.write(msg)
fw.write("\n")
def create_tmp_dir(self):
self.log_to_file(msg="Create temporary directory for sanity checks.")
tmp_dir = TemporaryDirectory(suffix="sanity_tests")
self.tmp_path_dir = Path(tmp_dir.name)
def download_and_load_lifecycle_file(self):
urllib3.disable_warnings()
response = requests.get(EOL_GA_URL, verify=False)
self.app_stream = yaml.safe_load(response.content)
def parse_args(self):
parser = argparse.ArgumentParser(
description="GitHubSanityChecker program go through all "
"https://github.com/sclorg container images and "
"detects if .exclude, .devel-repo are obsolete or missing."
"The output logs are stored in current working directory."
)
parser.add_argument(
"--send-email", action="store_true", help="The logs are send over SMTP mail.", default=False
)
parser.add_argument("--log-dir", help="The logs are stored in user defined directory")
return parser.parse_args()
def prepare(self) -> bool:
if self.args.log_dir:
if not os.path.exists(self.args.log_dir):
self.log_to_file(msg="Log dir you specified by --log-dir parameter does not exist.")
return False
self.log_dir = self.args.log_dir
self.create_tmp_dir()
self.download_and_load_lifecycle_file()
self.collect_data()
return True
def check_files(self, ver: str) -> List[str]:
checked_files = []
for os_ver in OS_HOSTS:
exclude_file = self.tmp_path_repo / ver / f".exclude-{os_ver}"
dockerfile = self.tmp_path_repo / ver / f"Dockerfile.{os_ver}"
devel_file = self.tmp_path_repo / ver / f".devel-repo-{os_ver}"
if exclude_file.exists():
checked_files.append(f".exclude-{os_ver}")
if dockerfile.exists():
checked_files.append(f"Dockerfile.{os_ver}")
if devel_file.exists():
checked_files.append(f".devel-repo-{os_ver}")
return checked_files
def check_file_exists(self, ver: str, file: str):
return [x for x in self.data_dict[self.repo][ver] if x.startswith(file)]
def exclude_file_exists(self, ver: str, os_ver: str):
return self.check_file_exists(ver=ver, file=f".exclude-{os_ver}")
def dockerfile_exists(self, ver: str, os_ver: str):
return self.check_file_exists(ver=ver, file=f"Dockerfile.{os_ver}")
def devel_repo_exists(self, ver: str, os_ver: str):
return self.check_file_exists(ver=ver, file=f".devel-repo-{os_ver}")
def check_exclude_file_not_dockerfile(self, ver: str, os_ver: str):
ret_val = True
if self.exclude_file_exists(ver=ver, os_ver=os_ver) and not self.dockerfile_exists(ver=ver, os_ver=os_ver):
self.log_to_file(msg=f"Exclude file exists and Dockerfile not for {ver} and {os_ver}")
self.update_message(
f"For version {ver} .exclude-{os_ver} is present "
f"but Dockerfile.{os_ver} not."
)
self.update_message(f"Think about for removal .exclude-{os_ver}.")
ret_val = False
if not ret_val:
self.log_to_file(msg=f"Check .exclude file and not Dockerfile hit some issue for {ver} in repo {self.repo}.")
return ret_val
def check_devel_repo_file(self, ver: str, os_ver: str):
ret_val = True
if self.devel_repo_exists(ver=ver, os_ver=os_ver) and self.dockerfile_exists(ver=ver, os_ver=os_ver):
self.log_to_file(msg=f".devel-repo exists and Dockerfile as well for {ver} and {os_ver}")
# TODO Check if .devel-repo file can not be removed
self.update_message(
f".devel-repo-{os_ver} is present in {ver} and Dockerfile.{os_ver}."
)
self.update_message(
"Check if image is not already GA."
)
ret_val = False
if not ret_val:
self.log_to_file(msg=f"Check for .devel-repo and Dockerfile hit some issue for {ver} in repo {self.repo}.")
return ret_val
def is_eol_version(self, ver: str, os_ver: str) -> int:
from datetime import datetime
today = datetime.today().date()
pkg_dict = [x for x in self.app_stream["lifecycles"] if x["name"] == self.pkg_name]
self.log_to_file(msg=f"OS is {os_ver} and version is {ver}")
for pkg in pkg_dict:
if pkg["stream"] != ver:
continue
if "enddate" not in pkg:
continue
# Get RHEL version without 'rhel' prefix
rhel_version = os_ver.lstrip("rhel")
if not pkg["initial_product_version"].startswith(rhel_version):
continue
self.log_to_file(msg=pkg)
enddate = datetime.strptime(pkg["enddate"], "%Y%m%d").date()
self.log_to_file(msg=f"{today} and {enddate}")
days_to_eol = (enddate - today).days
self.log_to_file(msg=f"Count of days till EOL {days_to_eol}")
# Already reached EOL.
if days_to_eol < 0:
return 1
# Less then 30 days to EOL.
if days_to_eol < int(30):
return 2
else:
return 0
def check_tested_version(self, ver: str, os_ver: str):
ret_val = True
eol_flag = -1
if self.exclude_file_exists(ver=ver, os_ver=os_ver) and self.dockerfile_exists(ver=ver, os_ver=os_ver):
self.log_to_file(msg=f"Exclude file exists and Dockerfile as well for {ver} and {os_ver}")
if os_ver.startswith("rhel"):
eol_flag = self.is_eol_version(ver=ver, os_ver=os_ver)
if eol_flag == 1:
self.log_to_file(msg=f"The version {ver} is not tested because it reached EOL already.")
self.update_message(
f"File .exclude-{os_ver} is present and Dockerfile.{os_ver} as well. "
f"The version {ver} is not tested because it reached EOL already."
)
ret_val = True
if eol_flag == 0:
self.update_message(f"File .exclude-{os_ver} is present and Dockerfile.{os_ver} as well."
f"The version {ver} does not reached EOL yet.")
ret_val = True
if eol_flag == 2:
self.update_message("The image will reach EOL during next 30 days.")
ret_val = False
if not ret_val:
self.log_to_file(msg=f"Check for .exclude and Dockerfile hit some issue for {ver} in repo {self.repo}.")
return ret_val
def get_all_supported_versions(self, repo_name: str):
versions = []
if not (self.tmp_path_repo / "Makefile").exists():
self.log_to_file(msg=f"Makefile for repository {repo_name} does not exist.")
return versions
with open(self.tmp_path_repo / "Makefile") as f:
ver_row = [x for x in f.readlines() if x.startswith("VERSIONS")][0]
ver_row = ver_row.split("=")[1].strip()
return ver_row.split(" ")
def clone_repository(self, repo_name: str):
try:
self.log_to_file(msg=f"Cloning repository {repo_name}")
repo = Repo.clone_from(f"https://github.com/sclorg/{repo_name}", to_path=self.tmp_path_dir / repo_name)
return True
except GitCommandError as ex:
self.write_to_textfile(f"Cloning repo has failed {ex}")
return False
def collect_data(self):
for repo, pkg_name in SCLORG_REPOS.items():
if not self.clone_repository(repo_name=repo):
continue
self.tmp_path_repo = self.tmp_path_dir / repo
os.chdir(self.tmp_path_repo)
versions_to_check = self.get_all_supported_versions(repo_name=repo)
for ver in versions_to_check:
if repo not in self.data_dict:
self.data_dict[repo] = {}
self.data_dict[repo][ver] = self.check_files(ver)
os.chdir(self.cwd)
rmtree(self.tmp_path_dir / repo)
def run_check(self):
for repo in self.data_dict:
self.log_to_file(msg=f"Repo to check {repo}...")
self.log_to_file(msg=f"Collected data: {self.data_dict[repo]}")
report_file = Path(self.log_dir) / f"{repo}.log"
if report_file.exists():
report_file.unlink()
self.repo = repo
self.pkg_name = SCLORG_REPOS[repo]
sanity_ok = True
for ver in self.data_dict[repo]:
self.message = f"--- repo: {self.repo} and version: {ver}:\n"
checker = True
for os_ver in OS_HOSTS:
self.log_to_file(msg=f"Checked OS is: {os_ver}")
if not self.check_exclude_file_not_dockerfile(ver, os_ver):
checker = False
if not self.check_devel_repo_file(ver, os_ver):
checker = False
if not self.check_tested_version(ver, os_ver):
checker = False
self.log_to_file(msg=f"Checker for {ver} is {checker}")
if not checker:
self.write_to_textfile(msg=self.message, report_file=report_file)
sanity_ok = False
else:
self.update_message(msg=f"{ver} is ok.")
if sanity_ok:
if repo not in self.success_repos:
self.success_repos.append(repo)
if report_file.exists():
os.unlink(report_file)
else:
if repo not in self.failed_repos:
self.failed_repos.append(repo)
self.global_result_flag = False
self.log_to_file(msg=f"Repo to check {repo} finished.")
print(f"Report text files are located here {self.log_dir}")
rmtree(self.tmp_path_dir)
def send_email(self):
if not self.args.send_email:
self.log_to_file(msg="Sending email is not allowed")
message = f"SCLORG sanity checker hit some issues in these repositories:\n"
message += "\n".join(self.failed_repos)
message += f"\n\nThese repositories did not have any issues:\n\n"
message += ", ".join(self.success_repos)
self.log_to_file(msg=message)
return
if self.global_result_flag:
subject_msg = "SCLORG: sanity checker did not hit any issue in https://github/sclorg containers"
message = f"SCLORG sanity checker did not hit any issues for https://github/sclorg containers."
else:
subject_msg = "SCLORG: sanity checker hit some issues in https://github/sclorg containers"
message = f"SCLORG sanity checker hit some issues in these repositories:\n"
message += "\n".join(self.failed_repos)
message += f"\n\nThese repositories did not have any issues:\n"
message += "\n".join(self.success_repos)
message += "\nIn case the information is wrong, please reach out " \
"phracek@redhat.com, pkubat@redhat.com or hhorak@redhat.com.\n"
message += "Or file an issue here: https://github.com/sclorg/ci-scripts/issues"
send_from = "phracek@redhat.com"
send_to = ["phracek@redhat.com", "pkubat@redhat.com", "hhorak@redhat.com", "zmiklank@redhat.com"]
msg = MIMEMultipart()
msg['From'] = ', '.join(send_from)
msg['To'] = ', '.join(send_to)
msg['Subject'] = subject_msg
msg.attach(MIMEText(message))
if not self.global_result_flag:
import glob
log_files = glob.glob(f"{self.log_dir}/*.log")
for log in log_files:
attach = MIMEApplication(open(log, 'r').read(), Name=os.path.basename(str(log)))
attach.add_header('Content-Disposition', 'attachment; filename="{}"'.format(os.path.basename(str(log))))
msg.attach(attach)
attach = MIMEApplication(open(self.debug_log, "r").read(), Name=SANITY_LOG)
attach.add_header('Content-Disposition', 'attachment; filename="{}"'.format(SANITY_LOG))
msg.attach(attach)
smtp = smtplib.SMTP("127.0.0.1")
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
if __name__ == "__main__":
sanity_checker = SclOrgSanityChecker()
if not sanity_checker.prepare():
print("Preparation for GitHub Sanity checker has failed. Please look what's wrong.")
sys.exit(1)
sanity_checker.run_check()
sanity_checker.send_email()
sys.exit(0)