xen-drbd

view xen-drbd.py @ 3:3ce02620180c

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