Newer
Older
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
#!/usr/bin/env python3
import argparse
import logging as log
import os
import re
import yaml
from pathlib import Path
def main(args):
compose_yml = os.path.abspath('./docker-compose.yml')
if not os.path.isfile(compose_yml):
raise Exception("Could not find docker-compose.yml at %s" % compose_yml)
with open(compose_yml) as f:
compose = yaml.safe_load(f)
if 'services' not in compose:
raise Exception("Could not find services tag in docker-compose.yml")
for service in compose['services']:
log.info("Service '%s'" % service)
if 'volumes' in compose['services'][service]:
for volume in compose['services'][service]['volumes']:
log.info(" Volume '%s'" % (volume))
reg = re.match(r"^(\./[^:]+/):[^:]+(:\w+)?$", volume)
if reg:
vol_dir = os.path.abspath('./' + reg.group(1))
log.info(" mkdir '%s' (from %s)" % (vol_dir, volume))
if not args.dry_run and not os.path.exists(vol_dir):
os.makedirs(vol_dir, exist_ok=True)
else:
reg = re.match(r"^(\./[^:]+):[^:]+(:\w+)?$", volume)
if reg:
vol_file = os.path.abspath('./' + reg.group(1))
vol_dir = os.path.dirname(vol_file)
log.info(" mkdir '%s' (from %s)" % (vol_dir, volume))
if not args.dry_run and not os.path.exists(vol_dir):
os.makedirs(vol_dir, exist_ok=True)
log.info(" touch '%s' (from %s)" % (vol_file, volume))
if not args.dry_run and not os.path.exists(vol_file):
Path(vol_file).touch()
else:
log.info(" skip")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Create local volume directories.'
)
parser.add_argument("-v", "--verbose", help="Increase output verbosity.",
action="store_true")
parser.add_argument("-d", "--dry-run", help="Dry run: no modification will be done, for testing purpose.",
action="store_true")
args = parser.parse_args()
log.basicConfig(level=log.INFO)
if args.verbose:
log.basicConfig(level=log.DEBUG)
main(args)