ORM: resource, actions, handlers fixes
This commit is contained in:
parent
81484ac8ab
commit
d90a5bee8e
@ -689,12 +689,13 @@ resources_to_run = [
|
||||
'neutron_agents_ml22',
|
||||
]
|
||||
|
||||
|
||||
@click.command()
|
||||
def deploy():
|
||||
setup_resources()
|
||||
|
||||
# run
|
||||
resources = map(resource.wrap_resource, db.get_list(collection=db.COLLECTIONS.resource))
|
||||
resources = resource.load_all()
|
||||
resources = {r.name: r for r in resources}
|
||||
|
||||
for name in resources_to_run:
|
||||
|
@ -25,7 +25,7 @@ _default_transports = {
|
||||
}
|
||||
|
||||
def resource_action(resource, action):
|
||||
handler = resource.metadata.get('handler', 'none')
|
||||
handler = resource.db_obj.handler or 'none'
|
||||
with handlers.get(handler)([resource], _default_transports) as h:
|
||||
return h.action(resource, action)
|
||||
|
||||
|
@ -72,8 +72,7 @@ class TempFileHandler(BaseHandler):
|
||||
def _render_action(self, resource, action):
|
||||
log.debug('Rendering %s %s', resource.name, action)
|
||||
|
||||
action_file = resource.metadata['actions'][action]
|
||||
action_file = os.path.join(resource.metadata['actions_path'], action_file)
|
||||
action_file = resource.actions[action]
|
||||
log.debug('action file: %s', action_file)
|
||||
args = self._make_args(resource)
|
||||
|
||||
@ -88,7 +87,7 @@ class TempFileHandler(BaseHandler):
|
||||
trg_templates_dir = None
|
||||
trg_scripts_dir = None
|
||||
|
||||
base_path = resource.metadata['base_path']
|
||||
base_path = resource.db_obj.base_path
|
||||
src_templates_dir = os.path.join(base_path, 'templates')
|
||||
if os.path.exists(src_templates_dir):
|
||||
trg_templates_dir = os.path.join(self.dirs[resource.name], 'templates')
|
||||
@ -111,7 +110,7 @@ class TempFileHandler(BaseHandler):
|
||||
|
||||
def _make_args(self, resource):
|
||||
args = {'resource_name': resource.name}
|
||||
args['resource_dir'] = resource.metadata['base_path']
|
||||
args['resource_dir'] = resource.db_obj.base_path
|
||||
args['templates_dir'] = 'templates/'
|
||||
args['scripts_dir'] = 'scripts/'
|
||||
args.update(resource.args)
|
||||
|
@ -31,7 +31,7 @@ class LibrarianPuppet(object):
|
||||
def install(self):
|
||||
puppet_module = '{}-{}'.format(
|
||||
self.organization,
|
||||
self.resource.metadata['puppet_module']
|
||||
self.resource.db_obj.puppet_module
|
||||
)
|
||||
|
||||
puppetlabs = self.transport_run.run(
|
||||
@ -40,7 +40,7 @@ class LibrarianPuppet(object):
|
||||
)
|
||||
log.debug('Puppetlabs file is: \n%s\n', puppetlabs)
|
||||
|
||||
git = self.resource.args['git'].value
|
||||
git = self.resource.args['git']
|
||||
|
||||
definition = "mod '{module_name}', :git => '{repository}', :ref => '{branch}'".format(
|
||||
module_name=puppet_module,
|
||||
@ -121,19 +121,19 @@ class Puppet(TempFileHandler):
|
||||
return cmd
|
||||
|
||||
def clone_manifests(self, resource):
|
||||
git = resource.args['git'].value
|
||||
git = resource.args['git']
|
||||
p = GitProvider(git['repository'], branch=git['branch'])
|
||||
|
||||
return p.directory
|
||||
|
||||
def upload_manifests(self, resource):
|
||||
if 'forge' in resource.args and resource.args['forge'].value:
|
||||
if 'forge' in resource.args and resource.args['forge']:
|
||||
self.upload_manifests_forge(resource)
|
||||
else:
|
||||
self.upload_manifests_librarian(resource)
|
||||
|
||||
def upload_manifests_forge(self, resource):
|
||||
forge = resource.args['forge'].value
|
||||
forge = resource.args['forge']
|
||||
|
||||
# Check if module already installed
|
||||
modules = self.transport_run.run(
|
||||
|
@ -21,23 +21,15 @@ from solar.interfaces import orm
|
||||
from solar import utils
|
||||
|
||||
|
||||
def prepare_meta(meta):
|
||||
actions_path = os.path.join(meta['base_path'], 'actions')
|
||||
meta['actions_path'] = actions_path
|
||||
meta['base_name'] = os.path.split(meta['base_path'])[-1]
|
||||
|
||||
meta['actions'] = {}
|
||||
if os.path.exists(meta['actions_path']):
|
||||
for f in os.listdir(meta['actions_path']):
|
||||
meta['actions'][os.path.splitext(f)[0]] = f
|
||||
|
||||
|
||||
def read_meta(base_path):
|
||||
base_meta_file = os.path.join(base_path, 'meta.yaml')
|
||||
|
||||
metadata = utils.yaml_load(base_meta_file)
|
||||
metadata['version'] = '1.0.0'
|
||||
metadata['base_path'] = os.path.abspath(base_path)
|
||||
actions_path = os.path.join(metadata['base_path'], 'actions')
|
||||
metadata['actions_path'] = actions_path
|
||||
metadata['base_name'] = os.path.split(metadata['base_path'])[-1]
|
||||
|
||||
return metadata
|
||||
|
||||
@ -50,9 +42,9 @@ class Resource(object):
|
||||
def __init__(self, name, base_path, args, tags=None, virtual_resource=None):
|
||||
self.name = name
|
||||
if base_path:
|
||||
self.metadata = read_meta(base_path)
|
||||
metadata = read_meta(base_path)
|
||||
else:
|
||||
self.metadata = deepcopy(self._metadata)
|
||||
metadata = deepcopy(self._metadata)
|
||||
|
||||
self.tags = tags or []
|
||||
self.virtual_resource = virtual_resource
|
||||
@ -60,12 +52,13 @@ class Resource(object):
|
||||
self.db_obj = orm.DBResource(**{
|
||||
'id': name,
|
||||
'name': name,
|
||||
'actions_path': self.metadata.get('actions_path', ''),
|
||||
'base_name': self.metadata.get('base_name', ''),
|
||||
'base_path': self.metadata.get('base_path', ''),
|
||||
'handler': self.metadata.get('handler', ''),
|
||||
'version': self.metadata.get('version', ''),
|
||||
'meta_inputs': self.metadata.get('input', {})
|
||||
'actions_path': metadata.get('actions_path', ''),
|
||||
'base_name': metadata.get('base_name', ''),
|
||||
'base_path': metadata.get('base_path', ''),
|
||||
'handler': metadata.get('handler', ''),
|
||||
'puppet_module': metadata.get('puppet_module', ''),
|
||||
'version': metadata.get('version', ''),
|
||||
'meta_inputs': metadata.get('input', {})
|
||||
})
|
||||
self.db_obj.save()
|
||||
|
||||
@ -82,7 +75,16 @@ class Resource(object):
|
||||
|
||||
@property
|
||||
def actions(self):
|
||||
return self.resource_db.actions or []
|
||||
ret = {
|
||||
os.path.splitext(p)[0]: os.path.join(
|
||||
self.db_obj.actions_path, p
|
||||
)
|
||||
for p in os.listdir(self.db_obj.actions_path)
|
||||
}
|
||||
|
||||
return {
|
||||
k: v for k, v in ret.items() if os.path.isfile(v)
|
||||
}
|
||||
|
||||
def create_inputs(self, args):
|
||||
for name, v in self.db_obj.meta_inputs.items():
|
||||
@ -124,5 +126,4 @@ def load(name):
|
||||
|
||||
# TODO
|
||||
def load_all():
|
||||
return [wrap_resource(r) for r
|
||||
in db.all(collection=db.COLLECTIONS.resource)]
|
||||
return [Resource(r) for r in orm.DBResource.load_all()]
|
||||
|
@ -13,6 +13,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from solar.core.log import log
|
||||
|
||||
|
||||
def guess_mapping(emitter, receiver):
|
||||
"""Guess connection mapping between emitter and receiver.
|
||||
@ -76,6 +78,9 @@ def connect_single(emitter, src, receiver, dst):
|
||||
if emitter_input in receiver_input.receivers.value:
|
||||
raise Exception('Prevented creating a cycle')
|
||||
|
||||
log.debug('Connecting {}::{} -> {}::{}'.format(
|
||||
emitter.name, emitter_input.name, receiver.name, receiver_input.name
|
||||
))
|
||||
emitter_input.receivers.add(receiver_input)
|
||||
|
||||
|
||||
|
@ -28,13 +28,13 @@ class _SSHTransport(object):
|
||||
def _fabric_settings(self, resource):
|
||||
return {
|
||||
'host_string': self._ssh_command_host(resource),
|
||||
'key_filename': resource.args['ssh_key'].value,
|
||||
'key_filename': resource.args['ssh_key'],
|
||||
}
|
||||
|
||||
# TODO: maybe static/class method ?
|
||||
def _ssh_command_host(self, resource):
|
||||
return '{}@{}'.format(resource.args['ssh_user'].value,
|
||||
resource.args['ip'].value)
|
||||
return '{}@{}'.format(resource.args['ssh_user'],
|
||||
resource.args['ip'])
|
||||
|
||||
|
||||
class SSHSyncTransport(SyncTransport, _SSHTransport):
|
||||
|
@ -171,14 +171,14 @@ def validate_resource(r):
|
||||
"""
|
||||
ret = {}
|
||||
|
||||
input_schemas = r.metadata['input']
|
||||
inputs = r.resource_inputs()
|
||||
args = r.args
|
||||
|
||||
for input_name, input_definition in input_schemas.items():
|
||||
for input_name, input_definition in inputs.items():
|
||||
errors = validate_input(
|
||||
args.get(input_name),
|
||||
jsonschema=input_definition.get('jsonschema'),
|
||||
schema=input_definition.get('schema')
|
||||
#jsonschema=input_definition.get('jsonschema'),
|
||||
schema=input_definition.schema
|
||||
)
|
||||
if errors:
|
||||
ret[input_name] = errors
|
||||
|
@ -316,6 +316,12 @@ class DBObject(object):
|
||||
|
||||
return cls(**r.properties)
|
||||
|
||||
@classmethod
|
||||
def load_all(cls):
|
||||
rs = db.all(collection=cls._collection)
|
||||
|
||||
return [cls(**r.properties) for r in rs]
|
||||
|
||||
def save(self):
|
||||
db.create(
|
||||
self._db_key,
|
||||
@ -365,6 +371,7 @@ class DBResource(DBObject):
|
||||
base_name = db_field(schema='str')
|
||||
base_path = db_field(schema='str')
|
||||
handler = db_field(schema='str') # one of: {'ansible_playbook', 'ansible_template', 'puppet', etc}
|
||||
puppet_module = db_field(schema='str')
|
||||
version = db_field(schema='str')
|
||||
tags = db_field(schema=[], default_value=[])
|
||||
meta_inputs = db_field(schema={}, default_value={})
|
||||
|
Loading…
x
Reference in New Issue
Block a user