From 5f49d353228f9bc8b9428a3eac61a5f89ca59852 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Tue, 31 Jan 2017 10:59:24 -0500 Subject: [PATCH] experiment with downloading images Signed-off-by: Doug Hellmann --- .gitignore | 1 + aerostat/app.py | 12 ++++++++-- aerostat/download.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 aerostat/download.py diff --git a/.gitignore b/.gitignore index 76f82d4..a6a9b51 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,4 @@ ChangeLog # Files created by releasenotes build releasenotes/build /clouds.yaml +/*.dat diff --git a/aerostat/app.py b/aerostat/app.py index 0ec073d..faed328 100644 --- a/aerostat/app.py +++ b/aerostat/app.py @@ -20,9 +20,11 @@ import pprint import sys import os_client_config +import progressbar import shade import yaml +from aerostat import download from aerostat import resolver @@ -39,8 +41,9 @@ def main(): tasks = [] - for server in cloud.list_servers(): - tasks.extend(res.server(server)) + # for server in cloud.list_servers(): + # tasks.extend(res.server(server)) + tasks.extend(res.server(cloud.get_server('dev1'))) playbook = [ {'hosts': 'localhost', @@ -51,6 +54,11 @@ def main(): print(yaml.dump(playbook, default_flow_style=False, explicit_start=True)) + print('downloading snapshot') + image = cloud.get_image('dev1-sn1') + with download.ProgressBarDownloader('dev1-sn1.dat', image.size) as out: + cloud.download_image('dev1-sn1', output_file=out) + # for volume in dev1.volumes: # vol = cloud.get_volume(volume.id) # pprint.pprint(vol) diff --git a/aerostat/download.py b/aerostat/download.py new file mode 100644 index 0000000..62f6055 --- /dev/null +++ b/aerostat/download.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + +# Copyright 2010-2011 OpenStack Foundation +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import progressbar + + +class ProgressBarDownloader: + + def __init__(self, output_path, max_value): + self.output_path = output_path + self.max_value = max_value + self.bar = None + self.fd = None + self.amt_read = 0 + + def __enter__(self): + self.bar = progressbar.ProgressBar( + widgets=[ + progressbar.Percentage(), + progressbar.Bar(), + progressbar.FileTransferSpeed(), + ' ', + progressbar.ETA(), + ], + max_value=self.max_value, + ) + self.fd = open(self.output_path, 'wb') + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.fd.close() + self.bar.finish() + return False + + def write(self, block): + if block: + self.fd.write(block) + self.amt_read += len(block) + self.bar.update(self.amt_read) diff --git a/requirements.txt b/requirements.txt index 48ad734..a97934f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ cliff>=2.3.0 shade>=1.12.0 os-client-config>=1.22.0 PyYAML>=3.10.0 +progressbar2>=3.12.0