This repository was archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
[CORE-1601] add hosting files upload #35
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5eb9d9f
[CORE-1601] add initial implementation of hosting in CLI - need to pr…
opalczynski 676ec2a
[CORE-1601] remove local paths from main.
opalczynski f627744
[CORE-1601] provide tests for hosting commands; also bump the syncano…
opalczynski 6fb65ff
[CORE-1601] set default as domain - if domain is not provided; add pu…
opalczynski 8dd7c9e
[CORE-1601] correct publish functionality (bad identation) and update…
opalczynski 8abefab
[CORE-1601] add info loggin in hosting;
opalczynski 0e181fd
[CORE-1601] correct isdir check;
opalczynski 0132add
[CORE-1601] leave only the default domain for now: remove commands: l…
opalczynski 2e603ac
[CORE-1601] correct isort issues;
opalczynski 765cefb
[CORE-1601] change throttling to 0.02 in upload files;
opalczynski b8d319d
[CORE-1601] simply the tests;
opalczynski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # -*- coding: utf-8 -*- |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # -*- coding: utf-8 -*- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import os | ||
| import sys | ||
|
|
||
| import click | ||
| from syncano_cli.base.connection import create_connection | ||
| from syncano_cli.config import ACCOUNT_CONFIG_PATH | ||
| from syncano_cli.hosting.utils import HostingCommands | ||
| from syncano_cli.logger import get_logger | ||
|
|
||
| LOG = get_logger('syncano-hosting') | ||
|
|
||
|
|
||
| @click.group() | ||
| def top_hosting(): | ||
| pass | ||
|
|
||
|
|
||
| @top_hosting.command() | ||
| @click.option('--config', help=u'Account configuration file.') | ||
| @click.argument('instance_name', envvar='SYNCANO_INSTANCE') | ||
| @click.option('--list-files', is_flag=True, help='List files within the hosting.') | ||
| @click.option('--publish', type=str, help='Publish files from the local directory to the Syncano Hosting.') | ||
| def hosting(config, instance_name, list_files, publish): | ||
| """ | ||
| Execute script endpoint in given instance | ||
| """ | ||
|
|
||
| def validate_domain(provided_domain=None): | ||
| return 'default' if not provided_domain else provided_domain | ||
|
|
||
| def validate_publish(base_dir): | ||
| if not os.path.isdir(base_dir): | ||
| LOG.error('You should provide a project root directory here.') | ||
| sys.exit(1) | ||
|
|
||
| config = config or ACCOUNT_CONFIG_PATH | ||
| try: | ||
| connection = create_connection(config) | ||
| instance = connection.Instance.please.get(name=instance_name) | ||
|
|
||
| hosting_commands = HostingCommands(instance) | ||
|
|
||
| if list_files: | ||
| domain = validate_domain() | ||
| LOG.info('List the hosting files: {} in instance: {}'.format(domain, instance_name)) | ||
| hosting_files = hosting_commands.list_hosting_files(domain=domain) | ||
| hosting_commands.print_hosting_files(hosting_files) | ||
|
|
||
| if publish: | ||
| domain = validate_domain() | ||
| LOG.info('Publish the hosting files: {} in instance: {}'.format(domain, instance_name)) | ||
| validate_publish(base_dir=publish) | ||
| uploaded_files = hosting_commands.publish(domain=domain, base_dir=publish) | ||
| hosting_commands.print_hosting_files(uploaded_files) | ||
|
|
||
| except Exception as e: | ||
| LOG.error(e) | ||
| sys.exit(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import os | ||
| import sys | ||
| import time | ||
|
|
||
| from syncano_cli.logger import get_logger | ||
|
|
||
| LOG = get_logger('syncano-hosting') | ||
|
|
||
|
|
||
| class HostingCommands(object): | ||
|
|
||
| def __init__(self, instance): | ||
| self.instance = instance | ||
|
|
||
| def list_hosting(self): | ||
| return [ | ||
| (hosting.label, hosting.domains) for hosting in self.instance.hostings.all() | ||
| ] | ||
|
|
||
| def list_hosting_files(self, domain): | ||
| hosting = self._get_hosting(domain=domain) | ||
| if not hosting: | ||
| LOG.warn('No default hosting found. Exit.') | ||
| sys.exit(1) | ||
|
|
||
| files_list = hosting.list_files() | ||
| return files_list | ||
|
|
||
| def publish(self, domain, base_dir): | ||
| uploaded_files = [] | ||
| hosting = self._get_hosting(domain=domain) | ||
| if not hosting: | ||
| # create a new hosting if no default is present; | ||
| hosting = self.create_hosting(label='Default hosting', domain=domain) | ||
|
|
||
| for folder, subs, files in os.walk(base_dir): | ||
| path = folder.split(base_dir)[1][1:] # skip the / | ||
| for single_file in files: | ||
| if path: | ||
| file_path = '{}/{}'.format(path, single_file) | ||
| else: | ||
| file_path = single_file | ||
|
|
||
| sys_path = os.path.join(folder, single_file) | ||
| with open(sys_path, 'rb') as upload_file: | ||
| LOG.info('Uploading file: {}'.format(file_path)) | ||
| hosting.upload_file(path=file_path, file=upload_file) | ||
|
|
||
| uploaded_files.append(file_path) | ||
| time.sleep(0.02) # avoid throttling; | ||
| return uploaded_files | ||
|
|
||
| def create_hosting(self, label, domain): | ||
| hosting = self.instance.hostings.create( | ||
| label=label, | ||
| domains=[domain] | ||
| ) | ||
| return hosting | ||
|
|
||
| def _get_hosting(self, domain): | ||
| hostings = self.instance.hostings.all() | ||
| for hosting in hostings: | ||
| if domain in hosting.domains: | ||
| return hosting | ||
|
|
||
| def print_hosting_files(self, hosting_files): | ||
| print('Hosting files:') | ||
| self._print_separator() | ||
| for file_path in hosting_files: | ||
| print(file_path) | ||
|
|
||
| @staticmethod | ||
| def _print_separator(): | ||
| print(79 * '-') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
|
|
||
| h1 { | ||
| color: #124500; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>My first HTML document</title> | ||
| </head> | ||
| <body> | ||
| <P>Hello world! | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from syncano_cli.main import cli | ||
| from tests.base import BaseCLITest | ||
|
|
||
|
|
||
| class HostingCommandsTest(BaseCLITest): | ||
|
|
||
| def test_file_list(self): | ||
| self._publish_files() | ||
|
|
||
| result = self.runner.invoke(cli, args=[ | ||
| 'hosting', self.instance.name, '--list-files' | ||
| ]) | ||
|
|
||
| self.assertIn('index.html', result.output) | ||
| self.assertIn('css/page.css', result.output) | ||
|
|
||
| def _publish_files(self): | ||
| result = self.runner.invoke(cli, args=[ | ||
| 'hosting', self.instance.name, '--publish', 'tests/hosting_files_examples' | ||
| ], obj={}) | ||
|
|
||
| self.assertIn('index.html', result.output) | ||
| self.assertIn('css/page.css', result.output) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it create a new instance? Or is the instance common for all tests and gets cleaned up afterwards?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseCLITest inherits from InstanceMixin:
So it's a instance common for all test - and then I need to correct the publish test then :) Good catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we could remove the publish_files test as test_file_list is kind of depending on that functionality anyway