|
1 | 1 | """ |
2 | | -This module sets up a multi-registry environment by creating configuration files |
3 | | -for Docker Compose, registry and Traefik. |
| 2 | +This module sets up a multi-registry environment by creating configuration files |
| 3 | +for Docker Compose, registry, and Traefik. |
4 | 4 | See the README.md file for more information. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + Run this script to generate configuration files based on the config.yaml. |
5 | 8 | """ |
6 | 9 | import os |
7 | 10 | import yaml |
|
11 | 14 | import copy |
12 | 15 |
|
13 | 16 |
|
14 | | -# Load config |
| 17 | +# Load configuration from config.yaml file |
15 | 18 | try: |
16 | 19 | with open('config.yaml', 'r', encoding='UTF-8') as file: |
17 | 20 | base_config = yaml.safe_load(file) |
|
20 | 23 | console.print(Text("Error: config.yaml not found", style="bold red")) |
21 | 24 | raise |
22 | 25 |
|
| 26 | +# Extract configuration sections from the loaded config |
23 | 27 | try: |
24 | 28 | registries = base_config['registries'] |
25 | 29 | docker_config = base_config['docker']['baseConfig'] |
|
31 | 35 | console.print(Text(f"Error: Missing key in config file - {e}", style="bold red")) |
32 | 36 | raise |
33 | 37 |
|
34 | | -# Create a compose directory to store the configuration files |
| 38 | +# Create a compose directory to store the configuration files if it does not exist |
35 | 39 | if not os.path.exists('compose'): |
36 | 40 | os.mkdir('compose') |
37 | 41 | os.mkdir('compose/acme') |
38 | 42 | console.print(Text("Compose directory created", style="bold green")) |
39 | 43 | else: |
40 | 44 | console.print(Text("Compose directory already exists", style="bold yellow")) |
41 | 45 |
|
42 | | -# Adding services to docker-compose and routers to traefik |
| 46 | +# Initialize Redis database count for registries |
43 | 47 | count_redis_db = 0 |
| 48 | + |
| 49 | +# Iterate over each registry to create configuration files and update docker and traefik configs |
44 | 50 | for registry in registries: |
45 | 51 | name = registry['name'] |
46 | 52 |
|
47 | | - # Creating registry configuration file |
| 53 | + # Create registry configuration file |
48 | 54 | try: |
49 | | - # Retrocompatibility with old config files without type field |
| 55 | + # Retrocompatibility with old config files without 'type' field |
50 | 56 | if 'type' not in registry: |
51 | 57 | registry['type'] = 'cache' |
52 | 58 | console.print(Text(f"No type specified for registry {name}, defaulting to cache", style="bold yellow")) |
53 | 59 |
|
| 60 | + # Deep copy base registry config and create specific config for this registry |
54 | 61 | registry_config_copy = copy.deepcopy(registry_config) |
55 | 62 | registry_config_file = functions.create_registry_config(registry_config_copy, registry, count_redis_db) |
56 | 63 | functions.write_yaml_file(f'compose/{name}.yaml', registry_config_file) |
|
59 | 66 | console.print(Text(f"Error creating registry configuration file for {name}: {e}", style="bold red")) |
60 | 67 | raise |
61 | 68 |
|
62 | | - # Unsset password before interpolate variables |
| 69 | + # Unset password before interpolating variables to avoid leaking sensitive data |
63 | 70 | try: |
64 | 71 | if 'password' in registry: |
65 | 72 | registry.pop('password') |
66 | 73 | except KeyError: |
67 | 74 | console.print(Text(f"Error: Missing 'password' key in registry configuration for {name}", style="bold red")) |
68 | 75 | raise |
69 | 76 |
|
70 | | - # Creating docker-compose and traefik configuration |
| 77 | + # Create docker-compose and traefik configuration entries for this registry |
71 | 78 | try: |
72 | 79 | docker_config['services'][name] = functions.create_docker_service(registry, docker_perregistry['compose']) |
73 | 80 | traefik_config['http']['routers'][name] = functions.create_traefik_router(registry, traefik_perregistry['router']) |
|
77 | 84 | console.print(Text(f"Error creating docker-compose and traefik configuration for {name}: {e}", style="bold red")) |
78 | 85 | raise |
79 | 86 |
|
| 87 | + # Increment Redis database count for next registry |
80 | 88 | count_redis_db += 1 |
81 | 89 |
|
| 90 | +# Write final configuration files and handle deprecated docker-compose.yml file |
82 | 91 | try: |
83 | 92 | functions.write_yaml_file('compose/compose.yaml', docker_config) |
84 | 93 | functions.write_yaml_file('compose/traefik.yaml', traefik_config) |
85 | 94 | functions.write_to_file('compose/redis.conf', f'databases {count_redis_db}') |
| 95 | + |
86 | 96 | # If docker-compose.yml exists, ask for confirmation before removing it |
87 | 97 | docker_compose_path = 'compose/docker-compose.yml' |
88 | 98 | if os.path.exists(docker_compose_path): |
|
93 | 103 | console.print(Text("Existing docker-compose.yml file removed", style="bold blue")) |
94 | 104 | else: |
95 | 105 | console.print(Text("docker-compose.yml file not removed", style="bold yellow")) |
96 | | - |
97 | | - |
| 106 | + |
| 107 | + # Write HTTP secret file |
98 | 108 | functions.write_http_secret() |
99 | | - |
| 109 | + |
100 | 110 | console.print(Text("Configuration files written successfully", style="bold green")) |
101 | 111 | except Exception as e: |
102 | 112 | console.print(Text(f"Error writing configuration files: {e}", style="bold red")) |
|
0 commit comments