xen-drbd
view xen-drbd.py @ 0:b6dc3480caae
xen-drbd merge
author | igor |
---|---|
date | Thu Sep 27 18:56:29 2007 +0300 (2007-09-27) |
parents | |
children | d6f7c53b085d |
line source
1 #!/usr/bin/python
3 network='eb'
5 import sys,os
6 from commands import mkarg
8 sys.path.append('/etc/xen')
9 try:
10 exec 'from %s import *' % (network)
11 except:
12 print "Can't find or interpret module %s with topology description" %(network)
13 sys.exit(1)
15 domain_create_line="xm create xen-drbd-start network="+network+" domain=%s"
17 def the_peer_of(node):
18 if node == node1:
19 another_node=node2
20 else:
21 another_node=node1
22 return another_node
24 he_is=the_peer_of(i_am)
26 def log_error(error):
27 print error
29 def run_now(command,node=i_am):
30 if node == i_am:
31 line=command+" > /dev/stderr"
32 else:
33 line="ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
34 (p, child_stdout, child_stderr) = os.popen3(line)
35 output = child_stderr.read()
36 #p = os.popen(line)
37 #output = p.read()
38 p.close()
39 return output
41 def run(command,node=i_am):
42 run_now(command,node)
43 # if node == i_am:
44 # print command
45 # else:
46 # print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
49 def get_drbd_resources(domain):
50 disk=[]
51 for disk_description in disk_table[domain]:
52 if disk_description.find(":") == -1:
53 disk.append(domain)
54 else:
55 disk.append((disk_description.split(':'))[0])
56 return disk
59 def set_drbd_primary(domain, node=i_am):
60 drbd_resources=get_drbd_resources(domain)
61 for drbd in drbd_resources:
62 run("drbdadm primary %s"%(drbd),node)
64 def set_drbd_secondary(domain, node=i_am):
65 drbd_resources=get_drbd_resources(domain)
66 for drbd in drbd_resources:
67 run("drbdadm secondary %s"%(drbd),node)
69 def get_domain_id(domain,node=i_am):
70 """
71 Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
72 """
73 res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
74 if not res:
75 res = -1
76 else:
77 res = int(res)
78 return res
80 def get_drbd_id(resource,node=i_am):
81 res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
82 if not res:
83 res = -1
84 else:
85 res = int(res)
86 return res
88 def get_drbd_state(resource,node=i_am):
89 res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
90 if not res:
91 res = -1
92 return res
94 def get_drbd_cstate(resource,node=i_am):
95 res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
96 if not res:
97 res = -1
98 return res
101 def start_domain(domain,node=i_am):
102 if (get_domain_id(domain,i_am) != -1):
103 log_error("Domain %s is running already on the node %s" % (domain,i_am))
104 return -1
105 if (get_domain_id(domain,he_is) != -1):
106 log_error("Domain %s is running already on the node %s" % (domain,he_is))
107 return -1
108 another_node=the_peer_of(node)
109 set_drbd_secondary(domain,another_node)
110 set_drbd_primary(domain,node)
111 run(domain_create_line % domain)
113 def migrate_domain_out(domain,node=i_am):
114 if (get_domain_id(domain,node) == -1):
115 log_error("Domain %s is not running on the node %s" % (domain,node))
116 return -1
117 another_node=the_peer_of(node)
118 print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
119 set_drbd_primary(domain,another_node)
120 run("xm migrate %s %s --live" % (domain,another_node),node)
121 run("sleep 2")
122 set_drbd_secondary(domain,node)
123 print "+ Done"
125 def migrate_domain_in(domain,node=i_am):
126 migrate_domain_out(domain,the_peer_of(node))
128 def migrate_domain(domain,node):
129 migrate_domain_in(domain,node)
131 def running_domains(node=i_am):
132 xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
133 return filter(lambda x: x in xm_domains, domains)
135 def migrate_all_out(node=i_am):
136 for domain in running_domains(node):
137 migrate_domain_out(domain,node)
139 def migrate_all_in(node=i_am):
140 for domain in running_domains(the_peer_of(node)):
141 migrate_domain_in(domain,node)
143 def start_all(node=i_am):
144 for domain in domains:
145 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
146 start_domain(domain,node)
148 def start_my_domains(node=i_am):
149 for domain in domain_home[node]:
150 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
151 start_domain(domain,node)
153 def migrate_my_domains_home(node=i_am):
154 for domain in domain_home[node]:
155 if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
156 migrate_domain_in(domain,node)
158 def migrate_and_start_all(node=i_am):
159 migrate_my_domains_home(node)
160 start_my_domains(node)
162 def show_usage():
163 print """
164 Usage:
165 xen-drbd command [domain]
167 Commands:
168 start domain
169 start-all
170 start-my-domains
172 migrate-out domain
173 migrate-in domain
174 migrate-all-out
175 migrate-all-in
176 migrate-my-domains-home
178 migrate-and-start-all
179 """
181 def test():
182 print "get_drbd_resources(samba)=",get_drbd_resources("samba")
183 print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
184 print "get_domain_id(samba)=",get_domain_id("samba")
185 print "get_drbd_id(samba)=",get_drbd_id("samba")
186 print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
187 print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
188 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
189 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
190 print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
191 print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
192 print "start_domain(samba)"
193 start_domain("samba")
194 print "migrate_domain_out(samba)"
195 migrate_domain_in("samba")
198 #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
199 #sys.exit(0)
201 if len(sys.argv) == 1:
202 show_usage()
203 sys.exit(0)
205 command=sys.argv[1]
206 if len(sys.argv) == 3:
207 domain=sys.argv[2]
208 if command == 'start':
209 start_domain(domain)
210 elif command == 'migrate-out':
211 migrate_domain_out(domain)
212 elif command == 'migrate-in':
213 migrate_domain_in(domain)
214 else:
215 show_usage()
216 sys.exit(0)
217 elif len(sys.argv) == 2:
218 if command == 'start-all':
219 start_all()
220 elif command == 'start-my-domains':
221 start_my_domains()
222 elif command == 'migrate-all-out':
223 migrate_all_out()
224 elif command == 'migrate-all-in':
225 migrate_all_in()
226 elif command == 'migrate-my-domains-home':
227 migrate_my_domains_home()
228 elif command == 'migrate-and-start-all':
229 migrate_and_start_all()
230 elif command == 'list':
231 print running_domains()
232 else:
233 show_usage()
234 sys.exit(0)