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
43 from os.path import basename
45 from kamaki.cli.config import Config
46 from kamaki.clients import ClientError
47 from kamaki.clients.image import ImageClient
48 from kamaki.clients.pithos import PithosClient
49 from kamaki.clients.astakos import CachedAstakosClient as AstakosClient
53 except Exception as e:
54 sys.stderr.write("Kamaki config error: %s\n" % str(e))
59 """Wrapper class for the ./kamaki library"""
63 def get_default_cloud_name():
64 """Returns the name of the default cloud"""
65 clouds = config.keys('cloud')
66 default = config.get('global', 'default_cloud')
68 return clouds[0] if len(clouds) else ""
69 return default if default in clouds else ""
72 def set_default_cloud(name):
73 """Sets a cloud account as default"""
74 config.set('global', 'default_cloud', name)
79 """Returns the list of available clouds"""
80 names = config.keys('cloud')
84 clouds[name] = config.get('cloud', name)
89 def get_cloud_by_name(name):
90 """Returns a dict with cloud info"""
91 return config.get('cloud', name)
94 def save_cloud(name, url, token, description=""):
95 """Save a new cloud account"""
96 cloud = {'url': url, 'token': token}
98 cloud['description'] = description
99 config.set('cloud', name, cloud)
101 # Make the saved cloud the default one
102 config.set('global', 'default_cloud', name)
106 def remove_cloud(name):
107 """Deletes an existing cloud from the Kamaki configuration file"""
108 config.remove_option('cloud', name)
112 def create_account(url, token):
113 """Given a valid (URL, tokens) pair this method returns an Astakos
116 client = AstakosClient(url, token)
118 client.authenticate()
125 def get_account(cloud_name):
126 """Given a saved cloud name this method returns an Astakos client
129 cloud = config.get('cloud', cloud_name)
130 assert cloud, "cloud: `%s' does not exist" % cloud_name
131 assert 'url' in cloud, "url attr is missing in %s" % cloud_name
132 assert 'token' in cloud, "token attr is missing in %s" % cloud_name
134 return Kamaki.create_account(cloud['url'], cloud['token'])
136 def __init__(self, account, output):
137 """Create a Kamaki instance"""
138 self.account = account
141 self.pithos = PithosClient(
142 self.account.get_service_endpoints('object-store')['publicURL'],
144 self.account.user_info()['id'],
147 self.image = ImageClient(
148 self.account.get_service_endpoints('image')['publicURL'],
151 def upload(self, file_obj, size=None, remote_path=None, hp=None, up=None):
152 """Upload a file to pithos"""
154 path = basename(file_obj.name) if remote_path is None else remote_path
157 self.pithos.create_container(self.CONTAINER)
158 except ClientError as e:
159 if e.status != 202: # Ignore container already exists errors
162 hash_cb = self.out.progress_generator(hp) if hp is not None else None
163 upload_cb = self.out.progress_generator(up) if up is not None else None
165 self.pithos.upload_object(path, file_obj, size, hash_cb, upload_cb)
167 return "pithos://%s/%s/%s" % (self.account.user_info()['id'],
168 self.CONTAINER, path)
170 def register(self, name, location, metadata, public=False):
171 """Register an image with cyclades"""
173 # Convert all metadata to strings
175 for (key, value) in metadata.iteritems():
176 str_metadata[str(key)] = str(value)
177 is_public = 'true' if public else 'false'
178 params = {'is_public': is_public, 'disk_format': 'diskdump'}
179 return self.image.register(name, location, params, str_metadata)
181 def share(self, location):
182 """Share this file with all the users"""
184 self.pithos.set_object_sharing(location, "*")
186 def object_exists(self, location):
187 """Check if an object exists in pythos"""
190 self.pithos.get_object_info(location)
191 except ClientError as e:
192 if e.status == 404: # Object not found error
198 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :