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 |
Scott Baker | f41fe2c | 2015-07-09 19:06:08 -0700 | [diff] [blame] | 18 | from django.core.exceptions import PermissionDenied, ValidationError |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 19 | |
| 20 | config = Config() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 21 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 22 | def get_default_flavor(controller = None): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 23 | # 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] | 24 | # useful in evolution. It's also intended this helper function can be used |
| 25 | # for admin.py when users |
| 26 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 27 | if controller: |
| 28 | flavors = controller.flavors.all() |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 29 | else: |
| 30 | flavors = Flavor.objects.all() |
| 31 | |
| 32 | if not flavors: |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 33 | return None |
| 34 | |
| 35 | for flavor in flavors: |
| 36 | if flavor.default: |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 37 | return flavor |
| 38 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 39 | return flavors[0] |
| 40 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 41 | class InstanceDeletionManager(PlCoreBaseDeletionManager): |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 42 | def get_queryset(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 43 | parent=super(InstanceDeletionManager, self) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 44 | try: |
| 45 | backend_type = config.observer_backend_type |
| 46 | except AttributeError: |
| 47 | backend_type = None |
| 48 | |
| 49 | parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set() |
| 50 | if (backend_type): |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 51 | return parent_queryset.filter(Q(node__controller__backend_type=backend_type)) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 52 | else: |
| 53 | return parent_queryset |
| 54 | |
| 55 | # deprecated in django 1.7 in favor of get_queryset(). |
| 56 | def get_query_set(self): |
| 57 | return self.get_queryset() |
| 58 | |
| 59 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 60 | class InstanceManager(PlCoreBaseManager): |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 61 | def get_queryset(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 62 | parent=super(InstanceManager, self) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 63 | |
| 64 | try: |
| 65 | backend_type = config.observer_backend_type |
| 66 | except AttributeError: |
| 67 | backend_type = None |
| 68 | |
| 69 | parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set() |
| 70 | |
| 71 | if backend_type: |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 72 | return parent_queryset.filter(Q(node__controller__backend_type=backend_type)) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 73 | else: |
| 74 | return parent_queryset |
| 75 | |
| 76 | # deprecated in django 1.7 in favor of get_queryset(). |
| 77 | def get_query_set(self): |
| 78 | return self.get_queryset() |
| 79 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 80 | # Create your models here. |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 81 | class Instance(PlCoreBase): |
Scott Baker | 1f454e3 | 2015-11-12 17:25:53 -0800 | [diff] [blame] | 82 | ISOLATION_CHOICES = (('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM')) |
Scott Baker | dcf9e0d | 2015-11-09 16:17:11 -0800 | [diff] [blame] | 83 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 84 | objects = InstanceManager() |
| 85 | deleted_objects = InstanceDeletionManager() |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 86 | instance_id = StrippedCharField(null=True, blank=True, max_length=200, help_text="Nova instance id") |
| 87 | 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] | 88 | name = StrippedCharField(max_length=200, help_text="Instance name") |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 89 | 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] | 90 | ip = models.GenericIPAddressField(help_text="Instance ip address", blank=True, null=True) |
| 91 | image = models.ForeignKey(Image, related_name='instances') |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 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") |
Scott Baker | dcf9e0d | 2015-11-09 16:17:11 -0800 | [diff] [blame] | 100 | isolation = models.CharField(null=False, blank=False, max_length=30, choices=ISOLATION_CHOICES, default="vm") |
Scott Baker | e317a99 | 2015-11-10 17:07:23 -0800 | [diff] [blame] | 101 | volumes = models.TextField(null=True, blank=True, help_text="Comma-separated list of directories to expose to parent context") |
Scott Baker | 1f454e3 | 2015-11-12 17:25:53 -0800 | [diff] [blame] | 102 | parent = models.ForeignKey("Instance", null=True, blank=True, help_text="Parent Instance for containers nested inside of VMs") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 103 | |
Sapan Bhatia | 5b2ec27 | 2016-02-10 17:51:26 +0100 | [diff] [blame] | 104 | def get_controller (self): |
Sapan Bhatia | ddcac19 | 2016-02-10 18:11:30 +0100 | [diff] [blame] | 105 | return self.node.site_deployment.controller |
Sapan Bhatia | 5b2ec27 | 2016-02-10 17:51:26 +0100 | [diff] [blame] | 106 | |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 107 | def __unicode__(self): |
Scott Baker | c145221 | 2015-08-04 23:50:51 -0700 | [diff] [blame] | 108 | if self.name and Slice.objects.filter(id=self.slice_id) and (self.name != self.slice.name): |
| 109 | # 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] | 110 | # deleting the slice before the instance. |
Scott Baker | 02f913f | 2015-08-04 16:05:18 -0700 | [diff] [blame] | 111 | return u'%s' % self.name |
| 112 | elif self.instance_name: |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 113 | return u'%s' % (self.instance_name) |
| 114 | elif self.id: |
| 115 | return u'uninstantiated-%s' % str(self.id) |
| 116 | elif self.slice: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 117 | return u'unsaved-instance on %s' % self.slice.name |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 118 | else: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 119 | return u'unsaved-instance' |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 120 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 121 | def save(self, *args, **kwds): |
Scott Baker | 02f913f | 2015-08-04 16:05:18 -0700 | [diff] [blame] | 122 | if not self.name: |
| 123 | self.name = self.slice.name |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 124 | if not self.creator and hasattr(self, 'caller'): |
| 125 | self.creator = self.caller |
Scott Baker | 2d599df | 2015-01-29 17:55:40 -0800 | [diff] [blame] | 126 | if not self.creator: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 127 | raise ValidationError('instance has no creator') |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 128 | |
Scott Baker | 1f454e3 | 2015-11-12 17:25:53 -0800 | [diff] [blame] | 129 | if (self.isolation == "container") or (self.isolation == "container_vm"): |
Scott Baker | 73b39ee | 2015-11-11 09:24:03 -0800 | [diff] [blame] | 130 | if (self.image.kind != "container"): |
Pingping Lin | fc9256a | 2016-03-03 09:52:24 -0800 | [diff] [blame] | 131 | raise ValidationError("Container instance must use container image") |
Scott Baker | 73b39ee | 2015-11-11 09:24:03 -0800 | [diff] [blame] | 132 | elif (self.isolation == "vm"): |
| 133 | if (self.image.kind != "vm"): |
Pingping Lin | fc9256a | 2016-03-03 09:52:24 -0800 | [diff] [blame] | 134 | raise ValidationError("VM instance must use VM image") |
Scott Baker | 73b39ee | 2015-11-11 09:24:03 -0800 | [diff] [blame] | 135 | |
Scott Baker | 1f454e3 | 2015-11-12 17:25:53 -0800 | [diff] [blame] | 136 | if (self.isolation == "container_vm") and (not self.parent): |
| 137 | raise ValidationError("Container-vm instance must have a parent") |
| 138 | |
| 139 | if (self.parent) and (self.isolation != "container_vm"): |
| 140 | raise ValidationError("Parent field can only be set on Container-vm instances") |
| 141 | |
Scott Baker | f41fe2c | 2015-07-09 19:06:08 -0700 | [diff] [blame] | 142 | if (self.slice.creator != self.creator): |
| 143 | # Check to make sure there's a slice_privilege for the user. If there |
| 144 | # isn't, then keystone will throw an exception inside the observer. |
| 145 | slice_privs = SlicePrivilege.objects.filter(slice=self.slice, user=self.creator) |
| 146 | if not slice_privs: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 147 | raise ValidationError('instance creator has no privileges on slice') |
Scott Baker | f41fe2c | 2015-07-09 19:06:08 -0700 | [diff] [blame] | 148 | |
Scott Baker | ec06eba | 2014-06-20 18:03:04 -0700 | [diff] [blame] | 149 | # 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] | 150 | # if not self.controllerNetwork.test_acl(slice=self.slice): |
| 151 | # 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] | 152 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 153 | super(Instance, self).save(*args, **kwds) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 154 | |
| 155 | def can_update(self, user): |
Tony Mack | 3428e6e | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 156 | return user.can_update_slice(self.slice) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 157 | |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 158 | def all_ips(self): |
| 159 | ips={} |
Scott Baker | 9a2b7c0 | 2015-08-28 11:37:50 -0700 | [diff] [blame] | 160 | for ns in self.ports.all(): |
Scott Baker | 01907e9 | 2015-08-18 23:02:11 -0700 | [diff] [blame] | 161 | if ns.ip: |
| 162 | ips[ns.network.name] = ns.ip |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 163 | return ips |
| 164 | |
| 165 | def all_ips_string(self): |
| 166 | result = [] |
| 167 | ips = self.all_ips() |
| 168 | for key in sorted(ips.keys()): |
| 169 | #result.append("%s = %s" % (key, ips[key])) |
| 170 | result.append(ips[key]) |
| 171 | return ", ".join(result) |
| 172 | all_ips_string.short_description = "addresses" |
| 173 | |
Scott Baker | 7d7d8cd | 2015-03-16 17:13:58 -0700 | [diff] [blame] | 174 | def get_public_ip(self): |
Scott Baker | 9a2b7c0 | 2015-08-28 11:37:50 -0700 | [diff] [blame] | 175 | for ns in self.ports.all(): |
Scott Baker | f8ec83d | 2015-03-16 16:44:41 -0700 | [diff] [blame] | 176 | if (ns.ip) and (ns.network.template.visibility=="public") and (ns.network.template.translation=="none"): |
| 177 | return ns.ip |
| 178 | return None |
| 179 | |
Scott Baker | 1eb90b7 | 2015-10-20 21:21:05 -0700 | [diff] [blame] | 180 | # return an address on nat-net |
| 181 | def get_network_ip(self, pattern): |
Scott Baker | 5bdada3 | 2015-10-19 21:18:29 -0700 | [diff] [blame] | 182 | for ns in self.ports.all(): |
Scott Baker | 1eb90b7 | 2015-10-20 21:21:05 -0700 | [diff] [blame] | 183 | if pattern in ns.network.name.lower(): |
Scott Baker | 5bdada3 | 2015-10-19 21:18:29 -0700 | [diff] [blame] | 184 | return ns.ip |
| 185 | return None |
| 186 | |
Scott Baker | 1eb90b7 | 2015-10-20 21:21:05 -0700 | [diff] [blame] | 187 | # return an address that the synchronizer can use to SSH to the instance |
| 188 | def get_ssh_ip(self): |
Scott Baker | f5478bf | 2016-02-10 17:24:22 -0800 | [diff] [blame] | 189 | management=self.get_network_ip("management") |
| 190 | if management: |
| 191 | return management |
Scott Baker | 1eb90b7 | 2015-10-20 21:21:05 -0700 | [diff] [blame] | 192 | return self.get_network_ip("nat") |
| 193 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 194 | @staticmethod |
| 195 | def select_by_user(user): |
| 196 | if user.is_admin: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 197 | qs = Instance.objects.all() |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 198 | else: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 199 | slices = Slice.select_by_user(user) |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 200 | qs = Instance.objects.filter(slice__in=slices) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 201 | return qs |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 202 | |
| 203 | def get_cpu_stats(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 204 | filter = 'instance_id=%s'%self.instance_id |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 205 | return monitor.get_meter('cpu',filter,None) |
| 206 | |
| 207 | def get_bw_stats(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 208 | filter = 'instance_id=%s'%self.instance_id |
Sapan Bhatia | eec8acc | 2014-12-16 01:08:51 -0500 | [diff] [blame] | 209 | return monitor.get_meter('network.outgoing.bytes',filter,None) |
| 210 | |
| 211 | def get_node_stats(self): |
| 212 | # Note sure what should go back here |
| 213 | return 1 |
Scott Baker | 7ccc6ad | 2015-01-25 22:16:13 -0800 | [diff] [blame] | 214 | |
| 215 | def get_ssh_command(self): |
| 216 | if (not self.instance_id) or (not self.node) or (not self.instance_name): |
| 217 | return None |
| 218 | else: |
| 219 | 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] | 220 | |
| 221 | def get_public_keys(self): |
| 222 | slice_memberships = SlicePrivilege.objects.filter(slice=self.slice) |
| 223 | pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key]) |
| 224 | |
| 225 | if self.creator.public_key: |
| 226 | pubkeys.add(self.creator.public_key) |
| 227 | |
| 228 | if self.slice.creator.public_key: |
| 229 | pubkeys.add(self.slice.creator.public_key) |
| 230 | |
| 231 | if self.slice.service and self.slice.service.public_key: |
| 232 | pubkeys.add(self.slice.service.public_key) |
| 233 | |
| 234 | return pubkeys |
Sapan Bhatia | 52ba542 | 2015-05-13 15:48:02 +0200 | [diff] [blame] | 235 | |
| 236 | def controller_setter(instance, **kwargs): |
| 237 | try: |
| 238 | instance.controller = instance.node.site_deployment.controller |
| 239 | except: |
| 240 | instance.controller = None |
| 241 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 242 | models.signals.post_init.connect(controller_setter, Instance) |