From 00ac4e3a0b9c6d3b13d0e4a71b4192052d54f983 Mon Sep 17 00:00:00 2001 From: Jedrzej Nowak Date: Fri, 18 Sep 2015 15:29:51 +0200 Subject: [PATCH] Still wip --- resources/hosts_file/meta.yaml | 9 ----- resources/ro_node/meta.yaml | 5 ++- resources/transport_ssh/meta.yaml | 4 +++ resources/transports/meta.yaml | 8 +++++ solar/solar/core/resource/resource.py | 17 ++++++++- solar/solar/core/signals.py | 50 ++++++++++++++++++++++----- solar/solar/interfaces/orm.py | 7 ++++ 7 files changed, 81 insertions(+), 19 deletions(-) diff --git a/resources/hosts_file/meta.yaml b/resources/hosts_file/meta.yaml index b55243f3..255209e2 100644 --- a/resources/hosts_file/meta.yaml +++ b/resources/hosts_file/meta.yaml @@ -3,14 +3,5 @@ handler: ansible version: 1.0.0 input: - ip: - schema: str! - value: - ssh_key: - schema: str! - value: - ssh_user: - schema: str! - value: hosts: schema: [{name: str!, ip: str!}] diff --git a/resources/ro_node/meta.yaml b/resources/ro_node/meta.yaml index 32ee7d1e..1f762669 100644 --- a/resources/ro_node/meta.yaml +++ b/resources/ro_node/meta.yaml @@ -14,4 +14,7 @@ input: # value: name: schema: str - value: + value: a node + location_id: + schema: str! + value: $uuid diff --git a/resources/transport_ssh/meta.yaml b/resources/transport_ssh/meta.yaml index a05a5c8a..4a614ffa 100644 --- a/resources/transport_ssh/meta.yaml +++ b/resources/transport_ssh/meta.yaml @@ -12,3 +12,7 @@ input: name: schema: str! value: ssh + location_id: + schema: str! + value: + reverse: True diff --git a/resources/transports/meta.yaml b/resources/transports/meta.yaml index a7e596ce..b5c70947 100644 --- a/resources/transports/meta.yaml +++ b/resources/transports/meta.yaml @@ -3,3 +3,11 @@ input: transports: schema: [{user: str, password: str, port: int!, key: str, name: str!}] value: [] + transports_id: + schema: str! + value: $uuid + reverse: True + location_id: + schema: str! + value: + reverse: True diff --git a/solar/solar/core/resource/resource.py b/solar/solar/core/resource/resource.py index b80d48b5..6d6db633 100644 --- a/solar/solar/core/resource/resource.py +++ b/solar/solar/core/resource/resource.py @@ -87,8 +87,23 @@ class Resource(object): self.virtual_resource = None def auto_extend_inputs(self, inputs): - inputs.setdefault('location_id', {'value': md5(self.name + uuid4().hex).hexdigest(), + # inputs.setdefault('location_id', {'value': md5(self.name + uuid4().hex).hexdigest(), + # 'schema': 'str!'}) + inputs.setdefault('location_id', {'value': "", 'schema': 'str!'}) + inputs.setdefault('transports_id', {'value': "", + 'schema': 'str'}) + for inp in ('transports_id', 'location_id'): + if inputs[inp]['value'] == '$uuid': + inputs[inp]['value'] = md5(self.name + uuid4().hex).hexdigest() + + def transports(self): + inputs = self.resource_inputs() + location_id = inputs['transports_id'] + print 'tu' + print location_id.backtrack_value(lazy_val='transports') + print 'tu2' + @property def actions(self): diff --git a/solar/solar/core/signals.py b/solar/solar/core/signals.py index af75c8f2..e01a8754 100644 --- a/solar/solar/core/signals.py +++ b/solar/solar/core/signals.py @@ -46,17 +46,50 @@ def guess_mapping(emitter, receiver): return guessed -def extend_mapping_by_defaults(mapping): - if isinstance(mapping, set): - mapping.add('location_id') - else: - mapping['location_id'] = 'location_id' + +def location_and_transports(emitter, receiver, orig_mapping): + + def _remove_from_mapping(single): + if single in orig_mapping: + if isinstance(orig_mapping, dict): + del orig_mapping[single] + elif isinstance(orig_mapping, set): + orig_mapping.remove(single) + + def _single(single): + emitter_single = emitter.db_obj.meta_inputs[single] + receiver_single = receiver.db_obj.meta_inputs[single] + emitter_single_reverse = emitter_single.get('reverse') + receiver_single_reverse = receiver_single.get('reverse') + print emitter_single, receiver_single + # connect in other direction + if emitter_single_reverse: + connect_single(receiver, single, emitter, single) + _remove_from_mapping(single) + return + if isinstance(orig_mapping, dict): + orig_mapping[single] = single + # if we have reverse then do it in reverse way + # if emitter_single.get('reverse'): + # connect_single(receiver, single, emitter, single) + # # maybe already connected somewhere, then don't create this relation + # if not emitter.resource_inputs()[single].backtrack_value(): + # connect_single(receiver, single, emitter, single) + # elif not receiver_single.get('reverse', False): + # # maybe already connected somewhere, then don't create this relation + # if not receiver.resource_inputs()[single].backtrack_value(): + # if isinstance(orig_mapping, dict): + # orig_mapping[single] = single + + for single in ('transports_id', 'location_id'): + _single(single) + return + def connect(emitter, receiver, mapping=None, events=None): mapping = mapping or guess_mapping(emitter, receiver) - extend_mapping_by_defaults(mapping) - + location_and_transports(emitter, receiver, mapping) if isinstance(mapping, set): mapping = {src: src for src in mapping} @@ -102,7 +135,8 @@ def connect_single(emitter, src, receiver, dst): # Check for cycles # TODO: change to get_paths after it is implemented in drivers if emitter_input in receiver_input.receivers.as_set(): - raise Exception('Prevented creating a cycle') + raise Exception('Prevented creating a cycle on %s::%s' % (emitter.name, + emitter_input.name)) log.debug('Connecting {}::{} -> {}::{}'.format( emitter.name, emitter_input.name, receiver.name, receiver_input.name diff --git a/solar/solar/interfaces/orm.py b/solar/solar/interfaces/orm.py index 3cb69113..9b369eb1 100644 --- a/solar/solar/interfaces/orm.py +++ b/solar/solar/interfaces/orm.py @@ -445,6 +445,13 @@ class DBResourceInput(DBObject): if not inputs: return self + # if lazy_val is None: + # return self.value + # print self.resource.name + # print [x.name for x in self.resource.inputs.as_set()] + # _input = next(x for x in self.resource.inputs.as_set() if x.name == lazy_val) + # return _input.backtrack_value() + # # return self.value if self.is_list: if not self.is_hash: return [backtrack_func(i) for i in inputs]