xen-drbd

annotate xen-drbd.py @ 39:263d00b75422

path to network file
author igor@manas.xt.vpn
date Mon Sep 22 15:06:26 2008 +0300 (2008-09-22)
parents 30671fe7fd79
children 26d7bae7dcde
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@0 3
igor@0 4 def the_peer_of(node):
igor@2 5 if node == node1:
igor@2 6 another_node=node2
igor@2 7 else:
igor@2 8 another_node=node1
igor@2 9 return another_node
igor@0 10
igor@0 11
igor@0 12 def log_error(error):
igor@2 13 print error
igor@0 14
igor@37 15 def run_now(command,node):
igor@2 16 if node == i_am:
igor@26 17 line=command+" > /dev/stderr 2>&1"
igor@2 18 else:
igor@26 19 line="ssh %s %s < /dev/null > /dev/stderr 2>&1" % (node,mkarg(command))
igor@26 20 if debug > 2:
igor@26 21 print "debug:", line
igor@2 22 (p, child_stdout, child_stderr) = os.popen3(line)
igor@2 23 output = child_stderr.read()
igor@2 24 #p = os.popen(line)
igor@2 25 #output = p.read()
igor@2 26 p.close()
igor@2 27 return output
igor@0 28
igor@37 29 def run(command,node):
igor@25 30 return run_now(command,node)
igor@25 31 #if node == i_am:
igor@25 32 # print command
igor@25 33 #else:
igor@25 34 # print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
igor@0 35
igor@0 36
igor@0 37 def get_drbd_resources(domain):
igor@2 38 disk=[]
igor@2 39 for disk_description in disk_table[domain]:
igor@2 40 if disk_description.find(":") == -1:
igor@2 41 disk.append(domain)
igor@2 42 else:
igor@25 43 disk.append((disk_description.split(':'))[1])
igor@2 44 return disk
igor@0 45
igor@37 46 def set_drbd_primary(domain, node):
igor@26 47 res=""
igor@2 48 drbd_resources=get_drbd_resources(domain)
igor@2 49 for drbd in drbd_resources:
igor@26 50 res += run("drbdadm primary %s"%(drbd),node)
igor@26 51 print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
igor@26 52 return res
igor@0 53
igor@37 54 def set_drbd_secondary(domain, node):
igor@26 55 res=""
igor@2 56 drbd_resources=get_drbd_resources(domain)
igor@2 57 for drbd in drbd_resources:
igor@27 58 res += run("until drbdadm secondary %s; do echo Trying to siwtch resource <%s> on the node <%s> to secondary state; sleep 5; done"%(drbd,drbd,node),node)
igor@26 59 print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
igor@26 60 return res
igor@0 61
igor@37 62 def get_domain_id(domain,node):
igor@2 63 """
igor@2 64 Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
igor@2 65 """
igor@2 66 res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
igor@2 67 if not res:
igor@2 68 res = -1
igor@2 69 else:
igor@2 70 res = int(res)
igor@2 71 return res
igor@0 72
igor@37 73 def get_drbd_id(resource,node):
igor@2 74 res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
igor@2 75 if not res:
igor@2 76 res = -1
igor@2 77 else:
igor@2 78 res = int(res)
igor@2 79 return res
igor@0 80
igor@37 81 def get_drbd_state(resource,node):
igor@2 82 res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
igor@2 83 if not res:
igor@2 84 res = -1
igor@2 85 return res
igor@0 86
igor@37 87 def get_drbd_cstate(resource,node):
igor@2 88 res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
igor@2 89 if not res:
igor@2 90 res = -1
igor@2 91 return res
igor@0 92
igor@37 93 def start_domain(domain,node):
igor@2 94 if (get_domain_id(domain,i_am) != -1):
igor@2 95 log_error("Domain %s is running already on the node %s" % (domain,i_am))
igor@2 96 return -1
igor@2 97 if (get_domain_id(domain,he_is) != -1):
igor@2 98 log_error("Domain %s is running already on the node %s" % (domain,he_is))
igor@2 99 return -1
igor@2 100 another_node=the_peer_of(node)
igor@26 101 print set_drbd_secondary(domain,another_node)
igor@26 102 print set_drbd_primary(domain,node)
igor@29 103 print run(domain_create_line % domain, node)
igor@0 104
igor@37 105 def migrate_domain_out(domain,node):
igor@2 106 if (get_domain_id(domain,node) == -1):
igor@2 107 log_error("Domain %s is not running on the node %s" % (domain,node))
igor@2 108 return -1
igor@2 109 another_node=the_peer_of(node)
igor@2 110 print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
igor@2 111 set_drbd_primary(domain,another_node)
igor@2 112 run("xm migrate %s %s --live" % (domain,another_node),node)
igor@2 113 run("sleep 2")
igor@2 114 set_drbd_secondary(domain,node)
igor@2 115 print "+ Done"
igor@2 116
igor@37 117 def migrate_domain_in(domain,node):
igor@2 118 migrate_domain_out(domain,the_peer_of(node))
igor@0 119
igor@0 120 def migrate_domain(domain,node):
igor@2 121 migrate_domain_in(domain,node)
igor@0 122
igor@37 123 def running_domains(node):
igor@2 124 xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
igor@2 125 return filter(lambda x: x in xm_domains, domains)
igor@0 126
igor@37 127 def migrate_out_all(node):
igor@2 128 for domain in running_domains(node):
igor@2 129 migrate_domain_out(domain,node)
igor@0 130
igor@37 131 def migrate_in_all(node):
igor@2 132 for domain in running_domains(the_peer_of(node)):
igor@2 133 migrate_domain_in(domain,node)
igor@0 134
igor@37 135 def start_all(node):
igor@2 136 for domain in domains:
igor@2 137 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
igor@2 138 start_domain(domain,node)
igor@0 139
igor@37 140 def start_my_domains(node):
igor@2 141 for domain in domain_home[node]:
igor@2 142 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
igor@2 143 start_domain(domain,node)
igor@0 144
igor@37 145 def migrate_my_domains_home(node):
igor@2 146 for domain in domain_home[node]:
igor@2 147 if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
igor@2 148 migrate_domain_in(domain,node)
igor@28 149
igor@37 150 def migrate_and_start_my_domains(node):
igor@28 151 migrate_my_domains_home(node)
igor@28 152 start_my_domains(node)
igor@2 153
igor@37 154 def migrate_and_start_all(node):
igor@2 155 migrate_my_domains_home(node)
igor@2 156 start_my_domains(node)
igor@28 157 migrate_my_domains_home(the_peer_of(node))
igor@28 158 start_my_domains(the_peer_of(node))
igor@2 159
igor@2 160 def do_import(name, source):
igor@2 161 module = imp.new_module(name)
igor@2 162 sys.modules[name] = module
igor@2 163 exec source in vars(module)
igor@2 164 return module
igor@2 165
igor@2 166 def load_file(file):
igor@2 167 f=open(file)
igor@2 168 result=f.read()
igor@2 169 return result
igor@2 170
igor@2 171 def dump_config(domain):
igor@2 172 xen_domain=load_file(xen_drbd_start)
igor@2 173 xen_domain="domain=\"%s\"\n" % (domain) + xen_domain
igor@2 174 xen_domain="network=\"%s\"\n" % (network) + xen_domain
igor@2 175 do_import('xen_domain_module',xen_domain)
igor@38 176 #print "domain=\"%s\"\n" % (domain)
igor@38 177 #print "network=\"%s\"\n" % (network)
igor@2 178 import xen_domain_module
igor@2 179 xen_domain_module.print_config()
igor@0 180
igor@0 181 def show_usage():
igor@2 182 print """
igor@0 183 Usage:
igor@37 184 xen-drbd [options] command [domain]
igor@0 185
igor@0 186 Commands:
igor@2 187 start domain
igor@2 188 start-all
igor@2 189 start-my-domains
igor@0 190
igor@2 191 migrate-out domain
igor@2 192 migrate-in domain
igor@2 193 migrate-all-out
igor@2 194 migrate-all-in
igor@2 195 migrate-my-domains-home
igor@28 196 migrate-and-start-my-domains
igor@2 197 migrate-and-start-all
igor@0 198
igor@2 199 dump-config domain
igor@37 200
igor@37 201 Options:
igor@37 202 --help (-h) -- show usage information
igor@37 203 --network=name (-n) -- the network descriptions is in the file name.py (default: network.py)
igor@37 204
igor@37 205
igor@2 206 """
igor@0 207
igor@0 208 def test():
igor@2 209 print "get_drbd_resources(samba)=",get_drbd_resources("samba")
igor@2 210 print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
igor@2 211 print "get_domain_id(samba)=",get_domain_id("samba")
igor@2 212 print "get_drbd_id(samba)=",get_drbd_id("samba")
igor@2 213 print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
igor@2 214 print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
igor@2 215 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
igor@2 216 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
igor@2 217 print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
igor@2 218 print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
igor@2 219 print "start_domain(samba)"
igor@2 220 start_domain("samba")
igor@2 221 print "migrate_domain_out(samba)"
igor@2 222 migrate_domain_in("samba")
igor@0 223
igor@0 224
igor@0 225 #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
igor@0 226 #sys.exit(0)
igor@0 227
igor@37 228 network='network'
igor@37 229 import sys,os,imp,getopt
igor@37 230 from commands import mkarg
igor@37 231
igor@37 232 try:
igor@37 233 opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
igor@37 234 except getopt.GetoptError, err:
igor@38 235 print str(err)
igor@37 236 usage()
igor@37 237 sys.exit(2)
igor@37 238
igor@37 239 for o, a in opts:
igor@37 240 if o in ("-h", "--help"):
igor@37 241 show_usage()
igor@37 242 sys.exit()
igor@37 243 elif o in ("-n", "--network"):
igor@37 244 network = a
igor@37 245 else:
igor@37 246 assert False, "unhandled option"
igor@37 247
igor@37 248
igor@37 249 debug=2
igor@37 250
igor@38 251 if len(args) == 2:
igor@38 252 domain=args[1]
igor@37 253
igor@37 254 sys.path.append('/etc/xen')
igor@37 255 try:
igor@39 256 execfile('/etc/xen/'+network)
igor@38 257 except IOError:
igor@38 258 print "Network configuration file <%s> not found " % network
igor@38 259 if network == 'network':
igor@38 260 print "You can use -n option to specify network filename"
igor@38 261 sys.exit(2)
igor@38 262 except NameError:
igor@38 263 domain=domains[0]
igor@38 264 execfile(network)
igor@37 265
igor@37 266 xen_drbd_start="/etc/xen/xen-drbd-start"
igor@37 267 domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
igor@37 268
igor@37 269 if len(args) == 0:
igor@2 270 show_usage()
igor@2 271 sys.exit(0)
igor@0 272
igor@37 273 he_is=the_peer_of(i_am)
igor@37 274
igor@37 275 command=args[0]
igor@37 276 if len(args) == 2:
igor@37 277 domain=args[1]
igor@2 278 if command == 'start':
igor@37 279 start_domain(domain, i_am)
igor@2 280 elif command == 'migrate-out':
igor@37 281 migrate_domain_out(domain, i_am)
igor@2 282 elif command == 'migrate-in':
igor@37 283 migrate_domain_in(domain, i_am)
igor@2 284 elif command == 'dump-config':
igor@2 285 dump_config(domain)
igor@2 286 else:
igor@2 287 show_usage()
igor@2 288 sys.exit(0)
igor@37 289 elif len(args) == 1:
igor@2 290 if command == 'start-all':
igor@37 291 start_all(i_am)
igor@2 292 elif command == 'start-my-domains':
igor@37 293 start_my_domains(i_am)
igor@31 294 elif command == 'migrate-out-all':
igor@37 295 migrate_out_all(i_am)
igor@31 296 elif command == 'migrate-in-all':
igor@37 297 migrate_in_all(i_am)
igor@2 298 elif command == 'migrate-my-domains-home':
igor@37 299 migrate_my_domains_home(i_am)
igor@28 300 elif command == 'migrate-and-start-my-domains':
igor@37 301 migrate_and_start_my_domains(i_am)
igor@2 302 elif command == 'migrate-and-start-all':
igor@37 303 migrate_and_start_all(i_am)
igor@2 304 elif command == 'list':
igor@37 305 print running_domains(i_am)
igor@2 306 else:
igor@2 307 show_usage()
igor@2 308 sys.exit(0)
igor@0 309
igor@0 310
igor@0 311
igor@0 312