Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1 | from django.db import models |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 2 | from core.models import User, Service, SingletonModel, PlCoreBase |
Scott Baker | 3b5fa5b | 2015-03-18 17:37:46 -0700 | [diff] [blame] | 3 | from core.models.plcorebase import StrippedCharField |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 4 | import os |
| 5 | from django.db import models |
| 6 | from django.forms.models import model_to_dict |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 7 | from django.db.models import Q |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 8 | |
| 9 | |
| 10 | # Create your models here. |
| 11 | |
Scott Baker | 3035039 | 2015-03-13 09:17:33 -0700 | [diff] [blame] | 12 | class HpcService(Service): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 13 | |
| 14 | class Meta: |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 15 | app_label = "hpc" |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 16 | verbose_name = "HPC Service" |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 17 | |
Scott Baker | 1fa4e64 | 2015-03-30 19:14:52 -0700 | [diff] [blame] | 18 | cmi_hostname = StrippedCharField(max_length=254, null=True, blank=True) |
Scott Baker | 3035039 | 2015-03-13 09:17:33 -0700 | [diff] [blame] | 19 | |
Scott Baker | 1facc3e | 2015-05-11 16:38:16 -0700 | [diff] [blame] | 20 | @property |
| 21 | def scale(self): |
| 22 | hpc_slices = [x for x in self.slices.all() if "hpc" in x.name] |
| 23 | if not hpc_slices: |
| 24 | return 0 |
| 25 | return hpc_slices[0].slivers.count() |
| 26 | |
| 27 | @scale.setter |
| 28 | def scale(self, value): |
| 29 | self.set_scale = value |
| 30 | |
| 31 | def save(self, *args, **kwargs): |
| 32 | super(HpcService, self).save(*args, **kwargs) |
| 33 | |
| 34 | # scale up/down |
| 35 | scale = getattr(self, "set_scale", None) |
| 36 | if scale is not None: |
| 37 | exclude_slices = [x for x in self.slices.all() if "cmi" in x.name] |
| 38 | self.adjust_scale(slice_hint="hpc", scale=scale, exclusive_slices = exclude_slices) |
| 39 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 40 | class ServiceProvider(PlCoreBase): |
| 41 | class Meta: |
| 42 | app_label = "hpc" |
Scott Baker | e728c89 | 2014-01-12 11:41:09 -0800 | [diff] [blame] | 43 | |
Scott Baker | 2691018 | 2015-03-31 21:45:14 -0700 | [diff] [blame] | 44 | hpcService = models.ForeignKey(HpcService) |
Scott Baker | e728c89 | 2014-01-12 11:41:09 -0800 | [diff] [blame] | 45 | service_provider_id = models.IntegerField(null=True, blank=True) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 46 | name = models.CharField(max_length=254,help_text="Service Provider Name") |
| 47 | description = models.TextField(max_length=254,null=True, blank=True, help_text="Description of Service Provider") |
| 48 | enabled = models.BooleanField(default=True) |
| 49 | |
| 50 | def __unicode__(self): return u'%s' % (self.name) |
| 51 | |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 52 | @classmethod |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 53 | def filter_by_hpcService(cls, qs, hpcService): |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 54 | # This should be overridden by descendant classes that want to perform |
| 55 | # filtering of visible objects by user. |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 56 | return qs.filter(hpcService=hpcService) |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 57 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 58 | class ContentProvider(PlCoreBase): |
| 59 | class Meta: |
| 60 | app_label = "hpc" |
Scott Baker | e728c89 | 2014-01-12 11:41:09 -0800 | [diff] [blame] | 61 | |
Scott Baker | 7a09076 | 2014-05-07 09:04:07 -0700 | [diff] [blame] | 62 | # legacy vicci content providers already have names. |
| 63 | CP_TO_ACCOUNT = {"ON.LAB": "onlabcp", |
| 64 | "Syndicate": "syndicatecp"} |
| 65 | |
Scott Baker | e728c89 | 2014-01-12 11:41:09 -0800 | [diff] [blame] | 66 | content_provider_id = models.IntegerField(null=True, blank=True) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 67 | name = models.CharField(max_length=254) |
| 68 | enabled = models.BooleanField(default=True) |
| 69 | description = models.TextField(max_length=254,null=True, blank=True,help_text="Description of Content Provider") |
| 70 | serviceProvider = models.ForeignKey(ServiceProvider) |
| 71 | |
| 72 | # Note user relationships are directed not requiring a role. |
| 73 | users = models.ManyToManyField(User) |
| 74 | |
| 75 | def __unicode__(self): return u'%s' % (self.name) |
| 76 | |
Scott Baker | 7a09076 | 2014-05-07 09:04:07 -0700 | [diff] [blame] | 77 | @property |
| 78 | def account(self): |
| 79 | return self.CP_TO_ACCOUNT.get(self.name, self.name) |
| 80 | |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 81 | @classmethod |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 82 | def filter_by_hpcService(cls, qs, hpcService): |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 83 | # This should be overridden by descendant classes that want to perform |
| 84 | # filtering of visible objects by user. |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 85 | return qs.filter(serviceProvider__hpcService=hpcService) |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 86 | |
| 87 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 88 | class OriginServer(PlCoreBase): |
| 89 | class Meta: |
| 90 | app_label = "hpc" |
Scott Baker | e728c89 | 2014-01-12 11:41:09 -0800 | [diff] [blame] | 91 | |
| 92 | origin_server_id = models.IntegerField(null=True, blank=True) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 93 | url = models.URLField() |
| 94 | contentProvider = models.ForeignKey(ContentProvider) |
| 95 | |
| 96 | authenticated = models.BooleanField(default=False, help_text="Status for this Site") |
| 97 | enabled = models.BooleanField(default=True, help_text="Status for this Site") |
| 98 | PROTOCOL_CHOICES = (('http', 'HTTP'),('rtmp', 'RTMP'), ('rtp', 'RTP'),('shout', 'SHOUTcast')) |
| 99 | protocol = models.CharField(default="HTTP", max_length = 12, choices=PROTOCOL_CHOICES) |
| 100 | redirects = models.BooleanField(default=True, help_text="Indicates whether Origin Server redirects should be used for this Origin Server") |
| 101 | description = models.TextField(null=True, blank=True, max_length=255) |
| 102 | |
| 103 | def __unicode__(self): return u'%s' % (self.url) |
| 104 | |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 105 | @classmethod |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 106 | def filter_by_hpcService(cls, qs, hpcService): |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 107 | # This should be overridden by descendant classes that want to perform |
| 108 | # filtering of visible objects by user. |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 109 | return qs.filter(contentProvider__serviceProvider__hpcService=hpcService) |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 110 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 111 | class CDNPrefix(PlCoreBase): |
| 112 | class Meta: |
| 113 | app_label = "hpc" |
Scott Baker | e728c89 | 2014-01-12 11:41:09 -0800 | [diff] [blame] | 114 | |
| 115 | cdn_prefix_id = models.IntegerField(null=True, blank=True) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 116 | prefix = models.CharField(max_length=200, help_text="Registered Prefix for Domain") |
| 117 | contentProvider = models.ForeignKey(ContentProvider) |
| 118 | description = models.TextField(max_length=254,null=True, blank=True,help_text="Description of Content Provider") |
| 119 | |
Scott Baker | 9d4b1c3 | 2014-08-27 11:20:33 -0700 | [diff] [blame] | 120 | defaultOriginServer = models.ForeignKey(OriginServer, blank=True, null=True) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 121 | enabled = models.BooleanField(default=True) |
| 122 | |
| 123 | def __unicode__(self): return u'%s' % (self.prefix) |
| 124 | |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 125 | @classmethod |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 126 | def filter_by_hpcService(cls, qs, hpcService): |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 127 | # This should be overridden by descendant classes that want to perform |
| 128 | # filtering of visible objects by user. |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 129 | return qs.filter(contentProvider__serviceProvider__hpcService=hpcService) |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 130 | |
Scott Baker | 77bfa22 | 2015-03-03 00:06:53 -0800 | [diff] [blame] | 131 | class AccessMap(PlCoreBase): |
| 132 | class Meta: |
| 133 | app_label = "hpc" |
| 134 | |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 135 | contentProvider = models.ForeignKey(ContentProvider) |
| 136 | name = models.CharField(max_length=64, help_text="Name of the Access Map") |
| 137 | description = models.TextField(null=True, blank=True,max_length=130) |
| 138 | map = models.FileField(upload_to="maps/", help_text="specifies which client requests are allowed") |
| 139 | |
| 140 | def __unicode__(self): return self.name |
| 141 | |
Scott Baker | 77bfa22 | 2015-03-03 00:06:53 -0800 | [diff] [blame] | 142 | class SiteMap(PlCoreBase): |
| 143 | class Meta: |
| 144 | app_label = "hpc" |
| 145 | |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 146 | """ can be bound to a ContentProvider, ServiceProvider, or neither """ |
| 147 | contentProvider = models.ForeignKey(ContentProvider, blank=True, null=True) |
| 148 | serviceProvider = models.ForeignKey(ServiceProvider, blank=True, null=True) |
Scott Baker | 77bfa22 | 2015-03-03 00:06:53 -0800 | [diff] [blame] | 149 | cdnPrefix = models.ForeignKey(CDNPrefix, blank = True, null=True) |
Scott Baker | 3035039 | 2015-03-13 09:17:33 -0700 | [diff] [blame] | 150 | hpcService = models.ForeignKey(HpcService, blank = True, null=True) |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 151 | name = models.CharField(max_length=64, help_text="Name of the Site Map") |
| 152 | description = models.TextField(null=True, blank=True,max_length=130) |
| 153 | map = models.FileField(upload_to="maps/", help_text="specifies how to map requests to hpc instances") |
Scott Baker | ee53c44 | 2015-03-09 16:22:56 -0700 | [diff] [blame] | 154 | map_id = models.IntegerField(null=True, blank=True) |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 155 | |
| 156 | def __unicode__(self): return self.name |
Scott Baker | 77bfa22 | 2015-03-03 00:06:53 -0800 | [diff] [blame] | 157 | |
| 158 | def save(self, *args, **kwds): |
Scott Baker | 3035039 | 2015-03-13 09:17:33 -0700 | [diff] [blame] | 159 | if (self.contentProvider) and (self.serviceProvider or self.cdnPrefix or self.hpcService): |
| 160 | raise ValueError("You may only set one of contentProvider, serviceProvider, cdnPrefix, or hpcService") |
| 161 | if (self.serviceProvider) and (self.cdnPrefix or self.hpcService): |
| 162 | raise ValueError("You may only set one of contentProvider, serviceProvider, cdnPrefix, or hpcService") |
| 163 | if (self.cdnPrefix) and (self.hpcService): |
| 164 | raise ValueError("You may only set one of contentProvider, serviceProvider, cdnPrefix, or hpcService") |
Scott Baker | 77bfa22 | 2015-03-03 00:06:53 -0800 | [diff] [blame] | 165 | |
| 166 | super(SiteMap, self).save(*args, **kwds) |
Scott Baker | 3b5fa5b | 2015-03-18 17:37:46 -0700 | [diff] [blame] | 167 | |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 168 | @classmethod |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 169 | def filter_by_hpcService(cls, qs, hpcService): |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 170 | # This should be overridden by descendant classes that want to perform |
| 171 | # filtering of visible objects by user. |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 172 | return qs.filter(Q(hpcService=hpcService) | |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 173 | Q(serviceProvider__hpcService=hpcService) | |
| 174 | Q(contentProvider__serviceProvider__hpcService=hpcService) | |
| 175 | Q(cdnPrefix__contentProvider__serviceProvider__hpcService=hpcService)) |
| 176 | |
Scott Baker | 3b5fa5b | 2015-03-18 17:37:46 -0700 | [diff] [blame] | 177 | class HpcHealthCheck(PlCoreBase): |
| 178 | class Meta: |
| 179 | app_label = "hpc" |
| 180 | |
| 181 | KIND_CHOICES = (('dns', 'DNS'), ('http', 'HTTP')) |
| 182 | |
| 183 | hpcService = models.ForeignKey(HpcService, blank = True, null=True) |
| 184 | kind = models.CharField(max_length=30, choices=KIND_CHOICES, default="dns") |
| 185 | resource_name = StrippedCharField(max_length=1024, blank=False, null=False) |
| 186 | result_contains = StrippedCharField(max_length=1024, blank=True, null=True) |
| 187 | result_min_size = models.IntegerField(null=True, blank=True) |
| 188 | result_max_size = models.IntegerField(null=True, blank=True) |
| 189 | |
| 190 | def __unicode__(self): return self.resource_name |
| 191 | |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 192 | @classmethod |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 193 | def filter_by_hpcService(cls, qs, hpcService): |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 194 | # This should be overridden by descendant classes that want to perform |
| 195 | # filtering of visible objects by user. |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 196 | return qs.filter(hpcService=hpcService) |
Scott Baker | 02c5177 | 2015-04-02 21:31:50 -0700 | [diff] [blame] | 197 | |
| 198 | |