-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
67 lines (51 loc) · 1.63 KB
/
util.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
__author__ = 'xsank'
import sys
import json
import time
import urllib
import httplib
import base64
from optparse import OptionParser
MAIN_HOST = "10.154.156.150"
PORT = 8888
USER = "root"
PASSWORD = "root"
old_container_names = set()
new_container_names = set()
name_ips = {}
name_hosts={}
def get_container_names(cluster_name):
_, data = http_request(
MAIN_HOST, PORT, user=(
USER, PASSWORD), method='GET', path='/containerCluster/createResult/%s' % cluster_name)
obj = json.loads(data)
if obj['response']['code'] == '000000':
for container in obj["response"]["containers"]:
name_ips[container["containerName"]] = container["ipAddr"]
name_hosts[container["containerName"]] = container["hostIp"]
assert name_ips and name_hosts
return name_ips
def get_container_host(container_name):
return name_hosts.get(container_name,'')
def http_request(host='127.0.0.1', port=80, user=(),
method='GET', path='/', data={}):
params = urllib.urlencode(data)
headers = {
"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain",
}
if user:
encode_user = base64.encodestring("%s:%s" % (user[0], user[1]))[:-1]
auth = "Basic %s" % encode_user
headers.update({"Authorization": auth})
con = httplib.HTTPConnection(host, port)
con.request(method, path, params, headers)
response = con.getresponse()
data = response.read()
con.close()
return response.status, data
def clear():
old_container_names.clear()
new_container_names.clear()
name_ips.clear()
name_hosts.clear()