-
Notifications
You must be signed in to change notification settings - Fork 0
/
maestro_etcd_map.py
87 lines (84 loc) · 4.75 KB
/
maestro_etcd_map.py
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
import os
import etcd
import docker
import ast
# import maestro.guestutils as m
# TODO replace hardcoded address
etcd_host_address = os.environ['ETCD_HOST_ADDRESS']
client = etcd.Client(host=etcd_host_address, port=4001)
"""
assumes that these env variables are passed into container.
SERVICE_NAME: the friendly name of the service the container is an instance of. Note that it is possible to have multiple clusters of the same kind of application by giving them distinct friendly names.
CONTAINER_NAME: the friendly name of the instance, which is also used as the name of the container itself. This will also be the visible hostname from inside the container.
CONTAINER_HOST_ADDRESS: the external IP address of the host of the container. This can be used as the "advertised" address when services use dynamic service discovery techniques.
"""
def get_environment_name():
# returns the name of the environment as defined in the description file. Could be useful to namespace information inside ZooKeeper for example.
return 'cassandra'
def get_service_name():
# returns the friendly name of the service the container is a member of.
return os.environ['SERVICE_NAME']
def get_container_name():
# returns the friendly name of the container itself.
return os.environ['CONTAINER_NAME']
def get_container_host_address():
# returns the IP address or hostname of the host of the container. Useful if your application needs to advertise itself to some service discovery system.
return os.environ['CONTAINER_HOST_ADDRESS']
def get_container_internal_address():
# returns the IP address assigned to the container itself by Docker (its private IP address).
host_ip = get_container_host_address()
dockerclient = docker.Client(base_url = "tcp://"+host_ip+":4243",
version = '1.12',
timeout=10)
details = dockerclient.inspect_container(get_container_name())
return str(details['NetworkSettings']['IPAddress'])
def get_port(name, default = 'default_port'):
# will return the exposed (internal) port number of a given named port for the current container instance. This is useful to set configuration parameters for example.
# 'port_mapping': {'rpc': {'external': ('0.0.0.0', '9160/tcp'), 'exposed': '9160/tcp'}, 'storage': {'external': ('0.0.0.0', '7000/tcp'), 'exposed': '7000/tcp'}
if default != 'default_port':
return get_specific_exposed_port(get_service_name(), get_container_name(), name, default)
return get_specific_exposed_port(get_service_name(), get_container_name(), name)
def get_node_list(service, ports=[], minimum=1):
# It takes in a service name and an optional list of port names and returns the list of IP addresses/hostname of the containers of that service. For each port specified, in order, it will append :<port number> to each host with the external port number. For example, if you want to return the list of ZooKeeper endpoints with their client ports:
# get_node_list('zookeeper', ports=['client']) -> ['c414.ore1.domain.com:2181', 'c415.ore1.domain.com:2181']
nodes = []
all_service_instances = client.read('/' + service).value
all_service_instances = ast.literal_eval(all_service_instances)
for key in all_service_instances.keys():
instance = all_service_instances[key]
node = instance['instance_host']
portlist = ""
for port in ports:
p = get_specific_port(instance['service_name'], instance['instance_name'], port)
p = ":" + p
portlist = portlist + p
nodes.append(node + portlist)
return nodes
def get_specific_host(service, container):
# which can be used to return the hostname or IP address of a specific container from a given service, and
all_service_instances = client.read('/' + service).value
all_service_instances = ast.literal_eval(all_service_instances)
try:
return all_service_instances[container]['instance_host']
except:
return None
def get_specific_port(service, container, port, default='default'):
# to retrieve the external port number of a specific named port of a given container.
all_service_instances = client.read('/'+service).value
all_service_instances = ast.literal_eval(all_service_instances)
my_instance = all_service_instances[container]
port_mappings = my_instance['port_mapping']
port_mapping = port_mappings.get(port)
if port_mapping is None:
return default
return port_mapping['external'][1].replace('/tcp', '')
def get_specific_exposed_port(service, container, port, default='default'):
# to retrieve the exposed (internal) port number of a specific named port of a given container.
all_service_instances = client.read('/'+service).value
all_service_instances = ast.literal_eval(all_service_instances)
my_instance = all_service_instances[container]
port_mappings = my_instance['port_mapping']
port_mapping = port_mappings.get(port)
if port_mapping is None:
return default
return port_mapping['exposed'].replace('/tcp', '')