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