root / db / models.py @ 5b32775a
History | View | Annotate | Download (4.2 kB)
1 |
# vim: ts=4 sts=4 et ai sw=4 fileencoding=utf-8
|
---|---|
2 |
|
3 |
from django.conf import settings |
4 |
from django.db import models |
5 |
from django.contrib.auth.models import User |
6 |
from django.utils.translation import gettext_lazy as _ |
7 |
|
8 |
import datetime |
9 |
|
10 |
import vocabs |
11 |
|
12 |
ganeti_prefix_id = settings.GANETI_PREFIX_ID |
13 |
|
14 |
|
15 |
def id_from_instance_name(name): |
16 |
"Returns VirtualMachine's Django id, given a ganeti machine name"
|
17 |
"Strips the ganeti prefix atm. Needs a better name!"
|
18 |
return '%s' % (str(name).strip(ganeti_prefix_id)) |
19 |
|
20 |
|
21 |
|
22 |
class Limit(models.Model): |
23 |
description = models.CharField(max_length=45)
|
24 |
|
25 |
class Meta: |
26 |
verbose_name = u'Available limits for users'
|
27 |
|
28 |
def __unicode__(self): |
29 |
return self.description |
30 |
|
31 |
|
32 |
class OceanUser(models.Model): |
33 |
name = models.CharField(max_length=255)
|
34 |
credit = models.IntegerField() |
35 |
quota = models.IntegerField() |
36 |
created = models.DateField() |
37 |
monthly_rate = models.IntegerField() |
38 |
user = models.ForeignKey(User, unique=True)
|
39 |
limits = models.ManyToManyField(Limit, through='UserLimit')
|
40 |
|
41 |
class Meta: |
42 |
verbose_name = u'Ocean User'
|
43 |
|
44 |
def __unicode__(self): |
45 |
return self.name |
46 |
|
47 |
|
48 |
class UserLimit(models.Model): |
49 |
user = models.ForeignKey(OceanUser) |
50 |
limit = models.ForeignKey(Limit) |
51 |
value = models.IntegerField() |
52 |
|
53 |
class Meta: |
54 |
unique_together = ('user', 'limit') |
55 |
verbose_name = u'Enforced limits for each user'
|
56 |
|
57 |
def __unicode__(self): |
58 |
return u'Limit %s for user %s: %d' % (self.limit, self.user, self.value) |
59 |
|
60 |
|
61 |
class Flavor(models.Model): |
62 |
cpu = models.IntegerField(default=0)
|
63 |
ram = models.IntegerField(default=0)
|
64 |
disk = models.IntegerField(default=0)
|
65 |
cost_active = models.PositiveIntegerField() |
66 |
cost_inactive = models.PositiveIntegerField() |
67 |
|
68 |
class Meta: |
69 |
verbose_name = u'Virtual Machine flavors'
|
70 |
|
71 |
def _get_name(self): |
72 |
return u'c%dr%dd%d' % ( self.cpu, self.ram, self.disk ) |
73 |
|
74 |
name = property(_get_name)
|
75 |
|
76 |
def __unicode__(self): |
77 |
return self.name |
78 |
|
79 |
|
80 |
class VirtualMachine(models.Model): |
81 |
name = models.CharField(max_length=255)
|
82 |
created = models.DateTimeField() |
83 |
state = models.CharField(choices=vocabs.STATES, max_length=30)
|
84 |
charged = models.DateTimeField() |
85 |
imageid = models.IntegerField() |
86 |
hostid = models.CharField(max_length=100)
|
87 |
server_label = models.CharField(max_length=100)
|
88 |
image_version = models.CharField(max_length=100)
|
89 |
ipfour = models.IPAddressField() |
90 |
ipsix = models.CharField(max_length=100)
|
91 |
owner = models.ForeignKey(OceanUser) |
92 |
flavor = models.ForeignKey(Flavor) |
93 |
|
94 |
class Meta: |
95 |
verbose_name = u'Virtual machine instances'
|
96 |
get_latest_by = 'created'
|
97 |
|
98 |
def __unicode__(self): |
99 |
return self.name |
100 |
|
101 |
def _get_ganeti_id(self): |
102 |
"Returns the ganeti-prefix + id of the VM, for ganeti"
|
103 |
return '%s%s' % (ganeti_prefix_id, str(self.id)) |
104 |
|
105 |
ganeti_id = property(_get_ganeti_id)
|
106 |
|
107 |
class VirtualMachineMetadata(models.Model): |
108 |
meta_key = models.CharField(max_length=50)
|
109 |
meta_value = models.CharField(max_length=500)
|
110 |
vm = models.ForeignKey(VirtualMachine) |
111 |
|
112 |
class Meta: |
113 |
verbose_name = u'Metadata for virtual machine instances'
|
114 |
|
115 |
def __unicode__(self): |
116 |
return u'%s, %s for %s' % ( self.key, self.value, self.vm.name ) |
117 |
|
118 |
|
119 |
class AccountingLog(models.Model): |
120 |
vm = models.ForeignKey(VirtualMachine) |
121 |
date = models.DateTimeField() |
122 |
state = models.CharField(choices=vocabs.STATES, max_length=30)
|
123 |
|
124 |
class Meta: |
125 |
verbose_name = u'Charging log'
|
126 |
|
127 |
def __unicode__(self): |
128 |
return u'%s %s %s' % ( self.vm.name, self.date, self.state ) |
129 |
|
130 |
|
131 |
class Image(models.Model): |
132 |
name = models.CharField(max_length=255, help_text=_('description')) |
133 |
updated = models.DateTimeField(help_text=_("Image update date"))
|
134 |
created = models.DateTimeField(help_text=_("Image creation date"), default=datetime.datetime.now)
|
135 |
state = models.CharField(choices=vocabs.STATES, max_length=30)
|
136 |
description = models.TextField(help_text=_('description'))
|
137 |
serverid = models.IntegerField(help_text=_('description'))
|
138 |
vm = models.ForeignKey(VirtualMachine) |
139 |
|
140 |
class Meta: |
141 |
verbose_name = u'Image'
|
142 |
|
143 |
def __unicode__(self): |
144 |
return u'%s' % ( self.name ) |