Tony Mack | 5cbec5d | 2013-04-02 07:55:11 -0400 | [diff] [blame] | 1 | import os |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 2 | from django.db import models |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 3 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 4 | |
| 5 | # Create your models here. |
| 6 | |
| 7 | class PlCoreBase(models.Model): |
| 8 | |
| 9 | created = models.DateTimeField(auto_now_add=True) |
| 10 | updated = models.DateTimeField(auto_now=True) |
| 11 | |
| 12 | class Meta: |
| 13 | abstract = True |
| 14 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 15 | class Role(PlCoreBase): |
| 16 | |
| 17 | ROLE_CHOICES = (('admin', 'Admin'), ('pi', 'Principle Investigator'), ('user','User')) |
| 18 | role_type = models.CharField(max_length=80, unique=True, choices=ROLE_CHOICES) |
| 19 | |
| 20 | def __unicode__(self): return u'%s' % (self.role_type) |
| 21 | |
| 22 | def save(self): |
| 23 | if not self.id: |
| 24 | self.created = datetime.date.today() |
| 25 | self.updated = datetime.datetime.today() |
| 26 | super(Role, self).save() |
| 27 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 28 | class Site(PlCoreBase): |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 29 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 67f60ce | 2013-04-02 10:46:07 -0400 | [diff] [blame] | 30 | name = models.CharField(max_length=200, help_text="Name for this Site") |
Tony Mack | c689d50 | 2013-04-02 11:32:04 -0400 | [diff] [blame] | 31 | site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 32 | enabled = models.BooleanField(default=True, help_text="Status for this Site") |
| 33 | longitude = models.FloatField(null=True, blank=True) |
| 34 | latitude = models.FloatField(null=True, blank=True) |
| 35 | login_base = models.CharField(max_length=50, help_text="Prefix for Slices associated with this Site") |
| 36 | is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members") |
| 37 | abbreviated_name = models.CharField(max_length=80) |
| 38 | |
| 39 | def __unicode__(self): return u'%s' % (self.name) |
| 40 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 41 | def save(self, *args, **kwargs): |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 42 | driver = OpenStackDriver() |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 43 | if not self.id: |
Tony Mack | 39444e4 | 2013-04-02 10:22:24 -0400 | [diff] [blame] | 44 | tenant = driver.create_tenant(tenant_name=self.login_base, |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 45 | description=self.name, |
| 46 | enabled=self.enabled) |
Tony Mack | d8c390f | 2013-04-02 10:36:19 -0400 | [diff] [blame] | 47 | self.tenant_id = tenant.id |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 48 | else: |
| 49 | # update record |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 50 | self.driver.update_tenant(self.tenant_id, name=self.login_base, |
| 51 | description=self.name, enabled=self.enabled) |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 52 | super(Site, self).save(*args, **kwargs) |
| 53 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 54 | def delete(self, *args, **kwds): |
| 55 | # delete keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 56 | driver = OpenStackDriver() |
| 57 | driver.delete_tenant(self.tenant_id) |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 58 | super(Site, self).delete(*args, **kwargs) |
| 59 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 60 | class User(PlCoreBase): |
| 61 | user_id = models.CharField(max_length=256, unique=True) |
| 62 | firstname = models.CharField(help_text="person's given name", max_length=200) |
| 63 | lastname = models.CharField(help_text="person's surname", max_length=200) |
| 64 | email = models.EmailField(help_text="e-mail address") |
| 65 | phone = models.CharField(help_text="phone number contact", max_length=100) |
| 66 | user_url = models.URLField() |
| 67 | site = models.ForeignKey(Site, related_name='site_user', verbose_name="Site this user will be homed too") |
| 68 | |
| 69 | def __unicode__(self): return u'%s' % (self.email) |
| 70 | |
| 71 | def save(self): |
| 72 | if not self.id: |
| 73 | self.created = datetime.date.today() |
| 74 | self.updated = datetime.datetime.today() |
| 75 | super(User, self).save() |
| 76 | |
| 77 | class SitePrivilege(PlCoreBase): |
| 78 | |
| 79 | user = models.ForeignKey('User') |
| 80 | site = models.ForeignKey('Site') |
| 81 | role = models.ForeignKey('Role') |
| 82 | |
| 83 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 84 | |
| 85 | def save(self): |
| 86 | if not self.id: |
| 87 | self.created = datetime.date.today() |
| 88 | self.updated = datetime.datetime.today() |
| 89 | super(SitePrivilege, self).save() |
| 90 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 91 | class Slice(PlCoreBase): |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 92 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 93 | name = models.CharField(help_text="The Name of the Slice", max_length=80) |
Tony Mack | a3a40cf | 2013-04-02 11:21:21 -0400 | [diff] [blame] | 94 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 95 | SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None')) |
| 96 | instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES) |
| 97 | omf_friendly = models.BooleanField() |
| 98 | description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024) |
Tony Mack | c689d50 | 2013-04-02 11:32:04 -0400 | [diff] [blame] | 99 | slice_url = models.URLField(blank=True, max_length=512) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 100 | site = models.ForeignKey(Site, related_name='site_slice', help_text="The Site this Node belongs too") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 101 | |
| 102 | def __unicode__(self): return u'%s' % (self.name) |
| 103 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 104 | def save(self, *args, **kwds): |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 105 | # sync keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 106 | driver = OpenStackDriver() |
| 107 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 108 | if not self.id: |
Tony Mack | 39444e4 | 2013-04-02 10:22:24 -0400 | [diff] [blame] | 109 | tenant = driver.create_tenant(tenant_name=self.name, |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 110 | description=self.description, |
| 111 | enabled=self.enabled) |
Tony Mack | d8c390f | 2013-04-02 10:36:19 -0400 | [diff] [blame] | 112 | self.tenant_id = tenant.id |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 113 | |
| 114 | # create router |
| 115 | driver.create_router(name=self.name) |
| 116 | |
| 117 | # create a network |
| 118 | driver.create_network(name=self.name) |
| 119 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 120 | else: |
| 121 | # update record |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 122 | self.driver.update_tenant(self.tenant_id, name=self.name, |
| 123 | description=self.description, enabled=self.enabled) |
Tony Mack | 6299cb7 | 2013-04-02 11:23:26 -0400 | [diff] [blame] | 124 | super(Slice, self).save(*args, **kwds) |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 125 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 126 | def delete(self, *args, **kwds): |
| 127 | # delete keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 128 | driver = OpenStackDriver() |
| 129 | driver.delete_tenant(self.tenant_id) |
Tony Mack | 6299cb7 | 2013-04-02 11:23:26 -0400 | [diff] [blame] | 130 | super(Slice, self).delete(*args, **kwds) |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 131 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 132 | class SliceMembership(PlCoreBase): |
| 133 | user = models.ForeignKey('User') |
| 134 | slice = models.ForeignKey('Slice') |
| 135 | role = models.ForeignKey('Role') |
| 136 | |
| 137 | def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role) |
| 138 | |
| 139 | def save(self): |
| 140 | if not self.id: |
| 141 | self.created = datetime.date.today() |
| 142 | self.updated = datetime.datetime.today() |
| 143 | super(SliceMembership, self).save() |
| 144 | |
| 145 | class SubNet(PlCoreBase): |
| 146 | subnet_id = models.CharField(max_length=256, unique=True) |
| 147 | cidr = models.CharField(max_length=20) |
| 148 | ip_version = models.IntegerField() |
| 149 | start = models.IPAddressField() |
| 150 | end = models.IPAddressField() |
| 151 | slice = models.ForeignKey(Slice, related_name='slice_subnet') |
| 152 | |
| 153 | def __unicode__(self): return u'%s' % (self.name) |
| 154 | |
| 155 | def save(self, *args, **kwargs): |
| 156 | driver = OpenStackDriver() |
| 157 | if not self.id: |
| 158 | subnet = driver.create_subnet(network_name=self.slice.name, |
| 159 | cidr_ip = self.cidr, |
| 160 | ip_version=self.ip_version, |
| 161 | start = self.start, |
| 162 | end = self.end) |
| 163 | |
| 164 | self.subnet_id = subnet.id |
| 165 | |
| 166 | super(SubNet, self).save(*args, **kwargs) |
| 167 | |
| 168 | |
| 169 | def delete(self, *args, **kwargs): |
| 170 | # delete quantum network |
| 171 | driver = OpenStackDriver() |
| 172 | driver.delete_subnet(self.subnet_id) |
| 173 | super(SubNet, self).delete(*args, **kwargs) |
| 174 | |
| 175 | |
| 176 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 177 | class DeploymentNetwork(PlCoreBase): |
| 178 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network") |
| 179 | |
| 180 | def __unicode__(self): return u'%s' % (self.name) |
| 181 | |
| 182 | class SiteDeploymentNetwork(PlCoreBase): |
| 183 | class Meta: |
| 184 | unique_together = ['site', 'deploymentNetwork'] |
| 185 | |
| 186 | site = models.ForeignKey(Site, related_name='deploymentNetworks') |
| 187 | deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites') |
| 188 | name = models.CharField(default="Blah", max_length=100) |
| 189 | |
| 190 | |
| 191 | def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork) |
| 192 | |
| 193 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 194 | class Node(PlCoreBase): |
| 195 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Node") |
| 196 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.") |
| 197 | |
| 198 | def __unicode__(self): return u'%s' % (self.name) |
| 199 | |
| 200 | class Image(PlCoreBase): |
| 201 | image_id = models.CharField(max_length=256, unique=True) |
| 202 | name = models.CharField(max_length=256, unique=True) |
| 203 | disk_format = models.CharField(max_length=256) |
| 204 | container_format = models.CharField(max_length=256) |
| 205 | |
| 206 | def __unicode__(self): return u'%s' % (self.name) |
| 207 | |
| 208 | |
| 209 | class Flavor(PlCoreBase): |
| 210 | flavor_id = models.IntegerField(unique=True) |
| 211 | name = models.CharField(max_length=256, unique=True) |
| 212 | memory_mb = models.IntegerField() |
| 213 | disk_gb = models.IntegerField() |
| 214 | vcpus = models.IntegerField() |
| 215 | |
| 216 | def __unicode__(self): return u'%s' % (self.name) |
| 217 | |
| 218 | class Key(PlCoreBase): |
| 219 | name = models.CharField(max_length=256, unique=True) |
| 220 | key = models.CharField(max_length=512) |
| 221 | type = models.CharField(max_length=256) |
| 222 | blacklisted = models.BooleanField() |
| 223 | user = models.ForeignKey(User) |
| 224 | |
| 225 | def __unicode__(self): return u'%s' % (self.name) |
| 226 | |
| 227 | def save(self, *args, **kwds): |
| 228 | driver = OpenStackDriver() |
| 229 | if not self.id: |
| 230 | keypair = driver.create_keypair(name=self.name, key=self.key) |
| 231 | super(Key, self).save(*args, **kwds) |
| 232 | |
| 233 | def delete(self, *args, **kwds): |
| 234 | driver = OpenStackDriver() |
| 235 | driver.delete_keypair(self.name) |
| 236 | super(Key, self).delete(*args, **kwds) |
| 237 | |
| 238 | |
| 239 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 240 | class Sliver(PlCoreBase): |
Tony Mack | ebb2487 | 2013-04-02 08:20:59 -0400 | [diff] [blame] | 241 | instance_id = models.CharField(max_length=200, help_text="Nova instance id") |
Tony Mack | d9ae9da | 2013-04-02 08:58:22 -0400 | [diff] [blame] | 242 | name = models.CharField(max_length=200, help_text="Sliver name") |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 243 | flavor = models.ForeignKey(Flavor, related_name='sliver_flavor') |
| 244 | image = models.ForeignKey(Image, related_name='sliver_image') |
| 245 | key = models.ForeignKey(Key, related_name='sliver_key') |
Tony Mack | 27527b9 | 2013-04-02 08:59:35 -0400 | [diff] [blame] | 246 | slice = models.ForeignKey(Slice, related_name='sliver_slice') |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 247 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, related_name='sliver_deployment') |
| 248 | node = models.ForeignKey(Node, related_name='sliver_node') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 249 | |
| 250 | def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork) |
| 251 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 252 | def save(self, *args, **kwds): |
| 253 | driver = OpenStackDriver() |
| 254 | instance = driver.spawn_instances(name=self.name, |
| 255 | keyname=self.name, |
| 256 | hostnames=self.node.name, |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 257 | flavor=self.flavor.name, |
| 258 | image=self.image.name) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 259 | self.instance_id = instance.id |
| 260 | super(Sliver, self).save(*args, **kwds) |
| 261 | |
| 262 | def delete(self, *args, **kwds): |
| 263 | driver = OpenStackDriver() |
| 264 | driver.destroy_instance(name=self.name, id=self.instance_id) |
| 265 | super(Sliver, self).delete(*args, **kwds) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 266 | |