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