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
54 """Wrapper class for the ./kamaki library"""
58 def get_default_cloud_name():
59 """Returns the name of the default cloud"""
60 clouds = config.keys('cloud')
61 default = config.get('global', 'default_cloud')
63 return clouds[0] if len(clouds) else ""
64 return default if default in clouds else ""
67 def set_default_cloud(name):
68 """Sets a cloud account as default"""
69 config.set('global', 'default_cloud', name)
74 """Returns the list of available clouds"""
75 names = config.keys('cloud')
79 clouds[name] = config.get('cloud', name)
84 def get_cloud_by_name(name):
85 """Returns a dict with cloud info"""
86 return config.get('cloud', name)
89 def save_cloud(name, url, token, description=""):
90 """Save a new cloud account"""
91 cloud = {'url': url, 'token': token}
93 cloud['description'] = description
94 config.set('cloud', name, cloud)
96 # Make the saved cloud the default one
97 config.set('global', 'default_cloud', name)
101 def remove_cloud(name):
102 """Deletes an existing cloud from the Kamaki configuration file"""
103 config.remove_option('cloud', name)
107 def create_account(url, token):
108 """Given a valid (URL, tokens) pair this method returns an Astakos
111 client = AstakosClient(url, token)
113 client.authenticate()
120 def get_account(cloud_name):
121 """Given a saved cloud name this method returns an Astakos client
124 cloud = config.get('cloud', cloud_name)
125 assert cloud, "cloud: `%s' does not exist" % cloud_name
126 assert 'url' in cloud, "url attr is missing in %s" % cloud_name
127 assert 'token' in cloud, "token attr is missing in %s" % cloud_name
129 return Kamaki.create_account(cloud['url'], cloud['token'])
131 def __init__(self, account, output):
132 """Create a Kamaki instance"""
133 self.account = account
136 self.pithos = PithosClient(
137 self.account.get_service_endpoints('object-store')['publicURL'],
139 self.account.user_info()['id'],
142 self.image = ImageClient(
143 self.account.get_service_endpoints('image')['publicURL'],
146 def upload(self, file_obj, size=None, remote_path=None, hp=None, up=None):
147 """Upload a file to pithos"""
149 path = basename(file_obj.name) if remote_path is None else remote_path
152 self.pithos.create_container(self.CONTAINER)
153 except ClientError as e:
154 if e.status != 202: # Ignore container already exists errors
157 hash_cb = self.out.progress_generator(hp) if hp is not None else None
158 upload_cb = self.out.progress_generator(up) if up is not None else None
160 self.pithos.upload_object(path, file_obj, size, hash_cb, upload_cb)
162 return "pithos://%s/%s/%s" % (self.account.user_info()['id'],
163 self.CONTAINER, path)
165 def register(self, name, location, metadata, public=False):
166 """Register an image with cyclades"""
168 # Convert all metadata to strings
170 for (key, value) in metadata.iteritems():
171 str_metadata[str(key)] = str(value)
172 is_public = 'true' if public else 'false'
173 params = {'is_public': is_public, 'disk_format': 'diskdump'}
174 return self.image.register(name, location, params, str_metadata)
176 def share(self, location):
177 """Share this file with all the users"""
179 self.pithos.set_object_sharing(location, "*")
181 def object_exists(self, location):
182 """Check if an object exists in pythos"""
185 self.pithos.get_object_info(location)
186 except ClientError as e:
187 if e.status == 404: # Object not found error
193 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :