Commit 176894c1 authored by Giorgos Verigakis's avatar Giorgos Verigakis
Browse files

Add a store download command

parent d2cea1e2
......@@ -75,7 +75,7 @@ from grp import getgrgid
from optparse import OptionParser
from os.path import abspath, basename, exists
from pwd import getpwuid
from sys import argv, exit
from sys import argv, exit, stdout
from kamaki import clients
from kamaki.config import Config, ConfigError
......@@ -641,9 +641,8 @@ class glance_setmembers(object):
self.client.set_members(image_id, member)
@command(api='storage')
class store_container(object):
"""get container info"""
class store_command(object):
"""base class for all store_* commands"""
@classmethod
def update_parser(cls, parser):
......@@ -655,29 +654,28 @@ class store_container(object):
def main(self):
self.config.override('storage_account', self.options.account)
self.config.override('storage_container', self.options.container)
# Use the more efficient Pithos client if available
if 'pithos' in self.config.get('apis').split():
self.client = clients.PithosClient(self.config)
@command(api='storage')
class store_container(store_command):
"""get container info"""
def main(self):
store_command.main(self)
reply = self.client.get_container_meta()
print_dict(reply)
@command(api='storage')
class store_upload(object):
class store_upload(store_command):
"""upload a file"""
@classmethod
def update_parser(cls, parser):
parser.add_option('--account', dest='account', metavar='ACCOUNT',
help='use account ACCOUNT')
parser.add_option('--container', dest='container', metavar='CONTAINER',
help='use container CONTAINER')
def main(self, path, remote_path=None):
self.config.override('storage_account', self.options.account)
self.config.override('storage_container', self.options.container)
# Use the more efficient Pithos client if available
if 'pithos' in self.config.get('apis').split():
self.client = clients.PithosClient(self.config)
store_command.main(self)
if remote_path is None:
remote_path = basename(path)
with open(path) as f:
......@@ -685,19 +683,26 @@ class store_upload(object):
@command(api='storage')
class store_delete(object):
"""delete a file"""
class store_download(store_command):
"""download a file"""
@classmethod
def update_parser(cls, parser):
parser.add_option('--account', dest='account', metavar='ACCOUNT',
help='use account ACCOUNT')
parser.add_option('--container', dest='container', metavar='CONTAINER',
help='use container CONTAINER')
def main(self, remote_path, local_path):
store_command.main(self)
f = self.client.get_object(remote_path)
out = open(local_path, 'w') if local_path != '-' else stdout
block = 4096
data = f.read(block)
while data:
out.write(data)
data = f.read(block)
@command(api='storage')
class store_delete(store_command):
"""delete a file"""
def main(self, path):
self.config.override('storage_account', self.options.account)
self.config.override('storage_container', self.options.container)
store_command.main(self)
self.client.delete_object(path)
......
......@@ -62,7 +62,7 @@ class HTTPClient(object):
return token
def raw_http_cmd(self, method, path, body=None, headers=None, success=200,
json_reply=False):
json_reply=False, skip_read=False):
p = urlparse(self.url)
path = p.path + path
if p.scheme == 'http':
......@@ -89,7 +89,7 @@ class HTTPClient(object):
conn.request(method, path, body, headers)
resp = conn.getresponse()
reply = resp.read()
reply = '' if skip_read else resp.read()
log.debug('<' * 50)
log.info('%d %s', resp.status, resp.reason)
......
......@@ -82,6 +82,12 @@ class StorageClient(HTTPClient):
data = f.read()
self.http_put(path, data, success=201)
def get_object(self, object):
path = '/%s/%s/%s' % (self.account, self.container, object)
resp, reply = self.raw_http_cmd('GET', path, success=200,
skip_read=True)
return resp.fp
def delete_object(self, object):
path = '/%s/%s/%s' % (self.account, self.container, object)
self.http_delete(path)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment