xen-drbd
view xen-drbd.py @ 1:d6f7c53b085d
several changes
author | eb |
---|---|
date | Thu Sep 27 23:27:58 2007 +0300 (2007-09-27) |
parents | b6dc3480caae |
children | afe3c0992ec3 |
line source
1 #!/usr/bin/python
3 network='eb'
5 import sys,os
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 domain_create_line="xm create xen-drbd-start network="+network+" domain=%s"
18 def the_peer_of(node):
19 if node == node1:
20 another_node=node2
21 else:
22 another_node=node1
23 return another_node
25 he_is=the_peer_of(i_am)
27 def log_error(error):
28 print error
30 def run_now(command,node=i_am):
31 if node == i_am:
32 line=command+" > /dev/stderr"
33 else:
34 line="ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
35 (p, child_stdout, child_stderr) = os.popen3(line)
36 output = child_stderr.read()
37 #p = os.popen(line)
38 #output = p.read()
39 p.close()
40 return output
42 def run(command,node=i_am):
43 run_now(command,node)
44 # if node == i_am:
45 # print command
46 # else:
47 # print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
50 def get_drbd_resources(domain):
51 disk=[]
52 for disk_description in disk_table[domain]:
53 if disk_description.find(":") == -1:
54 disk.append(domain)
55 else:
56 disk.append((disk_description.split(':'))[0])
57 return disk
60 def set_drbd_primary(domain, node=i_am):
61 drbd_resources=get_drbd_resources(domain)
62 for drbd in drbd_resources:
63 run("drbdadm primary %s"%(drbd),node)
65 def set_drbd_secondary(domain, node=i_am):
66 drbd_resources=get_drbd_resources(domain)
67 for drbd in drbd_resources:
68 run("drbdadm secondary %s"%(drbd),node)
70 def get_domain_id(domain,node=i_am):
71 """
72 Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
73 """
74 res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
75 if not res:
76 res = -1
77 else:
78 res = int(res)
79 return res
81 def get_drbd_id(resource,node=i_am):
82 res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
83 if not res:
84 res = -1
85 else:
86 res = int(res)
87 return res
89 def get_drbd_state(resource,node=i_am):
90 res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
91 if not res:
92 res = -1
93 return res
95 def get_drbd_cstate(resource,node=i_am):
96 res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
97 if not res:
98 res = -1
99 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 show_usage():
164 print """
165 Usage:
166 xen-drbd command [domain]
168 Commands:
169 start domain
170 start-all
171 start-my-domains
173 migrate-out domain
174 migrate-in domain
175 migrate-all-out
176 migrate-all-in
177 migrate-my-domains-home
179 migrate-and-start-all
180 """
182 def test():
183 print "get_drbd_resources(samba)=",get_drbd_resources("samba")
184 print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
185 print "get_domain_id(samba)=",get_domain_id("samba")
186 print "get_drbd_id(samba)=",get_drbd_id("samba")
187 print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
188 print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
189 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
190 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
191 print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
192 print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
193 print "start_domain(samba)"
194 start_domain("samba")
195 print "migrate_domain_out(samba)"
196 migrate_domain_in("samba")
199 #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
200 #sys.exit(0)
202 if len(sys.argv) == 1:
203 show_usage()
204 sys.exit(0)
206 command=sys.argv[1]
207 if len(sys.argv) == 3:
208 domain=sys.argv[2]
209 if command == 'start':
210 start_domain(domain)
211 elif command == 'migrate-out':
212 migrate_domain_out(domain)
213 elif command == 'migrate-in':
214 migrate_domain_in(domain)
215 else:
216 show_usage()
217 sys.exit(0)
218 elif len(sys.argv) == 2:
219 if command == 'start-all':
220 start_all()
221 elif command == 'start-my-domains':
222 start_my_domains()
223 elif command == 'migrate-all-out':
224 migrate_all_out()
225 elif command == 'migrate-all-in':
226 migrate_all_in()
227 elif command == 'migrate-my-domains-home':
228 migrate_my_domains_home()
229 elif command == 'migrate-and-start-all':
230 migrate_and_start_all()
231 elif command == 'list':
232 print running_domains()
233 else:
234 show_usage()
235 sys.exit(0)