Statistics
| Branch: | Tag: | Revision:

root / image_creator / kamaki_wrapper.py @ bf3a282c

History | View | Annotate | Download (4.8 kB)

1 40e7a487 Nikos Skalkotos
# Copyright 2012 GRNET S.A. All rights reserved.
2 40e7a487 Nikos Skalkotos
#
3 40e7a487 Nikos Skalkotos
# Redistribution and use in source and binary forms, with or
4 40e7a487 Nikos Skalkotos
# without modification, are permitted provided that the following
5 40e7a487 Nikos Skalkotos
# conditions are met:
6 40e7a487 Nikos Skalkotos
#
7 40e7a487 Nikos Skalkotos
#   1. Redistributions of source code must retain the above
8 40e7a487 Nikos Skalkotos
#      copyright notice, this list of conditions and the following
9 40e7a487 Nikos Skalkotos
#      disclaimer.
10 40e7a487 Nikos Skalkotos
#
11 40e7a487 Nikos Skalkotos
#   2. Redistributions in binary form must reproduce the above
12 40e7a487 Nikos Skalkotos
#      copyright notice, this list of conditions and the following
13 40e7a487 Nikos Skalkotos
#      disclaimer in the documentation and/or other materials
14 40e7a487 Nikos Skalkotos
#      provided with the distribution.
15 40e7a487 Nikos Skalkotos
#
16 40e7a487 Nikos Skalkotos
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 40e7a487 Nikos Skalkotos
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 40e7a487 Nikos Skalkotos
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 40e7a487 Nikos Skalkotos
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 40e7a487 Nikos Skalkotos
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 40e7a487 Nikos Skalkotos
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 40e7a487 Nikos Skalkotos
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 40e7a487 Nikos Skalkotos
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 40e7a487 Nikos Skalkotos
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 40e7a487 Nikos Skalkotos
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 40e7a487 Nikos Skalkotos
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 40e7a487 Nikos Skalkotos
# POSSIBILITY OF SUCH DAMAGE.
28 40e7a487 Nikos Skalkotos
#
29 40e7a487 Nikos Skalkotos
# The views and conclusions contained in the software and
30 40e7a487 Nikos Skalkotos
# documentation are those of the authors and should not be
31 40e7a487 Nikos Skalkotos
# interpreted as representing official policies, either expressed
32 40e7a487 Nikos Skalkotos
# or implied, of GRNET S.A.
33 40e7a487 Nikos Skalkotos
34 40e7a487 Nikos Skalkotos
from os.path import basename
35 40e7a487 Nikos Skalkotos
36 3dfe3651 Nikos Skalkotos
from kamaki.cli.config import Config
37 997ac76a Nikos Skalkotos
from kamaki.clients import ClientError
38 40e7a487 Nikos Skalkotos
from kamaki.clients.image import ImageClient
39 997ac76a Nikos Skalkotos
from kamaki.clients.pithos import PithosClient
40 31160dc8 Nikos Skalkotos
from kamaki.clients.astakos import AstakosClient
41 40e7a487 Nikos Skalkotos
42 40e7a487 Nikos Skalkotos
43 e77e66a9 Nikos Skalkotos
class Kamaki(object):
44 cf4f52b6 Nikos Skalkotos
45 31160dc8 Nikos Skalkotos
    CONTAINER = "images"
46 cf4f52b6 Nikos Skalkotos
47 24684bbb Nikos Skalkotos
    @staticmethod
48 24684bbb Nikos Skalkotos
    def get_token():
49 88f83027 Nikos Skalkotos
        """Get the saved token"""
50 24684bbb Nikos Skalkotos
        config = Config()
51 24684bbb Nikos Skalkotos
        return config.get('global', 'token')
52 cf4f52b6 Nikos Skalkotos
53 cf4f52b6 Nikos Skalkotos
    @staticmethod
54 31160dc8 Nikos Skalkotos
    def save_token(token):
55 88f83027 Nikos Skalkotos
        """Save this token to the configuration file"""
56 cf4f52b6 Nikos Skalkotos
        config = Config()
57 31160dc8 Nikos Skalkotos
        config.set('global', 'token', token)
58 cf4f52b6 Nikos Skalkotos
        config.write()
59 cf4f52b6 Nikos Skalkotos
60 cf4f52b6 Nikos Skalkotos
    @staticmethod
61 31160dc8 Nikos Skalkotos
    def get_account(token):
62 88f83027 Nikos Skalkotos
        """Return the account corresponding to this token"""
63 cf4f52b6 Nikos Skalkotos
        config = Config()
64 ee0719db Nikos Skalkotos
        astakos = AstakosClient(config.get('user', 'url'), token)
65 31160dc8 Nikos Skalkotos
        try:
66 31160dc8 Nikos Skalkotos
            account = astakos.info()
67 31160dc8 Nikos Skalkotos
        except ClientError as e:
68 31160dc8 Nikos Skalkotos
            if e.status == 401:  # Unauthorized: invalid token
69 31160dc8 Nikos Skalkotos
                return None
70 31160dc8 Nikos Skalkotos
            else:
71 31160dc8 Nikos Skalkotos
                raise
