-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroll_restart_node.py
153 lines (132 loc) · 4.08 KB
/
roll_restart_node.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
# coding=utf8
# usage: 修改如下参数
from config import *
##############################
role = role.upper()
##############################
import sys
import urllib
import urllib2
import urlparse
import json
import time
if host_reg:
import re
host_p = re.compile(host_reg, re.IGNORECASE)
# url: UrlParse
def build_opener(url):
if not url.username:
return urllib2.build_opener()
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "%s://%s:%d" % (url.scheme, url.hostname, url.port), url.username, url.password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
return opener
def get(url):
#print "get", url
url = urlparse.urlparse(url)
opener = build_opener(url)
url = urlparse.urlunparse((url.scheme, "%s:%d" % (url.hostname, url.port), url.path, url.params, url.query, url.fragment))
#print url
f = opener.open(url)
return f.read()
def post(url, data, headers):
#print "post", url, data, headers
url = urlparse.urlparse(url)
opener = build_opener(url)
url = urlparse.urlunparse((url.scheme, "%s:%d" % (url.hostname, url.port), url.path, url.params, url.query, url.fragment))
req = urllib2.Request(url, data, headers)
f = opener.open(req)
return f.read()
def hostId2Hostname(hostId):
#http://cnode756:7180/api/v10/hosts/60bb7200-a3e0-4315-9831-b9340210f8b1
url = "%s/hosts/%s" % (API, hostId)
host = get(url)
host = json.loads(host)
hostname = host['hostname']
return hostname
def restart(name):
#print "restart", name
url = "%s/clusters/%s/services/%s/roleCommands/restart" % (API, clusterName, serviceName)
data = "{\"items\":[\"%s\"]}" % name
headers = {"Content-Type": "application/json"}
rep = post(url, data, headers)
rep = json.loads(rep)
return rep['errors']
def info(name):
#print "info", name
url = "%s/clusters/%s/services/%s/roles/%s" % (API, clusterName, serviceName, name)
rep = get(url)
#print rep
rep = json.loads(rep)
return rep
url="http://%s:%s@%s:%d/api/version" % (user, passwd, host, port)
version = get(url)
API = "http://%s:%s@%s:%d/api/%s" % (user, passwd, host, port, version)
#url="%s/clusters" % API
#clusters = get(url)
#clusters = json.loads(clusters)
#clusterName = clusters['items'][0]['name']
#print clusterName
#clusterName = urllib.quote(clusterName)
url="%s/clusters/%s/services" % (API, clusterName)
services = get(url)
#print services
services = json.loads(services)
#serviceName = services['items'][0]['name']
#serviceName = "%s1" % service
serviceName = service
#####
url = "%s/clusters/%s/services/%s/roles" % (API, clusterName, serviceName)
#print url
roles = get(url)
#print roles
roles = json.loads(roles)
roleNames = {}
for item in roles['items']:
if item['type'] != role: continue
hostId = item['hostRef']['hostId']
roleHost = hostId2Hostname(hostId)
if host_reg:
m = host_p.match(roleHost)
if not m:
print "INFO: %s not match host reg rule, skip it" % roleHost
continue
# 启动状态并且配置不过时,总是跳过
if item['roleState'] == 'STARTED':
if ('configStale' in item and not item['configStale']) or \
('configStalenessStatus' in item and item['configStalenessStatus'] == 'FRESH'):
print "INFO: %s's config is not statle, skip it" % roleHost
continue
# 非启动状态,如果没有设定restart_all,跳过
if item['roleState'] != 'STARTED':
if not restart_all:
print "INFO: %s is not started, forget it" % roleHost
continue
roleNames[roleHost]=item['name']
first = True
for khost, kname in roleNames.items():
if just_show_host:
print "%s@%s" % (kname,khost)
continue
if first:
first = False
else:
print "Sleep ..."
time.sleep(interval)
print "restart", khost
errs = restart(kname)
if errs:
print "ERROR:", errs
break
for i in xrange(1, 100):
time.sleep(3)
krole = info(kname)
state = krole['roleState']
print "INFO:", khost, "state", state
if state == "STARTED": break
if i == 100:
print "ERROR:", "waiting restart timeout"
break
print "INFO:", "restart OK"