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 |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 6 | from core.models.plcorebase import StrippedCharField |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 7 | from core.models import Image |
Scott Baker | a115c1e | 2015-05-07 14:58:46 -0700 | [diff] [blame] | 8 | from core.models import Slice, SlicePrivilege |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 9 | from core.models import Node |
| 10 | from core.models import Site |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 11 | from core.models import Deployment |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 12 | from core.models import Controller |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 13 | from core.models import User |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 14 | from core.models import Tag |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 15 | from core.models import Flavor |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 16 | from django.contrib.contenttypes import generic |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 17 | from xos.config import Config |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 18 | from monitor import driver as monitor |
Scott Baker | f41fe2c | 2015-07-09 19:06:08 -0700 | [diff] [blame] | 19 | from django.core.exceptions import PermissionDenied, ValidationError |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 20 | |
| 21 | config = Config() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 22 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 23 | def get_default_flavor(controller = None): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 24 | # Find a default flavor that can be used for a instance. This is particularly |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 25 | # useful in evolution. It's also intended this helper function can be used |
| 26 | # for admin.py when users |
| 27 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 28 | if controller: |
| 29 | flavors = controller.flavors.all() |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 30 | else: |
| 31 | flavors = Flavor.objects.all() |
| 32 | |
| 33 | if not flavors: |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 34 | return None |
| 35 | |
| 36 | for flavor in flavors: |
| 37 | if flavor.default: |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 38 | return flavor |
| 39 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 40 | return flavors[0] |
| 41 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 42 | class InstanceDeletionManager(PlCoreBaseDeletionManager): |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 43 | def get_queryset(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 44 | parent=super(InstanceDeletionManager, self) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 45 | try: |
| 46 | backend_type = config.observer_backend_type |
| 47 | except AttributeError: |
| 48 | backend_type = None |
| 49 | |
| 50 | parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set() |
| 51 | if (backend_type): |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 52 | return parent_queryset.filter(Q(node__controller__backend_type=backend_type)) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 53 | else: |
| 54 | return parent_queryset |
| 55 | |
| 56 | # deprecated in django 1.7 in favor of get_queryset(). |
| 57 | def get_query_set(self): |
| 58 | return self.get_queryset() |
| 59 | |
| 60 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 61 | class InstanceManager(PlCoreBaseManager): |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 62 | def get_queryset(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 63 | parent=super(InstanceManager, self) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 64 | |
| 65 | try: |
| 66 | backend_type = config.observer_backend_type |
| 67 | except AttributeError: |
| 68 | backend_type = None |
| 69 | |
| 70 | parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set() |
| 71 | |
| 72 | if backend_type: |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 73 | return parent_queryset.filter(Q(node__controller__backend_type=backend_type)) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 74 | else: |
| 75 | return parent_queryset |
| 76 | |
| 77 | # deprecated in django 1.7 in favor of get_queryset(). |
| 78 | def get_query_set(self): |
| 79 | return self.get_queryset() |
| 80 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 81 | # Create your models here. |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 82 | class Instance(PlCoreBase): |
| 83 | objects = InstanceManager() |
| 84 | deleted_objects = InstanceDeletionManager() |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 85 | instance_id = StrippedCharField(null=True, blank=True, max_length=200, help_text="Nova instance id") |
| 86 | instance_uuid = StrippedCharField(null=True, blank=True, max_length=200, help_text="Nova instance uuid") |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 87 | name = StrippedCharField(max_length=200, help_text="Instance name") |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 88 | instance_name = StrippedCharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name") |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 89 | ip = models.GenericIPAddressField(help_text="Instance ip address", blank=True, null=True) |
| 90 | image = models.ForeignKey(Image, related_name='instances') |
| 91 | #key = models.ForeignKey(Key, related_name='instances') |
| 92 | creator = models.ForeignKey(User, related_name='instances', blank=True, null=True) |
| 93 | slice = models.ForeignKey(Slice, related_name='instances') |
| 94 | deployment = models.ForeignKey(Deployment, verbose_name='deployment', related_name='instance_deployment') |
| 95 | node = models.ForeignKey(Node, related_name='instances') |
| 96 | numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for instance", default=0) |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 97 | 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] | 98 | tags = generic.GenericRelation(Tag) |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 99 | 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] | 100 | |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 101 | def __unicode__(self): |
Scott Baker | c145221 | 2015-08-04 23:50:51 -0700 | [diff] [blame] | 102 | if self.name and Slice.objects.filter(id=self.slice_id) and (self.name != self.slice.name): |
| 103 | # NOTE: The weird check on self.slice_id was due to a problem when |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 104 | # deleting the slice before the instance. |
Scott Baker | 02f913f | 2015-08-04 16:05:18 -0700 | [diff] [blame] | 105 | return u'%s' % self.name |
| 106 | elif self.instance_name: |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 107 | return u'%s' % (self.instance_name) |
| 108 | elif self.id: |
| 109 | return u'uninstantiated-%s' % str(self.id) |
| 110 | elif self.slice: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 111 | return u'unsaved-instance on %s' % self.slice.name |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 112 | else: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 113 | return u'unsaved-instance' |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 114 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 115 | def save(self, *args, **kwds): |
Scott Baker | 02f913f | 2015-08-04 16:05:18 -0700 | [diff] [blame] | 116 | if not self.name: |
| 117 | self.name = self.slice.name |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 118 | if not self.creator and hasattr(self, 'caller'): |
| 119 | self.creator = self.caller |
Scott Baker | 2d599df | 2015-01-29 17:55:40 -0800 | [diff] [blame] | 120 | if not self.creator: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 121 | raise ValidationError('instance has no creator') |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 122 | |
Scott Baker | f41fe2c | 2015-07-09 19:06:08 -0700 | [diff] [blame] | 123 | if (self.slice.creator != self.creator): |
| 124 | # Check to make sure there's a slice_privilege for the user. If there |
| 125 | # isn't, then keystone will throw an exception inside the observer. |
| 126 | slice_privs = SlicePrivilege.objects.filter(slice=self.slice, user=self.creator) |
| 127 | if not slice_privs: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 128 | raise ValidationError('instance creator has no privileges on slice') |
Scott Baker | f41fe2c | 2015-07-09 19:06:08 -0700 | [diff] [blame] | 129 | |
Scott Baker | ec06eba | 2014-06-20 18:03:04 -0700 | [diff] [blame] | 130 | # 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] | 131 | # if not self.controllerNetwork.test_acl(slice=self.slice): |
| 132 | # 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] | 133 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 134 | super(Instance, self).save(*args, **kwds) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 135 | |
| 136 | def can_update(self, user): |
Tony Mack | 3428e6e | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 137 | return user.can_update_slice(self.slice) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 138 | |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 139 | def all_ips(self): |
| 140 | ips={} |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 141 | for ns in self.networkinstances.all(): |
Scott Baker | 01907e9 | 2015-08-18 23:02:11 -0700 | [diff] [blame] | 142 | if ns.ip: |
| 143 | ips[ns.network.name] = ns.ip |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 144 | return ips |
| 145 | |
| 146 | def all_ips_string(self): |
| 147 | result = [] |
| 148 | ips = self.all_ips() |
| 149 | for key in sorted(ips.keys()): |
| 150 | #result.append("%s = %s" % (key, ips[key])) |
| 151 | result.append(ips[key]) |
| 152 | return ", ".join(result) |
| 153 | all_ips_string.short_description = "addresses" |
| 154 | |
Scott Baker | 7d7d8cd | 2015-03-16 17:13:58 -0700 | [diff] [blame] | 155 | def get_public_ip(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 156 | for ns in self.networkinstances.all(): |
Scott Baker | f8ec83d | 2015-03-16 16:44:41 -0700 | [diff] [blame] | 157 | if (ns.ip) and (ns.network.template.visibility=="public") and (ns.network.template.translation=="none"): |
| 158 | return ns.ip |
| 159 | return None |
| 160 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 161 | @staticmethod |
| 162 | def select_by_user(user): |
| 163 | if user.is_admin: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 164 | qs = Instance.objects.all() |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 165 | else: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 166 | slices = Slice.select_by_user(user) |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 167 | qs = Instance.objects.filter(slice__in=slices) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 168 | return qs |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 169 | |
| 170 | def get_cpu_stats(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 171 | filter = 'instance_id=%s'%self.instance_id |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 172 | return monitor.get_meter('cpu',filter,None) |
| 173 | |
| 174 | def get_bw_stats(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 175 | filter = 'instance_id=%s'%self.instance_id |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 176 | return monitor.get_meter('network.outgoing.bytes',filter,None) |
| 177 | |
| 178 | def get_node_stats(self): |
| 179 | # Note sure what should go back here |
| 180 | return 1 |
Scott Baker | 7ccc6ad | 2015-01-25 22:16:13 -0800 | [diff] [blame] | 181 | |
| 182 | def get_ssh_command(self): |
| 183 | if (not self.instance_id) or (not self.node) or (not self.instance_name): |
| 184 | return None |
| 185 | else: |
| 186 | return 'ssh -o "ProxyCommand ssh -q %s@%s" ubuntu@%s' % (self.instance_id, self.node.name, self.instance_name) |
Scott Baker | a115c1e | 2015-05-07 14:58:46 -0700 | [diff] [blame] | 187 | |
| 188 | def get_public_keys(self): |
| 189 | slice_memberships = SlicePrivilege.objects.filter(slice=self.slice) |
| 190 | pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key]) |
| 191 | |
| 192 | if self.creator.public_key: |
| 193 | pubkeys.add(self.creator.public_key) |
| 194 | |
| 195 | if self.slice.creator.public_key: |
| 196 | pubkeys.add(self.slice.creator.public_key) |
| 197 | |
| 198 | if self.slice.service and self.slice.service.public_key: |
| 199 | pubkeys.add(self.slice.service.public_key) |
| 200 | |
| 201 | return pubkeys |
Sapan Bhatia | 52ba542 | 2015-05-13 15:48:02 +0200 | [diff] [blame] | 202 | |
| 203 | def controller_setter(instance, **kwargs): |
| 204 | try: |
| 205 | instance.controller = instance.node.site_deployment.controller |
| 206 | except: |
| 207 | instance.controller = None |
| 208 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame^] | 209 | models.signals.post_init.connect(controller_setter, Instance) |