Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 0 additions & 12 deletions syncano_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 2 additions & 8 deletions syncano_cli/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@

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",
"text"}

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']
Expand Down Expand Up @@ -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))


Expand Down
61 changes: 41 additions & 20 deletions syncano_cli/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import os
import re
from collections import defaultdict
from syncano.exceptions import SyncanoRequestError

from syncano.models import fields
from syncano.models.incentives import Script

from . import LOG, mute_log
from . import LOG

ALLOWED_RUNTIMES = {
'golang': '.go',
Expand All @@ -17,25 +15,13 @@
'nodejs_library_v1.0': '.js',
'php': '.php',
'python': '.py',
'python3': '.py',
'python_library_v4.2': '.py',
'python_library_v5.0': '.py',
'ruby': '.rb',
'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:
Expand Down Expand Up @@ -68,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:
Expand Down Expand Up @@ -101,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
Expand All @@ -109,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
Expand All @@ -118,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:
Expand All @@ -145,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):
Expand Down