From 07e5d9845a7757a13bb0f2348ac397caa8f30c08 Mon Sep 17 00:00:00 2001 From: Przemyslaw Kaminski Date: Wed, 27 May 2015 17:06:19 +0200 Subject: [PATCH] Validation fixes --- example.py | 16 ++++++++++---- resources/data_container/actions/run.yml | 6 +++++- resources/docker_container/actions/run.yml | 4 ++++ resources/docker_container/meta.yaml | 4 ++-- resources/haproxy/meta.yaml | 10 ++++----- resources/haproxy_keystone_config/meta.yaml | 4 ++-- resources/mariadb_db/actions/run.yml | 6 +++--- resources/mariadb_service/meta.yaml | 4 ++-- resources/mariadb_user/actions/run.yml | 10 ++++----- solar/solar/core/handlers/base.py | 24 +++++++++++---------- solar/solar/core/observer.py | 2 +- 11 files changed, 54 insertions(+), 36 deletions(-) diff --git a/example.py b/example.py index 67ffcad7..179be71d 100644 --- a/example.py +++ b/example.py @@ -1,6 +1,7 @@ import shutil import os import requests +import sys import time from solar.core import resource @@ -20,15 +21,15 @@ os.mkdir('rs') node1 = resource.create('node1', 'resources/ro_node/', {'ip':'10.0.0.3', 'ssh_key' : '/vagrant/.vagrant/machines/solar-dev2/virtualbox/private_key', 'ssh_user':'vagrant'}) node2 = resource.create('node2', 'resources/ro_node/', {'ip':'10.0.0.4', 'ssh_key' : '/vagrant/.vagrant/machines/solar-dev3/virtualbox/private_key', 'ssh_user':'vagrant'}) -mariadb_service1 = resource.create('mariadb_service1', 'resources/mariadb_service', {'image':'mariadb', 'root_password' : 'mariadb', 'port' : '3306', 'ip': '', 'ssh_user': '', 'ssh_key': ''}) +mariadb_service1 = resource.create('mariadb_service1', 'resources/mariadb_service', {'image':'mariadb', 'root_password' : 'mariadb', 'port' : 3306, 'ip': '', 'ssh_user': '', 'ssh_key': ''}) keystone_db = resource.create('keystone_db', 'resources/mariadb_db/', {'db_name':'keystone_db', 'login_password':'', 'login_user':'root', 'login_port': '', 'ip':'', 'ssh_user':'', 'ssh_key':''}) keystone_db_user = resource.create('keystone_db_user', 'resources/mariadb_user/', {'new_user_name' : 'keystone', 'new_user_password' : 'keystone', 'db_name':'', 'login_password':'', 'login_user':'root', 'login_port': '', 'ip':'', 'ssh_user':'', 'ssh_key':''}) keystone_config1 = resource.create('keystone_config1', 'resources/keystone_config/', {'config_dir' : '/etc/solar/keystone', 'ip':'', 'ssh_user':'', 'ssh_key':'', 'admin_token':'admin', 'db_password':'', 'db_name':'', 'db_user':'', 'db_host':''}) -keystone_service1 = resource.create('keystone_service1', 'resources/keystone_service/', {'port':'5001', 'admin_port':'35357', 'image': '', 'ip':'', 'ssh_key':'', 'ssh_user':'', 'config_dir':''}) +keystone_service1 = resource.create('keystone_service1', 'resources/keystone_service/', {'port': 5001, 'admin_port': 35357, 'image': '', 'ip':'', 'ssh_key':'', 'ssh_user':'', 'config_dir':''}) keystone_config2 = resource.create('keystone_config2', 'resources/keystone_config/', {'config_dir' : '/etc/solar/keystone', 'ip':'', 'ssh_user':'', 'ssh_key':'', 'admin_token':'admin', 'db_password':'', 'db_name':'', 'db_user':'', 'db_host':''}) -keystone_service2 = resource.create('keystone_service2', 'resources/keystone_service/', {'port':'5002', 'admin_port':'35357', 'image': '', 'ip':'', 'ssh_key':'', 'ssh_user':'', 'config_dir':''}) +keystone_service2 = resource.create('keystone_service2', 'resources/keystone_service/', {'port': 5002, 'admin_port': 35357, 'image': '', 'ip':'', 'ssh_key':'', 'ssh_user':'', 'config_dir':''}) haproxy_keystone_config = resource.create('haproxy_keystone1_config', 'resources/haproxy_keystone_config/', {'name':'keystone_config', 'listen_port':'5000', 'servers':[], 'ports':[]}) @@ -78,6 +79,7 @@ signals.connect(haproxy_config, haproxy_service, {'listen_ports':'ports', 'confi from solar.core import validation +has_errors = False for r in [node1, node2, mariadb_service1, @@ -90,7 +92,13 @@ for r in [node1, haproxy_keystone_config, haproxy_config, haproxy_service]: - validation.validate_resource(r) + errors = validation.validate_resource(r) + if errors: + has_errors = True + print 'ERROR: %s: %s' % (r.name, errors) + +if has_errors: + sys.exit(1) #run diff --git a/resources/data_container/actions/run.yml b/resources/data_container/actions/run.yml index 3a63f6fc..9bccb8cf 100644 --- a/resources/data_container/actions/run.yml +++ b/resources/data_container/actions/run.yml @@ -6,10 +6,13 @@ image: {{ image }} state: running net: host + {% if ports.value %} ports: {% for port in ports.value %} - {{ port['value'] }}:{{ port['value'] }} {% endfor %} + {% endif %} + {% if host_binds.value %} volumes: # TODO: host_binds might need more work # Currently it's not that trivial to pass custom src: dst here @@ -17,4 +20,5 @@ # so we mount it to the same directory as on host {% for bind in host_binds.value %} - {{ bind['value']['src'] }}:{{ bind['value']['dst'] }}:{{ bind['value'].get('mode', 'ro') }} - {% endfor %} \ No newline at end of file + {% endfor %} + {% endif %} diff --git a/resources/docker_container/actions/run.yml b/resources/docker_container/actions/run.yml index 6dc492a7..d0527737 100644 --- a/resources/docker_container/actions/run.yml +++ b/resources/docker_container/actions/run.yml @@ -7,12 +7,15 @@ image: {{ image }} state: running net: host + {% if ports.value %} ports: {% for port in ports.value %} {% for p in port['value'] %} - {{ p['value'] }}:{{ p['value'] }} {% endfor %} {% endfor %} + {% endif %} + {% if host_binds.value %} volumes: # TODO: host_binds might need more work # Currently it's not that trivial to pass custom src: dst here @@ -21,3 +24,4 @@ {% for bind in host_binds.value %} - {{ bind['value']['src'] }}:{{ bind['value']['dst'] }}:{{ bind['value'].get('mode', 'ro') }} {% endfor %} + {% endif %} diff --git a/resources/docker_container/meta.yaml b/resources/docker_container/meta.yaml index 8d3c666d..9808149d 100644 --- a/resources/docker_container/meta.yaml +++ b/resources/docker_container/meta.yaml @@ -9,10 +9,10 @@ input: schema: str! value: ports: - schema: [int] + schema: [{value: [{value: int}]}] value: [] host_binds: - schema: [[int]] + schema: [{value: {src: str, dst: str}}] value: [] volume_binds: schema: [{src: str, dst: str}] diff --git a/resources/haproxy/meta.yaml b/resources/haproxy/meta.yaml index e884a883..7e3125cd 100644 --- a/resources/haproxy/meta.yaml +++ b/resources/haproxy/meta.yaml @@ -3,22 +3,22 @@ handler: ansible version: 1.0.0 input: ip: - schema: int! + schema: str! value: config_dir: schema: {src: str!, dst: str!} value: {src: /etc/solar/haproxy, dst: /etc/haproxy} listen_ports: - schema: [int] + schema: [{value: int}] value: [] configs: - schema: [[str]] + schema: [{value: [{value: str}]}] value: [] configs_names: - schema: [str] + schema: [{value: str}] value: [] configs_ports: - schema: [[int]] + schema: [{value: [{value: int}]}] value: [] ssh_user: schema: str! diff --git a/resources/haproxy_keystone_config/meta.yaml b/resources/haproxy_keystone_config/meta.yaml index 0d33630f..15bf930c 100644 --- a/resources/haproxy_keystone_config/meta.yaml +++ b/resources/haproxy_keystone_config/meta.yaml @@ -9,10 +9,10 @@ input: schema: int! value: 9999 ports: - schema: [int] + schema: [{value: int}] value: servers: - schema: [str] + schema: [{value: str}] value: tags: [resources/haproxy, resource/haproxy_keystone_config] diff --git a/resources/mariadb_db/actions/run.yml b/resources/mariadb_db/actions/run.yml index 0efb73ed..4b315b9b 100644 --- a/resources/mariadb_db/actions/run.yml +++ b/resources/mariadb_db/actions/run.yml @@ -3,9 +3,9 @@ tasks: - name: mariadb db mysql_db: - name: {{db_name}} + name: {{ db_name }} state: present login_user: root - login_password: {{login_password}} - login_port: {{login_port}} + login_password: {{ login_password }} + login_port: {{ login_port }} login_host: 127.0.0.1 diff --git a/resources/mariadb_service/meta.yaml b/resources/mariadb_service/meta.yaml index 0df9dd00..826a0625 100644 --- a/resources/mariadb_service/meta.yaml +++ b/resources/mariadb_service/meta.yaml @@ -9,10 +9,10 @@ input: schema: str! value: password port: - schema: str! + schema: int! value: 3306 ip: - schema: int! + schema: str! value: ssh_key: schema: str! diff --git a/resources/mariadb_user/actions/run.yml b/resources/mariadb_user/actions/run.yml index b0981803..d600c13d 100644 --- a/resources/mariadb_user/actions/run.yml +++ b/resources/mariadb_user/actions/run.yml @@ -3,12 +3,12 @@ tasks: - name: mariadb user mysql_user: - name: {{new_user_name}} - password: {{new_user_password}} - priv: {{db_name}}.*:ALL + name: {{ new_user_name }} + password: {{ new_user_password }} + priv: {{ db_name }}.*:ALL host: '%' state: present login_user: root - login_password: {{login_password}} - login_port: {{login_port}} + login_password: {{ login_password }} + login_port: {{ login_port }} login_host: 127.0.0.1 diff --git a/solar/solar/core/handlers/base.py b/solar/solar/core/handlers/base.py index f801a1ec..6fb11623 100644 --- a/solar/solar/core/handlers/base.py +++ b/solar/solar/core/handlers/base.py @@ -24,22 +24,24 @@ class BaseHandler(object): shutil.rmtree(self.dst) def _compile_action_file(self, resource, action): - action_file = resource.metadata['actions'][action] - action_file = os.path.join(resource.metadata['actions_path'], action_file) dir_path = self.dirs[resource.name] dest_file = tempfile.mkstemp(text=True, prefix=action, dir=dir_path)[1] - args = self._make_args(resource) - self._compile_file(action_file, dest_file, args) + + with open(dest_file, 'w') as f: + f.write(self._render_action(resource, action)) + return dest_file - def _compile_file(self, template, dest_file, args): - print 'Rendering', template, args - with open(template) as f: - tpl = Template(f.read()) - tpl = tpl.render(args, zip=zip) + def _render_action(self, resource, action): + print 'Rendering %s %s' % (resource.name, action) - with open(dest_file, 'w') as g: - g.write(tpl) + action_file = resource.metadata['actions'][action] + action_file = os.path.join(resource.metadata['actions_path'], action_file) + args = self._make_args(resource) + + with open(action_file) as f: + tpl = Template(f.read()) + return tpl.render(str=str, zip=zip, **args) def _make_args(self, resource): args = {'name': resource.name} diff --git a/solar/solar/core/observer.py b/solar/solar/core/observer.py index d2d37d1f..3c3cd718 100644 --- a/solar/solar/core/observer.py +++ b/solar/solar/core/observer.py @@ -23,7 +23,7 @@ class BaseObserver(object): return '[{}:{}] {}'.format(self.attached_to.name, self.name, self.value) def __unicode__(self): - return self.value + return unicode(self.value) def __eq__(self, other): if isinstance(other, BaseObserver):