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

Expanduser for shares as well as additional share properties. #145

Merged
merged 2 commits into from May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lxdock/conf/config.py
Expand Up @@ -75,7 +75,7 @@ def from_base_dir(cls, base_dir='.'):
config.interpolate()

try:
schema(config._dict)
config._dict = schema(config._dict)
except Invalid as e:
# Formats the voluptuous error
path = ' @ %s' % '.'.join(map(str, e.path)) if e.path else ''
Expand Down
9 changes: 4 additions & 5 deletions lxdock/conf/schema.py
@@ -1,8 +1,7 @@
from voluptuous import (ALLOW_EXTRA, All, Any, Coerce, Extra, In, IsDir, Length, Required, Schema,
Url)
from voluptuous import ALLOW_EXTRA, All, Any, Coerce, Extra, In, Length, Required, Schema, Url

from ..provisioners import Provisioner
from .validators import Hostname, LXDIdentifier
from .validators import ExpandUserIfExists, Hostname, LXDIdentifier


def get_schema():
Expand All @@ -18,10 +17,10 @@ def get_schema():
'provisioning': [], # will be set dynamically using provisioner classes...
'server': Url(),
'shares': [{
# The existence of the source directory will be checked!
'source': IsDir(),
'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
9 changes: 9 additions & 0 deletions lxdock/conf/validators.py
@@ -1,5 +1,7 @@
import os.path
import re

from voluptuous import Invalid
from voluptuous.schema_builder import message
from voluptuous.validators import truth

Expand Down Expand Up @@ -29,3 +31,10 @@ def LXDIdentifier(v):
if len(v) > 63:
return False
return lxd_identifier_re.match(v)


def ExpandUserIfExists(path):
if type(path) != str:
raise Invalid("expected a string 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
10 changes: 9 additions & 1 deletion tests/unit/conf/test_validators.py
@@ -1,7 +1,9 @@
import os.path

import pytest
from voluptuous.error import ValueInvalid

from lxdock.conf.validators import Hostname, LXDIdentifier
from lxdock.conf.validators import ExpandUserIfExists, Hostname, LXDIdentifier


class TestHostnameValidator:
Expand Down Expand Up @@ -43,3 +45,9 @@ def test_cannot_allow_identifiers_longer_than_63_characters(self):
assert id_validator('i' * 63)
with pytest.raises(ValueInvalid):
id_validator('i' * 64)


class TestExpandUserIfExists:
def test_converts_dir_if_encountering_tilde(self):
expanded_path = ExpandUserIfExists("~/.ssh")
assert expanded_path == os.path.expanduser("~/.ssh")