rev |
line source |
igor@0
|
1 #!/usr/bin/python
|
igor@0
|
2
|
igor@23
|
3 network='od'
|
igor@26
|
4 debug=2
|
igor@0
|
5
|
igor@2
|
6 import sys,os,imp
|
igor@0
|
7 from commands import mkarg
|
igor@0
|
8
|
eb@1
|
9
|
igor@0
|
10 sys.path.append('/etc/xen')
|
igor@0
|
11 try:
|
igor@0
|
12 exec 'from %s import *' % (network)
|
igor@23
|
13 except ImportError:
|
igor@0
|
14 print "Can't find or interpret module %s with topology description" %(network)
|
igor@0
|
15 sys.exit(1)
|
igor@0
|
16
|
eb@5
|
17 xen_drbd_start="/etc/xen/xen-drbd-start"
|
igor@2
|
18 domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
|
igor@0
|
19
|
igor@0
|
20 def the_peer_of(node):
|
igor@2
|
21 if node == node1:
|
igor@2
|
22 another_node=node2
|
igor@2
|
23 else:
|
igor@2
|
24 another_node=node1
|
igor@2
|
25 return another_node
|
igor@0
|
26
|
igor@0
|
27 he_is=the_peer_of(i_am)
|
igor@0
|
28
|
igor@0
|
29 def log_error(error):
|
igor@2
|
30 print error
|
igor@0
|
31
|
igor@0
|
32 def run_now(command,node=i_am):
|
igor@2
|
33 if node == i_am:
|
igor@26
|
34 line=command+" > /dev/stderr 2>&1"
|
igor@2
|
35 else:
|
igor@26
|
36 line="ssh %s %s < /dev/null > /dev/stderr 2>&1" % (node,mkarg(command))
|
igor@26
|
37 if debug > 2:
|
igor@26
|
38 print "debug:", line
|
igor@2
|
39 (p, child_stdout, child_stderr) = os.popen3(line)
|
igor@2
|
40 output = child_stderr.read()
|
igor@2
|
41 #p = os.popen(line)
|
igor@2
|
42 #output = p.read()
|
igor@2
|
43 p.close()
|
igor@2
|
44 return output
|
igor@0
|
45
|
igor@0
|
46 def run(command,node=i_am):
|
igor@25
|
47 return run_now(command,node)
|
igor@25
|
48 #if node == i_am:
|
igor@25
|
49 # print command
|
igor@25
|
50 #else:
|
igor@25
|
51 # print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
|
igor@0
|
52
|
igor@0
|
53
|
igor@0
|
54 def get_drbd_resources(domain):
|
igor@2
|
55 disk=[]
|
igor@2
|
56 for disk_description in disk_table[domain]:
|
igor@2
|
57 if disk_description.find(":") == -1:
|
igor@2
|
58 disk.append(domain)
|
igor@2
|
59 else:
|
igor@25
|
60 disk.append((disk_description.split(':'))[1])
|
igor@2
|
61 return disk
|
igor@0
|
62
|
igor@0
|
63 def set_drbd_primary(domain, node=i_am):
|
igor@26
|
64 res=""
|
igor@2
|
65 drbd_resources=get_drbd_resources(domain)
|
igor@2
|
66 for drbd in drbd_resources:
|
igor@26
|
67 res += run("drbdadm primary %s"%(drbd),node)
|
igor@26
|
68 print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
|
igor@26
|
69 return res
|
igor@0
|
70
|
igor@0
|
71 def set_drbd_secondary(domain, node=i_am):
|
igor@26
|
72 res=""
|
igor@2
|
73 drbd_resources=get_drbd_resources(domain)
|
igor@2
|
74 for drbd in drbd_resources:
|
igor@27
|
75 res += run("until drbdadm secondary %s; do echo Trying to siwtch resource <%s> on the node <%s> to secondary state; sleep 5; done"%(drbd,drbd,node),node)
|
igor@26
|
76 print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
|
igor@26
|
77 return res
|
igor@0
|
78
|
igor@0
|
79 def get_domain_id(domain,node=i_am):
|
igor@2
|
80 """
|
igor@2
|
81 Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
|
igor@2
|
82 """
|
igor@2
|
83 res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
|
igor@2
|
84 if not res:
|
igor@2
|
85 res = -1
|
igor@2
|
86 else:
|
igor@2
|
87 res = int(res)
|
igor@2
|
88 return res
|
igor@0
|
89
|
igor@0
|
90 def get_drbd_id(resource,node=i_am):
|
igor@2
|
91 res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
|
igor@2
|
92 if not res:
|
igor@2
|
93 res = -1
|
igor@2
|
94 else:
|
igor@2
|
95 res = int(res)
|
igor@2
|
96 return res
|
igor@0
|
97
|
igor@0
|
98 def get_drbd_state(resource,node=i_am):
|
igor@2
|
99 res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
|
igor@2
|
100 if not res:
|
igor@2
|
101 res = -1
|
igor@2
|
102 return res
|
igor@0
|
103
|
igor@0
|
104 def get_drbd_cstate(resource,node=i_am):
|
igor@2
|
105 res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
|
igor@2
|
106 if not res:
|
igor@2
|
107 res = -1
|
igor@2
|
108 return res
|
igor@0
|
109
|
igor@0
|
110 def start_domain(domain,node=i_am):
|
igor@2
|
111 if (get_domain_id(domain,i_am) != -1):
|
igor@2
|
112 log_error("Domain %s is running already on the node %s" % (domain,i_am))
|
igor@2
|
113 return -1
|
igor@2
|
114 if (get_domain_id(domain,he_is) != -1):
|
igor@2
|
115 log_error("Domain %s is running already on the node %s" % (domain,he_is))
|
igor@2
|
116 return -1
|
igor@2
|
117 another_node=the_peer_of(node)
|
igor@26
|
118 print set_drbd_secondary(domain,another_node)
|
igor@26
|
119 print set_drbd_primary(domain,node)
|
igor@26
|
120 print run(domain_create_line % domain)
|
igor@0
|
121
|
igor@0
|
122 def migrate_domain_out(domain,node=i_am):
|
igor@2
|
123 if (get_domain_id(domain,node) == -1):
|
igor@2
|
124 log_error("Domain %s is not running on the node %s" % (domain,node))
|
igor@2
|
125 return -1
|
igor@2
|
126 another_node=the_peer_of(node)
|
igor@2
|
127 print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
|
igor@2
|
128 set_drbd_primary(domain,another_node)
|
igor@2
|
129 run("xm migrate %s %s --live" % (domain,another_node),node)
|
igor@2
|
130 run("sleep 2")
|
igor@2
|
131 set_drbd_secondary(domain,node)
|
igor@2
|
132 print "+ Done"
|
igor@2
|
133
|
igor@0
|
134 def migrate_domain_in(domain,node=i_am):
|
igor@2
|
135 migrate_domain_out(domain,the_peer_of(node))
|
igor@0
|
136
|
igor@0
|
137 def migrate_domain(domain,node):
|
igor@2
|
138 migrate_domain_in(domain,node)
|
igor@0
|
139
|
igor@0
|
140 def running_domains(node=i_am):
|
igor@2
|
141 xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
|
igor@2
|
142 return filter(lambda x: x in xm_domains, domains)
|
igor@0
|
143
|
igor@0
|
144 def migrate_all_out(node=i_am):
|
igor@2
|
145 for domain in running_domains(node):
|
igor@2
|
146 migrate_domain_out(domain,node)
|
igor@0
|
147
|
igor@0
|
148 def migrate_all_in(node=i_am):
|
igor@2
|
149 for domain in running_domains(the_peer_of(node)):
|
igor@2
|
150 migrate_domain_in(domain,node)
|
igor@0
|
151
|
igor@0
|
152 def start_all(node=i_am):
|
igor@2
|
153 for domain in domains:
|
igor@2
|
154 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
|
igor@2
|
155 start_domain(domain,node)
|
igor@0
|
156
|
igor@0
|
157 def start_my_domains(node=i_am):
|
igor@2
|
158 for domain in domain_home[node]:
|
igor@2
|
159 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
|
igor@2
|
160 start_domain(domain,node)
|
igor@0
|
161
|
igor@0
|
162 def migrate_my_domains_home(node=i_am):
|
igor@2
|
163 for domain in domain_home[node]:
|
igor@2
|
164 if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
|
igor@2
|
165 migrate_domain_in(domain,node)
|
igor@2
|
166
|
igor@0
|
167 def migrate_and_start_all(node=i_am):
|
igor@2
|
168 migrate_my_domains_home(node)
|
igor@2
|
169 start_my_domains(node)
|
igor@2
|
170
|
igor@2
|
171 def do_import(name, source):
|
igor@2
|
172 module = imp.new_module(name)
|
igor@2
|
173 sys.modules[name] = module
|
igor@2
|
174 exec source in vars(module)
|
igor@2
|
175 return module
|
igor@2
|
176
|
igor@2
|
177 def load_file(file):
|
igor@2
|
178 f=open(file)
|
igor@2
|
179 result=f.read()
|
igor@2
|
180 return result
|
igor@2
|
181
|
igor@2
|
182 def dump_config(domain):
|
igor@2
|
183 xen_domain=load_file(xen_drbd_start)
|
igor@2
|
184 xen_domain="domain=\"%s\"\n" % (domain) + xen_domain
|
igor@2
|
185 xen_domain="network=\"%s\"\n" % (network) + xen_domain
|
igor@2
|
186 do_import('xen_domain_module',xen_domain)
|
igor@2
|
187 import xen_domain_module
|
igor@2
|
188 xen_domain_module.print_config()
|
igor@0
|
189
|
igor@0
|
190 def show_usage():
|
igor@2
|
191 print """
|
igor@0
|
192 Usage:
|
igor@2
|
193 xen-drbd command [domain]
|
igor@0
|
194
|
igor@0
|
195 Commands:
|
igor@2
|
196 start domain
|
igor@2
|
197 start-all
|
igor@2
|
198 start-my-domains
|
igor@0
|
199
|
igor@2
|
200 migrate-out domain
|
igor@2
|
201 migrate-in domain
|
igor@2
|
202 migrate-all-out
|
igor@2
|
203 migrate-all-in
|
igor@2
|
204 migrate-my-domains-home
|
igor@2
|
205 migrate-and-start-all
|
igor@0
|
206
|
igor@2
|
207 dump-config domain
|
igor@2
|
208 """
|
igor@0
|
209
|
igor@0
|
210 def test():
|
igor@2
|
211 print "get_drbd_resources(samba)=",get_drbd_resources("samba")
|
igor@2
|
212 print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
|
igor@2
|
213 print "get_domain_id(samba)=",get_domain_id("samba")
|
igor@2
|
214 print "get_drbd_id(samba)=",get_drbd_id("samba")
|
igor@2
|
215 print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
|
igor@2
|
216 print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
|
igor@2
|
217 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
|
igor@2
|
218 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
|
igor@2
|
219 print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
|
igor@2
|
220 print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
|
igor@2
|
221 print "start_domain(samba)"
|
igor@2
|
222 start_domain("samba")
|
igor@2
|
223 print "migrate_domain_out(samba)"
|
igor@2
|
224 migrate_domain_in("samba")
|
igor@0
|
225
|
igor@0
|
226
|
igor@0
|
227 #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
|
igor@0
|
228 #sys.exit(0)
|
igor@0
|
229
|
igor@0
|
230 if len(sys.argv) == 1:
|
igor@2
|
231 show_usage()
|
igor@2
|
232 sys.exit(0)
|
igor@0
|
233
|
igor@0
|
234 command=sys.argv[1]
|
igor@0
|
235 if len(sys.argv) == 3:
|
igor@2
|
236 domain=sys.argv[2]
|
igor@2
|
237 if command == 'start':
|
igor@2
|
238 start_domain(domain)
|
igor@2
|
239 elif command == 'migrate-out':
|
igor@2
|
240 migrate_domain_out(domain)
|
igor@2
|
241 elif command == 'migrate-in':
|
igor@2
|
242 migrate_domain_in(domain)
|
igor@2
|
243 elif command == 'dump-config':
|
igor@2
|
244 dump_config(domain)
|
igor@2
|
245 else:
|
igor@2
|
246 show_usage()
|
igor@2
|
247 sys.exit(0)
|
igor@0
|
248 elif len(sys.argv) == 2:
|
igor@2
|
249 if command == 'start-all':
|
igor@2
|
250 start_all()
|
igor@2
|
251 elif command == 'start-my-domains':
|
igor@2
|
252 start_my_domains()
|
igor@2
|
253 elif command == 'migrate-all-out':
|
igor@2
|
254 migrate_all_out()
|
igor@2
|
255 elif command == 'migrate-all-in':
|
igor@2
|
256 migrate_all_in()
|
igor@2
|
257 elif command == 'migrate-my-domains-home':
|
igor@2
|
258 migrate_my_domains_home()
|
igor@2
|
259 elif command == 'migrate-and-start-all':
|
igor@2
|
260 migrate_and_start_all()
|
igor@2
|
261 elif command == 'list':
|
igor@2
|
262 print running_domains()
|
igor@2
|
263 else:
|
igor@2
|
264 show_usage()
|
igor@2
|
265 sys.exit(0)
|
igor@0
|
266
|
igor@0
|
267
|
igor@0
|
268
|
igor@0
|
269
|