2024-07-09 18:25:21 +02:00
|
|
|
#!/bin/python3
|
|
|
|
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-07-10 19:01:46 +02:00
|
|
|
ECG is a program that automates software environment checking
|
|
|
|
for scientific artifacts.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
|
|
|
It is meant to be executed periodically to analyze variations in the
|
|
|
|
software environment of the artifact through time.
|
|
|
|
"""
|
|
|
|
|
2024-07-09 18:25:21 +02:00
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import argparse
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import zipfile
|
|
|
|
import tarfile
|
|
|
|
import pathlib
|
2024-07-10 12:23:07 +02:00
|
|
|
import logging
|
2024-07-10 19:01:46 +02:00
|
|
|
import datetime
|
2024-07-11 17:14:58 +02:00
|
|
|
import sys
|
2024-07-24 18:58:50 +02:00
|
|
|
import string
|
2024-07-29 15:17:22 +02:00
|
|
|
import traceback
|
2024-07-11 17:14:58 +02:00
|
|
|
|
2024-07-24 18:58:50 +02:00
|
|
|
def trim(url):
|
2024-07-12 12:10:03 +02:00
|
|
|
"""
|
2024-07-24 18:58:50 +02:00
|
|
|
Trims given URL to make it contain only lowercase letters and numbers,
|
|
|
|
as well as with a maximum length of 128.
|
2024-07-12 12:10:03 +02:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
url: str
|
|
|
|
URL to trim.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
str
|
|
|
|
Trimmed URL.
|
|
|
|
"""
|
|
|
|
trimmed = ""
|
2024-07-24 18:58:50 +02:00
|
|
|
url_lc = url.lower()
|
|
|
|
i = 0
|
|
|
|
while i < len(url_lc) and i < 128:
|
|
|
|
c = url_lc[i]
|
|
|
|
if c in string.ascii_lowercase or c in [str(x) for x in range(0, 10)]:
|
2024-07-12 12:10:03 +02:00
|
|
|
trimmed += c
|
2024-07-24 18:58:50 +02:00
|
|
|
i += 1
|
2024-07-12 12:10:03 +02:00
|
|
|
return trimmed
|
|
|
|
|
2024-07-16 12:04:29 +02:00
|
|
|
def download_file(url, dest):
|
|
|
|
"""
|
|
|
|
Downloads the file stored at the given URL and returns its hash
|
|
|
|
and location.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
url: str
|
|
|
|
URL to the file to download.
|
|
|
|
dest: str
|
|
|
|
Path to where the file should be stored.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
str
|
2024-07-26 13:28:26 +02:00
|
|
|
Hash of the downloaded file, or empty string if download failed.
|
2024-07-16 12:04:29 +02:00
|
|
|
"""
|
2024-08-05 16:29:07 +02:00
|
|
|
file_hash = "-1"
|
2024-07-26 13:28:26 +02:00
|
|
|
try:
|
|
|
|
req = requests.get(url)
|
|
|
|
if req.status_code != 404:
|
|
|
|
file = open(dest, "wb")
|
|
|
|
file.write(req.content)
|
|
|
|
file.close()
|
|
|
|
hash_process = subprocess.run(f"sha256sum {file.name} | cut -d ' ' -f 1 | tr -d '\n'", capture_output=True, shell=True)
|
|
|
|
file_hash = hash_process.stdout.decode("utf-8")
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
# We can just ignore this exception, as we will just return an empty
|
|
|
|
# hash to indicate the error:
|
|
|
|
pass
|
|
|
|
return file_hash
|
2024-07-16 12:04:29 +02:00
|
|
|
|
2024-08-07 11:22:54 +02:00
|
|
|
def download_sources(config, arthashlog_path, dl_dir, use_cache, artifact_name):
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-07-12 12:10:03 +02:00
|
|
|
Downloads the source of the artifact in 'config'.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
config: dict
|
2024-07-16 11:01:08 +02:00
|
|
|
Parsed config file.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-23 09:38:03 +02:00
|
|
|
arthashlog_path: str
|
|
|
|
Path to the artifact hash log file.
|
|
|
|
|
2024-07-23 17:27:10 +02:00
|
|
|
dl_dir: str
|
|
|
|
Path to the directory where to download the artifact.
|
2024-07-23 09:38:03 +02:00
|
|
|
|
|
|
|
use_cache: bool
|
2024-07-23 17:27:10 +02:00
|
|
|
Indicates whether the cache should be used or not.
|
2024-07-23 09:38:03 +02:00
|
|
|
|
2024-08-07 11:22:54 +02:00
|
|
|
artifact_name: str
|
|
|
|
Name of the artifact, for the artifact hash log.
|
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
Returns
|
|
|
|
-------
|
2024-07-23 17:27:10 +02:00
|
|
|
temp_dir: str
|
2024-07-26 13:28:26 +02:00
|
|
|
Path to the directory where the artifact is downloaded to, or empty
|
|
|
|
string if download failed.
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-07-09 18:25:21 +02:00
|
|
|
url = config["artifact_url"]
|
2024-07-25 09:27:30 +02:00
|
|
|
artcache_dir = trim(url)
|
|
|
|
artifact_dir = os.path.join(dl_dir, artcache_dir)
|
2024-07-12 12:10:03 +02:00
|
|
|
# Checking if artifact in cache. Not downloading if it is:
|
2024-07-16 17:50:55 +02:00
|
|
|
if not os.path.exists(artifact_dir) or not use_cache:
|
2024-07-12 12:10:03 +02:00
|
|
|
logging.info(f"Downloading artifact from {url}")
|
2024-07-16 17:50:55 +02:00
|
|
|
# In case cache was used before:
|
|
|
|
if not use_cache:
|
|
|
|
os.system(f"rm -rf {artifact_dir}")
|
2024-07-12 12:10:03 +02:00
|
|
|
os.mkdir(artifact_dir)
|
2024-07-16 12:04:29 +02:00
|
|
|
artifact_file = tempfile.NamedTemporaryFile()
|
|
|
|
artifact_path = artifact_file.name
|
|
|
|
artifact_hash = download_file(url, artifact_path)
|
2024-07-26 13:28:26 +02:00
|
|
|
# If download was successful:
|
2024-08-05 16:29:07 +02:00
|
|
|
if artifact_hash != "-1":
|
2024-07-26 13:28:26 +02:00
|
|
|
if config["type"] == "zip":
|
|
|
|
artifact = zipfile.ZipFile(artifact_path)
|
|
|
|
elif config["type"] == "tar":
|
|
|
|
artifact = tarfile.open(artifact_path)
|
|
|
|
logging.info(f"Extracting artifact at {artifact_dir}")
|
|
|
|
artifact.extractall(artifact_dir)
|
|
|
|
# If download failed:
|
|
|
|
else:
|
2024-08-05 16:29:07 +02:00
|
|
|
os.rmdir(artifact_dir)
|
2024-07-26 13:28:26 +02:00
|
|
|
artifact_dir = ""
|
2024-07-16 15:34:15 +02:00
|
|
|
# Logging the current hash of the artifact:
|
|
|
|
arthashlog_file = open(arthashlog_path, "a")
|
2024-07-16 12:04:29 +02:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
timestamp = str(datetime.datetime.timestamp(now))
|
2024-07-26 13:28:26 +02:00
|
|
|
# Artifact hash will be an empty string if download failed:
|
2024-08-07 11:22:54 +02:00
|
|
|
arthashlog_file.write(f"{timestamp},{artifact_hash},{artifact_name}\n")
|
2024-07-16 15:34:15 +02:00
|
|
|
arthashlog_file.close()
|
2024-07-12 12:10:03 +02:00
|
|
|
else:
|
|
|
|
logging.info(f"Cache found for {url}, skipping download")
|
|
|
|
return artifact_dir
|
2024-07-09 18:25:21 +02:00
|
|
|
|
2024-07-26 19:00:25 +02:00
|
|
|
def builderror_identifier(output):
|
|
|
|
|
2024-07-12 17:50:19 +02:00
|
|
|
"""
|
2024-07-26 19:00:25 +02:00
|
|
|
Parses the given 'output' to indentify the error.
|
2024-07-12 17:50:19 +02:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
output: str
|
2024-07-23 17:27:10 +02:00
|
|
|
Output of Docker.
|
2024-07-12 17:50:19 +02:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2024-07-26 19:00:25 +02:00
|
|
|
found_error: str
|
|
|
|
The error that has been found in the output, according to the
|
|
|
|
categories. If there is more than one, only the latest is taken into
|
|
|
|
account.
|
2024-07-12 17:50:19 +02:00
|
|
|
"""
|
2024-07-23 09:38:03 +02:00
|
|
|
# Possible error messages given by 'docker build' and their category.
|
|
|
|
# The key is the category, the value is a tuple of error messages belonging to
|
|
|
|
# to this category:
|
|
|
|
build_errors = {
|
|
|
|
"package_unavailable":("Unable to locate package"),
|
2024-07-26 13:28:26 +02:00
|
|
|
"baseimage_unavailable":("manifest unknown: manifest unknown"),
|
2024-07-26 19:00:25 +02:00
|
|
|
"dockerfile_not_found":("Dockerfile: no such file or directory")
|
2024-07-23 09:38:03 +02:00
|
|
|
}
|
|
|
|
|
2024-07-26 19:00:25 +02:00
|
|
|
found_error = ""
|
|
|
|
unknown_error = True
|
|
|
|
for error_cat, error in build_errors.items():
|
|
|
|
if error in output:
|
|
|
|
unknown_error = False
|
|
|
|
found_error = error_cat
|
|
|
|
if unknown_error:
|
|
|
|
found_error = "unknown_error"
|
|
|
|
return found_error
|
|
|
|
|
|
|
|
def buildresult_saver(result, buildstatus_path, config_path):
|
|
|
|
"""
|
|
|
|
Saves the given result in the 'build_status' file.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
result: str
|
|
|
|
The result of the build. Either a Docker 'build' error
|
|
|
|
(see 'builderror_identifier'), another type of error
|
|
|
|
(for instance 'artifact_unavailable'), or 'success'
|
|
|
|
if build is successful.
|
|
|
|
|
|
|
|
buildstatus_path: str
|
|
|
|
Path to the build status file.
|
|
|
|
|
|
|
|
config_path: str
|
|
|
|
Path to the config file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
None
|
|
|
|
"""
|
2024-07-12 17:50:19 +02:00
|
|
|
file_exists = os.path.exists(buildstatus_path)
|
2024-07-15 11:28:38 +02:00
|
|
|
buildstatus_file = open(buildstatus_path, "a")
|
2024-07-21 17:32:04 +02:00
|
|
|
artifact_name = os.path.basename(config_path).split(".")[0]
|
2024-07-15 11:00:08 +02:00
|
|
|
# # Writing header in case file didn't exist:
|
|
|
|
# if not file_exists:
|
|
|
|
# buildstatus_file.write("yaml_path,timestamp,error")
|
2024-07-26 19:00:25 +02:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
timestamp = str(datetime.datetime.timestamp(now))
|
|
|
|
buildstatus_file.write(f"{artifact_name},{timestamp},{result}\n")
|
2024-07-12 17:50:19 +02:00
|
|
|
buildstatus_file.close()
|
|
|
|
|
2024-07-24 18:58:50 +02:00
|
|
|
def build_image(config, src_dir, image_name, docker_cache = False):
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-07-12 12:10:03 +02:00
|
|
|
Builds the given Docker image in 'config'.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
config: dict
|
2024-07-16 11:01:08 +02:00
|
|
|
Parsed config file.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-23 17:27:10 +02:00
|
|
|
src_dir: str
|
|
|
|
Path to the directory where the artifact is stored.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-24 18:58:50 +02:00
|
|
|
image_name: str
|
|
|
|
Name of the Docker image.
|
|
|
|
|
2024-07-24 18:05:31 +02:00
|
|
|
docker_cache: bool
|
|
|
|
Enables or disables Docker 'build' cache.
|
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
Returns
|
|
|
|
-------
|
2024-07-18 17:56:57 +02:00
|
|
|
return_code: bool, build_output: str
|
|
|
|
Return code and output of Docker 'build'.
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-07-24 18:58:50 +02:00
|
|
|
cache_arg = " --no-cache"
|
2024-07-24 18:05:31 +02:00
|
|
|
if docker_cache:
|
|
|
|
cache_arg = ""
|
2024-07-24 18:58:50 +02:00
|
|
|
logging.info(f"Starting building image {image_name}")
|
2024-07-30 18:06:16 +02:00
|
|
|
path = os.path.join(src_dir, config["buildfile_dir"])
|
2024-07-24 18:58:50 +02:00
|
|
|
# Using trimmed artifact URL as name:
|
2024-07-25 09:27:30 +02:00
|
|
|
build_command = f"docker build{cache_arg} -t {image_name} ."
|
2024-07-12 11:21:14 +02:00
|
|
|
build_process = subprocess.run(build_command.split(" "), cwd=path, capture_output=True)
|
2024-07-16 17:35:09 +02:00
|
|
|
build_output = f"stdout:\n{build_process.stdout.decode('utf-8')}\nstderr:\n{build_process.stderr.decode('utf-8')}"
|
2024-07-15 11:28:38 +02:00
|
|
|
# build_output = build_process.stderr.decode("utf-8")
|
2024-07-12 11:21:14 +02:00
|
|
|
logging.info(f"Output of '{build_command}':")
|
2024-07-12 17:50:19 +02:00
|
|
|
logging.info(build_output)
|
2024-07-09 18:25:21 +02:00
|
|
|
return_code = build_process.returncode
|
2024-07-10 17:06:54 +02:00
|
|
|
logging.info(f"Command '{build_command}' exited with code {return_code}")
|
2024-07-18 17:56:57 +02:00
|
|
|
return return_code, build_output
|
2024-07-09 18:25:21 +02:00
|
|
|
|
2024-08-05 17:04:42 +02:00
|
|
|
def check_env(config, src_dir, artifact_name, pkglist_path):
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-07-12 12:10:03 +02:00
|
|
|
Builds a list of all software packages installed in the
|
|
|
|
Docker image given in 'config', depending on the package managers
|
|
|
|
specified in the configuration, then stores it in a CSV file.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
config: dict
|
2024-07-16 11:01:08 +02:00
|
|
|
Parsed config file.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-23 17:27:10 +02:00
|
|
|
src_dir: str
|
|
|
|
Path to the directory where the artifact is stored.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-08-05 17:04:42 +02:00
|
|
|
artifact_name: str
|
|
|
|
Name of the artifact. Used both as the Docker image name, and for the
|
|
|
|
packages list for tracking purpose during the output analysis.
|
2024-07-24 18:58:50 +02:00
|
|
|
|
2024-07-23 09:38:03 +02:00
|
|
|
pkglist_path: str
|
|
|
|
Path to the package list file.
|
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
None
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-08-06 16:59:36 +02:00
|
|
|
# Saving the current time to add it to every row:
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
timestamp = str(datetime.datetime.timestamp(now))
|
|
|
|
|
2024-07-23 09:38:03 +02:00
|
|
|
# Commands to list installed packages along with their versions and the name
|
|
|
|
# of the package manager, depending on the package managers.
|
|
|
|
# Each package manager is associated with a tuple, the first item being
|
2024-07-23 21:04:34 +02:00
|
|
|
# the package manager's command, the second being the arguments for the
|
|
|
|
# query (they must be separate for the "--entrypoint" argument of Docker
|
|
|
|
# 'run', see below), and the third one being the command that will format
|
2024-07-23 09:38:03 +02:00
|
|
|
# the output of the query command (this one can be an empty string in case
|
|
|
|
# the formatting part is already done using the options of the first command).
|
2024-07-23 21:04:34 +02:00
|
|
|
# The first command needs to be run on the container, and the second on the
|
|
|
|
# host, to take into account container images that do not have the formatting
|
2024-07-23 09:38:03 +02:00
|
|
|
# packages installed.
|
|
|
|
pkgmgr_cmd = {
|
2024-08-06 16:59:36 +02:00
|
|
|
"dpkg": ("dpkg", "-l", "awk 'NR>5 {print $2 \",\" $3 \",dpkg," + artifact_name + "," + timestamp + "\"}'"), \
|
|
|
|
"rpm":("rpm", "-qa --queryformat '%{NAME},%{VERSION},rpm," + artifact_name + "," + timestamp + "\\n'", ""), \
|
|
|
|
"pacman":("pacman", "-Q", "awk '{print $0 \",\" $1 \",pacman," + artifact_name + "," + timestamp + "\"}'"), \
|
|
|
|
"pip":("pip", "list", "awk 'NR>2 {print $1 \",\" $2 \",\" \"pip," + artifact_name + "," + timestamp + "\"}'"), \
|
|
|
|
"conda":("/root/.conda/bin/conda", "list -e", "sed 's/=/ /g' | awk 'NR>3 {print $1 \",\" $2 \",conda," + artifact_name + "," + timestamp + "\"}'")
|
2024-07-23 09:38:03 +02:00
|
|
|
}
|
2024-07-23 21:04:34 +02:00
|
|
|
# Command to obtain the latest commit hash in a git repository (separated
|
|
|
|
# into 2 parts for "--entrypoint"):
|
|
|
|
gitcmd = ("git", "log -n 1 --pretty=format:%H")
|
2024-07-23 09:38:03 +02:00
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
logging.info("Checking software environment")
|
2024-07-11 17:14:58 +02:00
|
|
|
pkglist_file = open(pkglist_path, "w")
|
2024-07-15 11:00:08 +02:00
|
|
|
# pkglist_file.write("package,version,package_manager\n")
|
2024-07-30 18:06:16 +02:00
|
|
|
path = os.path.join(src_dir, config["buildfile_dir"])
|
2024-07-24 18:05:31 +02:00
|
|
|
# Package managers:
|
2024-07-09 18:25:21 +02:00
|
|
|
for pkgmgr in config["package_managers"]:
|
2024-07-23 21:04:34 +02:00
|
|
|
# "--entrypoint" requires command and arguments to be separated.
|
|
|
|
# This Docker 'run' option is used to prevent the shell from printing
|
|
|
|
# a login message, if any.
|
2024-07-16 10:20:04 +02:00
|
|
|
pkglist_cmd = pkgmgr_cmd[pkgmgr][0]
|
2024-07-23 21:04:34 +02:00
|
|
|
pkglist_cmdargs = pkgmgr_cmd[pkgmgr][1].split(" ")
|
|
|
|
listformat_cmd = pkgmgr_cmd[pkgmgr][2]
|
2024-07-09 18:25:21 +02:00
|
|
|
logging.info(f"Checking '{pkgmgr}'")
|
2024-07-23 21:04:34 +02:00
|
|
|
# pkglist_process = subprocess.run(["docker", "run", "--rm", config["image_name"]] + pkglist_cmd.split(" "), cwd=path, capture_output=True)
|
2024-08-05 17:04:42 +02:00
|
|
|
pkglist_process = subprocess.run(["docker", "run", "--rm", "--entrypoint", pkglist_cmd, artifact_name] + pkglist_cmdargs, cwd=path, capture_output=True)
|
|
|
|
format_process = subprocess.run(f"cat << EOF | {listformat_cmd}\n{pkglist_process.stdout.decode('utf-8')}EOF", cwd=path, capture_output=True, shell=True)
|
|
|
|
pkglist = format_process.stdout.decode("utf-8")
|
|
|
|
pkglist_file.write(pkglist)
|
|
|
|
# Python venvs:
|
|
|
|
logging.info("Checking Python venvs")
|
|
|
|
for venv in config["python_venvs"]:
|
|
|
|
pipcmd = pkgmgr_cmd["pip"][0]
|
|
|
|
pipcmd_args = pkgmgr_cmd["pip"][1]
|
2024-08-05 18:22:38 +02:00
|
|
|
pkglist_process = subprocess.run(["docker", "run", "--rm", "-w", venv["path"], "--entrypoint", venv["path"] + "/bin/" + pipcmd, artifact_name] + pipcmd_args.split(" "), cwd=path, capture_output=True)
|
2024-07-16 17:35:09 +02:00
|
|
|
format_process = subprocess.run(f"cat << EOF | {listformat_cmd}\n{pkglist_process.stdout.decode('utf-8')}EOF", cwd=path, capture_output=True, shell=True)
|
2024-07-10 15:44:01 +02:00
|
|
|
pkglist = format_process.stdout.decode("utf-8")
|
2024-07-10 10:35:30 +02:00
|
|
|
pkglist_file.write(pkglist)
|
2024-07-24 18:05:31 +02:00
|
|
|
|
|
|
|
# Git packages:
|
|
|
|
logging.info("Checking Git packages")
|
|
|
|
for repo in config["git_packages"]:
|
2024-08-05 17:04:42 +02:00
|
|
|
pkglist_process = subprocess.run(["docker", "run", "--rm", "-w", repo["location"], "--entrypoint", gitcmd[0], artifact_name] + gitcmd[1].split(" "), cwd=path, capture_output=True)
|
2024-08-06 16:59:36 +02:00
|
|
|
repo_row = f"{repo['name']},{pkglist_process.stdout.decode('utf-8')},git,{artifact_name},{timestamp}"
|
2024-07-24 18:05:31 +02:00
|
|
|
pkglist_file.write(f"{repo_row}\n")
|
|
|
|
|
|
|
|
# Misc packages:
|
|
|
|
logging.info("Checking packages obtained outside of a package manager or VCS")
|
|
|
|
for pkg in config["misc_packages"]:
|
|
|
|
logging.info(f"Downloading package {pkg['name']} from {pkg['url']}")
|
|
|
|
pkg_file = tempfile.NamedTemporaryFile()
|
|
|
|
pkg_path = pkg_file.name
|
|
|
|
pkg_hash = download_file(pkg["url"], pkg_path)
|
2024-07-26 13:28:26 +02:00
|
|
|
# Package hash will be an empty string if download failed:
|
2024-08-06 16:59:36 +02:00
|
|
|
pkg_row = f"{pkg['name']},{pkg_hash},misc,{artifact_name},{timestamp}"
|
2024-07-24 18:05:31 +02:00
|
|
|
pkglist_file.write(f"{pkg_row}\n")
|
2024-07-10 10:35:30 +02:00
|
|
|
pkglist_file.close()
|
2024-07-09 18:25:21 +02:00
|
|
|
|
2024-07-24 18:58:50 +02:00
|
|
|
def remove_image(config, image_name):
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-07-12 12:10:03 +02:00
|
|
|
Removes the Docker image given in 'config'.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
config: dict
|
2024-07-16 11:01:08 +02:00
|
|
|
Parsed config file.
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-24 18:58:50 +02:00
|
|
|
image_name: str
|
|
|
|
Name of the Docker image.
|
|
|
|
|
2024-07-12 12:10:03 +02:00
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
None
|
2024-07-10 12:23:07 +02:00
|
|
|
"""
|
2024-07-24 18:58:50 +02:00
|
|
|
logging.info(f"Removing image '{image_name}'")
|
|
|
|
subprocess.run(["docker", "rmi", image_name], capture_output = True)
|
2024-07-09 18:25:21 +02:00
|
|
|
|
|
|
|
def main():
|
2024-07-23 09:38:03 +02:00
|
|
|
# Paths:
|
|
|
|
config_path = ""
|
|
|
|
pkglist_path = "" # Package list being generated
|
2024-07-25 18:03:14 +02:00
|
|
|
buildstatus_path = "" # Status of the build process of the image, when it fails
|
2024-07-23 09:38:03 +02:00
|
|
|
arthashlog_path = "" # Log of the hash of the downloaded artifact
|
2024-07-23 17:27:10 +02:00
|
|
|
cache_dir = "" # Artifact cache directory, when using one. 'None' value indicates no cache.
|
|
|
|
use_cache = False
|
2024-07-10 19:01:46 +02:00
|
|
|
|
|
|
|
# Command line arguments parsing:
|
2024-07-09 18:25:21 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
2024-07-10 19:01:46 +02:00
|
|
|
prog = "ecg",
|
2024-08-06 16:59:36 +02:00
|
|
|
description =
|
|
|
|
"""
|
|
|
|
ECG is a program that automates software environment checking for scientific artifacts.
|
|
|
|
It is meant to be executed periodically to analyze variations in the software environment of the artifact through time.
|
|
|
|
"""
|
2024-07-10 19:01:46 +02:00
|
|
|
)
|
2024-07-29 15:17:22 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'-v', '--verbose',
|
2024-07-16 10:20:04 +02:00
|
|
|
action = 'store_true',
|
|
|
|
help = "Shows more details on what is being done."
|
|
|
|
)
|
2024-07-10 19:01:46 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"config",
|
2024-07-11 17:14:58 +02:00
|
|
|
help = "The path to the configuration file of the artifact's Docker image."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p", "--pkg-list",
|
2024-07-16 10:20:04 +02:00
|
|
|
help = "Path to the file where the package list generated by the program should be written.",
|
|
|
|
required = True
|
2024-07-11 17:14:58 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l", "--log-path",
|
2024-07-16 10:20:04 +02:00
|
|
|
help = "Path to the file where to log the output of the program.",
|
|
|
|
required = True
|
2024-07-10 19:01:46 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2024-07-25 18:03:14 +02:00
|
|
|
"-b", "--build-status",
|
|
|
|
help = "Path to the file where to write the build status of the Docker image given in the configuration file.",
|
2024-07-16 10:20:04 +02:00
|
|
|
required = True
|
2024-07-10 19:01:46 +02:00
|
|
|
)
|
2024-07-16 12:04:29 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"-a", "--artifact-hash",
|
2024-07-16 15:34:15 +02:00
|
|
|
help = "Path to the file where to write the log of the hash of the downloaded artifact.",
|
2024-07-16 12:04:29 +02:00
|
|
|
required = True
|
|
|
|
)
|
2024-07-10 19:01:46 +02:00
|
|
|
parser.add_argument(
|
2024-07-11 17:14:58 +02:00
|
|
|
"-c", "--cache-dir",
|
2024-08-06 16:59:36 +02:00
|
|
|
help =
|
|
|
|
"""
|
|
|
|
Path to the cache directory, where artifacts that are downloaded will be stored for future usage.
|
|
|
|
If not specified, cache is disabled.
|
|
|
|
""",
|
2024-07-16 17:35:09 +02:00
|
|
|
required = False
|
2024-07-24 18:05:31 +02:00
|
|
|
),
|
2024-07-29 15:17:22 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'--docker-cache',
|
2024-07-24 18:05:31 +02:00
|
|
|
action = 'store_true',
|
|
|
|
help = "Use cache for Docker 'build'."
|
2024-07-10 19:01:46 +02:00
|
|
|
)
|
2024-07-09 18:25:21 +02:00
|
|
|
args = parser.parse_args()
|
2024-07-10 12:23:07 +02:00
|
|
|
|
2024-07-11 17:14:58 +02:00
|
|
|
# Setting up the paths of the outputs:
|
2024-07-12 12:10:03 +02:00
|
|
|
log_path = "log.txt" # Output of the program
|
2024-07-16 10:20:04 +02:00
|
|
|
pkglist_path = args.pkg_list
|
|
|
|
log_path = args.log_path
|
2024-07-25 18:03:14 +02:00
|
|
|
buildstatus_path = args.build_status
|
2024-07-16 15:34:15 +02:00
|
|
|
arthashlog_path = args.artifact_hash
|
2024-07-23 17:27:10 +02:00
|
|
|
cache_dir = args.cache_dir
|
2024-07-11 17:14:58 +02:00
|
|
|
|
2024-07-12 11:21:14 +02:00
|
|
|
# Setting up the log: will be displayed both on stdout and to the specified
|
|
|
|
# file:
|
2024-07-15 11:28:38 +02:00
|
|
|
print(f"Output will be stored in {log_path}")
|
2024-07-12 11:21:14 +02:00
|
|
|
logging.basicConfig(filename = log_path, filemode = "w", format = '%(levelname)s: %(message)s', level = logging.INFO)
|
2024-07-24 18:05:31 +02:00
|
|
|
if args.verbose:
|
2024-07-16 11:01:08 +02:00
|
|
|
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
|
2024-07-12 11:21:14 +02:00
|
|
|
|
2024-07-16 11:01:08 +02:00
|
|
|
# Parsing the input file including the configuration of the artifact's
|
|
|
|
# image:
|
2024-07-15 11:00:08 +02:00
|
|
|
config_path = args.config
|
2024-07-26 19:00:25 +02:00
|
|
|
status = ""
|
2024-07-29 15:17:22 +02:00
|
|
|
try:
|
|
|
|
config_file = open(config_path, "r")
|
|
|
|
config = json.loads(config_file.read())
|
|
|
|
# config = yaml.safe_load(config_file)
|
|
|
|
# print(config)
|
|
|
|
config_file.close()
|
|
|
|
|
|
|
|
dl_dir = None
|
|
|
|
# If not using cache, creates a temporary directory:
|
|
|
|
if cache_dir == None:
|
|
|
|
tmp_dir = tempfile.TemporaryDirectory()
|
|
|
|
dl_dir = tmp_dir.name
|
2024-07-26 13:28:26 +02:00
|
|
|
else:
|
2024-07-29 15:17:22 +02:00
|
|
|
use_cache = True
|
|
|
|
dl_dir = cache_dir
|
2024-08-07 11:22:54 +02:00
|
|
|
artifact_name = os.path.splitext(os.path.basename(config_path))[0]
|
|
|
|
artifact_dir = download_sources(config, arthashlog_path, dl_dir, use_cache, artifact_name)
|
2024-07-29 15:17:22 +02:00
|
|
|
# If download was successful:
|
|
|
|
if artifact_dir != "":
|
|
|
|
return_code, build_output = build_image(config, artifact_dir, artifact_name, args.docker_cache)
|
|
|
|
if return_code == 0:
|
|
|
|
status = "success"
|
|
|
|
check_env(config, artifact_dir, artifact_name, pkglist_path)
|
|
|
|
remove_image(config, artifact_name)
|
|
|
|
else:
|
|
|
|
status = builderror_identifier(build_output)
|
|
|
|
# Creates file if not already:
|
|
|
|
pathlib.Path(pkglist_path).touch()
|
|
|
|
# If download failed, we need to save the error to the build status log:
|
|
|
|
else:
|
|
|
|
logging.fatal("Artifact could not be downloaded!")
|
|
|
|
status = "artifact_unavailable"
|
|
|
|
except Exception as err:
|
|
|
|
# Handles any possible script's own crashes:
|
|
|
|
formatted_err = str(''.join(traceback.format_exception(None, err, err.__traceback__)))
|
|
|
|
log_file = open(log_path, "a")
|
|
|
|
log_file.write(formatted_err)
|
|
|
|
log_file.close()
|
2024-08-05 16:29:07 +02:00
|
|
|
logging.error(formatted_err)
|
2024-07-29 15:17:22 +02:00
|
|
|
status = "script_crash"
|
2024-07-26 19:00:25 +02:00
|
|
|
buildresult_saver(status, buildstatus_path, config_path)
|
2024-07-09 18:25:21 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|