summaryrefslogtreecommitdiffstats
path: root/compass-tasks/db/callback.py
blob: 35798bca26724c3a3f673259e42a7b28c16d6e78 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Copyright 2014 Huawei Technologies Co. Ltd
#
# 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.

"""Metadata Callback methods."""
import logging
import netaddr
import random
import re
import socket

from compass.db import exception
from compass.utils import setting_wrapper as setting
from compass.utils import util


CALLBACK_GLOBALS = globals()
CALLBACK_LOCALS = locals()
CALLBACK_CONFIGS = util.load_configs(
    setting.CALLBACK_DIR,
    config_name_suffix='.py',
    env_globals=CALLBACK_GLOBALS,
    env_locals=CALLBACK_LOCALS
)
for callback_config in CALLBACK_CONFIGS:
    CALLBACK_LOCALS.update(callback_config)


def default_proxy(name, **kwargs):
    return setting.COMPASS_SUPPORTED_PROXY


def proxy_options(name, **kwargs):
    return [setting.COMPASS_SUPPORTED_PROXY]


def default_noproxy(name, **kwargs):
    return setting.COMPASS_SUPPORTED_DEFAULT_NOPROXY


def noproxy_options(name, **kwargs):
    return setting.COMPASS_SUPPORTED_DEFAULT_NOPROXY


def default_ntp_server(name, **kwargs):
    return setting.COMPASS_SUPPORTED_NTP_SERVER


def ntp_server_options(name, **kwargs):
    return setting.COMPASS_SUPPORTED_NTP_SERVER


def default_dns_servers(name, **kwargs):
    return setting.COMPASS_SUPPORTED_DNS_SERVERS


def dns_servers_options(name, **kwargs):
    return setting.COMPASS_SUPPORTED_DNS_SERVERS


def default_domain(name, **kwargs):
    if setting.COMPASS_SUPPORTED_DOMAINS:
        return setting.COMPASS_SUPPORTED_DOMAINS[0]
    else:
        return None


def domain_options(name, **kwargs):
    return setting.COMPASS_SUPPORTED_DOMAINS


def default_search_path(name, **kwargs):
    return setting.COMPASS_SUPPORTED_DOMAINS


def search_path_options(name, **kwargs):
    return setting.COMPASS_SUPPORTED_DOMAINS


def default_gateway(name, **kwargs):
    return setting.COMPASS_SUPPORTED_DEFAULT_GATEWAY


def default_gateway_options(name, **kwargs):
    return [setting.COMPASS_SUPPORTED_DEFAULT_GATEWAY]


def default_localrepo(name, **kwargs):
    return setting.COMPASS_SUPPORTED_LOCAL_REPO


def default_localrepo_options(name, **kwargs):
    return [setting.COMPASS_SUPPORTED_LOCAL_REPO]


def autofill_callback_default(name, config, **kwargs):
    if config is None:
        if (
            'autofill_types' not in kwargs or
            not (set(kwargs['autofill_types']) & set(kwargs))
        ):
            return None
        if 'default_value' not in kwargs:
            return None
        return kwargs['default_value']
    return config


def autofill_callback_random_option(name, config, **kwargs):
    if config is None:
        if (
            'autofill_types' not in kwargs or
            not (set(kwargs['autofill_types']) & set(kwargs))
        ):
            return None
        if 'options' not in kwargs or not kwargs['options']:
            return None
        return random.choice(kwargs['options'])
    return config


def autofill_no_proxy(name, config, **kwargs):
    logging.debug(
        'autofill %s config %s by params %s',
        name, config, kwargs
    )
    if 'cluster' in kwargs:
        if config is None:
            config = []
        if 'default_value' in kwargs:
            for default_no_proxy in kwargs['default_value']:
                if default_no_proxy and default_no_proxy not in config:
                    config.append(default_no_proxy)
        cluster = kwargs['cluster']
        for clusterhost in cluster.clusterhosts:
            host = clusterhost.host
            hostname = host.name
            if hostname not in config:
                config.append(hostname)
            for host_network in host.host_networks:
                if host_network.is_mgmt:
                    ip = host_network.ip
                    if ip not in config:
                        config.append(ip)
    if not config:
        return config
    return [no_proxy for no_proxy in config if no_proxy]


def autofill_network_mapping(name, config, **kwargs):
    logging.debug(
        'autofill %s config %s by params %s',
        name, config, kwargs
    )
    if not config:
        return config
    if isinstance(config, basestring):
        config = {
            'interface': config,
            'subnet': None
        }
    if not isinstance(config, dict):
        return config
    if 'interface' not in config:
        return config
    subnet = None
    interface = config['interface']
    if 'cluster' in kwargs:
        cluster = kwargs['cluster']
        for clusterhost in cluster.clusterhosts:
            host = clusterhost.host
            for host_network in host.host_networks:
                if host_network.interface == interface:
                    subnet = host_network.subnet.subnet
    elif 'clusterhost' in kwargs:
        clusterhost = kwargs['clusterhost']
        host = clusterhost.host
        for host_network in host.host_networks:
            if host_network.interface == interface:
                subnet = host_network.subnet.subnet
    if not subnet:
        raise exception.InvalidParameter(
            'interface %s not found in host(s)' % interface
        )
    if 'subnet' not in config or not config['subnet']:
        config['subnet'] = subnet
    else:
        if config['subnet'] != subnet:
            raise exception.InvalidParameter(
                'subnet %s in config is not equal to subnet %s in hosts' % (
                    config['subnet'], subnet
                )
            )
    return config