From da3724cd3f436c8eb1cb9a6ebb15b02d87c8fcb0 Mon Sep 17 00:00:00 2001 From: Evgeniy L Date: Thu, 14 May 2015 16:02:40 +0200 Subject: [PATCH] Implemented resources to nodes assignment --- config.yml | 3 ++- solar/solar/cli.py | 21 ++++++++++++++++++--- solar/solar/core/resource.py | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/config.yml b/config.yml index 5ced636f..0889c6bb 100644 --- a/config.yml +++ b/config.yml @@ -9,4 +9,5 @@ file-system-db: storage-path: /vagrant/tmp/storage template-dir: /vagrant/templates -resources-files-mask: /vagrant/examples/resources/*.yml +resources-files-mask: /vagrant/x/resources/*/*.yaml +resource-instances-path: /vagrant/tmp/resource-instances diff --git a/solar/solar/cli.py b/solar/solar/cli.py index f5f1ee24..ea119d6a 100644 --- a/solar/solar/cli.py +++ b/solar/solar/cli.py @@ -28,6 +28,7 @@ import yaml from solar import extensions from solar import utils from solar.core import data +from solar.core.resource import assign_resources_to_nodes from solar.core.tags_set_parser import Expression from solar.interfaces.db import get_db @@ -109,15 +110,29 @@ class Cmd(object): def assign(self, args): nodes = filter( - lambda n: Expression(args.nodes, n['tags']).evaluate(), + lambda n: Expression(args.nodes, n.get('tags', [])).evaluate(), self.db.get_list('nodes')) resources = filter( - lambda r: Expression(args.resources, r['tags']).evaluate(), + lambda r: Expression(args.resources, r.get('tags', [])).evaluate(), self._get_resources_list()) + resource_instances_path = utils.read_config()['resource-instances-path'] + utils.create_dir(resource_instances_path) + assign_resources_to_nodes( + resources, + nodes, + resource_instances_path) + def _get_resources_list(self): - return utils.load_by_mask(utils.read_config()['resources-files-mask']) + result = [] + for path in utils.find_by_mask(utils.read_config()['resources-files-mask']): + resource = utils.yaml_load(path) + resource['path'] = path + resource['dir_path'] = os.path.dirname(path) + result.append(resource) + + return result def main(): diff --git a/solar/solar/core/resource.py b/solar/solar/core/resource.py index 93977bd5..10431af1 100644 --- a/solar/solar/core/resource.py +++ b/solar/solar/core/resource.py @@ -4,6 +4,8 @@ import json import os import shutil +from copy import deepcopy + import yaml from solar.core import actions @@ -174,3 +176,19 @@ def load_all(dest_path): signals.Connections.reconnect_all() return ret + + +def assign_resources_to_nodes(resources, nodes, dst_dir): + for node in nodes: + for resource in resources: + merged = deepcopy(resource) + # Node specific setting should override resource's + merged.update(deepcopy(node)) + # Tags for specific resource is set of tags from node and from resource + merged['tags'] = list(set(node.get('tags', [])) + set(resource.get('tags', []))) + + create( + format('{0}-{1}'.format(node['id'], resource['id'])), + resource['dir_path'], + dst_dir, + merged)