Remove download stage from 'fuel snapshot' cli

Remove download functionality from fuel snapshot
generation process and provide users an URL

DocImpact
Change-Id: I02a059aeda9094c3ebd074a717d35770b8665dfb
Closes-Bug: 1444343
This commit is contained in:
tivaliy 2016-03-23 22:30:30 +02:00
parent c7074223f4
commit 9506f4c564
3 changed files with 44 additions and 79 deletions

View File

@ -19,8 +19,6 @@ import yaml
from fuelclient.cli.actions.base import Action
import fuelclient.cli.arguments as Args
from fuelclient.cli.formatting import download_snapshot_with_progress_bar
from fuelclient.client import DefaultAPIClient
from fuelclient.objects.task import SnapshotTask
@ -32,23 +30,19 @@ class SnapshotAction(Action):
def __init__(self):
super(SnapshotAction, self).__init__()
self.args = (
Args.get_dir_arg("Directory to which download snapshot."),
Args.get_boolean_arg("conf",
help_="Provide this flag to generate conf")
help_="Provide this flag to generate conf"),
)
self.flag_func_map = (
('conf', self.get_snapshot_config),
(None, self.get_snapshot),
(None, self.create_snapshot),
)
def get_snapshot(self, params):
"""To download diagnostic snapshot:
def create_snapshot(self, params):
"""To create diagnostic snapshot:
fuel snapshot
To download diagnostic snapshot to specific directory:
fuel snapshot --dir path/to/directory
To specify config for snapshoting
To specify config for snapshotting:
fuel snapshot < conf.yaml
"""
@ -60,15 +54,17 @@ class SnapshotAction(Action):
snapshot_task = SnapshotTask.start_snapshot_task(conf)
self.serializer.print_to_output(
snapshot_task.data,
"Generating dump..."
"Generating diagnostic snapshot..."
)
snapshot_task.wait()
if snapshot_task.status == 'ready':
download_snapshot_with_progress_bar(
snapshot_task.connection.root + snapshot_task.data["message"],
auth_token=DefaultAPIClient.auth_token,
directory=params.dir
self.serializer.print_to_output(
snapshot_task.data,
"...Completed...\n"
"Diagnostic snapshot can be downloaded from " +
snapshot_task.connection.root +
snapshot_task.data["message"]
)
elif snapshot_task.status == 'error':
six.print_(
@ -78,11 +74,10 @@ class SnapshotAction(Action):
)
def get_snapshot_config(self, params):
"""Download default config for snapshot
"""Download default config for snapshot:
fuel snapshot --conf > dump_conf.yaml
To use json formatter
To use json formatter:
fuel snapshot --conf --json
"""
conf = SnapshotTask.get_default_config()

View File

