forked from python-oca/python-oca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.py
More file actions
208 lines (172 loc) · 5.47 KB
/
image.py
File metadata and controls
208 lines (172 loc) · 5.47 KB
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
# -*- coding: UTF-8 -*-
from .pool import Pool, PoolElement, Template, extractString
class Image(PoolElement):
METHODS = {
'info' : 'image.info',
'allocate' : 'image.allocate',
'delete' : 'image.delete',
'update' : 'image.update',
'enable' : 'image.enable',
'publish' : 'image.publish',
'chown' : 'image.chown',
'persistent' : 'image.persistent',
'clone' : 'image.clone',
}
XML_TYPES = {
'id' : int,
'uid' : int,
'gid' : int,
'uname' : extractString,
'gname' : extractString,
'name' : extractString,
#'permissions' : ???,
'type' : int,
'disk_type' : int,
'persistent' : int,
'regtime' : int,
'source' : extractString,
'path' : extractString,
'fstype' : extractString,
'size' : int,
'state' : int,
'running_vms' : int,
'cloning_ops' : int,
'cloning_id' : int,
'datastore_id': int,
'datastore' : extractString,
'vm_ids' : ["VMS", lambda vms: [int(vm_id.text) for vm_id in vms]],
'clone_ids' : ["CLONES", lambda clones: [int(clone_id.text) for clone_id in clones]],
'template' : ['TEMPLATE', Template],
}
INIT = 0
READY = 1
USED = 2
DISABLED = 3
IMAGE_STATES = ['INIT', 'READY', 'USED', 'DISABLED']
SHORT_IMAGE_STATES = {
"INIT" : "init",
"READY" : "rdy",
"USED" : "used",
"DISABLED" : "disa"
}
IMAGE_TYPES = ['OS', 'CDROM', 'DATABLOCK']
SHORT_IMAGE_TYPES = {
"OS" : "OS",
"CDROM" : "CD",
"DATABLOCK" : "DB"
}
ELEMENT_NAME = 'IMAGE'
@staticmethod
def allocate(client, template, datastore):
'''
Allocates a new image in OpenNebula
Arguments
``client``
oca.Client object
``template``
a string containing the template of the image
``datastore``
the datastore id where the image is to be allocated
'''
image_id = client.call(Image.METHODS['allocate'], template, datastore)
return image_id
def __init__(self, xml, client):
super(Image, self).__init__(xml, client)
self.id = self['ID'] if self['ID'] else None
def update(self, template):
'''
Replaces the template contents
Arguments
``template``
New template contents
'''
self.client.call(self.METHODS['update'], self.id, template)
def enable(self):
'''
Enables an image
'''
self.client.call(self.METHODS['enable'], self.id, True)
def disable(self):
'''
Disables an image
'''
self.client.call(self.METHODS['enable'], self.id, False)
def publish(self):
'''
Publishes an image
'''
self.client.call(self.METHODS['publish'], self.id, True)
def unpublish(self):
'''
Unpublishes an image
'''
self.client.call(self.METHODS['publish'], self.id, False)
def set_persistent(self):
'''
Set Image as persistent
'''
self.client.call(self.METHODS['persistent'], self.id, True)
def set_nonpersistent(self):
'''
Set Image as non persistent
'''
self.client.call(self.METHODS['persistent'], self.id, False)
def chown(self, uid, gid):
'''
Changes the owner/group
Arguments
``uid``
New owner id. Set to -1 to leave current value
``gid``
New group id. Set to -1 to leave current value
'''
self.client.call(self.METHODS['chown'], self.id, uid, gid)
def clone(self, name='', datastore_id=-1):
'''
Creates a clone of an image
``name``
name of a target element
``datastore_id``
The ID of the target datastore. Optional, can be set to -1 to use the current one.
'''
self.client.call(self.METHODS['clone'], self.id, name, datastore_id)
@property
def str_state(self):
'''
String representation of image state.
One of 'INIT', 'READY', 'USED', 'DISABLED'
'''
return self.IMAGE_STATES[int(self.state)]
@property
def short_state(self):
'''
Short string representation of image state.
One of 'init', 'rdy', 'used', 'disa'
'''
return self.SHORT_IMAGE_STATES[self.str_state]
@property
def str_type(self):
'''
String representation of image type.
One of 'OS', 'CDROM', 'DATABLOCK'
'''
return self.IMAGE_TYPES[int(self.type)]
@property
def short_type(self):
'''
Short string representation of image type.
One of 'OS', 'CD', 'DB'
'''
return self.SHORT_IMAGE_TYPES[self.str_type]
def __repr__(self):
return '<oca.Image("%s")>' % self.name
class ImagePool(Pool):
METHODS = {
'info' : 'imagepool.info',
}
def __init__(self, client):
super(ImagePool, self).__init__('IMAGE_POOL', 'IMAGE', client)
def _factory(self, xml):
i = Image(xml, self.client)
i._convert_types()
return i