From e64f3882aacc2362ec9b5942fdc20dfe20f2549b Mon Sep 17 00:00:00 2001 From: Marcin Swiderski Date: Wed, 18 May 2016 12:23:52 +0200 Subject: [PATCH 1/3] [CORE-1514] Add support for python3 runtime --- syncano_cli/scripts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/syncano_cli/scripts.py b/syncano_cli/scripts.py index 776c037..d02f776 100644 --- a/syncano_cli/scripts.py +++ b/syncano_cli/scripts.py @@ -17,6 +17,7 @@ 'nodejs_library_v1.0': '.js', 'php': '.php', 'python': '.py', + 'python3': '.py', 'python_library_v4.2': '.py', 'python_library_v5.0': '.py', 'ruby': '.rb', From 80bf8c620d38412b23344fc5422fb0ebbe7510c0 Mon Sep 17 00:00:00 2001 From: Marcin Swiderski Date: Wed, 18 May 2016 13:03:08 +0200 Subject: [PATCH 2/3] [CORE-1514] Remove workarounds for syncano-python bugs. Use newer release of syncano-python. --- setup.py | 2 +- syncano_cli/__init__.py | 12 ------------ syncano_cli/classes.py | 10 ++-------- syncano_cli/scripts.py | 18 +----------------- 4 files changed, 4 insertions(+), 38 deletions(-) diff --git a/setup.py b/setup.py index 587caec..51e1f6c 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ url='https://github.com/Syncano/syncano-cli', packages=find_packages(), license='MIT', - install_requires=['syncano>=5.0', 'PyYaml>=3.11'], + install_requires=['syncano>=5.1.0', 'PyYaml>=3.11'], test_suite='tests', entry_points=""" [console_scripts] diff --git a/syncano_cli/__init__.py b/syncano_cli/__init__.py index 1b13beb..d454b94 100644 --- a/syncano_cli/__init__.py +++ b/syncano_cli/__init__.py @@ -1,15 +1,3 @@ import logging -from contextlib import contextmanager - LOG = logging.getLogger('syncano-sync') -SYNCANO_LOG = logging.getLogger('syncano') - - -@contextmanager -def mute_log(logger=None): - logger = logger or SYNCANO_LOG - level = logger.getEffectiveLevel() - logger.setLevel(logging.ERROR) - yield - logger.setLevel(level) diff --git a/syncano_cli/classes.py b/syncano_cli/classes.py index 7f23298..9b8f62c 100644 --- a/syncano_cli/classes.py +++ b/syncano_cli/classes.py @@ -3,9 +3,7 @@ import re -from syncano.models.classes import Class - -from . import LOG, mute_log +from . import LOG ALLOWED_TYPES = {"array", "boolean", "datetime", "file", "float", "geopoint", "integer", "object", "reference", "relation", "string", @@ -13,9 +11,6 @@ ALLOWED_PERMISIONS = ('none', 'read', 'create_objects') -# FIXME: We don't require it on backend so, we should not require it in library -Class._meta.get_field('schema').required = False - def field_schema_to_str(schema): out = schema['type'] @@ -120,8 +115,7 @@ def push_classes(instance, class_dict): schema.append(field) klass.schema = schema - with mute_log(): - klass.save() + klass.save() LOG.debug('Class {0} pushed.'.format(name)) diff --git a/syncano_cli/scripts.py b/syncano_cli/scripts.py index d02f776..320e72c 100644 --- a/syncano_cli/scripts.py +++ b/syncano_cli/scripts.py @@ -5,10 +5,7 @@ import re from collections import defaultdict -from syncano.models import fields -from syncano.models.incentives import Script - -from . import LOG, mute_log +from . import LOG ALLOWED_RUNTIMES = { 'golang': '.go', @@ -24,19 +21,6 @@ 'swift': '.swift', } -# FIXME: Waiting for python library runtime_name field fix. -runtime_field = Script._meta.get_field('runtime_name') -if isinstance(runtime_field, fields.ChoiceField): - field = fields.StringField( - name=runtime_field.name, - label=runtime_field.label, - model=runtime_field.model - ) - setattr(Script, 'runtime_name', field) - Script._meta.field_names.remove(runtime_field.name) - Script._meta.fields.remove(runtime_field) - Script._meta.add_field(field) - def get_runtime_extension(runtime): try: From c3fb59668554624dccde52d118aeed282a3d7a27 Mon Sep 17 00:00:00 2001 From: Marcin Swiderski Date: Fri, 20 May 2016 15:01:08 +0200 Subject: [PATCH 3/3] [CORE-1514] Pull and push script endpoints from/to syncano. --- syncano_cli/scripts.py | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/syncano_cli/scripts.py b/syncano_cli/scripts.py index 320e72c..2c7172f 100644 --- a/syncano_cli/scripts.py +++ b/syncano_cli/scripts.py @@ -4,6 +4,7 @@ import os import re from collections import defaultdict +from syncano.exceptions import SyncanoRequestError from . import LOG @@ -53,6 +54,10 @@ def pull_scripts(instance, include): LOG.debug("Creating scripts directory") os.makedirs('scripts') + script_endpoints = defaultdict(list) + for endpoint in instance.script_endpoints.all(): + script_endpoints[endpoint.script].append(endpoint.name) + for script in instance.scripts.all(): if include and script.label not in include: @@ -86,6 +91,8 @@ def pull_scripts(instance, include): 'script': path, 'runtime': script.runtime_name } + if script.id in script_endpoints: + script_info['endpoints'] = script_endpoints[script.id] if script.config: script_info['config'] = script.config @@ -94,7 +101,7 @@ def pull_scripts(instance, include): return pulled -def push_scripts(instance, scripts): +def push_scripts(instance, scripts, config_only=True): """ Push selected scripts to instance. - instance - instance object to push scripts to @@ -103,10 +110,18 @@ def push_scripts(instance, scripts): """ LOG.debug('Pushing scripts') LOG.debug('Pulling remote scripts') + + endpoints = {} remote_scripts_mapping = defaultdict(list) for remote_script in instance.scripts.all(): remote_scripts_mapping[remote_script.label].append(remote_script) + existing_endpoints = defaultdict(list) + + for endpoint in instance.script_endpoints.all(): + existing_endpoints[endpoint.script].append(endpoint.name) + endpoints[endpoint.name] = endpoint + LOG.debug('Pushing local scripts') for s in scripts: if s['label'] in remote_scripts_mapping: @@ -130,8 +145,29 @@ def push_scripts(instance, scripts): remote_script.config.update(config) LOG.debug('Pushing script {label}'.format(**s)) - with mute_log(): - remote_script.save() + remote_script.save() + + existing_set = {name for name in existing_endpoints[remote_script.id]} + script_endpoints = set(s.get('endpoints', [])) + new_endpoints = script_endpoints - existing_set + old_endpoints = existing_set - script_endpoints + + for name in old_endpoints: + endpoints[name].delete() + + for name in new_endpoints: + endpoint = instance.script_endpoints.model( + instance_name=instance.name, + name=name, + script=remote_script.id + ) + try: + endpoint.save() + except SyncanoRequestError as e: + raise ValueError( + 'Error when saving endpoint "{0}" for script "{1}": {2}.' + .format(name, remote_script.label, e.message) + ) def validate_script(script):