blob: c4449fec81b9ad584aac06cfb48b65d9064a106b [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 Mackb0d97422013-06-10 09:57:45 -040011from core.models import User
Siobhan Tullyde5450d2013-06-21 11:35:33 -040012from core.models import Tag
Scott Baker7a61dc42014-09-02 17:08:20 -070013from core.models import Flavor
Siobhan Tullyde5450d2013-06-21 11:35:33 -040014from django.contrib.contenttypes import generic
Sapan Bhatia475c5972014-11-05 10:32:41 -050015from planetstack.config import Config
Sapan Bhatia78de56d2014-12-16 01:08:51 -050016from monitor import driver as monitor
Sapan Bhatia475c5972014-11-05 10:32:41 -050017
18config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040019
Tony Mack336e0f92014-11-30 15:53:08 -050020def get_default_flavor(controller = None):
Scott Baker7a61dc42014-09-02 17:08:20 -070021 # Find a default flavor that can be used for a sliver. This is particularly
22 # useful in evolution. It's also intended this helper function can be used
23 # for admin.py when users
24
Tony Mack336e0f92014-11-30 15:53:08 -050025 if controller:
26 flavors = controller.flavors.all()
Scott Baker7a61dc42014-09-02 17:08:20 -070027 else:
28 flavors = Flavor.objects.all()
29
30 if not flavors:
Scott Baker7a61dc42014-09-02 17:08:20 -070031 return None
32
33 for flavor in flavors:
34 if flavor.default:
Scott Baker7a61dc42014-09-02 17:08:20 -070035 return flavor
36
Scott Baker7a61dc42014-09-02 17:08:20 -070037 return flavors[0]
38
Sapan Bhatia475c5972014-11-05 10:32:41 -050039class SliverDeletionManager(PlCoreBaseDeletionManager):
40 def get_queryset(self):
41 parent=super(SliverDeletionManager, self)
42 try:
43 backend_type = config.observer_backend_type
44 except AttributeError:
45 backend_type = None
46
47 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
48 if (backend_type):
Tony Mackf3bbe472014-11-30 15:33:35 -050049 return parent_queryset.filter(Q(node__controller__backend_type=backend_type))
Sapan Bhatia475c5972014-11-05 10:32:41 -050050 else:
51 return parent_queryset
52
53 # deprecated in django 1.7 in favor of get_queryset().
54 def get_query_set(self):
55 return self.get_queryset()
56
57
58class SliverManager(PlCoreBaseManager):
59 def get_queryset(self):
60 parent=super(SliverManager, self)
61
62 try:
63 backend_type = config.observer_backend_type
64 except AttributeError:
65 backend_type = None
66
67 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
68
69 if backend_type:
Tony Mackf3bbe472014-11-30 15:33:35 -050070 return parent_queryset.filter(Q(node__controller__backend_type=backend_type))
Sapan Bhatia475c5972014-11-05 10:32:41 -050071 else:
72 return parent_queryset
73
74 # deprecated in django 1.7 in favor of get_queryset().
75 def get_query_set(self):
76 return self.get_queryset()
77
Siobhan Tully4bc09f22013-04-10 21:15:21 -040078# Create your models here.
79class Sliver(PlCoreBase):
Sapan Bhatia475c5972014-11-05 10:32:41 -050080 objects = SliverManager()
81 deleted_objects = SliverDeletionManager()
Scott Baker7a61dc42014-09-02 17:08:20 -070082 instance_id = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance id")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040083 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack38119372013-04-30 18:17:38 -040084 instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name")
Tony Mack1eeeae92013-04-16 23:04:44 -040085 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040086 image = models.ForeignKey(Image, related_name='slivers')
Tony Mackb0d97422013-06-10 09:57:45 -040087 #key = models.ForeignKey(Key, related_name='slivers')
88 creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040089 slice = models.ForeignKey(Slice, related_name='slivers')
90 node = models.ForeignKey(Node, related_name='slivers')
Tony Mack336e0f92014-11-30 15:53:08 -050091 controllerNetwork = models.ForeignKey(Deployment, verbose_name='controller', related_name='sliver_controllerNetwork')
smbakerddc21c72013-05-05 13:58:52 -070092 numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0)
Scott Baker7a61dc42014-09-02 17:08:20 -070093 flavor = models.ForeignKey(Flavor, help_text="Flavor of this instance", default=get_default_flavor)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040094 tags = generic.GenericRelation(Tag)
Scott Baker9c7a1e12014-05-30 14:42:42 -070095 userData = models.TextField(blank=True, null=True, help_text="user_data passed to instance during creation")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040096
Scott Baker3015c762013-08-23 10:28:07 -070097 def __unicode__(self):
98 if self.instance_name:
99 return u'%s' % (self.instance_name)
100 elif self.id:
101 return u'uninstantiated-%s' % str(self.id)
102 elif self.slice:
103 return u'unsaved-sliver on %s' % self.slice.name
104 else:
105 return u'unsaved-sliver'
106
Tony Mackcdec0902013-04-15 00:38:49 -0400107 def save(self, *args, **kwds):
Tony Mack80f1a4b2014-09-24 10:08:51 -0400108 self.name = self.slice.slicename
Tony Mack2bd5b412013-06-11 21:05:06 -0400109 if not self.creator and hasattr(self, 'caller'):
110 self.creator = self.caller
Tony Mack336e0f92014-11-30 15:53:08 -0500111 self.controllerNetwork = self.node.controller
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