Revision 49c07ce3 image_creator/kamaki_wrapper.py
b/image_creator/kamaki_wrapper.py | ||
---|---|---|
47 | 47 |
from kamaki.clients.astakos import AstakosClient |
48 | 48 |
|
49 | 49 |
|
50 |
config = Config() |
|
51 |
|
|
52 |
|
|
50 | 53 |
class Kamaki(object): |
51 | 54 |
"""Wrapper class for the ./kamaki library""" |
52 | 55 |
CONTAINER = "images" |
53 | 56 |
|
54 | 57 |
@staticmethod |
55 |
def get_token(): |
|
56 |
"""Get the saved token""" |
|
57 |
config = Config() |
|
58 |
return config.get('global', 'token') |
|
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') |
|
62 |
if not default: |
|
63 |
return clouds[0] if len(clouds) else "" |
|
64 |
return default if default in clouds else "" |
|
65 |
|
|
66 |
@staticmethod |
|
67 |
def set_default_cloud(name): |
|
68 |
"""Sets a cloud account as default""" |
|
69 |
config.set('global', 'default_cloud', name) |
|
70 |
config.write() |
|
71 |
|
|
72 |
@staticmethod |
|
73 |
def get_clouds(): |
|
74 |
"""Returns the list of available clouds""" |
|
75 |
names = config.keys('cloud') |
|
76 |
|
|
77 |
clouds = {} |
|
78 |
for name in names: |
|
79 |
clouds[name] = config.get('cloud', name) |
|
80 |
|
|
81 |
return clouds |
|
82 |
|
|
83 |
@staticmethod |
|
84 |
def get_cloud_by_name(name): |
|
85 |
"""Returns a dict with cloud info""" |
|
86 |
return config.get('cloud', name) |
|
59 | 87 |
|
60 | 88 |
@staticmethod |
61 |
def save_token(token): |
|
62 |
"""Save this token to the configuration file""" |
|
63 |
config = Config() |
|
64 |
config.set('global', 'token', token) |
|
89 |
def save_cloud(name, url, token, description=""): |
|
90 |
"""Save a new cloud account""" |
|
91 |
cloud = {'url': url, 'token': token} |
|
92 |
if len(description): |
|
93 |
cloud['description'] = description |
|
94 |
config.set('cloud', name, cloud) |
|
95 |
|
|
96 |
# Make the saved cloud the default one |
|
97 |
config.set('global', 'default_cloud', name) |
|
65 | 98 |
config.write() |
66 | 99 |
|
67 | 100 |
@staticmethod |
68 |
def get_account(token): |
|
69 |
"""Return the account corresponding to this token""" |
|
70 |
config = Config() |
|
71 |
astakos = AstakosClient(config.get('user', 'url'), token) |
|
101 |
def remove_cloud(name): |
|
102 |
"""Deletes an existing cloud from the Kamaki configuration file""" |
|
103 |
config.remove_option('cloud', name) |
|
104 |
config.write() |
|
105 |
|
|
106 |
@staticmethod |
|
107 |
def create_account(url, token): |
|
108 |
"""Given a valid (URL, tokens) pair this method returns an Astakos |
|
109 |
client instance |
|
110 |
""" |
|
111 |
client = AstakosClient(url, token) |
|
72 | 112 |
try: |
73 |
account = astakos.info() |
|
74 |
except ClientError as e: |
|
75 |
if e.status == 401: # Unauthorized: invalid token |
|
76 |
return None |
|
77 |
else: |
|
78 |
raise |
|
79 |
return account |
|
113 |
client.authenticate() |
|
114 |
except ClientError: |
|
115 |
return None |
|
116 |
|
|
117 |
return client |
|
118 |
|
|
119 |
@staticmethod |
|
120 |
def get_account(cloud_name): |
|
121 |
"""Given a saved cloud name this method returns an Astakos client |
|
122 |
instance |
|
123 |
""" |
|
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 |
|
128 |
|
|
129 |
return Kamaki.create_account(cloud['url'], cloud['token']) |
|
80 | 130 |
|
81 | 131 |
def __init__(self, account, output): |
82 | 132 |
"""Create a Kamaki instance""" |
83 | 133 |
self.account = account |
84 | 134 |
self.out = output |
85 | 135 |
|
86 |
config = Config() |
|
87 |
|
|
88 |
pithos_url = config.get('file', 'url') |
|
89 |
self.pithos_client = PithosClient( |
|
90 |
pithos_url, self.account['auth_token'], self.account['uuid'], |
|
136 |
self.pithos = PithosClient( |
|
137 |
self.account.get_service_endpoints('object-store')['publicURL'], |
|
138 |
self.account.token, |
|
139 |
self.account.user_info()['id'], |
|
91 | 140 |
self.CONTAINER) |
92 | 141 |
|
93 |
image_url = config.get('image', 'url') |
|
94 |
self.image_client = ImageClient(image_url, self.account['auth_token']) |
|
142 |
self.image = ImageClient( |
|
143 |
self.account.get_service_endpoints('image')['publicURL'], |
|
144 |
self.account.token) |
|
95 | 145 |
|
96 | 146 |
def upload(self, file_obj, size=None, remote_path=None, hp=None, up=None): |
97 | 147 |
"""Upload a file to pithos""" |
... | ... | |
99 | 149 |
path = basename(file_obj.name) if remote_path is None else remote_path |
100 | 150 |
|
101 | 151 |
try: |
102 |
self.pithos_client.create_container(self.CONTAINER)
|
|
152 |
self.pithos.create_container(self.CONTAINER) |
|
103 | 153 |
except ClientError as e: |
104 | 154 |
if e.status != 202: # Ignore container already exists errors |
105 | 155 |
raise e |
... | ... | |
107 | 157 |
hash_cb = self.out.progress_generator(hp) if hp is not None else None |
108 | 158 |
upload_cb = self.out.progress_generator(up) if up is not None else None |
109 | 159 |
|
110 |
self.pithos_client.upload_object(path, file_obj, size, hash_cb, |
|
111 |
upload_cb) |
|
160 |
self.pithos.upload_object(path, file_obj, size, hash_cb, upload_cb) |
|
112 | 161 |
|
113 |
return "pithos://%s/%s/%s" % (self.account['uuid'], self.CONTAINER,
|
|
114 |
path) |
|
162 |
return "pithos://%s/%s/%s" % (self.account.user_info()['id'],
|
|
163 |
self.CONTAINER, path)
|
|
115 | 164 |
|
116 | 165 |
def register(self, name, location, metadata, public=False): |
117 | 166 |
"""Register an image to ~okeanos""" |
... | ... | |
122 | 171 |
str_metadata[str(key)] = str(value) |
123 | 172 |
is_public = 'true' if public else 'false' |
124 | 173 |
params = {'is_public': is_public, 'disk_format': 'diskdump'} |
125 |
self.image_client.register(name, location, params, str_metadata)
|
|
174 |
return self.image.register(name, location, params, str_metadata)
|
|
126 | 175 |
|
127 | 176 |
def share(self, location): |
128 | 177 |
"""Share this file with all the users""" |
129 | 178 |
|
130 |
self.pithos_client.set_object_sharing(location, "*")
|
|
179 |
self.pithos.set_object_sharing(location, "*") |
|
131 | 180 |
|
132 | 181 |
def object_exists(self, location): |
133 | 182 |
"""Check if an object exists in pythos""" |
134 | 183 |
|
135 | 184 |
try: |
136 |
self.pithos_client.get_object_info(location)
|
|
185 |
self.pithos.get_object_info(location) |
|
137 | 186 |
except ClientError as e: |
138 | 187 |
if e.status == 404: # Object not found error |
139 | 188 |
return False |
Also available in: Unified diff