summaryrefslogtreecommitdiff
path: root/heat/common/service_utils.py
blob: a59fef8e8bb69409cdbdefac5fb2ea88b6dc747e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import uuid

from oslo_utils import timeutils

from heat.rpc import listener_client

SERVICE_KEYS = (
    SERVICE_ID,
    SERVICE_HOST,
    SERVICE_HOSTNAME,
    SERVICE_BINARY,
    SERVICE_TOPIC,
    SERVICE_ENGINE_ID,
    SERVICE_REPORT_INTERVAL,
    SERVICE_CREATED_AT,
    SERVICE_UPDATED_AT,
    SERVICE_DELETED_AT,
    SERVICE_STATUS
) = (
    'id',
    'host',
    'hostname',
    'binary',
    'topic',
    'engine_id',
    'report_interval',
    'created_at',
    'updated_at',
    'deleted_at',
    'status'
)


def format_service(service):
    if service is None:
        return

    status = 'down'
    last_updated = service.updated_at or service.created_at
    check_interval = (timeutils.utcnow() - last_updated).total_seconds()
    if check_interval <= 2 * service.report_interval:
        status = 'up'

    result = {
        SERVICE_ID: service.id,
        SERVICE_BINARY: service.binary,
        SERVICE_ENGINE_ID: service.engine_id,
        SERVICE_HOST: service.host,
        SERVICE_HOSTNAME: service.hostname,
        SERVICE_TOPIC: service.topic,
        SERVICE_REPORT_INTERVAL: service.report_interval,
        SERVICE_CREATED_AT: service.created_at,
        SERVICE_UPDATED_AT: service.updated_at,
        SERVICE_DELETED_AT: service.deleted_at,
        SERVICE_STATUS: status
    }
    return result


def engine_alive(context, engine_id):
    return listener_client.EngineListenerClient(
        engine_id).is_alive(context)


def generate_engine_id():
    return str(uuid.uuid4())