Revision d08a5f6f db/models.py
b/db/models.py | ||
---|---|---|
7 | 7 |
|
8 | 8 |
import datetime |
9 | 9 |
|
10 |
import vocabs |
|
11 |
|
|
12 | 10 |
backend_prefix_id = settings.BACKEND_PREFIX_ID |
13 | 11 |
|
14 |
#this needs to be called not only from inside VirtualMachine. |
|
15 |
#atm it is not working with the current ganeti naming - machine1, machine2 |
|
16 |
def id_from_instance_name(name): |
|
17 |
""" Returns VirtualMachine's Django id, given a ganeti machine name. |
|
18 |
|
|
19 |
Strips the ganeti prefix atm. Needs a better name! |
|
20 |
""" |
|
21 |
return '%s' % (str(name).strip(backend_prefix_id)) |
|
22 |
|
|
23 | 12 |
|
24 | 13 |
class Limit(models.Model): |
25 | 14 |
description = models.CharField(max_length=45) |
... | ... | |
31 | 20 |
return self.description |
32 | 21 |
|
33 | 22 |
|
34 |
class OceanUser(models.Model):
|
|
23 |
class SynnefoUser(models.Model):
|
|
35 | 24 |
name = models.CharField(max_length=255) |
36 | 25 |
credit = models.IntegerField() |
37 | 26 |
quota = models.IntegerField() |
38 | 27 |
created = models.DateField() |
39 | 28 |
monthly_rate = models.IntegerField() |
40 |
user = models.ForeignKey(User, unique=True) |
|
29 |
#user = models.ForeignKey(User, unique=True)
|
|
41 | 30 |
limits = models.ManyToManyField(Limit, through='UserLimit') |
42 | 31 |
|
43 | 32 |
class Meta: |
44 |
verbose_name = u'Ocean User'
|
|
33 |
verbose_name = u'Synnefo User'
|
|
45 | 34 |
|
46 | 35 |
def __unicode__(self): |
47 | 36 |
return self.name |
... | ... | |
76 | 65 |
|
77 | 66 |
|
78 | 67 |
class UserLimit(models.Model): |
79 |
user = models.ForeignKey(OceanUser)
|
|
68 |
user = models.ForeignKey(SynnefoUser)
|
|
80 | 69 |
limit = models.ForeignKey(Limit) |
81 | 70 |
value = models.IntegerField() |
82 | 71 |
|
... | ... | |
98 | 87 |
|
99 | 88 |
def _get_name(self): |
100 | 89 |
"""Returns flavor name""" |
101 |
return u'C%dR%dD%d' % ( self.cpu, self.ram, self.disk )
|
|
90 |
return u'C%dR%dD%d' % (self.cpu, self.ram, self.disk)
|
|
102 | 91 |
|
103 | 92 |
def _get_cost_inactive(self): |
104 | 93 |
self._update_costs() |
... | ... | |
133 | 122 |
verbose_name = u'Pricing history for flavors' |
134 | 123 |
|
135 | 124 |
def __unicode__(self): |
136 |
return u'Costs (up, down)=(%d, %d) for %s since %s' % ( cost_active, cost_inactive, flavor.name, effective_from )
|
|
125 |
return u'Costs (up, down)=(%d, %d) for %s since %s' % (cost_active, cost_inactive, flavor.name, effective_from)
|
|
137 | 126 |
|
138 | 127 |
|
139 | 128 |
class VirtualMachine(models.Model): |
129 |
ACTIONS = ( |
|
130 |
('CREATE', 'Create VM'), |
|
131 |
('START', 'Start VM'), |
|
132 |
('STOP', 'Shutdown VM'), |
|
133 |
('SUSPEND', 'Admin Suspend VM'), |
|
134 |
('REBOOT', 'Reboot VM'), |
|
135 |
('DESTROY', 'Destroy VM') |
|
136 |
) |
|
137 |
|
|
138 |
OPER_STATES = ( |
|
139 |
('BUILD', 'Queued for creation'), |
|
140 |
('ERROR', 'Creation failed'), |
|
141 |
('STOPPED', 'Stopped'), |
|
142 |
('STARTED', 'Started'), |
|
143 |
('DESTROYED', 'Destroyed') |
|
144 |
) |
|
145 |
|
|
146 |
BACKEND_OPCODES = ( |
|
147 |
('OP_INSTANCE_CREATE', 'Create Instance'), |
|
148 |
('OP_INSTANCE_REMOVE', 'Remove Instance'), |
|
149 |
('OP_INSTANCE_STARTUP', 'Startup Instance'), |
|
150 |
('OP_INSTANCE_SHUTDOWN', 'Shutdown Instance'), |
|
151 |
('OP_INSTANCE_REBOOT', 'Reboot Instance') |
|
152 |
) |
|
153 |
|
|
154 |
BACKEND_STATUSES = ( |
|
155 |
('queued', 'request queued'), |
|
156 |
('waiting', 'request waiting for locks'), |
|
157 |
('canceling', 'request being canceled'), |
|
158 |
('running', 'request running'), |
|
159 |
('canceled', 'request canceled'), |
|
160 |
('success', 'request completed successfully'), |
|
161 |
('error', 'request returned error') |
|
162 |
) |
|
163 |
|
|
164 |
# The operating state of a VM, |
|
165 |
# upon the successful completion of a backend operation. |
|
166 |
OPER_STATE_FROM_OPCODE = { |
|
167 |
'OP_INSTANCE_CREATE': 'STARTED', |
|
168 |
'OP_INSTANCE_REMOVE': 'DESTROYED', |
|
169 |
'OP_INSTANCE_STARTUP': 'STARTED', |
|
170 |
'OP_INSTANCE_SHUTDOWN': 'STOPPED', |
|
171 |
'OP_INSTANCE_REBOOT': 'STARTED' |
|
172 |
} |
|
173 |
|
|
174 |
RSAPI_STATE_FROM_OPER_STATE = { |
|
175 |
"BUILD": "BUILD", |
|
176 |
"ERROR": "ERROR", |
|
177 |
"STOPPED": "STOPPED", |
|
178 |
"STARTED": "ACTIVE", |
|
179 |
"DESTROYED": "DELETED" |
|
180 |
} |
|
181 |
|
|
140 | 182 |
name = models.CharField(max_length=255) |
141 |
created = models.DateTimeField() |
|
142 |
state = models.CharField(choices=vocabs.STATES, max_length=30) |
|
183 |
created = models.DateTimeField(help_text=_('VM creation date'), default=datetime.datetime.now) |
|
143 | 184 |
charged = models.DateTimeField() |
144 |
imageid = models.IntegerField() |
|
185 |
# Use string reference to avoid circular ForeignKey def. |
|
186 |
# FIXME: "sourceimage" works, "image" causes validation errors. See "related_name" in the Django docs. |
|
187 |
sourceimage = models.ForeignKey("Image") |
|
145 | 188 |
hostid = models.CharField(max_length=100) |
146 |
server_label = models.CharField(max_length=100) |
|
147 |
image_version = models.CharField(max_length=100) |
|
189 |
description = models.TextField(help_text=_('description')) |
|
148 | 190 |
ipfour = models.IPAddressField() |
149 | 191 |
ipsix = models.CharField(max_length=100) |
150 |
owner = models.ForeignKey(OceanUser)
|
|
192 |
owner = models.ForeignKey(SynnefoUser)
|
|
151 | 193 |
flavor = models.ForeignKey(Flavor) |
152 |
|
|
153 |
class Meta: |
|
154 |
verbose_name = u'Virtual machine instance' |
|
155 |
get_latest_by = 'created' |
|
156 |
|
|
157 |
def __unicode__(self): |
|
158 |
return self.name |
|
194 |
suspended = models.BooleanField('Administratively Suspended') |
|
195 |
|
|
196 |
# VM State [volatile data] |
|
197 |
updated = models.DateTimeField(null=True) |
|
198 |
action = models.CharField(choices=ACTIONS, max_length=30, null=True) |
|
199 |
operstate = models.CharField(choices=OPER_STATES, max_length=30, null=True) |
|
200 |
backendjobid = models.PositiveIntegerField(null=True) |
|
201 |
backendopcode = models.CharField(choices=BACKEND_OPCODES, max_length=30, null=True) |
|
202 |
backendjobstatus = models.CharField(choices=BACKEND_STATUSES, max_length=30, null=True) |
|
203 |
backendlogmsg = models.TextField(null=True) |
|
204 |
|
|
205 |
# Error classes |
|
206 |
class InvalidBackendIdError(Exception): |
|
207 |
def __init__(self, value): |
|
208 |
self.value = value |
|
209 |
def __str__(self): |
|
210 |
return repr(self.value) |
|
211 |
|
|
212 |
class InvalidBackendMsgError(Exception): |
|
213 |
def __init__(self, opcode, status): |
|
214 |
self.opcode = opcode |
|
215 |
self.status = status |
|
216 |
def __str__(self): |
|
217 |
return repr("<opcode: %s, status: %s>" % (str(self.opcode), str(self.status))) |
|
218 |
|
|
219 |
class InvalidActionError(Exception): |
|
220 |
def __init__(self, action): |
|
221 |
self.action = action |
|
222 |
def __str__(self): |
|
223 |
return repr(str(self.action)) |
|
224 |
|
|
225 |
|
|
226 |
@staticmethod |
|
227 |
def id_from_instance_name(name): |
|
228 |
"""Returns VirtualMachine's Django id, given a ganeti machine name. |
|
229 |
|
|
230 |
Strips the ganeti prefix atm. Needs a better name! |
|
231 |
|
|
232 |
""" |
|
233 |
if not str(name).startswith(backend_prefix_id): |
|
234 |
raise VirtualMachine.InvalidBackendIdError(str(name)) |
|
235 |
ns = str(name).lstrip(backend_prefix_id) |
|
236 |
if not ns.isdigit(): |
|
237 |
raise VirtualMachine.InvalidBackendIdError(str(name)) |
|
238 |
return int(ns) |
|
239 |
|
|
240 |
def __init__(self, *args, **kw): |
|
241 |
"""Initialize state for just created VM instances.""" |
|
242 |
super(VirtualMachine, self).__init__(*args, **kw) |
|
243 |
# Before this instance gets save()d |
|
244 |
if not self.pk: |
|
245 |
self.action = None |
|
246 |
self.operstate = "BUILD" |
|
247 |
self.updated = datetime.datetime.now() |
|
248 |
self.backendjobid = None |
|
249 |
self.backendjobstatus = None |
|
250 |
self.backendopcode = None |
|
251 |
self.backendlogmsg = None |
|
252 |
|
|
253 |
def process_backend_msg(self, jobid, opcode, status, logmsg): |
|
254 |
"""Process a job progress notification from the backend. |
|
255 |
|
|
256 |
Process an incoming message from the backend (currently Ganeti). |
|
257 |
Job notifications with a terminating status (sucess, error, or canceled), |
|
258 |
also update the operating state of the VM. |
|
259 |
|
|
260 |
""" |
|
261 |
if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or |
|
262 |
status not in [x[0] for x in VirtualMachine.BACKEND_STATUSES]): |
|
263 |
raise VirtualMachine.InvalidBackendMsgError(opcode, status) |
|
264 |
|
|
265 |
self.backendjobid = jobid |
|
266 |
self.backendjobstatus = status |
|
267 |
self.backendopcode = opcode |
|
268 |
self.backendlogmsg = logmsg |
|
269 |
|
|
270 |
# Notifications of success change the operating state |
|
271 |
if status == 'success': |
|
272 |
self.operstate = VirtualMachine.OPER_STATE_FROM_OPCODE[opcode] |
|
273 |
# Special cases OP_INSTANCE_CREATE fails --> ERROR |
|
274 |
if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE': |
|
275 |
self.operstate = 'ERROR' |
|
276 |
# Any other notification of failure leaves the operating state unchanged |
|
277 |
|
|
278 |
# FIXME: Should be implemented in a pre-save signal handler. |
|
279 |
self.updated = datetime.datetime.now() |
|
280 |
self.save() |
|
281 |
|
|
282 |
def start_action(self, action): |
|
283 |
"""Update the state of a VM when a new action is initiated.""" |
|
284 |
if not action in [x[0] for x in VirtualMachine.ACTIONS]: |
|
285 |
raise VirtualMachine.InvalidActionError(action) |
|
286 |
|
|
287 |
self.action = action |
|
288 |
self.backendjobid = None |
|
289 |
self.backendopcode = None |
|
290 |
self.backendlogmsg = None |
|
291 |
self.updated = datetime.datetime.now() |
|
292 |
self.save() |
|
293 |
|
|
294 |
# FIXME: Perhaps move somewhere else, outside the model? |
|
295 |
def _get_rsapi_state(self): |
|
296 |
try: |
|
297 |
return VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[self.operstate] |
|
298 |
except KeyError: |
|
299 |
return "UNKNOWN" |
|
300 |
|
|
301 |
rsapi_state = property(_get_rsapi_state) |
|
159 | 302 |
|
160 | 303 |
def _get_backend_id(self): |
161 | 304 |
"""Returns the backend id for this VM by prepending backend-prefix.""" |
... | ... | |
163 | 306 |
|
164 | 307 |
backend_id = property(_get_backend_id) |
165 | 308 |
|
309 |
class Meta: |
|
310 |
verbose_name = u'Virtual machine instance' |
|
311 |
get_latest_by = 'created' |
|
312 |
|
|
313 |
def __unicode__(self): |
|
314 |
return self.name |
|
315 |
|
|
166 | 316 |
|
167 | 317 |
class VirtualMachineGroup(models.Model): |
168 |
"Groups of VM's for OceanUsers"
|
|
318 |
"Groups of VM's for SynnefoUsers"
|
|
169 | 319 |
name = models.CharField(max_length=255) |
170 | 320 |
owner = models.ForeignKey(User) |
171 | 321 |
machines = models.ManyToManyField(VirtualMachine) |
... | ... | |
188 | 338 |
verbose_name = u'Key-value pair of metadata for a VM.' |
189 | 339 |
|
190 | 340 |
def __unicode__(self): |
191 |
return u'%s, %s for %s' % ( self.key, self.value, self.vm.name )
|
|
341 |
return u'%s, %s for %s' % (self.key, self.value, self.vm.name)
|
|
192 | 342 |
|
193 | 343 |
|
194 | 344 |
class AccountingLog(models.Model): |
195 | 345 |
vm = models.ForeignKey(VirtualMachine) |
196 | 346 |
date = models.DateTimeField() |
197 |
state = models.CharField(choices=vocabs.STATES, max_length=30)
|
|
347 |
state = models.CharField(choices=VirtualMachine.OPER_STATES, max_length=30)
|
|
198 | 348 |
|
199 | 349 |
class Meta: |
200 | 350 |
verbose_name = u'Accounting log' |
201 | 351 |
|
202 | 352 |
def __unicode__(self): |
203 |
return u'%s %s %s' % ( self.vm.name, self.date, self.state )
|
|
353 |
return u'%s %s %s' % (self.vm.name, self.date, self.state)
|
|
204 | 354 |
|
205 | 355 |
|
206 | 356 |
class Image(models.Model): |
357 |
# This is WIP, FIXME |
|
358 |
IMAGE_STATES = ( |
|
359 |
('ACTIVE', 'Active'), |
|
360 |
('SAVING', 'Saving'), |
|
361 |
('DELETED', 'Deleted') |
|
362 |
) |
|
363 |
|
|
207 | 364 |
name = models.CharField(max_length=255, help_text=_('description')) |
208 | 365 |
updated = models.DateTimeField(help_text=_("Image update date")) |
209 | 366 |
created = models.DateTimeField(help_text=_("Image creation date"), default=datetime.datetime.now) |
210 |
state = models.CharField(choices=vocabs.STATES, max_length=30)
|
|
367 |
state = models.CharField(choices=IMAGE_STATES, max_length=30)
|
|
211 | 368 |
description = models.TextField(help_text=_('description')) |
212 |
serverid = models.IntegerField(help_text=_('description')) |
|
213 |
vm = models.ForeignKey(VirtualMachine) |
|
214 | 369 |
owner = models.ForeignKey(User,blank=True, null=True) |
215 |
|
|
370 |
vm = models.ForeignKey(VirtualMachine, null=True) |
|
371 |
#FIXME: ImageMetadata, as in VirtualMachineMetadata |
|
372 |
# "os" contained in metadata. Newly created Server inherits value of "os" metadata key from Image. |
|
373 |
# The Web UI uses the value of "os" to determine the icon to use. |
|
374 |
|
|
216 | 375 |
class Meta: |
217 | 376 |
verbose_name = u'Image' |
218 | 377 |
|
219 | 378 |
def __unicode__(self): |
220 |
return u'%s' % ( self.name ) |
|
379 |
return u'%s' % (self.name) |
Also available in: Unified diff