forked from schadr/photon-controller
/
image_manager.py
273 lines (221 loc) · 7.89 KB
/
image_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Copyright 2015 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, without
# warranties or conditions of any kind, EITHER EXPRESS OR IMPLIED. See the
# License for then specific language governing permissions and limitations
# under the License.
import abc
class ImageNotFoundException(Exception):
""" Exception thrown when image is not found on the datastore """
pass
class ImageInUse(Exception):
""" Exception thrown when we attempt to delete an in use image """
pass
class InvalidImageUpdate(Exception):
"""
Exception thrown when we attempt to transition a tombstoned image to a
non tombstoned image
"""
pass
class InvalidImageState(Exception):
"""
Exception thrown if the image is being manipulated in an invalid state
"""
pass
class DirectoryNotFound(Exception):
"""
Exception thrown when the specified directory is not found
"""
class ImageManager(object):
"""A class that wraps hypervisor specific image management.
Clarification of a few terms:
Image metadata: Contains Vm information that would pass to .vmx when a Vm
is created. The information is saved in .ecv file located
together with the image in every datastore.
Image manifest: Contains image general information which is useful for
housekeeper (and maybe other components). Basically it
contains information like: image type (management or
cloud), image replication type (eager or on demand). The
information is saved in .manifest file located together
with the image only in shared image datastore.
"""
__metaclass__ = abc.ABCMeta
def __init__(self):
pass
def datastores_with_image(self, image_id, datastores):
if image_id is None:
return []
return [ds for ds in datastores if self.check_image(image_id, ds)]
def image_metadata(self, image_id, datastores):
for ds in datastores:
if self.check_image(image_id, ds):
return self.get_image_metadata(image_id, ds)
@staticmethod
def get_image_id_from_disks(disks):
"""Find image id in the disk collection"""
if not disks:
return None
for disk in disks:
try:
if disk.image.id is not None:
return disk.image.id
except AttributeError:
continue
return None
@abc.abstractmethod
def check_image(self, image_id, datastore_id):
pass
@abc.abstractmethod
def check_and_validate_image(self, image_id, datastore_id):
"""
:param image_id:
:param datastore_id:
:return: boolean, True if the image is valid and
the timestamp file is found
This method is intended
as a replacement for check_image() in
the vm creation workflow compatible
with the new image sweeper.
For an image to be valid both the
directory and the image timestamp
file must exists on the datastore.
"""
pass
@abc.abstractmethod
def copy_image(self, src_datastore, src_id, dst_datastore, dst_id):
pass
@abc.abstractmethod
def delete_image(self, datastore_id, image_id, ds_type, force):
"""
Delete an image from a datastore of a given type
datastore_id: The datastore id of the datastore
image_id: The id of the image to delete.
ds_type: The thrift datastore type.
force: boolean indicating force delete.
@throws ImageInUse if force is true and the image is inuse.
"""
pass
@abc.abstractmethod
def get_image_metadata(self, image_id, datastore):
pass
@abc.abstractmethod
def get_image_directory_path(self, datastore_id, image_id):
"""Get absolute path of the image directory.
Args:
datastore_id: datastore where the image is located.
image_id: image id as a string.
Returns:
The absolute path of the image directory as a string.
"""
pass
@abc.abstractmethod
def get_image_path(self, datastore_id, image_id):
"""Get absolute path of the image vmdk.
Args:
datastore_id: datastore where the image is located.
image_id: image id as a string.
Returns:
The absolute path of the image vmdk as a string.
"""
pass
@abc.abstractmethod
def get_datastore_id_from_path(self, image_path):
"""Extract the datastore ID from the absolute path of an image.
Args:
image_path: absolute path to the image.
Returns:
datastore id.
"""
pass
@abc.abstractmethod
def get_image_id_from_path(self, image_path):
"""Extract the image ID from the absolute path of an image.
Args:
image_path: absolute path to the image.
Returns:
image id.
"""
pass
@abc.abstractmethod
def get_timestamp_mod_time_from_dir(self, dirname, renamed=False):
"""
:param dirname:
:return: a boolean and the mod time of timestamp file
"""
pass
@abc.abstractmethod
def get_tombstone_mod_time_from_dir(self, dirname):
"""
:param dirname:
:return: a boolean and the mod time of timestamp file
"""
pass
@abc.abstractmethod
def get_images(self, datastore):
""" Get all images from datastore
:param datastore: datastore id
:return: list of string: list of image ids
"""
pass
@abc.abstractmethod
def get_image_manifest(self, image_id):
""" Get image replication and type (eager or on demand, used for
cloud vms or management vms
:param image_id: str, image id
:return: (ImageType, ImageReplication)
"""
pass
@abc.abstractmethod
def image_size(self, image_id):
pass
@abc.abstractmethod
def mark_unused(self, image_scanner, active_image_list):
"""Mark unused image as candidates for removal
:param image_scanner: image_scanner
:param image_list: list of active images
:raise TimeoutError
"""
@abc.abstractmethod
def touch_image_timestamp(self, ds_id, image_id):
"""
Update Image timestamp, mod file. Throws exception
if the image doesn't exists or the image has
been tombstoned
:param dsid:
:param image_id:
:return:
"""
@abc.abstractmethod
def create_image_tombstone(self, ds_id, image_id):
"""
Create tombstone file for image. Throws exception
if the image doesn't exists
:param dsid:
:param image_id:
:return:
"""
@abc.abstractmethod
def create_image(self, datastore_id, tmp_dir, image_id):
""" Create an image with a given id by doing an atomic move from a tmp
folder location.
"""
pass
@abc.abstractmethod
def create_image_with_vm_disk(self, datastore_id, tmp_dir, image_id,
vm_disk_os_path):
""" Fills a temp image directory with a disk from a VM,
then installs directory in the shared image folder.
"""
pass
@abc.abstractmethod
def delete_tmp_dir(self, datastore_id, tmp_dir):
""" Delete the temp image directory.
Does not handle the case for concurrent deltes of the same directory
across hosts.
"""
pass