72 31160dc8 Nikos Skalkotos
        return account
73 cf4f52b6 Nikos Skalkotos
74 31160dc8 Nikos Skalkotos
    def __init__(self, account, output):
75 88f83027 Nikos Skalkotos
        """Create a Kamaki instance"""
76 997ac76a Nikos Skalkotos
        self.account = account
77 e77e66a9 Nikos Skalkotos
        self.out = output
78 40e7a487 Nikos Skalkotos
79 40e7a487 Nikos Skalkotos
        config = Config()
80 40e7a487 Nikos Skalkotos
81 ee0719db Nikos Skalkotos
        pithos_url = config.get('file', 'url')
82 37d581b8 Nikos Skalkotos
        self.pithos_client = PithosClient(
83 37d581b8 Nikos Skalkotos
            pithos_url, self.account['auth_token'], self.account['uuid'],
84 37d581b8 Nikos Skalkotos
            self.CONTAINER)
85 40e7a487 Nikos Skalkotos
86 40e7a487 Nikos Skalkotos
        image_url = config.get('image', 'url')
87 31160dc8 Nikos Skalkotos
        self.image_client = ImageClient(image_url, self.account['auth_token'])
88 40e7a487 Nikos Skalkotos
89 b1395967 Nikos Skalkotos
    def upload(self, file_obj, size=None, remote_path=None, hp=None, up=None):
90 b1aea98e Nikos Skalkotos
        """Upload a file to pithos"""
91 3b7d3fc7 Nikos Skalkotos
92 3b7d3fc7 Nikos Skalkotos
        path = basename(file_obj.name) if remote_path is None else remote_path
93 40e7a487 Nikos Skalkotos
94 b1395967 Nikos Skalkotos
        try:
95 31160dc8 Nikos Skalkotos
            self.pithos_client.create_container(self.CONTAINER)
96 b1395967 Nikos Skalkotos
        except ClientError as e:
97 b1395967 Nikos Skalkotos
            if e.status != 202:  # Ignore container already exists errors
98 3b7d3fc7 Nikos Skalkotos
                raise e
99 3b7d3fc7 Nikos Skalkotos
100 3b7d3fc7 Nikos Skalkotos
        hash_cb = self.out.progress_generator(hp) if hp is not None else None
101 3b7d3fc7 Nikos Skalkotos
        upload_cb = self.out.progress_generator(up) if up is not None else None
102 3b7d3fc7 Nikos Skalkotos
103 55fd2858 Nikos Skalkotos
        self.pithos_client.upload_object(path, file_obj, size, hash_cb,
104 3b7d3fc7 Nikos Skalkotos
                                         upload_cb)
105 3b7d3fc7 Nikos Skalkotos
106 31160dc8 Nikos Skalkotos
        return "pithos://%s/%s/%s" % (self.account['uuid'], self.CONTAINER,
107 31160dc8 Nikos Skalkotos
                                      path)
108 997ac76a Nikos Skalkotos
109 37d581b8 Nikos Skalkotos
    def register(self, name, location, metadata, public=False):
110 b1aea98e Nikos Skalkotos
        """Register an image to ~okeanos"""
111 3b7d3fc7 Nikos Skalkotos
112 d3445122 Nikos Skalkotos
        # Convert all metadata to strings
113 d3445122 Nikos Skalkotos
        str_metadata = {}
114 d3445122 Nikos Skalkotos
        for (key, value) in metadata.iteritems():
115 f99fe99d Nikos Skalkotos
            str_metadata[str(key)] = str(value)
116 37d581b8 Nikos Skalkotos
        is_public = 'true' if public else 'false'
117 37d581b8 Nikos Skalkotos
        params = {'is_public': is_public, 'disk_format': 'diskdump'}
118 d3445122 Nikos Skalkotos
        self.image_client.register(name, location, params, str_metadata)
119 40e7a487 Nikos Skalkotos
120 3afe6b44 Nikos Skalkotos
    def share(self, location):
121 3afe6b44 Nikos Skalkotos
        """Share this file with all the users"""
122 3afe6b44 Nikos Skalkotos
123 3afe6b44 Nikos Skalkotos
        self.pithos_client.set_object_sharing(location, "*")
124 3afe6b44 Nikos Skalkotos
125 03eb7dc8 Nikos Skalkotos
    def object_exists(self, location):
126 03eb7dc8 Nikos Skalkotos
        """Check if an object exists in pythos"""
127 03eb7dc8 Nikos Skalkotos
128 03eb7dc8 Nikos Skalkotos
        try:
129 03eb7dc8 Nikos Skalkotos
            self.pithos_client.get_object_info(location)
130 03eb7dc8 Nikos Skalkotos
        except ClientError as e:
131 03eb7dc8 Nikos Skalkotos
            if e.status == 404:  # Object not found error
132 03eb7dc8 Nikos Skalkotos
                return False
133 03eb7dc8 Nikos Skalkotos
            else:
134 03eb7dc8 Nikos Skalkotos
                raise
135 03eb7dc8 Nikos Skalkotos
        return True
136 03eb7dc8 Nikos Skalkotos
137 6152a559 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :