3 # Copyright (C) 2010 Oregon State University et al.
4 # Copyright (C) 2010 Greek Research and Technology Network
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 from datetime
import datetime
, timedelta
24 from hashlib
import sha1
31 from django
.conf
import settings
33 from django
.contrib
.auth
.models
import User
, Group
34 from django
.contrib
.contenttypes
.generic
import GenericForeignKey
35 from django
.contrib
.contenttypes
.models
import ContentType
36 from django
.contrib
.sites
import models
as sites_app
37 from django
.contrib
.sites
.management
import create_default_site
38 from django
.core
.validators
import RegexValidator
, MinValueValidator
39 from django
.db
import models
40 from django
.db
.models
import BooleanField
, Q
, Sum
41 from django
.db
.models
.query
import QuerySet
42 from django
.db
.models
.signals
import post_save
, post_syncdb
43 from django
.db
.utils
import DatabaseError
44 from django
.utils
.encoding
import force_unicode
45 from django
.utils
.translation
import ugettext_lazy
as _
47 from django_fields
.fields
import PickleField
49 from ganeti_web
.logs
import register_log_actions
51 from object_log
.models
import LogItem
52 log_action
= LogItem
.objects
.log_action
54 from object_permissions
.registration
import register
56 from muddle_users
import signals
as muddle_user_signals
58 from ganeti_web
import constants
, management
, permissions
59 from ganeti_web
.fields
import (PatchedEncryptedCharField
,
60 PreciseDateTimeField
, SumIf
)
61 from ganeti_web
.util
import client
62 from ganeti_web
.util
.client
import GanetiApiError
, REPLACE_DISK_AUTO
64 from south
.signals
import post_migrate
66 if settings
.VNC_PROXY
:
67 from ganeti_web
.util
.vncdaemon
.vapclient
import (request_forwarding
,
71 class QuerySetManager(models
.Manager
):
73 Useful if you want to define manager methods that need to chain. In this
74 case create a QuerySet class within your model and add all of your methods
75 directly to the queryset. Example:
77 class Foo(models.Model):
78 enabled = fields.BooleanField()
79 dirty = fields.BooleanField()
83 return self.filter(enabled=True)
85 return self.filter(dirty=False)
87 Foo.objects.active().clean()
90 def __getattr__(self
, name
, *args
):
91 # Cull under/dunder names to avoid certain kinds of recursion. Django
92 # isn't super-bright here.
93 if name
.startswith('_'):
95 return getattr(self
.get_query_set(), name
, *args
)
97 def get_query_set(self
):
98 return self
.model
.QuerySet(self
.model
)
101 def generate_random_password(length
=12):
102 "Generate random sequence of specified length"
103 return "".join(random
.sample(string
.letters
+ string
.digits
, length
))
105 FINISHED_JOBS
= 'success', 'unknown', 'error'
108 RAPI_CACHE_HASHES
= {}
111 def get_rapi(hash, cluster
):
113 Retrieves the cached Ganeti RAPI client for a given hash. The Hash is
114 derived from the connection credentials required for a cluster. If the
115 client is not yet cached, it will be created and added.
117 If a hash does not correspond to any cluster then Cluster.DoesNotExist will
120 @param cluster - either a cluster object, or ID of object. This is used
121 for resolving the cluster if the client is not already found. The id is
122 used rather than the hash, because the hash is mutable.
124 @return a Ganeti RAPI client.
126 if hash in RAPI_CACHE
:
127 return RAPI_CACHE
[hash]
129 # always look up the instance, even if we were given a Cluster instance
130 # it ensures we are retrieving the latest credentials. This helps avoid
131 # stale credentials. Retrieve only the values because we don't actually
132 # need another Cluster instance here.
133 if isinstance(cluster
, (Cluster
,)):
135 (credentials
,) = Cluster
.objects
.filter(id=cluster
) \
136 .values_list('hash', 'hostname', 'port', 'username', 'password')
137 hash, host
, port
, user
, password
= credentials
140 # XXX django-fields only stores str, convert to None if needed
141 password
= Cluster
.decrypt_password(password
) if password
else None
142 password
= None if password
in ('None', '') else password
144 # now that we know hash is fresh, check cache again. The original hash
145 # could have been stale. This avoids constructing a new RAPI that already
147 if hash in RAPI_CACHE
:
148 return RAPI_CACHE
[hash]
150 # delete any old version of the client that was cached.
151 if cluster
in RAPI_CACHE_HASHES
:
152 del RAPI_CACHE
[RAPI_CACHE_HASHES
[cluster
]]
154 # Set connect timeout in settings.py so that you do not learn patience.
155 rapi
= client
.GanetiRapiClient(host
, port
, user
, password
,
156 timeout
=settings
.RAPI_CONNECT_TIMEOUT
)
157 RAPI_CACHE
[hash] = rapi
158 RAPI_CACHE_HASHES
[cluster
] = hash
162 def clear_rapi_cache():
164 clears the rapi cache
167 RAPI_CACHE_HASHES
.clear()
170 ssh_public_key_re
= re
.compile(
171 r
'^ssh-(rsa|dsa|dss) [A-Z0-9+/=]+ .+$', re
.IGNORECASE
)
172 ssh_public_key_error
= _("Enter a valid RSA or DSA SSH key.")
173 validate_sshkey
= RegexValidator(ssh_public_key_re
, ssh_public_key_error
,
177 class CachedClusterObject(models
.Model
):
179 Parent class for objects which belong to Ganeti but have cached data in
182 The main point of this class is to permit saving lots of data from Ganeti
183 so that we don't have to look things up constantly. The Ganeti RAPI is
184 slow, so avoiding it as much as possible is a good idea.
186 This class provides transparent caching for all of the data that it
187 serializes; no explicit cache accesses are required.
189 This model is abstract and may not be instantiated on its own.
192 serialized_info
= models
.TextField(default
="", editable
=False)
193 mtime
= PreciseDateTimeField(null
=True, editable
=False)
194 cached
= PreciseDateTimeField(null
=True, editable
=False)
195 ignore_cache
= models
.BooleanField(default
=False)
206 def save(self
, *args
, **kwargs
):
208 overridden to ensure info is serialized prior to save
210 if not self
.serialized_info
:
211 self
.serialized_info
= cPickle
.dumps(self
.__info
)
212 super(CachedClusterObject
, self
).save(*args
, **kwargs
)
214 def __init__(self
, *args
, **kwargs
):
215 super(CachedClusterObject
, self
).__init
__(*args
, **kwargs
)
221 A dictionary of metadata for this object.
223 This is a proxy for the ``serialized_info`` field. Reads from this
224 property lazily access the field, and writes to this property will be
227 Writes to this property do *not* force serialization.
230 if self
.__info
is None:
231 if self
.serialized_info
:
232 self
.__info
= cPickle
.loads(str(self
.serialized_info
))
235 def _set_info(self
, value
):
237 if value
is not None:
239 self
.serialized_info
= ""
241 info
= info
.setter(_set_info
)
245 Load cached info retrieved from the ganeti cluster. This function
246 includes a lazy cache mechanism that uses a timer to decide whether or
247 not to refresh the cached information with new information from the
250 This will ignore the cache when self.ignore_cache is True
253 epsilon
= timedelta(0, 0, 0, settings
.LAZY_CACHE_REFRESH
)
256 if (self
.ignore_cache
257 or self
.cached
is None
258 or datetime
.now() > self
.cached
+ epsilon
):
261 self
.parse_transient_info()
263 self
.error
= 'No Cached Info'
265 def parse_info(self
):
267 Parse all of the attached metadata, and attach it to this object.
270 self
.parse_transient_info()
271 data
= self
.parse_persistent_info(self
.info
)
273 setattr(self
, k
, data
[k
])
277 Retrieve and parse info from the ganeti cluster. If successfully
278 retrieved and parsed, this method will also call save().
280 If communication with Ganeti fails, an error will be stored in
284 job_data
= self
.check_job_status()
285 for k
, v
in job_data
.items():
288 # XXX this try/except is far too big; see if we can pare it down.
290 info_
= self
._refresh
()
293 mtime
= datetime
.fromtimestamp(info_
['mtime'])
296 self
.cached
= datetime
.now()
298 # no info retrieved, use current mtime
301 if self
.id and (self
.mtime
is None or mtime
> self
.mtime
):
302 # there was an update. Set info and save the object
306 # There was no change on the server. Only update the cache
307 # time. This bypasses the info serialization mechanism and
308 # uses a smaller query.
310 self
.__class
__.objects
.filter(pk
=self
.id) \
311 .update(cached
=self
.cached
, **job_data
)
312 elif self
.id is not None:
313 self
.__class
__.objects
.filter(pk
=self
.id) \
314 .update(cached
=self
.cached
)
316 except GanetiApiError
, e
:
317 # Use regular expressions to match the quoted message
318 # given by GanetiApiError. '\\1' is a group substitution
319 # which places the first group '('|\")' in it's place.
320 comp
= re
.compile("('|\")(?P<msg>.*)\\1")
321 err
= comp
.search(str(e
))
322 # Any search that has 0 results will just return None.
323 # That is why we must check for err before proceeding.
325 msg
= err
.groupdict()['msg']
330 GanetiError
.store_error(msg
, obj
=self
, code
=e
.code
)
335 GanetiError
.objects
.clear_errors(obj
=self
)
339 Fetch raw data from the Ganeti cluster.
341 This must be implemented by children of this class.
344 raise NotImplementedError
346 def check_job_status(self
):
347 if not self
.last_job_id
:
350 ct
= ContentType
.objects
.get_for_model(self
)
351 qs
= Job
.objects
.filter(content_type
=ct
, object_id
=self
.pk
)
352 jobs
= qs
.order_by("job_id")
360 data
= self
.rapi
.GetJobStatus(job
.job_id
)
362 if Job
.valid_job(data
):
363 op
= data
['ops'][-1]['OP_ID']
364 status
= data
['status']
366 except GanetiApiError
:
369 if status
in ('success', 'error'):
370 for k
, v
in Job
.parse_persistent_info(data
).items():
373 if status
== 'unknown':
374 job
.status
= "unknown"
375 job
.ignore_cache
= False
377 if status
in ('success', 'error', 'unknown'):
378 _updates
= self
._complete
_job
(self
.cluster_id
,
379 self
.hostname
, op
, status
)
380 # XXX if the delete flag is set in updates then delete this
381 # model this happens here because _complete_job cannot delete
384 if 'deleted' in _updates
:
385 # Delete ourselves. Also delete the job that caused us
386 # to delete ourselves; see #8439 for "fun" details.
387 # Order matters; the job's deletion cascades over us.
388 # Revisit that when we finally nuke all this caching
393 updates
.update(_updates
)
395 # we only care about the very last job for resetting the cache flags
396 if not jobs
or status
in ('success', 'error', 'unknown'):
397 updates
['ignore_cache'] = False
398 updates
['last_job'] = None
403 def _complete_job(cls
, cluster_id
, hostname
, op
, status
):
405 Process a completed job. This method will make any updates to related
406 classes (like deleting an instance template) and return any data that
407 should be updated. This is a class method so that this processing can
408 be done without a full instance.
410 @returns dict of updated values
415 def parse_transient_info(self
):
417 Parse properties from cached info that is stored on the class but not
420 These properties will be loaded every time the object is instantiated.
421 Properties stored on the class cannot be search efficiently via the
424 This method is specific to the child object.
428 # XXX ganeti 2.1 ctime is always None
429 # XXX this means that we could nuke the conditionals!
430 if info_
['ctime'] is not None:
431 self
.ctime
= datetime
.fromtimestamp(info_
['ctime'])
434 def parse_persistent_info(cls
, info
):
436 Parse properties from cached info that are stored in the database.
438 These properties will be searchable by the django query api.
440 This method is specific to the child object.
443 # mtime is sometimes None if object has never been modified
444 if info
['mtime'] is None:
445 return {'mtime': None}
446 return {'mtime': datetime
.fromtimestamp(info
['mtime'])}
449 class JobManager(models
.Manager
):
451 Custom manager for Ganeti Jobs model
453 def create(self
, **kwargs
):
454 """ helper method for creating a job with disabled cache """
455 job
= Job(ignore_cache
=True, **kwargs
)
456 job
.save(force_insert
=True)
460 class Job(CachedClusterObject
):
462 model representing a job being run on a ganeti Cluster. This includes
463 operations such as creating or delting a virtual machine.
465 Jobs are a special type of CachedClusterObject. Job's run once then become
466 immutable. The lazy cache is modified to become permanent once a complete
467 status (success/error) has been detected. The cache can be disabled by
468 settning ignore_cache=True.
471 job_id
= models
.IntegerField()
472 content_type
= models
.ForeignKey(ContentType
, related_name
="+")
473 object_id
= models
.IntegerField()
474 obj
= GenericForeignKey('content_type', 'object_id')
475 cluster
= models
.ForeignKey('Cluster', related_name
='jobs', editable
=False)
476 cluster_hash
= models
.CharField(max_length
=40, editable
=False)
478 finished
= models
.DateTimeField(null
=True, blank
=True)
479 status
= models
.CharField(max_length
=10)
480 op
= models
.CharField(max_length
=50)
482 objects
= JobManager()
484 def save(self
, *args
, **kwargs
):
486 sets the cluster_hash for newly saved instances
488 if self
.id is None or self
.cluster_hash
== '':
489 self
.cluster_hash
= self
.cluster
.hash
491 super(Job
, self
).save(*args
, **kwargs
)
494 def get_absolute_url(self
):
495 job
= '%s/job/(?P<job_id>\d+)' % self
.cluster
497 return 'ganeti_web.views.jobs.detail', (), {'job': job
}
501 return get_rapi(self
.cluster_hash
, self
.cluster_id
)
504 return self
.rapi
.GetJobStatus(self
.job_id
)
508 Load info for class. This will load from ganeti if ignore_cache==True,
509 otherwise this will always load from the cache.
511 if self
.id and (self
.ignore_cache
or self
.info
is None):
514 except GanetiApiError
, e
:
515 # if the Job has been archived then we don't know whether it
516 # was successful or not. Mark it as unknown.
518 self
.status
= 'unknown'
521 # its possible the cluster or crednetials are bad. fail
526 info
= self
._refresh
()
527 valid
= self
.valid_job(info
)
532 # Job.objects.get(job_id=self.info['id']).delete()
535 def valid_job(cls
, info
):
536 status
= info
.get('status')
537 ops
= info
.get('ops')
538 return not (ops
is None and status
is None)
541 def parse_op(cls
, info
):
545 # Return the most recent operation
546 op
= ops
[-1]['OP_ID']
550 def parse_persistent_info(cls
, info
):
552 Parse status and turn off cache bypass flag if job has finished
554 if not cls
.valid_job(info
):
556 op
= cls
.parse_op(info
)
557 data
= {'status': info
['status'], 'op': op
}
558 if data
['status'] in ('error', 'success'):
559 data
['ignore_cache'] = False
561 data
['finished'] = cls
.parse_end_timestamp(info
)
565 def parse_end_timestamp(info
):
566 sec
, micro
= info
['end_ts']
567 return datetime
.fromtimestamp(sec
+ (micro
/ 1000000.0))
569 def parse_transient_info(self
):
573 def current_operation(self
):
575 Jobs may consist of multiple commands/operations. This helper
576 method will return the operation that is currently running or errored
577 out, or the last operation if all operations have completed
579 @returns raw name of the current operation
583 for i
in range(len(info
['opstatus'])):
584 if info
['opstatus'][i
] != 'success':
587 return info
['ops'][index
]['OP_ID']
592 Returns the last operation, which is generally the primary operation.
594 return self
.parse_op(self
.info
)
597 return "<Job %d (%d), status %r>" % (self
.id, self
.job_id
,
600 __unicode__
= __repr__
603 class VirtualMachine(CachedClusterObject
):
605 The VirtualMachine (VM) model represents VMs within a Ganeti cluster.
607 The majority of properties are a cache for data stored in the cluster.
608 All data retrieved via the RAPI is stored in VirtualMachine.info, and
609 serialized automatically into VirtualMachine.serialized_info.
611 Attributes that need to be searchable should be stored as model fields.
612 All other attributes will be stored within VirtualMachine.info.
614 This object uses a lazy update mechanism on instantiation. If the cached
615 info from the Ganeti cluster has expired, it will trigger an update. This
616 allows the cache to function in the absence of a periodic update mechanism
617 such as Cron, Celery, or Threads.
619 XXX Serialized_info can possibly be changed to a CharField if an upper
620 limit can be determined. (Later Date, if it will optimize db)
623 cluster
= models
.ForeignKey('Cluster', related_name
='virtual_machines',
624 editable
=False, default
=0)
625 hostname
= models
.CharField(max_length
=128, db_index
=True)
626 owner
= models
.ForeignKey('ClusterUser', related_name
='virtual_machines',
627 null
=True, blank
=True,
628 on_delete
=models
.SET_NULL
)
629 virtual_cpus
= models
.IntegerField(default
=-1)
630 disk_size
= models
.IntegerField(default
=-1)
631 ram
= models
.IntegerField(default
=-1)
632 minram
= models
.IntegerField(default
=-1)
633 cluster_hash
= models
.CharField(max_length
=40, editable
=False)
634 operating_system
= models
.CharField(max_length
=128)
635 status
= models
.CharField(max_length
=14)
638 primary_node
= models
.ForeignKey('Node', related_name
='primary_vms',
639 null
=True, blank
=True)
640 secondary_node
= models
.ForeignKey('Node', related_name
='secondary_vms',
641 null
=True, blank
=True)
643 # The last job reference indicates that there is at least one pending job
644 # for this virtual machine. There may be more than one job, and that can
645 # never be prevented. This just indicates that job(s) are pending and the
646 # job related code should be run (status, cleanup, etc).
647 last_job
= models
.ForeignKey('Job', related_name
="+", null
=True,
650 # deleted flag indicates a VM is being deleted, but the job has not
651 # completed yet. VMs that have pending_delete are still displayed in lists
652 # and counted in quotas, but only so status can be checked.
653 pending_delete
= models
.BooleanField(default
=False)
656 # Template temporarily stores parameters used to create this virtual
657 # machine. This template is used to recreate the values entered into the
659 template
= models
.ForeignKey("VirtualMachineTemplate",
660 related_name
="instances", null
=True,
664 ordering
= ["hostname"]
665 unique_together
= (("cluster", "hostname"),)
667 def __unicode__(self
):
670 def save(self
, *args
, **kwargs
):
672 sets the cluster_hash for newly saved instances
675 self
.cluster_hash
= self
.cluster
.hash
681 if self
.cluster
.username
:
682 for tag
in info_
['tags']:
683 # Update owner Tag. Make sure the tag is set to the owner
684 # that is set in webmgr.
685 if tag
.startswith(constants
.OWNER_TAG
):
686 id = int(tag
[len(constants
.OWNER_TAG
):])
687 # Since there is no 'update tag' delete old tag and
688 # replace with tag containing correct owner id.
689 if id == self
.owner_id
:
694 self
.rapi
.DeleteInstanceTags(self
.hostname
, remove
)
696 info_
['tags'].remove(tag
)
697 if self
.owner_id
and not found
:
698 tag
= '%s%s' % (constants
.OWNER_TAG
, self
.owner_id
)
699 self
.rapi
.AddInstanceTags(self
.hostname
, [tag
])
700 self
.info
['tags'].append(tag
)
702 super(VirtualMachine
, self
).save(*args
, **kwargs
)
705 def get_absolute_url(self
):
707 Return absolute url for this instance.
710 return 'instance-detail', (), {'cluster_slug': self
.cluster
.slug
,
711 'instance': self
.hostname
}
715 return get_rapi(self
.cluster_hash
, self
.cluster_id
)
718 def is_running(self
):
719 return self
.status
== 'running'
722 def parse_persistent_info(cls
, info
):
724 Loads all values from cached info, included persistent properties that
725 are stored in the database
727 data
= super(VirtualMachine
, cls
).parse_persistent_info(info
)
729 # Parse resource properties
730 data
['ram'] = info
['beparams']['memory']
731 data
['virtual_cpus'] = info
['beparams']['vcpus']
732 # Sum up the size of each disk used by the VM
734 for disk
in info
['disk.sizes']:
736 data
['disk_size'] = disk_size
737 data
['operating_system'] = info
['os']
738 data
['status'] = info
['status']
740 primary
= info
['pnode']
743 data
['primary_node'] = Node
.objects
.get(hostname
=primary
)
744 except Node
.DoesNotExist
:
745 # node is not created yet. fail silently
746 data
['primary_node'] = None
748 data
['primary_node'] = None
750 secondary
= info
['snodes']
752 secondary
= secondary
[0]
754 data
['secondary_node'] = Node
.objects
.get(hostname
=secondary
)
755 except Node
.DoesNotExist
:
756 # node is not created yet. fail silently
757 data
['secondary_node'] = None
759 data
['secondary_node'] = None
764 def _complete_job(cls
, cluster_id
, hostname
, op
, status
):
766 if the cache bypass is enabled then check the status of the last job
767 when the job is complete we can reenable the cache.
769 @returns - dictionary of values that were updates
772 if status
== 'unknown':
773 # unknown status, the job was archived before it's final status
774 # was polled. Impossible to tell what happened. Clear the job
775 # so it is no longer polled.
777 # XXX This VM might be added by the CLI and be in an invalid
778 # pending_delete state. clearing pending_delete prevents this
779 # but will result in "missing" vms in some cases.
780 return dict(pending_delete
=False)
782 base
= VirtualMachine
.objects
.filter(cluster
=cluster_id
,
784 if op
== 'OP_INSTANCE_REMOVE':
785 if status
== 'success':
786 # XXX can't actually delete here since it would cause a
788 return dict(deleted
=True)
790 elif op
== 'OP_INSTANCE_CREATE' and status
== 'success':
791 # XXX must update before deleting the template to maintain
792 # referential integrity. as a consequence return no other
794 base
.update(template
=None)
795 VirtualMachineTemplate
.objects \
796 .filter(instances__hostname
=hostname
,
797 instances__cluster
=cluster_id
) \
799 return dict(template
=None)
803 # XXX if delete is pending then no need to refresh this object.
804 if self
.pending_delete
or self
.template_id
:
806 return self
.rapi
.GetInstance(self
.hostname
)
808 def shutdown(self
, timeout
=None):
810 id = self
.rapi
.ShutdownInstance(self
.hostname
)
812 id = self
.rapi
.ShutdownInstance(self
.hostname
, timeout
=timeout
)
814 job
= Job
.objects
.create(job_id
=id, obj
=self
,
815 cluster_id
=self
.cluster_id
)
817 VirtualMachine
.objects
.filter(pk
=self
.id) \
818 .update(last_job
=job
, ignore_cache
=True)
822 id = self
.rapi
.StartupInstance(self
.hostname
)
823 job
= Job
.objects
.create(job_id
=id, obj
=self
,
824 cluster_id
=self
.cluster_id
)
826 VirtualMachine
.objects
.filter(pk
=self
.id) \
827 .update(last_job
=job
, ignore_cache
=True)
831 id = self
.rapi
.RebootInstance(self
.hostname
)
832 job
= Job
.objects
.create(job_id
=id, obj
=self
,
833 cluster_id
=self
.cluster_id
)
835 VirtualMachine
.objects
.filter(pk
=self
.id) \
836 .update(last_job
=job
, ignore_cache
=True)
839 def migrate(self
, mode
='live', cleanup
=False):
841 Migrates this VirtualMachine to another node.
843 Only works if the disk type is DRDB.
845 @param mode: live or non-live
846 @param cleanup: clean up a previous migration, default is False
848 id = self
.rapi
.MigrateInstance(self
.hostname
, mode
, cleanup
)
849 job
= Job
.objects
.create(job_id
=id, obj
=self
,
850 cluster_id
=self
.cluster_id
)
852 VirtualMachine
.objects
.filter(pk
=self
.id) \
853 .update(last_job
=job
, ignore_cache
=True)
856 def replace_disks(self
, mode
=REPLACE_DISK_AUTO
, disks
=None, node
=None,
858 id = self
.rapi
.ReplaceInstanceDisks(self
.hostname
, disks
, mode
, node
,
860 job
= Job
.objects
.create(job_id
=id, obj
=self
,
861 cluster_id
=self
.cluster_id
)
863 VirtualMachine
.objects
.filter(pk
=self
.id) \
864 .update(last_job
=job
, ignore_cache
=True)
867 def setup_ssh_forwarding(self
, sport
=0):
869 Poke a proxy to start SSH forwarding.
871 Returns None if no proxy is configured, or if there was an error
872 contacting the proxy.
875 command
= self
.rapi
.GetInstanceConsole(self
.hostname
)["command"]
877 if settings
.VNC_PROXY
:
878 proxy_server
= settings
.VNC_PROXY
.split(":")
879 password
= generate_random_password()
880 sport
= request_ssh(proxy_server
, sport
, self
.info
["pnode"],
881 self
.info
["network_port"], password
, command
)
884 return proxy_server
[0], sport
, password
886 def setup_vnc_forwarding(self
, sport
=0, tls
=False):
888 Obtain VNC forwarding information, optionally configuring a proxy.
890 Returns None if a proxy is configured and there was an error
891 contacting the proxy.
896 port
= info_
['network_port']
897 node
= info_
['pnode']
899 # use proxy for VNC connection
900 if settings
.VNC_PROXY
:
901 proxy_server
= settings
.VNC_PROXY
.split(":")
902 password
= generate_random_password()
903 result
= request_forwarding(proxy_server
, node
, port
, password
,
904 sport
=sport
, tls
=tls
)
906 return proxy_server
[0], int(result
), password
908 return node
, port
, password
911 return "<VirtualMachine: '%s'>" % self
.hostname
914 class Node(CachedClusterObject
):
916 The Node model represents nodes within a Ganeti cluster.
918 The majority of properties are a cache for data stored in the cluster.
919 All data retrieved via the RAPI is stored in VirtualMachine.info, and
920 serialized automatically into VirtualMachine.serialized_info.
922 Attributes that need to be searchable should be stored as model fields.
923 All other attributes will be stored within VirtualMachine.info.
926 ROLE_CHOICES
= ((k
, v
) for k
, v
in constants
.NODE_ROLE_MAP
.items())
928 cluster
= models
.ForeignKey('Cluster', related_name
='nodes')
929 hostname
= models
.CharField(max_length
=128, unique
=True)
930 cluster_hash
= models
.CharField(max_length
=40, editable
=False)
931 offline
= models
.BooleanField()
932 role
= models
.CharField(max_length
=1, choices
=ROLE_CHOICES
)
933 ram_total
= models
.IntegerField(default
=-1)
934 ram_free
= models
.IntegerField(default
=-1)
935 disk_total
= models
.IntegerField(default
=-1)
936 disk_free
= models
.IntegerField(default
=-1)
937 cpus
= models
.IntegerField(null
=True, blank
=True)
939 # The last job reference indicates that there is at least one pending job
940 # for this virtual machine. There may be more than one job, and that can
941 # never be prevented. This just indicates that job(s) are pending and the
942 # job related code should be run (status, cleanup, etc).
943 last_job
= models
.ForeignKey('Job', related_name
="+", null
=True,
946 def __unicode__(self
):
949 def save(self
, *args
, **kwargs
):
951 sets the cluster_hash for newly saved instances
954 self
.cluster_hash
= self
.cluster
.hash
955 super(Node
, self
).save(*args
, **kwargs
)
958 def get_absolute_url(self
):
960 Return absolute url for this node.
963 return 'node-detail', (), {'cluster_slug': self
.cluster
.slug
,
964 'host': self
.hostname
}
967 """ returns node info from the ganeti server """
968 return self
.rapi
.GetNode(self
.hostname
)
972 return get_rapi(self
.cluster_hash
, self
.cluster_id
)
975 def parse_persistent_info(cls
, info
):
977 Loads all values from cached info, included persistent properties that
978 are stored in the database
980 data
= super(Node
, cls
).parse_persistent_info(info
)
982 # Parse resource properties
983 data
['ram_total'] = info
.get("mtotal") or 0
984 data
['ram_free'] = info
.get("mfree") or 0
985 data
['disk_total'] = info
.get("dtotal") or 0
986 data
['disk_free'] = info
.get("dfree") or 0
987 data
['cpus'] = info
.get("csockets")
988 data
['offline'] = info
['offline']
989 data
['role'] = info
['role']
994 """ returns dict of free and total ram """
995 values
= VirtualMachine
.objects \
996 .filter(Q(primary_node
=self
) |
Q(secondary_node
=self
)) \
997 .filter(status
='running') \
998 .exclude(ram
=-1).order_by() \
999 .aggregate(used
=Sum('ram'))
1001 total
= self
.ram_total
1002 used
= total
- self
.ram_free
1003 allocated
= values
.get("used") or 0
1004 free
= total
- allocated
if allocated
>= 0 and total
>= 0 else -1
1009 'allocated': allocated
,
1015 """ returns dict of free and total disk space """
1016 values
= VirtualMachine
.objects \
1017 .filter(Q(primary_node
=self
) |
Q(secondary_node
=self
)) \
1018 .exclude(disk_size
=-1).order_by() \
1019 .aggregate(used
=Sum('disk_size'))
1021 total
= self
.disk_total
1022 used
= total
- self
.disk_free
1023 allocated
= values
.get("used") or 0
1024 free
= total
- allocated
if allocated
>= 0 and total
>= 0 else -1
1029 'allocated': allocated
,
1034 def allocated_cpus(self
):
1035 values
= VirtualMachine
.objects \
1036 .filter(primary_node
=self
, status
='running') \
1037 .exclude(virtual_cpus
=-1).order_by() \
1038 .aggregate(cpus
=Sum('virtual_cpus'))
1039 return values
.get("cpus") or 0
1041 def set_role(self
, role
, force
=False):
1043 Sets the role for this node
1045 @param role - one of the following choices:
1052 id = self
.rapi
.SetNodeRole(self
.hostname
, role
, force
)
1053 job
= Job
.objects
.create(job_id
=id, obj
=self
,
1054 cluster_id
=self
.cluster_id
)
1056 Node
.objects
.filter(pk
=self
.pk
).update(ignore_cache
=True, last_job
=job
)
1059 def evacuate(self
, iallocator
=None, node
=None):
1061 migrates all secondary instances off this node
1063 id = self
.rapi
.EvacuateNode(self
.hostname
, iallocator
=iallocator
,
1065 job
= Job
.objects
.create(job_id
=id, obj
=self
,
1066 cluster_id
=self
.cluster_id
)
1068 Node
.objects
.filter(pk
=self
.pk
) \
1069 .update(ignore_cache
=True, last_job
=job
)
1072 def migrate(self
, mode
=None):
1074 migrates all primary instances off this node
1076 id = self
.rapi
.MigrateNode(self
.hostname
, mode
)
1077 job
= Job
.objects
.create(job_id
=id, obj
=self
,
1078 cluster_id
=self
.cluster_id
)
1080 Node
.objects
.filter(pk
=self
.pk
).update(ignore_cache
=True, last_job
=job
)
1084 return "<Node: '%s'>" % self
.hostname
1087 class Cluster(CachedClusterObject
):
1089 A Ganeti cluster that is being tracked by this manager tool
1091 hostname
= models
.CharField(_('hostname'), max_length
=128, unique
=True)
1092 slug
= models
.SlugField(_('slug'), max_length
=50, unique
=True,
1094 port
= models
.PositiveIntegerField(_('port'), default
=5080)
1095 description
= models
.CharField(_('description'), max_length
=128,
1097 username
= models
.CharField(_('username'), max_length
=128, blank
=True)
1098 password
= PatchedEncryptedCharField(_('password'), default
="",
1099 max_length
=128, blank
=True)
1100 hash = models
.CharField(_('hash'), max_length
=40, editable
=False)
1103 virtual_cpus
= models
.IntegerField(_('Virtual CPUs'), null
=True,
1105 disk
= models
.IntegerField(_('disk'), null
=True, blank
=True)
1106 ram
= models
.IntegerField(_('ram'), null
=True, blank
=True)
1108 # The last job reference indicates that there is at least one pending job
1109 # for this virtual machine. There may be more than one job, and that can
1110 # never be prevented. This just indicates that job(s) are pending and the
1111 # job related code should be run (status, cleanup, etc).
1112 last_job
= models
.ForeignKey('Job', related_name
='cluster_last_job',
1113 null
=True, blank
=True)
1116 ordering
= ["hostname", "description"]
1118 def __unicode__(self
):
1119 return self
.hostname
1121 def save(self
, *args
, **kwargs
):
1122 self
.hash = self
.create_hash()
1123 super(Cluster
, self
).save(*args
, **kwargs
)
1126 def get_absolute_url(self
):
1127 return 'cluster-detail', (), {'cluster_slug': self
.slug
}
1131 def cluster_id(self
):
1135 def decrypt_password(cls
, value
):
1137 Convenience method for decrypting a password without an instance.
1138 This was partly cribbed from django-fields which only allows decrypting
1139 from a model instance.
1141 If the password appears to be encrypted, this method will decrypt it;
1142 otherwise, it will return the password unchanged.
1144 This method is bonghits.
1147 field
, chaff
, chaff
, chaff
= cls
._meta
.get_field_by_name('password')
1149 if value
.startswith(field
.prefix
):
1150 ciphertext
= value
[len(field
.prefix
):]
1151 plaintext
= field
.cipher
.decrypt(binascii
.a2b_hex(ciphertext
))
1152 password
= plaintext
.split('\0')[0]
1156 return force_unicode(password
)
1161 retrieves the rapi client for this cluster.
1163 # XXX always pass self in. not only does it avoid querying this object
1164 # from the DB a second time, it also prevents a recursion loop caused
1165 # by __init__ fetching info from the Cluster
1166 return get_rapi(self
.hash, self
)
1168 def create_hash(self
):
1170 Creates a hash for this cluster based on credentials required for
1171 connecting to the server
1173 s
= '%s%s%s%s' % (self
.username
, self
.password
, self
.hostname
,
1175 return sha1(s
).hexdigest()
1177 def get_default_quota(self
):
1179 Returns the default quota for this cluster
1185 "virtual_cpus": self
.virtual_cpus
,
1188 def get_quota(self
, user
=None):
1190 Get the quota for a ClusterUser
1192 @return user's quota, default quota, or none
1195 return self
.get_default_quota()
1197 # attempt to query user specific quota first. if it does not exist
1198 # then fall back to the default quota
1199 query
= Quota
.objects
.filter(cluster
=self
, user
=user
)
1200 quotas
= query
.values('ram', 'disk', 'virtual_cpus')
1203 quota
['default'] = 0
1206 return self
.get_default_quota()
1208 def set_quota(self
, user
, data
):
1210 Set the quota for a ClusterUser.
1212 If data is None, the quota will be removed.
1214 @param values: dictionary of values, or None to delete the quota
1217 kwargs
= {'cluster': self
, 'user': user
}
1219 Quota
.objects
.filter(**kwargs
).delete()
1221 quota
, new
= Quota
.objects
.get_or_create(**kwargs
)
1222 quota
.__dict
__.update(data
)
1226 def get_quotas(cls
, clusters
=None, user
=None):
1227 """ retrieve a bulk list of cluster quotas """
1229 if clusters
is None:
1230 clusters
= Cluster
.objects
.all()
1234 for cluster
in clusters
:
1238 'disk': cluster
.disk
,
1239 'virtual_cpus': cluster
.virtual_cpus
,
1241 cluster_id_map
[cluster
.id] = cluster
1243 # get user's custom queries if any
1244 if user
is not None:
1245 qs
= Quota
.objects
.filter(cluster__in
=clusters
, user
=user
)
1246 values
= qs
.values('ram', 'disk', 'virtual_cpus', 'cluster__id')
1248 for custom
in values
:
1250 cluster
= cluster_id_map
[custom
['cluster__id']]
1253 custom
['default'] = 0
1254 del custom
['cluster__id']
1255 quotas
[cluster
] = custom
1259 def sync_virtual_machines(self
, remove
=False):
1261 Synchronizes the VirtualMachines in the database with the information
1262 this ganeti cluster has:
1263 * VMs no longer in ganeti are deleted
1264 * VMs missing from the database are added
1266 ganeti
= self
.instances()
1267 db
= self
.virtual_machines
.all().values_list('hostname', flat
=True)
1269 # add VMs missing from the database
1270 for hostname
in filter(lambda x
: unicode(x
) not in db
, ganeti
):
1271 vm
= VirtualMachine
.objects
.create(cluster
=self
, hostname
=hostname
)
1274 # deletes VMs that are no longer in ganeti
1276 missing_ganeti
= filter(lambda x
: str(x
) not in ganeti
, db
)
1278 self
.virtual_machines \
1279 .filter(hostname__in
=missing_ganeti
).delete()
1281 def sync_nodes(self
, remove
=False):
1283 Synchronizes the Nodes in the database with the information
1284 this ganeti cluster has:
1285 * Nodes no longer in ganeti are deleted
1286 * Nodes missing from the database are added
1288 ganeti
= self
.rapi
.GetNodes()
1289 db
= self
.nodes
.all().values_list('hostname', flat
=True)
1291 # add Nodes missing from the database
1292 for hostname
in filter(lambda x
: unicode(x
) not in db
, ganeti
):
1293 node
= Node
.objects
.create(cluster
=self
, hostname
=hostname
)
1296 # deletes Nodes that are no longer in ganeti
1298 missing_ganeti
= filter(lambda x
: str(x
) not in ganeti
, db
)
1300 self
.nodes
.filter(hostname__in
=missing_ganeti
).delete()
1303 def missing_in_ganeti(self
):
1305 Returns a list of VirtualMachines that are missing from the Ganeti
1306 cluster but present in the database.
1308 ganeti
= self
.instances()
1309 qs
= self
.virtual_machines
.exclude(template__isnull
=False)
1310 db
= qs
.values_list('hostname', flat
=True)
1311 return [x
for x
in db
if str(x
) not in ganeti
]
1314 def missing_in_db(self
):
1316 Returns list of VirtualMachines that are missing from the database, but
1319 ganeti
= self
.instances()
1320 db
= self
.virtual_machines
.all().values_list('hostname', flat
=True)
1321 return [x
for x
in ganeti
if unicode(x
) not in db
]
1324 def nodes_missing_in_db(self
):
1326 Returns list of Nodes that are missing from the database, but present
1330 ganeti
= self
.rapi
.GetNodes()
1331 except GanetiApiError
:
1333 db
= self
.nodes
.all().values_list('hostname', flat
=True)
1334 return [x
for x
in ganeti
if unicode(x
) not in db
]
1337 def nodes_missing_in_ganeti(self
):
1339 Returns list of Nodes that are missing from the ganeti cluster
1340 but present in the database
1343 ganeti
= self
.rapi
.GetNodes()
1344 except GanetiApiError
:
1346 db
= self
.nodes
.all().values_list('hostname', flat
=True)
1347 return filter(lambda x
: str(x
) not in ganeti
, db
)
1350 def available_ram(self
):
1351 """ returns dict of free and total ram """
1352 nodes
= self
.nodes
.exclude(ram_total
=-1) \
1353 .aggregate(total
=Sum('ram_total'), free
=Sum('ram_free'))
1354 total
= max(nodes
.get("total", 0), 0)
1355 free
= max(nodes
.get("free", 0), 0)
1357 values
= self
.virtual_machines \
1358 .filter(status
='running') \
1359 .exclude(ram
=-1).order_by() \
1360 .aggregate(used
=Sum('ram'))
1362 if values
.get("used") is None:
1365 allocated
= values
["used"]
1367 free
= max(total
- allocated
, 0)
1372 'allocated': allocated
,
1377 def available_disk(self
):
1378 """ returns dict of free and total disk space """
1379 nodes
= self
.nodes
.exclude(disk_total
=-1) \
1380 .aggregate(total
=Sum('disk_total'), free
=Sum('disk_free'))
1381 total
= max(nodes
.get("total", 0), 0)
1382 free
= max(nodes
.get("free", 0), 0)
1384 values
= self
.virtual_machines \
1385 .exclude(disk_size
=-1).order_by() \
1386 .aggregate(used
=Sum('disk_size'))
1388 if values
.get("used") is None:
1391 allocated
= values
["used"]
1393 free
= max(total
- allocated
, 0)
1398 'allocated': allocated
,
1403 return self
.rapi
.GetInfo()
1405 def instances(self
, bulk
=False):
1406 """Gets all VMs which reside under the Cluster
1407 Calls the rapi client for all instances.
1410 return self
.rapi
.GetInstances(bulk
=bulk
)
1411 except GanetiApiError
:
1414 def instance(self
, instance
):
1415 """Get a single Instance
1416 Calls the rapi client for a specific instance.
1419 return self
.rapi
.GetInstance(instance
)
1420 except GanetiApiError
:
1423 def redistribute_config(self
):
1425 Redistribute config from cluster's master node to all
1428 # no exception handling, because it's being done in a view
1429 id = self
.rapi
.RedistributeConfig()
1430 job
= Job
.objects
.create(job_id
=id, obj
=self
, cluster_id
=self
.id)
1432 Cluster
.objects
.filter(pk
=self
.id) \
1433 .update(last_job
=job
, ignore_cache
=True)
1437 class VirtualMachineTemplate(models
.Model
):
1439 Virtual Machine Template holds all the values for the create virtual
1440 machine form so that they can automatically be used or edited by a user.
1443 template_name
= models
.CharField(max_length
=255, default
="")
1444 temporary
= BooleanField(verbose_name
=_("Temporary"), default
=False)
1445 description
= models
.CharField(max_length
=255, default
="")
1446 cluster
= models
.ForeignKey(Cluster
, related_name
="templates", null
=True,
1448 start
= models
.BooleanField(verbose_name
=_('Start up After Creation'),
1450 no_install
= models
.BooleanField(verbose_name
=_('Do not install OS'),
1452 ip_check
= BooleanField(verbose_name
=_("IP Check"), default
=True)
1453 name_check
= models
.BooleanField(verbose_name
=_('DNS Name Check'),
1455 iallocator
= models
.BooleanField(verbose_name
=_('Automatic Allocation'),
1457 iallocator_hostname
= models
.CharField(max_length
=255, blank
=True)
1458 disk_template
= models
.CharField(verbose_name
=_('Disk Template'),
1460 # XXX why aren't these FKs?
1461 pnode
= models
.CharField(verbose_name
=_('Primary Node'), max_length
=255,
1463 snode
= models
.CharField(verbose_name
=_('Secondary Node'), max_length
=255,
1465 os
= models
.CharField(verbose_name
=_('Operating System'), max_length
=255)
1467 # Backend parameters (BEPARAMS)
1468 vcpus
= models
.IntegerField(verbose_name
=_('Virtual CPUs'),
1469 validators
=[MinValueValidator(1)], null
=True,
1471 # XXX do we really want the minimum memory to be 100MiB? This isn't
1472 # strictly necessary AFAICT.
1473 memory
= models
.IntegerField(verbose_name
=_('Memory'),
1474 validators
=[MinValueValidator(100)],
1475 null
=True, blank
=True)
1476 minmem
= models
.IntegerField(verbose_name
=_('Minimum Memory'),
1477 validators
=[MinValueValidator(100)],
1478 null
=True, blank
=True)
1479 disks
= PickleField(verbose_name
=_('Disks'), null
=True, blank
=True)
1480 # XXX why isn't this an enum?
1481 disk_type
= models
.CharField(verbose_name
=_('Disk Type'), max_length
=255,
1483 nics
= PickleField(verbose_name
=_('NICs'), null
=True, blank
=True)
1484 # XXX why isn't this an enum?
1485 nic_type
= models
.CharField(verbose_name
=_('NIC Type'), max_length
=255,
1488 # Hypervisor parameters (HVPARAMS)
1489 kernel_path
= models
.CharField(verbose_name
=_('Kernel Path'),
1490 max_length
=255, default
="", blank
=True)
1491 root_path
= models
.CharField(verbose_name
=_('Root Path'), max_length
=255,
1492 default
='/', blank
=True)
1493 serial_console
= models
.BooleanField(
1494 verbose_name
=_('Enable Serial Console'))
1495 boot_order
= models
.CharField(verbose_name
=_('Boot Device'),
1496 max_length
=255, default
="")
1497 cdrom_image_path
= models
.CharField(verbose_name
=_('CD-ROM Image Path'),
1498 max_length
=512, blank
=True)
1499 cdrom2_image_path
= models
.CharField(
1500 verbose_name
=_('CD-ROM 2 Image Path'),
1501 max_length
=512, blank
=True)
1504 unique_together
= (("cluster", "template_name"),)
1506 def __unicode__(self
):
1508 return u
'(temporary)'
1510 return self
.template_name
1512 def set_name(self
, name
):
1514 Set this template's name.
1516 If the name is blank, this template will become temporary and its name
1517 will be set to a unique timestamp.
1521 self
.template_name
= name
1523 # The template is temporary and will be removed by the VM when the
1524 # VM successfully comes into existence.
1525 self
.temporary
= True
1526 # Give it a temporary name. Something unique. This is the number
1527 # of microseconds since the epoch; I figure that it'll work out
1529 self
.template_name
= str(int(time
.time() * (10 ** 6)))
1532 class GanetiError(models
.Model
):
1534 Class for storing errors which occured in Ganeti
1536 cluster
= models
.ForeignKey(Cluster
, related_name
="errors")
1537 msg
= models
.TextField()
1538 code
= models
.PositiveIntegerField(blank
=True, null
=True)
1540 # XXX could be fixed with django-model-util's TimeStampedModel
1541 timestamp
= models
.DateTimeField()
1543 # determines if the errors still appears or not
1544 cleared
= models
.BooleanField(default
=False)
1546 # cluster object (cluster, VM, Node) affected by the error (if any)
1547 obj_type
= models
.ForeignKey(ContentType
, related_name
="ganeti_errors")
1548 obj_id
= models
.PositiveIntegerField()
1549 obj
= GenericForeignKey("obj_type", "obj_id")
1551 objects
= QuerySetManager()
1554 ordering
= ("-timestamp", "code", "msg")
1556 def __unicode__(self
):
1557 base
= u
"[%s] %s" % (self
.timestamp
, self
.msg
)
1560 class QuerySet(QuerySet
):
1562 def clear_errors(self
, obj
=None):
1564 Clear errors instead of deleting them.
1567 qs
= self
.filter(cleared
=False)
1570 qs
= qs
.get_errors(obj
)
1572 return qs
.update(cleared
=True)
1574 def get_errors(self
, obj
):
1576 Manager method used for getting QuerySet of all errors depending
1577 on passed arguments.
1579 @param obj affected object (itself or just QuerySet)
1583 raise RuntimeError("Implementation error calling get_errors()"
1586 # Create base query of errors to return.
1588 # if it's a Cluster or a queryset for Clusters, then we need to
1589 # get all errors from the Clusters. Do this by filtering on
1590 # GanetiError.cluster instead of obj_id.
1591 if isinstance(obj
, (Cluster
,)):
1592 return self
.filter(cluster
=obj
)
1594 elif isinstance(obj
, (QuerySet
,)):
1595 if obj
.model
== Cluster
:
1596 return self
.filter(cluster__in
=obj
)
1598 ct
= ContentType
.objects
.get_for_model(obj
.model
)
1599 return self
.filter(obj_type
=ct
, obj_id__in
=obj
)
1602 ct
= ContentType
.objects
.get_for_model(obj
.__class
__)
1603 return self
.filter(obj_type
=ct
, obj_id
=obj
.pk
)
1606 return "<GanetiError '%s'>" % self
.msg
1609 def store_error(cls
, msg
, obj
, code
, **kwargs
):
1611 Create and save an error with the given information.
1613 @param msg error's message
1614 @param obj object (i.e. cluster or vm) affected by the error
1615 @param code error's code number
1617 ct
= ContentType
.objects
.get_for_model(obj
.__class
__)
1618 is_cluster
= isinstance(obj
, Cluster
)
1620 # 401 -- bad permissions
1621 # 401 is cluster-specific error and thus shouldn't appear on any other
1625 # NOTE: what we do here is almost like:
1626 # return self.store_error(msg=msg, code=code, obj=obj.cluster)
1627 # we just omit the recursiveness
1629 ct
= ContentType
.objects
.get_for_model(Cluster
)
1632 # 404 -- object not found
1633 # 404 can occur on any object, but when it occurs on a cluster, then
1634 # any of its children must not see the error again
1637 # return if the error exists for cluster
1639 c_ct
= ContentType
.objects
.get_for_model(Cluster
)
1640 return cls
.objects
.filter(msg
=msg
, obj_type
=c_ct
,
1642 obj_id
=obj
.cluster_id
,
1645 except (cls
.DoesNotExist
, IndexError):
1646 # we want to proceed when the error is not
1650 # XXX use a try/except instead of get_or_create(). get_or_create()
1651 # does not allow us to set cluster_id. This means we'd have to query
1652 # the cluster object to create the error. we can't guaranteee the
1653 # cluster will already be queried so use create() instead which does
1656 return cls
.objects
.filter(msg
=msg
, obj_type
=ct
, obj_id
=obj
.pk
,
1657 code
=code
, **kwargs
)[0]
1659 except (cls
.DoesNotExist
, IndexError):
1660 cluster_id
= obj
.pk
if is_cluster
else obj
.cluster_id
1662 return cls
.objects
.create(timestamp
=datetime
.now(), msg
=msg
,
1663 obj_type
=ct
, obj_id
=obj
.pk
,
1664 cluster_id
=cluster_id
, code
=code
,
1668 class ClusterUser(models
.Model
):
1670 Base class for objects that may interact with a Cluster or VirtualMachine.
1673 name
= models
.CharField(max_length
=128)
1674 real_type
= models
.ForeignKey(ContentType
, related_name
="+",
1675 editable
=False, null
=True, blank
=True)
1677 def __unicode__(self
):
1680 def save(self
, *args
, **kwargs
):
1682 self
.real_type
= self
._get
_real
_type
()
1683 super(ClusterUser
, self
).save(*args
, **kwargs
)
1685 def get_absolute_url(self
):
1686 return self
.cast().get_absolute_url()
1689 def permissable(self
):
1690 """ returns an object that can be granted permissions """
1691 return self
.cast().permissable
1694 def _get_real_type(cls
):
1695 return ContentType
.objects
.get_for_model(cls
)
1698 return self
.real_type
.get_object_for_this_type(pk
=self
.pk
)
1700 def used_resources(self
, cluster
=None, only_running
=True):
1702 Return dictionary of total resources used by VMs that this ClusterUser
1704 @param cluster if set, get only VMs from specified cluster
1705 @param only_running if set, get only running VMs
1707 # XXX - order_by must be cleared or it breaks annotation grouping since
1708 # the default order_by field is also added to the group_by clause
1709 base
= self
.virtual_machines
.all().order_by()
1711 # XXX - use a custom aggregate for ram and vcpu count when filtering by
1712 # running. this allows us to execute a single query.
1714 # XXX - quotes must be used in this order. postgresql quirk
1716 sum_ram
= SumIf('ram', condition
="status='running'")
1717 sum_vcpus
= SumIf('virtual_cpus', condition
="status='running'")
1719 sum_ram
= Sum('ram')
1720 sum_vcpus
= Sum('virtual_cpus')
1722 base
= base
.exclude(ram
=-1, disk_size
=-1, virtual_cpus
=-1)
1725 base
= base
.filter(cluster
=cluster
)
1726 result
= base
.aggregate(ram
=sum_ram
, disk
=Sum('disk_size'),
1727 virtual_cpus
=sum_vcpus
)
1729 # repack with zeros instead of Nones
1730 if result
['disk'] is None:
1732 if result
['ram'] is None:
1734 if result
['virtual_cpus'] is None:
1735 result
['virtual_cpus'] = 0
1739 base
= base
.values('cluster').annotate(uram
=sum_ram
,
1740 udisk
=Sum('disk_size'),
1741 uvirtual_cpus
=sum_vcpus
)
1743 # repack as dictionary
1746 # repack with zeros instead of Nones, change index names
1747 used
["ram"] = used
.pop("uram") or 0
1748 used
["disk"] = used
.pop("udisk") or 0
1749 used
["virtual_cpus"] = used
.pop("uvirtual_cpus") or 0
1750 result
[used
.pop('cluster')] = used
1755 class Profile(ClusterUser
):
1757 Profile associated with a django.contrib.auth.User object.
1759 user
= models
.OneToOneField(User
)
1761 def get_absolute_url(self
):
1762 return self
.user
.get_absolute_url()
1764 def grant(self
, perm
, obj
):
1765 self
.user
.grant(perm
, obj
)
1767 def set_perms(self
, perms
, obj
):
1768 self
.user
.set_perms(perms
, obj
)
1770 def get_objects_any_perms(self
, *args
, **kwargs
):
1771 return self
.user
.get_objects_any_perms(*args
, **kwargs
)
1773 def has_perm(self
, *args
, **kwargs
):
1774 return self
.user
.has_perm(*args
, **kwargs
)
1777 def permissable(self
):
1778 """ returns an object that can be granted permissions """
1782 class Organization(ClusterUser
):
1784 An organization is used for grouping Users.
1786 Organizations are matched with an instance of contrib.auth.models.Group.
1787 This model exists so that contrib.auth.models.Group have a 1:1 relation
1788 with a ClusterUser on which quotas and permissions can be assigned.
1791 group
= models
.OneToOneField(Group
, related_name
='organization')
1793 def get_absolute_url(self
):
1794 return self
.group
.get_absolute_url()
1796 def grant(self
, perm
, object):
1797 self
.group
.grant(perm
, object)
1799 def set_perms(self
, perms
, object):
1800 self
.group
.set_perms(perms
, object)
1802 def get_objects_any_perms(self
, *args
, **kwargs
):
1803 return self
.group
.get_objects_any_perms(*args
, **kwargs
)
1805 def has_perm(self
, *args
, **kwargs
):
1806 return self
.group
.has_perm(*args
, **kwargs
)
1809 def permissable(self
):
1810 """ returns an object that can be granted permissions """
1814 class Quota(models
.Model
):
1816 A resource limit imposed on a ClusterUser for a given Cluster. The
1817 attributes of this model represent maximum values the ClusterUser can
1818 consume. The absence of a Quota indicates unlimited usage.
1820 user
= models
.ForeignKey(ClusterUser
, related_name
='quotas')
1821 cluster
= models
.ForeignKey(Cluster
, related_name
='quotas')
1823 ram
= models
.IntegerField(default
=0, null
=True, blank
=True)
1824 disk
= models
.IntegerField(default
=0, null
=True, blank
=True)
1825 virtual_cpus
= models
.IntegerField(default
=0, null
=True, blank
=True)
1828 class SSHKey(models
.Model
):
1830 Model representing user's SSH public key. Virtual machines rely on
1833 key
= models
.TextField(validators
=[validate_sshkey
])
1834 #filename = models.CharField(max_length=128) # saves key file's name
1835 user
= models
.ForeignKey(User
, related_name
='ssh_keys')
1838 def create_profile(sender
, instance
, **kwargs
):
1840 Create a profile object whenever a new user is created, also keeps the
1841 profile name synchronized with the username
1844 profile
, new
= Profile
.objects
.get_or_create(user
=instance
)
1845 if profile
.name
!= instance
.username
:
1846 profile
.name
= instance
.username
1848 except DatabaseError
:
1849 # XXX - since we're using south to track migrations the Profile table
1850 # won't be available the first time syncdb is run. Catch the error
1851 # here and let the south migration handle it.
1855 def update_cluster_hash(sender
, instance
, **kwargs
):
1857 Updates the Cluster hash for all of it's VirtualMachines, Nodes, and Jobs
1859 instance
.virtual_machines
.all().update(cluster_hash
=instance
.hash)
1860 instance
.jobs
.all().update(cluster_hash
=instance
.hash)
1861 instance
.nodes
.all().update(cluster_hash
=instance
.hash)
1864 def update_organization(sender
, instance
, **kwargs
):
1866 Creates a Organizations whenever a contrib.auth.models.Group is created
1868 org
, new
= Organization
.objects
.get_or_create(group
=instance
)
1869 org
.name
= instance
.name
1872 post_save
.connect(create_profile
, sender
=User
)
1873 post_save
.connect(update_cluster_hash
, sender
=Cluster
)
1874 post_save
.connect(update_organization
, sender
=Group
)
1876 # Disconnect create_default_site from django.contrib.sites so that
1877 # the useless table for sites is not created. This will be
1878 # reconnected for other apps to use in update_sites_module.
1879 post_syncdb
.disconnect(create_default_site
, sender
=sites_app
)
1880 post_syncdb
.connect(management
.update_sites_module
, sender
=sites_app
,
1881 dispatch_uid
="ganeti.management.update_sites_module")
1884 def regenerate_cu_children(sender
, **kwargs
):
1886 Resets may destroy Profiles and/or Organizations. We need to regenerate
1890 # So. What are we actually doing here?
1891 # Whenever a User or Group is saved, the associated Profile or
1892 # Organization is also updated. This means that, if a Profile for a User
1893 # is absent, it will be created.
1894 # More importantly, *why* might a Profile be missing? Simple. Resets of
1895 # the ganeti app destroy them. This shouldn't happen in production, and
1896 # only occasionally in development, but it's good to explicitly handle
1897 # this particular case so that missing Profiles not resulting from a reset
1898 # are easier to diagnose.
1900 for user
in User
.objects
.filter(profile__isnull
=True):
1902 for group
in Group
.objects
.filter(organization__isnull
=True):
1904 except DatabaseError
:
1905 # XXX - since we're using south to track migrations the Profile table
1906 # won't be available the first time syncdb is run. Catch the error
1907 # here and let the south migration handle it.
1910 post_syncdb
.connect(regenerate_cu_children
)
1913 def log_group_create(sender
, editor
, **kwargs
):
1914 """ log group creation signal """
1915 log_action('CREATE', editor
, sender
)
1918 def log_group_edit(sender
, editor
, **kwargs
):
1919 """ log group edit signal """
1920 log_action('EDIT', editor
, sender
)
1923 muddle_user_signals
.view_group_created
.connect(log_group_create
)
1924 muddle_user_signals
.view_group_edited
.connect(log_group_edit
)
1927 def refresh_objects(sender
, **kwargs
):
1929 This was originally the code in the 0009
1930 and then 0010 'force_object_refresh' migration
1932 Force a refresh of all Cluster, Nodes, and VirtualMachines, and
1933 import any new Nodes.
1936 if kwargs
.get('app', False) and kwargs
['app'] == 'ganeti_web':
1937 Cluster
.objects
.all().update(mtime
=None)
1938 Node
.objects
.all().update(mtime
=None)
1939 VirtualMachine
.objects
.all().update(mtime
=None)
1941 write
= sys
.stdout
.write
1942 flush
= sys
.stdout
.flush
1944 def wf(str, newline
=False):
1950 wf('- Refresh Cached Cluster Objects')
1951 wf(' > Synchronizing Cluster Nodes ', True)
1953 for cluster
in Cluster
.objects
.all().iterator():
1955 cluster
.sync_nodes()
1957 except GanetiApiError
:
1960 wf(' > Refreshing Node Caches ', True)
1961 for node
in Node
.objects
.all().iterator():
1964 except GanetiApiError
:
1967 wf(' > Refreshing Instance Caches ', True)
1968 for instance
in VirtualMachine
.objects
.all().iterator():
1971 except GanetiApiError
:
1976 # Set this as post_migrate hook.
1977 post_migrate
.connect(refresh_objects
)
1979 # Register permissions on our models.
1980 # These are part of the DB schema and should not be changed without serious
1982 # You *must* syncdb after you change these.
1983 register(permissions
.CLUSTER_PARAMS
, Cluster
, 'ganeti_web')
1984 register(permissions
.VIRTUAL_MACHINE_PARAMS
, VirtualMachine
, 'ganeti_web')
1987 # register log actions
1988 register_log_actions()