blob: d2f794f4c9cbf34adedd937ca052e76effc8008c [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")
Sapan Bhatiacb5c24a2015-01-20 20:59:24 +000084 instance_uuid = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance uuid")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040085 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack38119372013-04-30 18:17:38 -040086 instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name")
Tony Mack1eeeae92013-04-16 23:04:44 -040087 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040088 image = models.ForeignKey(Image, related_name='slivers')
Tony Mackb0d97422013-06-10 09:57:45 -040089 #key = models.ForeignKey(Key, related_name='slivers')
90 creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040091 slice = models.ForeignKey(Slice, related_name='slivers')
Tony Mackbf6aa302014-12-26 13:38:02 -050092 deployment = models.ForeignKey(Deployment, verbose_name='deployment', related_name='sliver_deployment')
Tony Mack771f4b92013-04-12 01:49:57 -040093 node = models.ForeignKey(Node, related_name='slivers')
smbakerddc21c72013-05-05 13:58:52 -070094 numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0)
Scott Baker7a61dc42014-09-02 17:08:20 -070095 flavor = models.ForeignKey(Flavor, help_text="Flavor of this instance", default=get_default_flavor)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040096 tags = generic.GenericRelation(Tag)
Scott Baker9c7a1e12014-05-30 14:42:42 -070097 userData = models.TextField(blank=True, null=True, help_text="user_data passed to instance during creation")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040098
Scott Baker3015c762013-08-23 10:28:07 -070099 def __unicode__(self):
100 if self.instance_name:
101 return u'%s' % (self.instance_name)
102 elif self.id:
103 return u'uninstantiated-%s' % str(self.id)
104 elif self.slice:
105 return u'unsaved-sliver on %s' % self.slice.name
106 else:
107 return u'unsaved-sliver'
108
Tony Mackcdec0902013-04-15 00:38:49 -0400109 def save(self, *args, **kwds):
Tony Mack71dda3b2015-01-18 15:42:10 -0500110 self.name = self.slice.name
Tony Mack2bd5b412013-06-11 21:05:06 -0400111 if not self.creator and hasattr(self, 'caller'):
112 self.creator = self.caller
Scott Baker5380c522014-06-06 14:49:43 -0700113
Scott Bakerec06eba2014-06-20 18:03:04 -0700114# XXX smbaker - disabled for now, was causing fault in tenant view create slice
Tony Mack336e0f92014-11-30 15:53:08 -0500115# if not self.controllerNetwork.test_acl(slice=self.slice):
116# 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 -0700117
Tony Mackcdec0902013-04-15 00:38:49 -0400118 super(Sliver, self).save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -0500119
120 def can_update(self, user):
121 return self.slice.can_update(user)
122
Scott Baker434ca7e2014-08-15 12:29:20 -0700123 def all_ips(self):
124 ips={}
125 for ns in self.networksliver_set.all():
126 ips[ns.network.name] = ns.ip
127 return ips
128
129 def all_ips_string(self):
130 result = []
131 ips = self.all_ips()
132 for key in sorted(ips.keys()):
133 #result.append("%s = %s" % (key, ips[key]))
134 result.append(ips[key])
135 return ", ".join(result)
136 all_ips_string.short_description = "addresses"
137
Tony Mack5b061472014-02-04 07:57:10 -0500138 @staticmethod
139 def select_by_user(user):
140 if user.is_admin:
141 qs = Sliver.objects.all()
142 else:
Tony Macke4be32f2014-03-11 20:45:25 -0400143 slices = Slice.select_by_user(user)
144 qs = Sliver.objects.filter(slice__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -0500145 return qs
Sapan Bhatia78de56d2014-12-16 01:08:51 -0500146
147 def get_cpu_stats(self):
148 filter = 'instance_id=%s'%self.sliver_id
149 return monitor.get_meter('cpu',filter,None)
150
151 def get_bw_stats(self):
152 filter = 'instance_id=%s'%self.sliver_id
153 return monitor.get_meter('network.outgoing.bytes',filter,None)
154
155 def get_node_stats(self):
156 # Note sure what should go back here
157 return 1