1 # -*- coding: utf-8 -*-
3 # Copyright 2012 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials
16 # provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
31 # The views and conclusions contained in the software and
32 # documentation are those of the authors and should not be
33 # interpreted as representing official policies, either expressed
34 # or implied, of GRNET S.A.
36 """This modules provides the interface for working with the ./kamaki library.
37 The library is used to upload images to and register them with a Synnefo
41 from os.path import basename
43 from kamaki.cli.config import Config
44 from kamaki.clients import ClientError
45 from kamaki.clients.image import ImageClient
46 from kamaki.clients.pithos import PithosClient
47 from kamaki.clients.astakos import AstakosClient
51 """Wrapper class for the ./kamaki library"""
56 """Get the saved token"""
58 return config.get('global', 'token')
61 def save_token(token):
62 """Save this token to the configuration file"""
64 config.set('global', 'token', token)
68 def get_account(token):
69 """Return the account corresponding to this token"""
71 astakos = AstakosClient(config.get('user', 'url'), token)
73 account = astakos.info()
74 except ClientError as e:
75 if e.status == 401: # Unauthorized: invalid token
81 def __init__(self, account, output):
82 """Create a Kamaki instance"""
83 self.account = account
88 pithos_url = config.get('file', 'url')
89 self.pithos_client = PithosClient(
90 pithos_url, self.account['auth_token'], self.account['uuid'],
93 image_url = config.get('image', 'url')
94 self.image_client = ImageClient(image_url, self.account['auth_token'])
96 def upload(self, file_obj, size=None, remote_path=None, hp=None, up=None):
97 """Upload a file to pithos"""
99 path = basename(file_obj.name) if remote_path is None else remote_path
102 self.pithos_client.create_container(self.CONTAINER)
103 except ClientError as e:
104 if e.status != 202: # Ignore container already exists errors
107 hash_cb = self.out.progress_generator(hp) if hp is not None else None
108 upload_cb = self.out.progress_generator(up) if up is not None else None
110 self.pithos_client.upload_object(path, file_obj, size, hash_cb,
113 return "pithos://%s/%s/%s" % (self.account['uuid'], self.CONTAINER,
116 def register(self, name, location, metadata, public=False):
117 """Register an image to ~okeanos"""
119 # Convert all metadata to strings
121 for (key, value) in metadata.iteritems():
122 str_metadata[str(key)] = str(value)
123 is_public = 'true' if public else 'false'
124 params = {'is_public': is_public, 'disk_format': 'diskdump'}
125 self.image_client.register(name, location, params, str_metadata)
127 def share(self, location):
128 """Share this file with all the users"""
130 self.pithos_client.set_object_sharing(location, "*")
132 def object_exists(self, location):
133 """Check if an object exists in pythos"""
136 self.pithos_client.get_object_info(location)
137 except ClientError as e:
138 if e.status == 404: # Object not found error
144 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :