-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_stack_volume_dirs.py
54 lines (45 loc) · 1.63 KB
/
create_stack_volume_dirs.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
import os
import yaml
import sys
# Configuration for the device prefix
DEVICE_PREFIX = "/mnt/cephfs/volumes"
def create_directories_and_modify_yaml(yaml_file):
try:
# Read the YAML file
with open(yaml_file, 'r') as file:
data = yaml.safe_load(file)
# Extract and process volumes
volumes = data.get('volumes', {})
if not volumes:
print("Error: No volumes found in the YAML file.", file=sys.stderr)
sys.exit(1)
for volume_name in volumes.keys():
# Ensure directory exists
volume_path = os.path.join(DEVICE_PREFIX, volume_name)
os.makedirs(volume_path, exist_ok=True)
# Update the volume definition
volumes[volume_name] = {
"driver": "local",
"driver_opts": {
"type": "none",
"device": volume_path,
"o": "bind"
}
}
# Output the modified YAML to stdout
print(yaml.dump(data, default_flow_style=False))
except FileNotFoundError:
print(f"Error: File not found: {yaml_file}", file=sys.stderr)
sys.exit(1)
except yaml.YAMLError as e:
print(f"Error: Failed to parse YAML: {e}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <path_to_yaml_file>", file=sys.stderr)
sys.exit(1)
yaml_file_path = sys.argv[1]
create_directories_and_modify_yaml(yaml_file_path)