Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Specify additional disk mount options
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shuhaowu committed May 6, 2018
1 parent 82815c6 commit e1faae2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
5 changes: 3 additions & 2 deletions 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():
Expand All @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions lxdock/conf/validators.py
Expand Up @@ -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)
12 changes: 12 additions & 0 deletions lxdock/container.py
Expand Up @@ -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"]
Expand Down
11 changes: 3 additions & 8 deletions 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:
Expand Down Expand Up @@ -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")

0 comments on commit e1faae2

Please sign in to comment.