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