~ubuntu-branches/ubuntu/vivid/sahara/vivid-proposed

« back to all changes in this revision

Viewing changes to sahara/plugins/general/utils.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-09-24 16:34:46 UTC
  • Revision ID: package-import@ubuntu.com-20140924163446-8gu3zscu5e3n9lr2
Tags: upstream-2014.2~b3
ImportĀ upstreamĀ versionĀ 2014.2~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2013 Mirantis Inc.
 
2
#
 
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
 
6
#
 
7
#    http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
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
 
12
# implied.
 
13
# See the License for the specific language governing permissions and
 
14
# limitations under the License.
 
15
 
 
16
from oslo.utils import netutils
 
17
from six.moves.urllib import parse as urlparse
 
18
 
 
19
from sahara.i18n import _
 
20
from sahara.plugins.general import exceptions as ex
 
21
 
 
22
 
 
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)]
 
27
 
 
28
 
 
29
def get_instances_count(cluster, node_process=None):
 
30
    return sum([ng.count for ng in get_node_groups(cluster, node_process)])
 
31
 
 
32
 
 
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, [])
 
36
 
 
37
 
 
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
 
44
 
 
45
 
 
46
def generate_host_names(nodes):
 
47
    return "\n".join([n.hostname() for n in nodes])
 
48
 
 
49
 
 
50
def generate_fqdn_host_names(nodes):
 
51
    return "\n".join([n.fqdn() for n in nodes])
 
52
 
 
53
 
 
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
 
59
    if parse_result.port:
 
60
        return parse_result.port
 
61
    else:
 
62
        return netutils.parse_host_port(address)[1]