Skip to content

Commit

Permalink
Merge pull request #122 from DDS-Derek/master
Browse files Browse the repository at this point in the history
feat: directory mounting supports bind and volume modes
  • Loading branch information
lavie authored Jan 11, 2025
2 parents b8d376b + 85707e4 commit 3c6693d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions fixtures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ sudocker run -d --name runlike_fixture1 \
--memory="2147483648" \
--memory-reservation="1610612736" \
-v $(pwd):/workdir \
-v $(pwd):/workdir_ro:ro \
-v /random_volume \
--workdir=/workdir \
runlike_fixture
Expand Down
21 changes: 18 additions & 3 deletions runlike/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ def die(message):

class Inspector(object):

def __init__(self, container=None, no_name=None, pretty=None):
def __init__(self, container=None, no_name=None, use_volume_id=None, pretty=None):
self.container = container
self.no_name = no_name
self.use_volume_id = use_volume_id
self.pretty = pretty
self.container_facts = None
self.image_facts = None
Expand Down Expand Up @@ -127,6 +128,20 @@ def parse_ports(self):
if self.options[-1] == self.options[-2]:
self.options.pop()

def parse_volumes(self):
mounts = self.get_container_fact("Mounts")
for mount in mounts:
if mount["Type"] == "volume":
if self.use_volume_id:
volume_format = f'{mount["Name"]}:{mount["Destination"]}'
else:
volume_format = f'{mount["Destination"]}'
else:
volume_format = f'{mount["Source"]}:{mount["Destination"]}'
if not mount.get("RW"):
volume_format += ':ro'
self.options.append(f"--volume {volume_format}")

def parse_links(self):
links = self.get_container_fact("HostConfig.Links")
link_options = set()
Expand Down Expand Up @@ -253,9 +268,9 @@ def format_cli(self):
self.parse_cpuset()
self.parse_entrypoint()

self.parse_volumes()

self.multi_option("Config.Env", "env")
self.multi_option("HostConfig.Binds", "volume")
self.multi_option("Config.Volumes", "volume")
self.multi_option("HostConfig.VolumesFrom", "volumes-from")
self.multi_option("HostConfig.CapAdd", "cap-add")
self.multi_option("HostConfig.CapDrop", "cap-drop")
Expand Down
8 changes: 6 additions & 2 deletions runlike/runlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
"--no-name",
is_flag=True,
help="Do not include container name in output")
@click.option(
"--use-volume-id",
is_flag=True,
help="Keep the automatically assigned volume id")
@click.option("-p", "--pretty", is_flag=True)
@click.option("-s", "--stdin", is_flag=True)
def cli(container, no_name, pretty, stdin):
def cli(container, no_name, use_volume_id, pretty, stdin):

# TODO: -i, -t, -d as added options that override the inspection
if container or stdin:
ins = Inspector(container, no_name, pretty)
ins = Inspector(container, no_name, use_volume_id, pretty)
if container:
ins.inspect()
elif stdin:
Expand Down
8 changes: 6 additions & 2 deletions test_runlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ def test_udp_with_host_port_and_ip(self):

def test_host_volumes(self):
cur_dir = os.path.dirname(os.path.realpath(__file__))
self.expect_substr("--volume=%s:/workdir" % pipes.quote(cur_dir))
self.expect_substr("--volume %s:/workdir" % pipes.quote(cur_dir))

def test_host_volumes_ro(self):
cur_dir = os.path.dirname(os.path.realpath(__file__))
self.expect_substr("--volume %s:/workdir_ro:ro" % pipes.quote(cur_dir))

def test_no_host_volume(self):
self.expect_substr("--volume=/random_volume")
self.expect_substr("--volume /random_volume")

def test_tty(self):
self.expect_substr("-t \\")
Expand Down

0 comments on commit 3c6693d

Please sign in to comment.