blob: a3b7c90ba2e9eb8d269e1775a0e7d76289d101e3 [file] [log] [blame]
Siobhan Tullybfd11dc2013-09-03 12:59:24 -04001from django.db import models
Siobhan Tullycf04fb62014-01-11 11:25:57 -05002from core.models import User, Service, SingletonModel, PlCoreBase
Scott Baker3b5fa5b2015-03-18 17:37:46 -07003from core.models.plcorebase import StrippedCharField
Siobhan Tullybfd11dc2013-09-03 12:59:24 -04004import os
5from django.db import models
6from django.forms.models import model_to_dict
Scott Baker02c51772015-04-02 21:31:50 -07007from django.db.models import Q
Siobhan Tullybfd11dc2013-09-03 12:59:24 -04008
9
10# Create your models here.
11
Scott Baker30350392015-03-13 09:17:33 -070012class HpcService(Service):
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040013
14 class Meta:
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040015 app_label = "hpc"
Siobhan Tullycf04fb62014-01-11 11:25:57 -050016 verbose_name = "HPC Service"
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040017
Scott Baker1fa4e642015-03-30 19:14:52 -070018 cmi_hostname = StrippedCharField(max_length=254, null=True, blank=True)
Scott Baker30350392015-03-13 09:17:33 -070019
Scott Baker1facc3e2015-05-11 16:38:16 -070020 @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 Tullycf04fb62014-01-11 11:25:57 -050040class ServiceProvider(PlCoreBase):
41 class Meta:
42 app_label = "hpc"
Scott Bakere728c892014-01-12 11:41:09 -080043
Scott Baker26910182015-03-31 21:45:14 -070044 hpcService = models.ForeignKey(HpcService)
Scott Bakere728c892014-01-12 11:41:09 -080045 service_provider_id = models.IntegerField(null=True, blank=True)
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040046 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 Baker02c51772015-04-02 21:31:50 -070052 @classmethod
Scott Baker0e0d9682015-04-02 21:56:00 -070053 def filter_by_hpcService(cls, qs, hpcService):
Scott Baker02c51772015-04-02 21:31:50 -070054 # This should be overridden by descendant classes that want to perform
55 # filtering of visible objects by user.
Scott Baker0e0d9682015-04-02 21:56:00 -070056 return qs.filter(hpcService=hpcService)
Scott Baker02c51772015-04-02 21:31:50 -070057
Siobhan Tullycf04fb62014-01-11 11:25:57 -050058class ContentProvider(PlCoreBase):
59 class Meta:
60 app_label = "hpc"
Scott Bakere728c892014-01-12 11:41:09 -080061
Scott Baker7a090762014-05-07 09:04:07 -070062 # legacy vicci content providers already have names.
63 CP_TO_ACCOUNT = {"ON.LAB": "onlabcp",
64 "Syndicate": "syndicatecp"}
65
Scott Bakere728c892014-01-12 11:41:09 -080066 content_provider_id = models.IntegerField(null=True, blank=True)
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040067 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 Baker7a090762014-05-07 09:04:07 -070077 @property
78 def account(self):
79 return self.CP_TO_ACCOUNT.get(self.name, self.name)
80
Scott Baker02c51772015-04-02 21:31:50 -070081 @classmethod
Scott Baker0e0d9682015-04-02 21:56:00 -070082 def filter_by_hpcService(cls, qs, hpcService):
Scott Baker02c51772015-04-02 21:31:50 -070083 # This should be overridden by descendant classes that want to perform
84 # filtering of visible objects by user.
Scott Baker0e0d9682015-04-02 21:56:00 -070085 return qs.filter(serviceProvider__hpcService=hpcService)
Scott Baker02c51772015-04-02 21:31:50 -070086
87
Siobhan Tullycf04fb62014-01-11 11:25:57 -050088class OriginServer(PlCoreBase):
89 class Meta:
90 app_label = "hpc"
Scott Bakere728c892014-01-12 11:41:09 -080091
92 origin_server_id = models.IntegerField(null=True, blank=True)
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040093 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 Baker02c51772015-04-02 21:31:50 -0700105 @classmethod
Scott Baker0e0d9682015-04-02 21:56:00 -0700106 def filter_by_hpcService(cls, qs, hpcService):
Scott Baker02c51772015-04-02 21:31:50 -0700107 # This should be overridden by descendant classes that want to perform
108 # filtering of visible objects by user.
Scott Baker0e0d9682015-04-02 21:56:00 -0700109 return qs.filter(contentProvider__serviceProvider__hpcService=hpcService)
Scott Baker02c51772015-04-02 21:31:50 -0700110
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500111class CDNPrefix(PlCoreBase):
112 class Meta:
113 app_label = "hpc"
Scott Bakere728c892014-01-12 11:41:09 -0800114
115 cdn_prefix_id = models.IntegerField(null=True, blank=True)
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400116 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 Baker9d4b1c32014-08-27 11:20:33 -0700120 defaultOriginServer = models.ForeignKey(OriginServer, blank=True, null=True)
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400121 enabled = models.BooleanField(default=True)
122
123 def __unicode__(self): return u'%s' % (self.prefix)
124
Scott Baker02c51772015-04-02 21:31:50 -0700125 @classmethod
Scott Baker0e0d9682015-04-02 21:56:00 -0700126 def filter_by_hpcService(cls, qs, hpcService):
Scott Baker02c51772015-04-02 21:31:50 -0700127 # This should be overridden by descendant classes that want to perform
128 # filtering of visible objects by user.
Scott Baker0e0d9682015-04-02 21:56:00 -0700129 return qs.filter(contentProvider__serviceProvider__hpcService=hpcService)
Scott Baker02c51772015-04-02 21:31:50 -0700130
Scott Baker77bfa222015-03-03 00:06:53 -0800131class AccessMap(PlCoreBase):
132 class Meta:
133 app_label = "hpc"
134
Scott Baker53491082014-03-13 23:15:19 -0700135 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 Baker77bfa222015-03-03 00:06:53 -0800142class SiteMap(PlCoreBase):
143 class Meta:
144 app_label = "hpc"
145
Scott Baker53491082014-03-13 23:15:19 -0700146 """ 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 Baker77bfa222015-03-03 00:06:53 -0800149 cdnPrefix = models.ForeignKey(CDNPrefix, blank = True, null=True)
Scott Baker30350392015-03-13 09:17:33 -0700150 hpcService = models.ForeignKey(HpcService, blank = True, null=True)
Scott Baker53491082014-03-13 23:15:19 -0700151 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 Bakeree53c442015-03-09 16:22:56 -0700154 map_id = models.IntegerField(null=True, blank=True)
Scott Baker53491082014-03-13 23:15:19 -0700155
156 def __unicode__(self): return self.name
Scott Baker77bfa222015-03-03 00:06:53 -0800157
158 def save(self, *args, **kwds):
Scott Baker30350392015-03-13 09:17:33 -0700159 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 Baker77bfa222015-03-03 00:06:53 -0800165
166 super(SiteMap, self).save(*args, **kwds)
Scott Baker3b5fa5b2015-03-18 17:37:46 -0700167
Scott Baker02c51772015-04-02 21:31:50 -0700168 @classmethod
Scott Baker0e0d9682015-04-02 21:56:00 -0700169 def filter_by_hpcService(cls, qs, hpcService):
Scott Baker02c51772015-04-02 21:31:50 -0700170 # This should be overridden by descendant classes that want to perform
171 # filtering of visible objects by user.
Scott Baker0e0d9682015-04-02 21:56:00 -0700172 return qs.filter(Q(hpcService=hpcService) |
Scott Baker02c51772015-04-02 21:31:50 -0700173 Q(serviceProvider__hpcService=hpcService) |
174 Q(contentProvider__serviceProvider__hpcService=hpcService) |
175 Q(cdnPrefix__contentProvider__serviceProvider__hpcService=hpcService))
176
Scott Baker3b5fa5b2015-03-18 17:37:46 -0700177class 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 Baker02c51772015-04-02 21:31:50 -0700192 @classmethod
Scott Baker0e0d9682015-04-02 21:56:00 -0700193 def filter_by_hpcService(cls, qs, hpcService):
Scott Baker02c51772015-04-02 21:31:50 -0700194 # This should be overridden by descendant classes that want to perform
195 # filtering of visible objects by user.
Scott Baker0e0d9682015-04-02 21:56:00 -0700196 return qs.filter(hpcService=hpcService)
Scott Baker02c51772015-04-02 21:31:50 -0700197
198