Skip to content

Commit deeed66

Browse files
author
Marc Schöchlin
committed
feature: handle existing clous.yaml
merge the configuration of generated projects to a existing clouds.yaml and make a backup Signed-off-by: Marc Schöchlin <[email protected]>
1 parent f18b12f commit deeed66

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/openstack_workload_generator/__main__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
2+
import shutil
33
import sys
44
import os
55
import argparse
@@ -27,7 +27,8 @@
2727
# make: *** [Makefile:25: type-check] Error 1
2828

2929
from entities import WorkloadGeneratorDomain # type: ignore[import-not-found]
30-
from entities.helpers import setup_logging, cloud_checker, item_checker, Config # type: ignore[import-not-found]
30+
from entities.helpers import setup_logging, cloud_checker, item_checker, Config, \
31+
iso_timestamp, deep_merge_dict # type: ignore[import-not-found]
3132

3233
LOGGER = logging.getLogger()
3334

@@ -195,10 +196,18 @@ def establish_connection():
195196
machine_obj.delete_machine()
196197
if args.generate_clouds_yaml:
197198
LOGGER.info(f"Creating a a clouds yaml : {args.generate_clouds_yaml}")
198-
clouds_yaml_data = {"clouds": clouds_yaml_data}
199+
clouds_yaml_data_new = {"clouds": clouds_yaml_data}
200+
if os.path.exists(args.generate_clouds_yaml):
201+
with open(args.generate_clouds_yaml, 'r') as file:
202+
existing_data = yaml.safe_load(file)
203+
backup_file=f"{args.generate_clouds_yaml}_{iso_timestamp()}"
204+
logging.warning(f"File {args.generate_clouds_yaml}, making an backup to {backup_file} and adding the new values")
205+
shutil.copy2(args.generate_clouds_yaml, f"{args.generate_clouds_yaml}_{iso_timestamp()}")
206+
clouds_yaml_data_new = deep_merge_dict(existing_data,clouds_yaml_data_new)
207+
199208
with open(args.generate_clouds_yaml, "w") as file:
200209
yaml.dump(
201-
clouds_yaml_data,
210+
clouds_yaml_data_new,
202211
file,
203212
default_flow_style=False,
204213
explicit_start=True,

src/openstack_workload_generator/entities/helpers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
import sys
5+
from datetime import datetime
56
from typing import Tuple
67
import coloredlogs
78

@@ -250,3 +251,19 @@ def item_checker(value: str) -> str:
250251
if not re.fullmatch(r"[a-zA-Z0-9]+[a-zA-Z0-9\-]*[a-zA-Z0-9]+", value):
251252
raise argparse.ArgumentTypeError("specify a valid name for an item")
252253
return value
254+
255+
def iso_timestamp() -> str:
256+
return datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
257+
258+
def deep_merge_dict(d1: dict, d2: dict) -> dict:
259+
result = d1.copy()
260+
for key, value in d2.items():
261+
if (
262+
key in result
263+
and isinstance(result[key], dict)
264+
and isinstance(value, dict)
265+
):
266+
result[key] = deep_merge_dict(result[key], value)
267+
else:
268+
result[key] = value
269+
return result

0 commit comments

Comments
 (0)