This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
sdk_hosts.py
108 lines (81 loc) · 3.55 KB
/
sdk_hosts.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'''Utilities relating to mapping tasks and services to hostnames
************************************************************************
FOR THE TIME BEING WHATEVER MODIFICATIONS ARE APPLIED TO THIS FILE
SHOULD ALSO BE APPLIED TO sdk_hosts IN ANY OTHER PARTNER REPOS
************************************************************************
'''
import json
import retrying
import sdk_cmd
import sdk_utils
SYSTEM_HOST_SUFFIX = 'mesos'
AUTOIP_HOST_SUFFIX = 'autoip.dcos.thisdcos.directory'
VIP_HOST_SUFFIX = 'l4lb.thisdcos.directory'
def system_host(service_name, task_name, port=-1):
'''Returns the mesos DNS name for the host machine of a given task, with handling of foldered services.
This maps to the host IP, which may be different from the container IP if CNI is enabled.
service=marathon task=/path/to/scheduler => scheduler-to-path.marathon.mesos
service=/path/to/scheduler task=node-0 => node-0.pathtoscheduler.mesos
See also: https://dcos.io/docs/1.8/usage/service-discovery/dns-overview/'''
return _to_host(
_safe_mesos_dns_taskname(task_name),
_safe_name(service_name),
SYSTEM_HOST_SUFFIX,
port)
def autoip_host(service_name, task_name, port=-1):
'''Returns the autoip hostname for the container of a given task, with handling of foldered services.
In CNI cases, this may vary from the host of the agent system.'''
return _to_host(
_safe_name(task_name),
_safe_name(service_name),
AUTOIP_HOST_SUFFIX,
port)
def custom_host(service_name, task_name, custom_domain, port=-1):
"""
Returns a properly constructed hostname for the container of the given task using the
supplied custom domain.
"""
return _to_host(
_safe_name(task_name),
_safe_name(service_name),
custom_domain,
port)
def vip_host(service_name, vip_name, port=-1):
'''Returns the hostname of a specified service VIP, with handling of foldered services.'''
return _to_host(
_safe_name(vip_name),
_safe_name(service_name),
VIP_HOST_SUFFIX,
port)
def _safe_name(name):
'''Converts a potentially slash-delimited name to one that works for 'thisdcos.directory'
hostnames used by autoip and vips. In both cases the slashes may just be stripped out.'''
return name.replace('/', '')
def _safe_mesos_dns_taskname(task_name):
'''Converts a potentially slash-delimited task name to one that works for '.mesos' task names
Mesos DNS task names handle folders like this: /path/to/myservice => myservice-to-path'''
elems = task_name.strip('/').split('/')
elems.reverse()
return '-'.join(elems)
def _to_host(host_first, host_second, host_third, port):
host = '{}.{}.{}'.format(host_first, host_second, host_third)
if port != -1:
return '{}:{}'.format(host, port)
return host
def get_foldered_dns_name(service_name):
if sdk_utils.dcos_version_less_than('1.10'):
return service_name
return sdk_utils.get_foldered_name(service_name).replace("/", "")
@retrying.retry(
wait_fixed=2000,
stop_max_delay=5*60*1000)
def get_crypto_id_domain():
"""
Returns the cluster cryptographic ID equivalent of autoip.dcos.thisdcos.directory.
These addresses are routable within the cluster but can be used to test setting a custom
service domain.
"""
ok, lashup_response = sdk_cmd.master_ssh("curl localhost:62080/lashup/key/")
assert ok
crypto_id = json.loads(lashup_response.strip())["zbase32_public_key"]
return "autoip.dcos.{}.dcos.directory".format(crypto_id)