~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to nova/compute/api.py

  • Committer: Tarmac
  • Author(s): Vishvananda Ishaya, Anthony Young
  • Date: 2011-09-01 00:16:34 UTC
  • mfrom: (1468.3.13 os-simple-usage)
  • Revision ID: tarmac-20110901001634-bb3whyoipqxc60k5
Simple usage extension for nova.  Uses db to calculate tenant_usage for specified time periods.

Methods:
    * index: return a list of tenant_usages, with option of incuding detailed server_usage
    * show: returns a specific tenant_usage object

tenant_usage object:
    * tenant_usage.total_memory_mb_usage: sum of memory_mb * hours for all instances in tenant for this period
    * tenant_usage.total_local_gb_usage: sum of local_gb * hours for all instances in tenant for this period
    * tenant_usage.total_vcpus_usage: sum of vcpus * hours for all instances in tenant for this period
    * tenant_usage.total_hours: sum of all instance hours for this period
    * tenant_usage.server_usages: A detailed list of server_usages, which describe the usage of a specific server

For larger instances db tables, indexes on instance.launched_at and instance.terminated_at should significantly help performance.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
"""Handles all requests relating to instances (guest vms)."""
21
21
 
22
 
import eventlet
23
22
import novaclient
24
23
import re
25
24
import time
26
25
 
27
26
from nova import block_device
28
 
from nova import db
29
27
from nova import exception
30
28
from nova import flags
31
29
import nova.image
245
243
        self.ensure_default_security_group(context)
246
244
 
247
245
        if key_data is None and key_name:
248
 
            key_pair = db.key_pair_get(context, context.user_id, key_name)
 
246
            key_pair = self.db.key_pair_get(context, context.user_id, key_name)
249
247
            key_data = key_pair['public_key']
250
248
 
251
249
        if reservation_id is None:
397
395
 
398
396
        security_groups = []
399
397
        for security_group_name in security_group:
400
 
            group = db.security_group_get_by_name(context,
401
 
                                                  context.project_id,
402
 
                                                  security_group_name)
 
398
            group = self.db.security_group_get_by_name(context,
 
399
                    context.project_id,
 
400
                    security_group_name)
403
401
            security_groups.append(group['id'])
404
402
 
405
403
        for security_group_id in security_groups:
561
559
    def has_finished_migration(self, context, instance_uuid):
562
560
        """Returns true if an instance has a finished migration."""
563
561
        try:
564
 
            db.migration_get_by_instance_and_status(context, instance_uuid,
565
 
                    'finished')
 
562
            self.db.migration_get_by_instance_and_status(context,
 
563
                                                         instance_uuid,
 
564
                                                         'finished')
566
565
            return True
567
566
        except exception.NotFound:
568
567
            return False
576
575
        :param context: the security context
