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