1
# Copyright 2011 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
16
from nova import context
18
from nova.openstack.common import log as logging
21
LOG = logging.getLogger(__name__)
24
def notify(_context, message):
25
"""Look for specific compute manager events and interprete them
26
so as to keep the Capacity table up to date.
28
NOTE: the True/False return codes are only for testing.
31
# The event_type must start with 'compute.instance.'
32
event_type = message.get('event_type', None)
33
preamble = 'compute.instance.'
34
if not event_type or not event_type.startswith(preamble):
37
# Events we're interested in end with .start and .end
38
event = event_type[len(preamble):]
39
parts = event.split('.')
40
suffix = parts[-1].lower()
41
event = event[:(-len(suffix) - 1)]
43
if suffix not in ['start', 'end']:
45
started = suffix == 'start'
46
ended = suffix == 'end'
48
if started and event == 'create':
49
# We've already updated this stuff in the scheduler. Don't redo the
53
work = 1 if started else -1
55
# Extract the host name from the publisher id ...
56
publisher_preamble = 'compute.'
57
publisher = message.get('publisher_id', None)
58
if not publisher or not publisher.startswith(publisher_preamble):
60
host = publisher[len(publisher_preamble):]
62
# If we deleted an instance, make sure we reclaim the resources.
63
# We may need to do something explicit for rebuild/migrate.
67
if ended and event == 'delete':
69
payload = message.get('payload', {})
70
free_ram_mb = payload.get('memory_mb', 0)
71
free_disk_gb = payload.get('disk_gb', 0)
73
LOG.debug("EventType=%(event_type)s -> host %(host)s: "
74
"ram %(free_ram_mb)d, disk %(free_disk_gb)d, "
75
"work %(work)d, vms%(vms)d" % locals())
77
db.api.compute_node_utilization_update(context.get_admin_context(), host,
78
free_ram_mb_delta=free_ram_mb, free_disk_gb_delta=free_disk_gb,
79
work_delta=work, vm_delta=vms)