1
# Copyright (c) 2013 Mirantis Inc.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
16
from oslo.utils import netutils
17
from six.moves.urllib import parse as urlparse
19
from sahara.i18n import _
20
from sahara.plugins.general import exceptions as ex
23
def get_node_groups(cluster, node_process=None):
24
return [ng for ng in cluster.node_groups
25
if (node_process is None or
26
node_process in ng.node_processes)]
29
def get_instances_count(cluster, node_process=None):
30
return sum([ng.count for ng in get_node_groups(cluster, node_process)])
33
def get_instances(cluster, node_process=None):
34
nodes = get_node_groups(cluster, node_process)
35
return reduce(lambda a, b: a + b.instances, nodes, [])
38
def get_instance(cluster, node_process):
39
instances = get_instances(cluster, node_process)
40
if len(instances) > 1:
41
raise ex.InvalidComponentCountException(
42
node_process, _('0 or 1'), len(instances))
43
return instances[0] if instances else None
46
def generate_host_names(nodes):
47
return "\n".join([n.hostname() for n in nodes])
50
def generate_fqdn_host_names(nodes):
51
return "\n".join([n.fqdn() for n in nodes])
54
def get_port_from_address(address):
55
parse_result = urlparse.urlparse(address)
56
# urlparse do not parse values like 0.0.0.0:8000,
57
# netutils do not parse values like http://localhost:8000,
58
# so combine approach is using
60
return parse_result.port
62
return netutils.parse_host_port(address)[1]