577
576
        """
578
577
        try:
579
 
            db.security_group_get_by_name(context, context.project_id,
580
 
                                          'default')
 
578
            self.db.security_group_get_by_name(context,
 
579
                                               context.project_id,
 
580
                                               'default')
581
581
        except exception.NotFound:
582
582
            values = {'name': 'default',
583
583
                      'description': 'default',
584
584
                      'user_id': context.user_id,
585
585
                      'project_id': context.project_id}
586
 
            db.security_group_create(context, values)
 
586
            self.db.security_group_create(context, values)
587
587
 
588
588
    def trigger_security_group_rules_refresh(self, context, security_group_id):
589
589
        """Called when a rule is added to or removed from a security_group."""
648
648
        """Called when a rule is added to or removed from a security_group"""
649
649
 
650
650
        hosts = [x['host'] for (x, idx)
651
 
                           in db.service_get_all_compute_sorted(context)]
 
651
                           in self.db.service_get_all_compute_sorted(context)]
652
652
        for host in hosts:
653
653
            rpc.cast(context,
654
654
                     self.db.queue_get_for(context, FLAGS.compute_topic, host),
676
676
 
677
677
    def add_security_group(self, context, instance_id, security_group_name):
678
678
        """Add security group to the instance"""
679
 
        security_group = db.security_group_get_by_name(context,
680
 
                                                       context.project_id,
681
 
                                                       security_group_name)
 
679
        security_group = self.db.security_group_get_by_name(context,
 
680
                context.project_id,
 
681
                security_group_name)
682
682
        # check if the server exists
683
 
        inst = db.instance_get(context, instance_id)
 
683
        inst = self.db.instance_get(context, instance_id)
684
684
        #check if the security group is associated with the server
685
685
        if self._is_security_group_associated_with_server(security_group,
686
686
                                                        instance_id):
692
692
        if inst['state'] != power_state.RUNNING:
693
693
            raise exception.InstanceNotRunning(instance_id=instance_id)
694
694
 
695
 
        db.instance_add_security_group(context.elevated(),
696
 
                                       instance_id,
697
 
                                       security_group['id'])
 
695
        self.db.instance_add_security_group(context.elevated(),
 
696
                                            instance_id,
 
697
                                            security_group['id'])
698
698
        rpc.cast(context,
699
 
             db.queue_get_for(context, FLAGS.compute_topic, inst['host']),
 
699
             self.db.queue_get_for(context, FLAGS.compute_topic, inst['host']),
700
700
             {"method": "refresh_security_group_rules",
701
701
              "args": {"security_group_id": security_group['id']}})
702
702
 
703
703
    def remove_security_group(self, context, instance_id, security_group_name):
704
704
        """Remove the security group associated with the instance"""
705
 
        security_group = db.security_group_get_by_name(context,
706
 
                                                       context.project_id,
707
 
                                                       security_group_name)
 
705
        security_group = self.db.security_group_get_by_name(context,
 
706
                context.project_id,
 
707
                security_group_name)
708
708
        # check if the server exists
709
 
        inst = db.instance_get(context, instance_id)
 
709
        inst = self.db.instance_get(context, instance_id)
710
710
        #check if the security group is associated with the server
711
711
        if not self._is_security_group_associated_with_server(security_group,
712
712
                                                        instance_id):
718
718
        if inst['state'] != power_state.RUNNING:
719
719
            raise exception.InstanceNotRunning(instance_id=instance_id)
720
720
 
721
 
        db.instance_remove_security_group(context.elevated(),
722
 
                                       instance_id,
723
 
                                       security_group['id'])
 
721
        self.db.instance_remove_security_group(context.elevated(),
 
722
                                               instance_id,
 
723
                                               security_group['id'])
724
724
        rpc.cast(context,
725
 
             db.queue_get_for(context, FLAGS.compute_topic, inst['host']),
 
725
             self.db.queue_get_for(context, FLAGS.compute_topic, inst['host']),
726
726
             {"method": "refresh_security_group_rules",
727
727
              "args": {"security_group_id": security_group['id']}})
728
728
 
816
816
                  "args": {"topic": FLAGS.compute_topic,
817
817
                           "instance_id": instance_id}})
818
818
 
 
819
    def get_active_by_window(self, context, begin, end=None, project_id=None):
 
820
        """Get instances that were continuously active over a window."""
 
821
        return self.db.instance_get_active_by_window(context, begin, end,
 
822
                                                     project_id)
 
823
 
 
824
    def get_instance_type(self, context, instance_type_id):
 
825
        """Get an instance type by instance type id."""
 
826
        return self.db.instance_type_get(context, instance_type_id)
 
827
 
819
828
    def get(self, context, instance_id):
820
829
        """Get a single instance with the given instance_id."""
821
830
        # NOTE(sirp): id used to be exclusively integer IDs; now we're
1015
1024
        :param extra_properties: dict of extra image properties to include
1016
1025
 
1017
1026
        """
1018
 
        instance = db.api.instance_get(context, instance_id)
 
1027
        instance = self.db.instance_get(context, instance_id)
1019
1028
        properties = {'instance_uuid': instance['uuid'],
1020
1029
                      'user_id': str(context.user_id),
1021
1030
                      'image_state': 'creating',
1044
1053
    def rebuild(self, context, instance_id, image_href, admin_password,
1045
1054
                name=None, metadata=None, files_to_inject=None):
1046
1055
        """Rebuild the given instance with the provided metadata."""
1047
 
        instance = db.api.instance_get(context, instance_id)
 
1056
        instance = self.db.instance_get(context, instance_id)
1048
1057
        name = name or instance["display_name"]
1049
1058
 
1050
1059
        if instance["vm_state"] != vm_states.ACTIVE: