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