Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 3 | from django.db.models import Q |
Tony Mack | 2b241cf | 2013-04-16 21:57:55 -0400 | [diff] [blame] | 4 | from django.core import exceptions |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 5 | from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 6 | from core.models import Image |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 7 | from core.models import Slice |
| 8 | from core.models import Node |
| 9 | from core.models import Site |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 10 | from core.models import Deployment |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 11 | from core.models import Controller |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 12 | from core.models import User |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 13 | from core.models import Tag |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 14 | from core.models import Flavor |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 15 | from django.contrib.contenttypes import generic |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 16 | from planetstack.config import Config |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 17 | from monitor import driver as monitor |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 18 | |
| 19 | config = Config() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 20 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 21 | def get_default_flavor(controller = None): |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 22 | # 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 Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 26 | if controller: |
| 27 | flavors = controller.flavors.all() |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 28 | else: |
| 29 | flavors = Flavor.objects.all() |
| 30 | |
| 31 | if not flavors: |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 32 | return None |
| 33 | |
| 34 | for flavor in flavors: |
| 35 | if flavor.default: |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 36 | return flavor |
| 37 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 38 | return flavors[0] |
| 39 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 40 | class 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 Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 50 | return parent_queryset.filter(Q(node__controller__backend_type=backend_type)) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 51 | 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 | |
| 59 | class 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 Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 71 | return parent_queryset.filter(Q(node__controller__backend_type=backend_type)) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 72 | 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 Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 79 | # Create your models here. |
| 80 | class Sliver(PlCoreBase): |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 81 | objects = SliverManager() |
| 82 | deleted_objects = SliverDeletionManager() |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 83 | instance_id = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance id") |
Sapan Bhatia | 68b9c1c | 2015-01-20 20:59:24 +0000 | [diff] [blame] | 84 | instance_uuid = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance uuid") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 85 | name = models.CharField(max_length=200, help_text="Sliver name") |
Tony Mack | 3811937 | 2013-04-30 18:17:38 -0400 | [diff] [blame] | 86 | instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name") |
Tony Mack | 1eeeae9 | 2013-04-16 23:04:44 -0400 | [diff] [blame] | 87 | ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 88 | image = models.ForeignKey(Image, related_name='slivers') |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 89 | #key = models.ForeignKey(Key, related_name='slivers') |
| 90 | creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 91 | slice = models.ForeignKey(Slice, related_name='slivers') |
Tony Mack | b2dba4b | 2014-12-26 13:38:02 -0500 | [diff] [blame] | 92 | deployment = models.ForeignKey(Deployment, verbose_name='deployment', related_name='sliver_deployment') |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 93 | node = models.ForeignKey(Node, related_name='slivers') |
smbaker | ddc21c7 | 2013-05-05 13:58:52 -0700 | [diff] [blame] | 94 | numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0) |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 95 | flavor = models.ForeignKey(Flavor, help_text="Flavor of this instance", default=get_default_flavor) |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 96 | tags = generic.GenericRelation(Tag) |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 97 | userData = models.TextField(blank=True, null=True, help_text="user_data passed to instance during creation") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 98 | |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 99 | 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 Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 109 | def save(self, *args, **kwds): |
Tony Mack | 99f2351 | 2015-01-18 15:42:10 -0500 | [diff] [blame] | 110 | self.name = self.slice.name |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 111 | if not self.creator and hasattr(self, 'caller'): |
| 112 | self.creator = self.caller |
Scott Baker | 2d599df | 2015-01-29 17:55:40 -0800 | [diff] [blame] | 113 | if not self.creator: |
| 114 | raise ValidationError('sliver has no creator') |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 115 | |
Scott Baker | ec06eba | 2014-06-20 18:03:04 -0700 | [diff] [blame] | 116 | # XXX smbaker - disabled for now, was causing fault in tenant view create slice |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 117 | # 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 Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 119 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 120 | super(Sliver, self).save(*args, **kwds) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 121 | |
| 122 | def can_update(self, user): |
Tony Mack | 3428e6e | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 123 | return user.can_update_slice(self.slice) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 124 | |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 125 | def all_ips(self): |
| 126 | ips={} |
Scott Baker | 1c83dcf | 2015-01-30 07:37:19 -0800 | [diff] [blame] | 127 | for ns in self.networkslivers.all(): |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 128 | 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 140 | @staticmethod |
| 141 | def select_by_user(user): |
| 142 | if user.is_admin: |
| 143 | qs = Sliver.objects.all() |
| 144 | else: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 145 | slices = Slice.select_by_user(user) |
| 146 | qs = Sliver.objects.filter(slice__in=slices) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 147 | return qs |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 148 | |
| 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 Baker | 7ccc6ad | 2015-01-25 22:16:13 -0800 | [diff] [blame] | 160 | |
| 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) |