xen-drbd
view xen-drbd.py @ 61:e005bb283f54
minor fixes for usage with xen 3.4 (HVM)
author | igor@book.xt.vpn |
---|---|
date | Thu Jun 25 15:22:25 2009 +0300 (2009-06-25) |
parents | 8afb0999f562 |
children |
line source
1 #!/usr/bin/python
4 def the_peer_of(node):
5 if node == node1:
6 another_node=node2
7 else:
8 another_node=node1
9 return another_node
12 def log_error(error):
13 print error
15 def run_now(command,node):
16 if node == i_am:
17 line=command+" > /dev/stderr 2>&1"
18 else:
19 line="ssh %s %s < /dev/null > /dev/stderr 2>&1" % (node,mkarg(command))
20 if debug > 2:
21 print "debug:", line
22 (p, child_stdout, child_stderr) = os.popen3(line)
23 output = child_stderr.read()
24 #p = os.popen(line)
25 #output = p.read()
26 p.close()
27 return output
29 def run(command,node):
30 return run_now(command,node)
31 #if node == i_am:
32 # print command
33 #else:
34 # print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
37 def get_drbd_resources(domain):
38 disk=[]
39 for disk_description in disk_table[domain]:
40 if disk_description.find(":") == -1:
41 disk.append(domain)
42 else:
43 disk.append((disk_description.split(':'))[1])
44 return disk
46 def set_drbd_primary(domain, node):
47 res=""
48 drbd_resources=get_drbd_resources(domain)
49 for drbd in drbd_resources:
50 res += run("drbdadm primary %s"%(drbd),node)
51 print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
52 return res
54 def set_drbd_secondary(domain, node):
55 res=""
56 drbd_resources=get_drbd_resources(domain)
57 for drbd in drbd_resources:
58 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)
59 print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
60 return res
62 def get_domain_id(domain,node):
63 """
64 Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
65 """
66 res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
67 if not res:
68 res = -1
69 else:
70 res = int(res)
71 return res
73 def get_drbd_id(resource,node):
74 res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
75 if not res:
76 res = -1
77 else:
78 res = int(res)
79 return res
81 def get_drbd_state(resource,node):
82 #before DRBD 8.3.0: res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
83 res=run_now("drbdadm role %s | sed s@/.*@@" %resource, node).rstrip("\n")
84 if not res:
85 res = -1
86 return res
88 def get_drbd_cstate(resource,node):
89 res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
90 if not res:
91 res = -1
92 return res
94 def start_domain(domain,node):
95 if (get_domain_id(domain,i_am) != -1):
96 log_error("Domain %s is running already on the node %s" % (domain,i_am))
97 return -1
98 if (get_domain_id(domain,he_is) != -1):
99 log_error("Domain %s is running already on the node %s" % (domain,he_is))
100 return -1
101 another_node=the_peer_of(node)
102 print set_drbd_secondary(domain,another_node)
103 print set_drbd_primary(domain,node)
104 print run(domain_create_line % domain, node)
106 def migrate_domain_out(domain,node):
107 if (get_domain_id(domain,node) == -1):
108 log_error("Domain %s is not running on the node %s" % (domain,node))
109 return -1
110 another_node=the_peer_of(node)
111 print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
112 set_drbd_primary(domain,another_node)
113 run("xm migrate %s %s --live" % (domain,another_node),node)
114 run("sleep 2", node)
115 set_drbd_secondary(domain,node)
116 print "+ Done"
118 def migrate_domain_in(domain,node):
119 migrate_domain_out(domain,the_peer_of(node))
121 def migrate_domain(domain,node):
122 migrate_domain_in(domain,node)
124 def running_domains(node):
125 xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
126 return filter(lambda x: x in xm_domains, domains)
128 def migrate_out_all(node):
129 for domain in running_domains(node):
130 migrate_domain_out(domain,node)
132 def migrate_in_all(node):
133 for domain in running_domains(the_peer_of(node)):
134 migrate_domain_in(domain,node)
136 def start_all(node):
137 for domain in domains:
138 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
139 start_domain(domain,node)
141 def start_my_domains(node):
142 for domain in domain_home[node]:
143 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
144 start_domain(domain,node)
146 def migrate_my_domains_home(node):
147 for domain in domain_home[node]:
148 if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
149 migrate_domain_in(domain,node)
151 def migrate_and_start_my_domains(node):
152 migrate_my_domains_home(node)
153 start_my_domains(node)
155 def migrate_and_start_all(node):
156 migrate_my_domains_home(node)
157 start_my_domains(node)
158 migrate_my_domains_home(the_peer_of(node))
159 start_my_domains(the_peer_of(node))
161 def do_import(name, source):
162 module = imp.new_module(name)
163 sys.modules[name] = module
164 exec source in vars(module)
165 return module
167 def load_file(file):
168 f=open(file)
169 result=f.read()
170 return result
172 def dump_config(domain):
173 xen_domain=load_file(xen_drbd_start)
174 xen_domain="domain=\"%s\"\n" % (domain) + xen_domain
175 xen_domain="network=\"%s\"\n" % (network) + xen_domain
176 do_import('xen_domain_module',xen_domain)
177 #print "domain=\"%s\"\n" % (domain)
178 #print "network=\"%s\"\n" % (network)
179 import xen_domain_module
180 xen_domain_module.print_config()
182 def show_usage():
183 print """
184 Usage:
185 xen-drbd [options] command [domain]
187 Commands:
188 start domain
189 start-all
190 start-my-domains
192 migrate-out domain
193 migrate-in domain
194 migrate-out-all
195 migrate-in-all
196 migrate-my-domains-home
197 migrate-and-start-my-domains
198 migrate-and-start-all
200 dump-config domain
202 Options:
203 --help (-h) -- show usage information
204 --network=name (-n) -- the network descriptions is in the file name (default: ''network'')
207 """
209 def test():
210 print "get_drbd_resources(samba)=",get_drbd_resources("samba")
211 print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
212 print "get_domain_id(samba)=",get_domain_id("samba")
213 print "get_drbd_id(samba)=",get_drbd_id("samba")
214 print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
215 print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
216 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
217 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
218 print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
219 print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
220 print "start_domain(samba)"
221 start_domain("samba")
222 print "migrate_domain_out(samba)"
223 migrate_domain_in("samba")
226 #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
227 #sys.exit(0)
229 network='network'
230 import sys,os,imp,getopt
231 from commands import mkarg
233 try:
234 opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
235 except getopt.GetoptError, err:
236 print str(err)
237 usage()
238 sys.exit(2)
240 for o, a in opts:
241 if o in ("-h", "--help"):
242 show_usage()
243 sys.exit()
244 elif o in ("-n", "--network"):
245 network = a
246 else:
247 assert False, "unhandled option"
250 debug=2
252 if len(args) == 2:
253 domain=args[1]
255 sys.path.append('/etc/xen')
256 try:
257 execfile('/etc/xen/'+network)
258 except IOError:
259 print "Network configuration file <%s> not found " % network
260 if network == 'network':
261 print "You can use -n option to specify network filename"
262 sys.exit(2)
263 except NameError:
264 domain=domains[0]
265 execfile('/etc/xen/'+network)
267 xen_drbd_start="/etc/xen/xen-drbd-start"
268 domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
270 if len(args) == 0:
271 show_usage()
272 sys.exit(0)
274 he_is=the_peer_of(i_am)
276 command=args[0]
277 if len(args) == 2:
278 domain=args[1]
279 if command == 'start':
280 start_domain(domain, i_am)
281 elif command == 'migrate-out':
282 migrate_domain_out(domain, i_am)
283 elif command == 'migrate-in':
284 migrate_domain_in(domain, i_am)
285 elif command == 'dump-config':
286 dump_config(domain)
287 else:
288 show_usage()
289 sys.exit(0)
290 elif len(args) == 1:
291 if command == 'start-all':
292 start_all(i_am)
293 elif command == 'start-my-domains':
294 start_my_domains(i_am)
295 elif command == 'migrate-out-all':
296 migrate_out_all(i_am)
297 elif command == 'migrate-in-all':
298 migrate_in_all(i_am)
299 elif command == 'migrate-my-domains-home':
300 migrate_my_domains_home(i_am)
301 elif command == 'migrate-and-start-my-domains':
302 migrate_and_start_my_domains(i_am)
303 elif command == 'migrate-and-start-all':
304 migrate_and_start_all(i_am)
305 elif command == 'list':
306 print running_domains(i_am)
307 else:
308 show_usage()
309 sys.exit(0)