~tr3buchet/nova/lock

« back to all changes in this revision

Viewing changes to nova/virt/xenapi/vmops.py

  • Committer: Vishvananda Ishaya
  • Date: 2010-12-22 20:59:53 UTC
  • mto: This revision was merged to the branch mainline in revision 482.
  • Revision ID: vishvananda@gmail.com-20101222205953-j2j5t0qjwlcd0t2s
merge trunk and upgrade to cheetah templating

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
    """
35
35
    Management class for VM-related tasks
36
36
    """
 
37
 
37
38
    def __init__(self, session):
38
39
        global XenAPI
39
40
        if XenAPI is None:
43
44
        VMHelper.late_import()
44
45
 
45
46
    def list_instances(self):
46
 
        """ List VM instances """
47
 
        return [self._session.get_xenapi().VM.get_name_label(vm) \
48
 
                for vm in self._session.get_xenapi().VM.get_all()]
 
47
        """List VM instances"""
 
48
        vms = []
 
49
        for vm in self._session.get_xenapi().VM.get_all():
 
50
            rec = self._session.get_xenapi().VM.get_record(vm)
 
51
            if not rec["is_a_template"] and not rec["is_control_domain"]:
 
52
                vms.append(rec["name_label"])
 
53
        return vms
49
54
 
50
55
    def spawn(self, instance):
51
 
        """ Create VM instance """
 
56
        """Create VM instance"""
52
57
        vm = VMHelper.lookup(self._session, instance.name)
53
58
        if vm is not None:
54
59
            raise Exception('Attempted to create non-unique name %s' %
80
85
                     vm_ref)
81
86
 
82
87
    def reboot(self, instance):
83
 
        """ Reboot VM instance """
 
88
        """Reboot VM instance"""
84
89
        instance_name = instance.name
85
90
        vm = VMHelper.lookup(self._session, instance_name)
86
91
        if vm is None:
87
92
            raise Exception('instance not present %s' % instance_name)
88
93
        task = self._session.call_xenapi('Async.VM.clean_reboot', vm)
89
 
        self._session.wait_for_task(task)
 
94
        self._session.wait_for_task(instance.id, task)
90
95
 
91
96
    def destroy(self, instance):
92
 
        """ Destroy VM instance """
 
97
        """Destroy VM instance"""
93
98
        vm = VMHelper.lookup(self._session, instance.name)
94
99
        if vm is None:
95
100
            # Don't complain, just return.  This lets us clean up instances
100
105
        try:
101
106
            task = self._session.call_xenapi('Async.VM.hard_shutdown',
102
107
                                                   vm)
103
 
            self._session.wait_for_task(task)
 
108
            self._session.wait_for_task(instance.id, task)
104
109
        except XenAPI.Failure, exc:
105
110
            logging.warn(exc)
106
111
        # Disk clean-up
108
113
            for vdi in vdis:
109
114
                try:
110
115
                    task = self._session.call_xenapi('Async.VDI.destroy', vdi)
111
 
                    self._session.wait_for_task(task)
 
116
                    self._session.wait_for_task(instance.id, task)
112
117
                except XenAPI.Failure, exc:
113
118
                    logging.warn(exc)
114
119
        try:
115
120
            task = self._session.call_xenapi('Async.VM.destroy', vm)
116
 
            self._session.wait_for_task(task)
117
 
        except XenAPI.Failure, exc:
118
 
            logging.warn(exc)
 
121
            self._session.wait_for_task(instance.id, task)
 
122
        except XenAPI.Failure, exc:
 
123
            logging.warn(exc)
 
124
 
 
125
    def _wait_with_callback(self, instance_id, task, callback):
 
126
        ret = None
 
127
        try:
 
128
            ret = self._session.wait_for_task(instance_id, task)
 
129
        except XenAPI.Failure, exc:
 
130
            logging.warn(exc)
 
131
        callback(ret)
 
132
 
 
133
    def pause(self, instance, callback):
 
134
        """Pause VM instance"""
 
135
        instance_name = instance.name
 
136
        vm = VMHelper.lookup(self._session, instance_name)
 
137
        if vm is None:
 
138
            raise Exception('instance not present %s' % instance_name)
 
139
        task = self._session.call_xenapi('Async.VM.pause', vm)
 
140
        self._wait_with_callback(instance.id, task, callback)
 
141
 
 
142
    def unpause(self, instance, callback):
 
143
        """Unpause VM instance"""
 
144
        instance_name = instance.name
 
145
        vm = VMHelper.lookup(self._session, instance_name)
 
146
        if vm is None:
 
147
            raise Exception('instance not present %s' % instance_name)
 
148
        task = self._session.call_xenapi('Async.VM.unpause', vm)
 
149
        self._wait_with_callback(instance.id, task, callback)
119
150
 
120
151
    def get_info(self, instance_id):
121
 
        """ Return data about VM instance """
 
152
        """Return data about VM instance"""
122
153
        vm = VMHelper.lookup_blocking(self._session, instance_id)
123
154
        if vm is None:
124
155
            raise Exception('instance not present %s' % instance_id)
134
165
        return VMHelper.compile_diagnostics(self._session, rec)
135
166
 
136
167
    def get_console_output(self, instance):
137
 
        """ Return snapshot of console """
 
168
        """Return snapshot of console"""
138
169
        # TODO: implement this to fix pylint!
139
170
        return 'FAKE CONSOLE OUTPUT of instance'