blob: 0173887a73a43dcc1ba5abf74013023332beffd0 [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
Scott Baker76a840e2015-02-11 21:38:09 -080016from xos.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 Baker760b54b2015-01-29 17:55:40 -0800113 if not self.creator:
114 raise ValidationError('sliver has no creator')
Scott Baker5380c522014-06-06 14:49:43 -0700115
Scott Bakerec06eba2014-06-20 18:03:04 -0700116# XXX smbaker - disabled for now, was causing fault in tenant view create slice
Tony Mack336e0f92014-11-30 15:53:08 -0500117# if not self.controllerNetwork.test_acl(slice=self.slice):
118# 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 -0700119
Tony Mackcdec0902013-04-15 00:38:49 -0400120 super(Sliver, self).save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -0500121
122 def can_update(self, user):
Tony Mack5ff90fc2015-02-08 21:38:41 -0500123 return user.can_update_slice(self.slice)
Tony Mack5b061472014-02-04 07:57:10 -0500124
Scott Baker434ca7e2014-08-15 12:29:20 -0700125 def all_ips(self):
126 ips={}
Scott Bakerc052e5f2015-01-30 07:37:19 -0800127 for ns in self.networkslivers.all():
Scott Baker434ca7e2014-08-15 12:29:20 -0700128 ips[ns.network.name] = ns.ip
129 return ips
130
131 def all_ips_string(self):
132 result = []
133 ips = self.all_ips()
134 for key in sorted(ips.keys()):
135 #result.append("%s = %s" % (key, ips[key]))
136 result.append(ips[key])
137 return ", ".join(result)
138 all_ips_string.short_description = "addresses"
139
Tony Mack5b061472014-02-04 07:57:10 -0500140 @staticmethod
141 def select_by_user(user):
142 if user.is_admin:
143 qs = Sliver.objects.all()
144 else:
Tony Macke4be32f2014-03-11 20:45:25 -0400145 slices = Slice.select_by_user(user)
146 qs = Sliver.objects.filter(slice__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -0500147 return qs
Sapan Bhatia78de56d2014-12-16 01:08:51 -0500148
149 def get_cpu_stats(self):
150 filter = 'instance_id=%s'%self.sliver_id
151 return monitor.get_meter('cpu',filter,None)
152
153 def get_bw_stats(self):
154 filter = 'instance_id=%s'%self.sliver_id
155 return monitor.get_meter('network.outgoing.bytes',filter,None)
156
157 def get_node_stats(self):
158 # Note sure what should go back here
159 return 1
Scott Baker970314b2015-01-25 22:16:13 -0800160
161 def get_ssh_command(self):
162 if (not self.instance_id) or (not self.node) or (not self.instance_name):
163 return None
164 else:
165 return 'ssh -o "ProxyCommand ssh -q %s@%s" ubuntu@%s' % (self.instance_id, self.node.name, self.instance_name)