-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.py
88 lines (67 loc) · 2.95 KB
/
cli.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
import json
import subprocess
import os
import logging
import argparse
from multiprocessing import Pool
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
def run_docker_image(image_config, output_directory, completed_images):
try:
image_id = image_config.get("id")
# Check if the image has already been completed
if image_id in completed_images:
logging.info(f"{image_id} already completed.")
return
cli_args = image_config.get("cli-args",)
# Build the Docker command
docker_command = ["docker", "run", "--rm"]
# Add the command line args
docker_command.extend(cli_args)
# Run the Docker image
returncode = subprocess.run(docker_command, check=True).returncode
if returncode == 0:
completed_images.add(image_id)
with open("completed_images.txt", "a") as completed_file:
completed_file.write(f"{image_id}\n")
except Exception as e:
logging.error(f"Error running {image_id}: {str(e)}")
# find and replace all placeholders with the values
def replace_json_placeholders(json_str, values):
for k, v in values.items():
try:
placeholder = "<%s>" % k
json_str = json_str.replace(placeholder, v)
except Exception as e:
logging.warning(f"Failed to find and replace any placeholders with value {k} in config file")
return json_str
def run_docker_images_main(config_file):
try:
with open(config_file, "r") as f:
config = json.load(f)
placeholders = config.get("placeholders")
with open(config_file, "r") as f:
config_string = f.read()
config_string = replace_json_placeholders(config_string, placeholders)
config = json.loads(config_string)
images = config.get("images", [])
output_directory = config['values']['output_directory']
# Load the list of completed images
completed_images = set()
if os.path.isfile("completed_images.txt"):
with open("completed_images.txt", "r") as completed_file:
completed_images = set(completed_file.read().splitlines())
# Create a Pool to run Docker images in parallel
pool = Pool(processes=config['values']['parallel_limit'])
pool.starmap(run_docker_image, [(image, output_directory, completed_images) for image in images])
pool.close()
pool.join()
except FileNotFoundError:
logging.error(f"Config file '{config_file}' not found.")
except json.JSONDecodeError:
logging.error(f"Invalid JSON in '{config_file}'.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run Docker images based on a JSON configuration file.")
parser.add_argument("config_file", help="Path to the JSON configuration file")
args = parser.parse_args()
run_docker_images_main(args.config_file)