blob: 4d89d9fac5479b20e4f3ac77621668dce1dc5e02 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Sapan Bhatia475c5972014-11-05 10:32:41 -05003from django.db.models import Q
Tony Mack2b241cf2013-04-16 21:57:55 -04004from django.core import exceptions
Sapan Bhatia475c5972014-11-05 10:32:41 -05005from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager
Siobhan Tully30fd4292013-05-10 08:59:56 -04006from core.models import Image
Siobhan Tully30fd4292013-05-10 08:59:56 -04007from core.models import Slice
8from core.models import Node
9from core.models import Site
Siobhan Tullybf1153a2013-05-27 20:53:48 -040010from core.models import Deployment
Tony Mack93d1b032014-12-08 16:43:02 -050011from core.models import Controller
Tony Mackb0d97422013-06-10 09:57:45 -040012from core.models import User
Siobhan Tullyde5450d2013-06-21 11:35:33 -040013from core.models import Tag
Scott Baker7a61dc42014-09-02 17:08:20 -070014from core.models import Flavor
Siobhan Tullyde5450d2013-06-21 11:35:33 -040015from django.contrib.contenttypes import generic
Sapan Bhatia475c5972014-11-05 10:32:41 -050016from planetstack.config import Config
Sapan Bhatia78de56d2014-12-16 01:08:51 -050017from monitor import driver as monitor
Sapan Bhatia475c5972014-11-05 10:32:41 -050018
19config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040020
Tony Mack336e0f92014-11-30 15:53:08 -050021def get_default_flavor(controller = None):
Scott Baker7a61dc42014-09-02 17:08:20 -070022 # Find a default flavor that can be used for a sliver. This is particularly
23 # useful in evolution. It's also intended this helper function can be used
24 # for admin.py when users
25
Tony Mack336e0f92014-11-30 15:53:08 -050026 if controller:
27 flavors = controller.flavors.all()
Scott Baker7a61dc42014-09-02 17:08:20 -070028 else:
29 flavors = Flavor.objects.all()
30
31 if not flavors:
Scott Baker7a61dc42014-09-02 17:08:20 -070032 return None
33
34 for flavor in flavors:
35 if flavor.default:
Scott Baker7a61dc42014-09-02 17:08:20 -070036 return flavor
37
Scott Baker7a61dc42014-09-02 17:08:20 -070038 return flavors[0]
39
Sapan Bhatia475c5972014-11-05 10:32:41 -050040class SliverDeletionManager(PlCoreBaseDeletionManager):
41 def get_queryset(self):
42 parent=super(SliverDeletionManager, self)
43 try:
44 backend_type = config.observer_backend_type
45 except AttributeError:
46 backend_type = None
47
48 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
49 if (backend_type):
Tony Mackf3bbe472014-11-30 15:33:35 -050050 return parent_queryset.filter(Q(node__controller__backend_type=backend_type))
Sapan Bhatia475c5972014-11-05 10:32:41 -050051 else:
52 return parent_queryset
53
54 # deprecated in django 1.7 in favor of get_queryset().
55 def get_query_set(self):
56 return self.get_queryset()
57
58
59class SliverManager(PlCoreBaseManager):
60 def get_queryset(self):
61 parent=super(SliverManager, self)
62
63 try:
64 backend_type = config.observer_backend_type
65 except AttributeError:
66 backend_type = None
67
68 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
69
70 if backend_type:
Tony Mackf3bbe472014-11-30 15:33:35 -050071 return parent_queryset.filter(Q(node__controller__backend_type=backend_type))
Sapan Bhatia475c5972014-11-05 10:32:41 -050072 else:
73 return parent_queryset
74
75 # deprecated in django 1.7 in favor of get_queryset().
76 def get_query_set(self):
77 return self.get_queryset()
78
Siobhan Tully4bc09f22013-04-10 21:15:21 -040079# Create your models here.
80class Sliver(PlCoreBase):
Sapan Bhatia475c5972014-11-05 10:32:41 -050081 objects = SliverManager()
82 deleted_objects = SliverDeletionManager()
Scott Baker7a61dc42014-09-02 17:08:20 -070083 instance_id = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance id")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040084 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack38119372013-04-30 18:17:38 -040085 instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name")
Tony Mack1eeeae92013-04-16 23:04:44 -040086 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040087 image = models.ForeignKey(Image, related_name='slivers')
Tony Mackb0d97422013-06-10 09:57:45 -040088 #key = models.ForeignKey(Key, related_name='slivers')
89 creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040090 slice = models.ForeignKey(Slice, related_name='slivers')
Tony Mackbf6aa302014-12-26 13:38:02 -050091 deployment = models.ForeignKey(Deployment, verbose_name='deployment', related_name='sliver_deployment')
Tony Mack771f4b92013-04-12 01:49:57 -040092 node = models.ForeignKey(Node, related_name='slivers')
smbakerddc21c72013-05-05 13:58:52 -070093 numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0)
Scott Baker7a61dc42014-09-02 17:08:20 -070094 flavor = models.ForeignKey(Flavor, help_text="Flavor of this instance", default=get_default_flavor)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040095 tags = generic.GenericRelation(Tag)
Scott Baker9c7a1e12014-05-30 14:42:42 -070096 userData = models.TextField(blank=True, null=True, help_text="user_data passed to instance during creation")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040097
Scott Baker3015c762013-08-23 10:28:07 -070098 def __unicode__(self):
99 if self.instance_name:
100 return u'%s' % (self.instance_name)
101 elif self.id:
102 return u'uninstantiated-%s' % str(self.id)
103 elif self.slice:
104 return u'unsaved-sliver on %s' % self.slice.name
105 else:
106 return u'unsaved-sliver'
107
Tony Mackcdec0902013-04-15 00:38:49 -0400108 def save(self, *args, **kwds):
Tony Mack71dda3b2015-01-18 15:42:10 -0500109 self.name = self.slice.name
Tony Mack2bd5b412013-06-11 21:05:06 -0400110 if not self.creator and hasattr(self, 'caller'):
111 self.creator = self.caller
Scott Baker5380c522014-06-06 14:49:43 -0700112
Scott Bakerec06eba2014-06-20 18:03:04 -0700113# XXX smbaker - disabled for now, was causing fault in tenant view create slice
Tony Mack336e0f92014-11-30 15:53:08 -0500114# if not self.controllerNetwork.test_acl(slice=self.slice):
115# raise exceptions.ValidationError("Deployment %s's ACL does not allow any of this slice %s's users" % (self.controllerNetwork.name, self.slice.name))
Scott Baker5380c522014-06-06 14:49:43 -0700116
Tony Mackcdec0902013-04-15 00:38:49 -0400117 super(Sliver, self).save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -0500118
119 def can_update(self, user):
120 return self.slice.can_update(user)
121
Scott Baker434ca7e2014-08-15 12:29:20 -0700122 def all_ips(self):
123 ips={}
124 for ns in self.networksliver_set.all():
125 ips[ns.network.name] = ns.ip
126 return ips
127
128 def all_ips_string(self):
129 result = []
130 ips = self.all_ips()
131 for key in sorted(ips.keys()):
132 #result.append("%s = %s" % (key, ips[key]))
133 result.append(ips[key])
134 return ", ".join(result)
135 all_ips_string.short_description = "addresses"
136
Tony Mack5b061472014-02-04 07:57:10 -0500137 @staticmethod
138 def select_by_user(user):
139 if user.is_admin:
140 qs = Sliver.objects.all()
141 else:
Tony Macke4be32f2014-03-11 20:45:25 -0400142 slices = Slice.select_by_user(user)
143 qs = Sliver.objects.filter(slice__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -0500144 return qs
Sapan Bhatia78de56d2014-12-16 01:08:51 -0500145
146 def get_cpu_stats(self):
147 filter = 'instance_id=%s'%self.sliver_id
148 return monitor.get_meter('cpu',filter,None)
149
150 def get_bw_stats(self):
151 filter = 'instance_id=%s'%self.sliver_id
152 return monitor.get_meter('network.outgoing.bytes',filter,None)
153
154 def get_node_stats(self):
155 # Note sure what should go back here
156 return 1