From e1faae24c99816a7326ced8fc345f9208400ff74 Mon Sep 17 00:00:00 2001 From: Shuhao Wu Date: Sat, 5 May 2018 20:36:12 -0400 Subject: [PATCH] Specify additional disk mount options Any options as exposed by LXD [1] is supported. This allows a mount to be optional/readonly/etc. [1]: https://github.com/lxc/lxd/blob/65cec7f2c884ea2490fe9376d10abe8aefa97b84/doc/containers.md#type-disk --- lxdock/conf/schema.py | 5 +++-- lxdock/conf/validators.py | 8 ++------ lxdock/container.py | 12 ++++++++++++ tests/unit/conf/test_validators.py | 11 +++-------- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lxdock/conf/schema.py b/lxdock/conf/schema.py index b9be7c2..c4e2d64 100644 --- a/lxdock/conf/schema.py +++ b/lxdock/conf/schema.py @@ -1,7 +1,7 @@ from voluptuous import ALLOW_EXTRA, All, Any, Coerce, Extra, In, Length, Required, Schema, Url from ..provisioners import Provisioner -from .validators import Hostname, IsDirAfterExpandUser, LXDIdentifier +from .validators import ExpandUserIfExists, Hostname, LXDIdentifier def get_schema(): @@ -17,9 +17,10 @@ def get_schema(): 'provisioning': [], # will be set dynamically using provisioner classes... 'server': Url(), 'shares': [{ - 'source': IsDirAfterExpandUser, + 'source': ExpandUserIfExists, 'dest': str, 'set_host_acl': bool, # TODO: need a way to deprecate this + 'share_properties': {Extra: Coerce(str)}, }], 'shell': { 'user': str, diff --git a/lxdock/conf/validators.py b/lxdock/conf/validators.py index 17070ac..88ee07e 100644 --- a/lxdock/conf/validators.py +++ b/lxdock/conf/validators.py @@ -33,12 +33,8 @@ def LXDIdentifier(v): return lxd_identifier_re.match(v) -def IsDirAfterExpandUser(path): +def ExpandUserIfExists(path): if type(path) != str: raise Invalid("expected a string path") - path = os.path.expanduser(path) - if not os.path.isdir(path): - raise Invalid("expected path {} to exists".format(path)) - - return path + return os.path.expanduser(path) diff --git a/lxdock/container.py b/lxdock/container.py index 1bbad65..aefeb5f 100644 --- a/lxdock/container.py +++ b/lxdock/container.py @@ -376,6 +376,18 @@ def _setup_shares(self): for i, share in enumerate(self.options.get('shares', []), start=1): source = os.path.join(self.homedir, share['source']) shareconf = {'type': 'disk', 'source': source, 'path': share['dest'], } + + extra_properties = share.pop('share_properties', {}) + extra_properties.pop("type", None) + extra_properties.pop("source", None) + extra_properties.pop("path", None) + shareconf.update(extra_properties) + + # Upstream issue: https://github.com/lxc/lxd/issues/4538 + if shareconf.get("optional", "false").lower() in {"true", "1", "on", "yes"}: + if not os.path.exists(source): + continue + container.devices['lxdockshare%s' % i] = shareconf guest_username = self.options.get("users", [{"name": "root"}])[0]["name"] diff --git a/tests/unit/conf/test_validators.py b/tests/unit/conf/test_validators.py index f7580a8..2fe34dd 100644 --- a/tests/unit/conf/test_validators.py +++ b/tests/unit/conf/test_validators.py @@ -1,10 +1,9 @@ import os.path import pytest -from voluptuous import Invalid from voluptuous.error import ValueInvalid -from lxdock.conf.validators import Hostname, IsDirAfterExpandUser, LXDIdentifier +from lxdock.conf.validators import ExpandUserIfExists, Hostname, LXDIdentifier class TestHostnameValidator: @@ -48,11 +47,7 @@ def test_cannot_allow_identifiers_longer_than_63_characters(self): id_validator('i' * 64) -class TestIsDirAfterExpandUser: +class TestExpandUserIfExists: def test_converts_dir_if_encountering_tilde(self): - expanded_path = IsDirAfterExpandUser("~/.ssh") + expanded_path = ExpandUserIfExists("~/.ssh") assert expanded_path == os.path.expanduser("~/.ssh") - - def test_raise_invalid_if_not_directory(self): - with pytest.raises(Invalid): - IsDirAfterExpandUser("/adkfjak/adfkjakf/adkfjakjdf")