2014-02-23 20:14:23 +01:00
|
|
|
from common.tools import load_json
|
2013-06-09 20:29:54 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2013-06-09 15:50:00 +02:00
|
|
|
|
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
class Manifest(object):
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
self.load()
|
|
|
|
self.validate()
|
|
|
|
self.parse()
|
2013-06-09 15:50:00 +02:00
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
def load(self):
|
2014-02-23 20:14:23 +01:00
|
|
|
self.data = load_json(self.path)
|
2014-01-05 14:03:04 +01:00
|
|
|
provider_modname = 'providers.{provider}'.format(provider=self.data['provider'])
|
|
|
|
log.debug('Loading provider `{modname}\''.format(modname=provider_modname))
|
|
|
|
self.modules = {'provider': __import__(provider_modname, fromlist=['providers']),
|
|
|
|
'plugins': [],
|
|
|
|
}
|
|
|
|
if 'plugins' in self.data:
|
|
|
|
for plugin_name, plugin_data in self.data['plugins'].iteritems():
|
|
|
|
modname = 'plugins.{plugin}'.format(plugin=plugin_name)
|
|
|
|
log.debug('Loading plugin `{modname}\''.format(modname=modname))
|
|
|
|
plugin = __import__(modname, fromlist=['plugins'])
|
|
|
|
self.modules['plugins'].append(plugin)
|
2013-05-02 19:13:35 +02:00
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
self.modules['provider'].initialize()
|
|
|
|
for module in self.modules['plugins']:
|
|
|
|
init = getattr(module, 'initialize', None)
|
|
|
|
if callable(init):
|
|
|
|
init()
|
2013-05-02 19:13:35 +02:00
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
def validate(self):
|
|
|
|
from . import validate_manifest
|
|
|
|
validate_manifest(self.data, self.schema_validator, self.validation_error)
|
|
|
|
self.modules['provider'].validate_manifest(self.data, self.schema_validator, self.validation_error)
|
|
|
|
for plugin in self.modules['plugins']:
|
|
|
|
validate = getattr(plugin, 'validate_manifest', None)
|
|
|
|
if callable(validate):
|
|
|
|
validate(self.data, self.schema_validator, self.validation_error)
|
2013-06-26 20:14:37 +02:00
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
def parse(self):
|
|
|
|
self.provider = self.data['provider']
|
|
|
|
self.bootstrapper = self.data['bootstrapper']
|
|
|
|
self.image = self.data['image']
|
|
|
|
self.volume = self.data['volume']
|
|
|
|
self.system = self.data['system']
|
2014-02-23 20:53:58 +01:00
|
|
|
self.packages = self.data['packages'] if 'packages' in self.data else {}
|
2014-01-05 14:03:04 +01:00
|
|
|
self.plugins = self.data['plugins'] if 'plugins' in self.data else {}
|
2013-06-23 21:50:18 +02:00
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
def load_json(self, path):
|
|
|
|
import json
|
|
|
|
from minify_json import json_minify
|
|
|
|
with open(path) as stream:
|
|
|
|
return json.loads(json_minify(stream.read(), False))
|
2013-06-26 20:14:37 +02:00
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
def schema_validator(self, data, schema_path):
|
2013-06-23 23:37:21 +02:00
|
|
|
import jsonschema
|
2014-02-23 20:14:23 +01:00
|
|
|
schema = load_json(schema_path)
|
2013-06-23 23:37:21 +02:00
|
|
|
try:
|
|
|
|
jsonschema.validate(data, schema)
|
|
|
|
except jsonschema.ValidationError as e:
|
2014-01-05 14:03:04 +01:00
|
|
|
self.validation_error(e.message, e.path)
|
2013-05-02 19:13:35 +02:00
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
def validation_error(self, message, json_path=None):
|
2014-01-05 15:29:30 +01:00
|
|
|
from common.exceptions import ManifestError
|
|
|
|
raise ManifestError(message, self.path, json_path)
|