-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_vol_mount.py
executable file
·91 lines (76 loc) · 2.92 KB
/
docker_vol_mount.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
89
90
91
#!/usr/bin/env python
'''
Print mount path for container volumes, and optionally:
(a) create symlink in a shortcut directory
(b) add g+w DAC privilege to volume path
(c) exec chcon on volume path
'''
__author__ = 'r2h2'
import argparse
import json
import os
import stat
from subprocess import call, check_output, CalledProcessError
def main():
get_args()
print_volume_mount_path()
add_group_write_DAC_privileges()
create_shortcut_symlink()
do_chcon()
def get_args():
parser = argparse.ArgumentParser(description='Print Docker volume mount path')
parser.add_argument('-g', '--groupwrite', dest='groupwrite', action="store_true",
help='Execute `chmod g+w` on mountpoint')
parser.add_argument('-p', '--prefix', dest='prefix', default='/dv', help='Shortcur directory for symlink')
parser.add_argument('-S', '--sudo', dest='sudo', action="store_true",
help='exec shell commands with sudo')
parser.add_argument('-s', '--symlink', dest='symlink', action="store_true",
help='Create symlink at path prefix')
parser.add_argument('-t', '--selinux-type', dest='type', help='Execute `chcon -Rt <type>`')
parser.add_argument('-v', '--verbose', dest='verbose', action="store_true")
parser.add_argument('-V', '--volume', dest='volume', required=True, help='Name of Docker volume')
global args
args = parser.parse_args()
def print_volume_mount_path():
try:
cmd = ["docker", "volume", "inspect", args.volume]
if args.sudo:
cmd.insert (0, 'sudo')
in_str = check_output(cmd)
except CalledProcessError as e:
print("cannot execute 'docker volume inspect '" + args.volume)
raise
container = json.loads(in_str)
global linkto_path
linkto_path = container[0]['Mountpoint']
if args.verbose:
print(container[0]['Name'] + ': ' + linkto_path)
def add_group_write_DAC_privileges():
if args.groupwrite:
try:
st = os.stat(linkto_path)
os.chmod(linkto_path, st.st_mode | stat.S_IWGRP) # add g+w
except:
pass
def create_shortcut_symlink():
if args.symlink:
linkfrom_path = os.path.join(args.prefix, args.volume)
try:
os.remove(linkfrom_path)
except OSError:
pass
try:
os.symlink(linkto_path, linkfrom_path)
if args.verbose:
print("created symlink %s -> %s" % (linkfrom_path, linkto_path))
except OSError as e:
print("error when creating symlink (use symlink dir where you have rights). Link: %s -> %s: %s" % (linkfrom_path, linkto_path, str(e)))
def do_chcon():
if args.type:
cmd = ["chcon", "-Rt", args.type, linkto_path]
if args.sudo:
cmd.insert (0, 'sudo')
call(cmd)
if args.verbose:
print("set label %s on %s" % (args.type, linkto_path))
main()