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

Commit

Permalink
Moved provisioner specific schema rules to provisioner subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Aubert committed Mar 22, 2017
1 parent 96d5f9c commit b2c3337
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lxdock/conf/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from voluptuous import All, In, IsDir, Length, Required, Schema, Url
from voluptuous import All, Any, In, IsDir, Length, Required, Schema, Url

from ..provisioners import Provisioner

from .validators import Hostname, LXDIdentifier

Expand All @@ -9,13 +11,7 @@
'mode': In(['local', 'pull', ]),
'privileged': bool,
'protocol': In(['lxd', 'simplestreams', ]),
'provisioning': [{
# Common options
'type': str,

# Ansible specific options
'playbook': str,
}],
'provisioning': [], # will be set dynamically using provisioner classes...
'server': Url(),
'shares': [{
# The existence of the source directory will be checked!
Expand All @@ -33,6 +29,12 @@
}],
}

# Inserts provisioner specific schema rules in the global schema dict.
_top_level_and_containers_common_options['provisioning'] = [
Any(*[dict([('type', provisioner.name), ] + list(provisioner.schema.items()))
for provisioner in Provisioner.provisioners.values()]),
]

_container_options = {
Required('name'): LXDIdentifier(),
}
Expand Down
5 changes: 5 additions & 0 deletions lxdock/provisioners/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import subprocess
import tempfile

from voluptuous import Required

from ..network import get_ipv4_ip

from .base import Provisioner
Expand All @@ -14,6 +16,9 @@ class AnsibleProvisioner(Provisioner):
""" Allows to perform provisioning operations using Ansible. """

name = 'ansible'
schema = {
Required('playbook'): str,
}

def provision(self):
""" Performs the provisioning operations using the considered provisioner. """
Expand Down
8 changes: 8 additions & 0 deletions lxdock/provisioners/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def __new__(cls, name, bases, attrs):
if not new_provisioner.name:
raise InvalidProvisioner(
"The 'name' attribute of Provisioner subclasses cannot be None")
if not new_provisioner.schema:
raise InvalidProvisioner(
"The 'schema' attribute of Provisioner subclasses cannot be None")

return new_provisioner

Expand Down Expand Up @@ -68,6 +71,11 @@ class Provisioner(with_metaclass(_ProvisionerBase)):
# subclasses.
name = None

# The `schema` of a provisioner should be a dictionary using voluptuous helpers. This dictionary
# must define the "local" schema that should be provided in the LXDock file in order to use the
# considered provisioner.
schema = None

def __init__(self, homedir, lxd_container, options):
self.homedir = homedir
self.lxd_container = lxd_container
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/provisioners/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ def test_subclasses_cannot_be_registered_if_they_do_not_provide_a_provisioner_na
with pytest.raises(InvalidProvisioner):
class InvalidDummyHost(Provisioner):
name = None
schema = {}

def test_subclasses_cannot_be_registered_if_they_do_not_provide_a_schema_dict(self):
with pytest.raises(InvalidProvisioner):
class InvalidDummyHost(Provisioner):
name = 'myprovisioner'
schema = None

0 comments on commit b2c3337

Please sign in to comment.