@ -17,15 +17,12 @@ from functools import partial
from itertools import chain
import math
from operator import itemgetter
import os
import sys
from time import sleep
import six
from fuelclient.cli.error import DeployProgressError
from fuelclient.cli.error import InvalidDirectoryException
from six.moves import urllib
def format_table(data, acceptable_keys=None, column_to_join=None):
@ -102,42 +99,6 @@ def get_bar_for_progress(full_width, progress):
)
def download_snapshot_with_progress_bar(
url, auth_token, directory=os.path.curdir):
"""downloads file from specific 'url' with progress bar and save it
to some 'directory'.
"""
if not os.path.exists(directory):
raise InvalidDirectoryException(
"Folder {0} doesn't exist.".format(directory))
file_name = os.path.join(
os.path.abspath(directory),
url.split('/')[-1]
)
request = urllib.request.Request(url, headers={'x-auth-token': auth_token})
download_handle = urllib.request.urlopen(request)
with open(file_name, 'wb') as file_handle:
meta = download_handle.info()
file_size = int(meta.getheaders("Content-Length")[0])
print("Downloading: {0} Bytes: {1}".format(url, file_size))
file_size_dl = 0
block_size = 8192
bar = partial(get_bar_for_progress, 80)
while True:
data_buffer = download_handle.read(block_size)
if not data_buffer:
break
file_size_dl += len(data_buffer)
file_handle.write(data_buffer)
progress = int(100 * float(file_size_dl) / file_size)
sys.stdout.write("\r{0}".format(
bar(progress)
))
sys.stdout.flush()
sleep(1 / 10)
print()
def print_deploy_progress(deploy_task):
"""Receives 'deploy_task' and depending on terminal availability
starts progress printing routines with or without curses.

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from mock import call
from mock import Mock
from mock import patch
@ -37,13 +36,10 @@ class TestSnapshot(base.UnitTestCase):
self.execute(['fuel', 'snapshot', '--conf'])
self.assertEqual(mstdout.write.call_args_list, [call('key: value\n')])
@patch('fuelclient.cli.actions.snapshot.DefaultAPIClient',
mock.Mock(auth_token='token123'))
@patch('fuelclient.cli.actions.snapshot.SnapshotTask.start_snapshot_task')
@patch('fuelclient.cli.actions.snapshot.'
'download_snapshot_with_progress_bar')
@patch('sys.stdin')
def test_snapshot_with_provided_conf(self, mstdin, mdownload, mstart):
@patch('sys.stdout')
def test_create_snapshot_with_provided_conf(self, mstdout, mstdin, mstart):
conf = 'key: value\n'
mstdin.isatty.return_value = False
@ -55,19 +51,13 @@ class TestSnapshot(base.UnitTestCase):
mstart.assert_called_once_with({'key': 'value'})
self.assertEqual(mstdin.read.call_count, 1)
msg = mstdout.write.call_args_list[2][0][0]
self.assertIn("Diagnostic snapshot can be downloaded from", msg)
mdownload.assert_called_once_with(
mock.ANY,
auth_token='token123',
directory='.')
@patch('fuelclient.cli.actions.snapshot.DefaultAPIClient',
mock.Mock(auth_token='token123'))
@patch('fuelclient.cli.actions.snapshot.SnapshotTask.start_snapshot_task')
@patch('fuelclient.cli.actions.snapshot.'
'download_snapshot_with_progress_bar')
@patch('sys.stdin')
def test_snapshot_without_conf(self, mstdin, mdownload, mstart):
@patch('sys.stdout')
def test_create_snapshot_without_conf(self, mstdout, mstdin, mstart):
mstdin.isatty.return_value = True
@ -76,15 +66,14 @@ class TestSnapshot(base.UnitTestCase):
self.execute(['fuel', 'snapshot'])
mstart.assert_called_once_with({})
mdownload.assert_called_once_with(
mock.ANY,
auth_token='token123',
directory='.')
msg = mstdout.write.call_args_list[2][0][0]
self.assertIn("Diagnostic snapshot can be downloaded from", msg)
@patch('fuelclient.cli.actions.snapshot.SnapshotTask.start_snapshot_task')
@patch('sys.stdin')
@patch('sys.stderr')
def test_get_snapshot_when_task_is_failed(self, mstderr, mstdin, mstart):
def test_create_snapshot_when_task_is_failed(
self, mstderr, mstdin, mstart):
mstdin.isatty.return_value = True
mdata = {'message': 'mock task message'}
@ -98,3 +87,23 @@ class TestSnapshot(base.UnitTestCase):
err_msg = ("Snapshot generating task ended with error. "
"Task message: {0}".format(mdata['message']))
mstderr.write.called_once_with(err_msg)
@patch('fuelclient.cli.actions.snapshot.SnapshotTask.start_snapshot_task')
@patch('sys.stdin')
@patch('sys.stdout')
def test_downloadable_snapshot_url(self, mstdout, mstdin, mstart):
expected_url = 'http://127.0.0.1:8000'
mdata = {'message': '/api/dump/mock-fuel-snapshot.tar.xz'}
mstdin.isatty.return_value = True
self.mtask.connection.root = expected_url
self.mtask.data = mdata
mstart.return_value = self.mtask
self.execute(['fuel', 'snapshot'])
expected_msg = ("...Completed...\n"
"Diagnostic snapshot can be downloaded from "
"{}{}".format(expected_url, mdata['message']))
msg = mstdout.write.call_args_list[2][0][0]
self.assertEqual(expected_msg, msg)