xen-drbd

annotate xen-drbd.py @ 37:e80bdf496aa2

1. --network (-n) option; topology name is not hardcoded since present
2. fix for incorrect processing of the paramater spicified in form like drbd8=sda1 in xen-drbd-install
author igor@manas.xt.vpn
date Mon Sep 15 10:03:24 2008 +0300 (2008-09-15)
parents 5e499179b5ed
children 30671fe7fd79
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@2 176 import xen_domain_module
igor@2 177 xen_domain_module.print_config()
igor@0 178
igor@0 179 def show_usage():
igor@2 180 print """
igor@0 181 Usage:
igor@37 182 xen-drbd [options] command [domain]
igor@0 183
igor@0 184 Commands:
igor@2 185 start domain
igor@2 186 start-all
igor@2 187 start-my-domains
igor@0 188
igor@2 189 migrate-out domain
igor@2 190 migrate-in domain
igor@2 191 migrate-all-out
igor@2 192 migrate-all-in
igor@2 193 migrate-my-domains-home
igor@28 194 migrate-and-start-my-domains
igor@2 195 migrate-and-start-all
igor@0 196
igor@2 197 dump-config domain
igor@37 198
igor@37 199 Options:
igor@37 200 --help (-h) -- show usage information
igor@37 201 --network=name (-n) -- the network descriptions is in the file name.py (default: network.py)
igor@37 202
igor@37 203
igor@2 204 """
igor@0 205
igor@0 206 def test():
igor@2 207 print "get_drbd_resources(samba)=",get_drbd_resources("samba")
igor@2 208 print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
igor@2 209 print "get_domain_id(samba)=",get_domain_id("samba")
igor@2 210 print "get_drbd_id(samba)=",get_drbd_id("samba")
igor@2 211 print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
igor@2 212 print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
igor@2 213 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
igor@2 214 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
igor@2 215 print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
igor@2 216 print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
igor@2 217 print "start_domain(samba)"
igor@2 218 start_domain("samba")
igor@2 219 print "migrate_domain_out(samba)"
igor@2 220 migrate_domain_in("samba")
igor@0 221
igor@0 222
igor@0 223 #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
igor@0 224 #sys.exit(0)
igor@0 225
igor@37 226 network='network'
igor@37 227 import sys,os,imp,getopt
igor@37 228 from commands import mkarg
igor@37 229
igor@37 230 try:
igor@37 231 opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
igor@37 232 except getopt.GetoptError, err:
igor@37 233 # print help information and exit:
igor@37 234 print str(err) # will print something like "option -a not recognized"
igor@37 235 usage()
igor@37 236 sys.exit(2)
igor@37 237
igor@37 238 for o, a in opts:
igor@37 239 if o in ("-h", "--help"):
igor@37 240 show_usage()
igor@37 241 sys.exit()
igor@37 242 elif o in ("-n", "--network"):
igor@37 243 network = a
igor@37 244 else:
igor@37 245 assert False, "unhandled option"
igor@37 246
igor@37 247
igor@37 248 debug=2
igor@37 249
igor@37 250
igor@37 251
igor@37 252 sys.path.append('/etc/xen')
igor@37 253 try:
igor@37 254 exec 'from %s import *' % (network)
igor@37 255 except ImportError:
igor@37 256 print "Can't find or interpret module <%s> with topology description" %(network)
igor@37 257 sys.exit(1)
igor@37 258
igor@37 259 xen_drbd_start="/etc/xen/xen-drbd-start"
igor@37 260 domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
igor@37 261
igor@37 262 if len(args) == 0:
igor@2 263 show_usage()
igor@2 264 sys.exit(0)
igor@0 265
igor@37 266 he_is=the_peer_of(i_am)
igor@37 267
igor@37 268 command=args[0]
igor@37 269 if len(args) == 2:
igor@37 270 domain=args[1]
igor@2 271 if command == 'start':
igor@37 272 start_domain(domain, i_am)
igor@2 273 elif command == 'migrate-out':
igor@37 274 migrate_domain_out(domain, i_am)
igor@2 275 elif command == 'migrate-in':
igor@37 276 migrate_domain_in(domain, i_am)
igor@2 277 elif command == 'dump-config':
igor@2 278 dump_config(domain)
igor@2 279 else:
igor@2 280 show_usage()
igor@2 281 sys.exit(0)
igor@37 282 elif len(args) == 1:
igor@2 283 if command == 'start-all':
igor@37 284 start_all(i_am)
igor@2 285 elif command == 'start-my-domains':
igor@37 286 start_my_domains(i_am)
igor@31 287 elif command == 'migrate-out-all':
igor@37 288 migrate_out_all(i_am)
igor@31 289 elif command == 'migrate-in-all':
igor@37 290 migrate_in_all(i_am)
igor@2 291 elif command == 'migrate-my-domains-home':
igor@37 292 migrate_my_domains_home(i_am)
igor@28 293 elif command == 'migrate-and-start-my-domains':
igor@37 294 migrate_and_start_my_domains(i_am)
igor@2 295 elif command == 'migrate-and-start-all':
igor@37 296 migrate_and_start_all(i_am)
igor@2 297 elif command == 'list':
igor@37 298 print running_domains(i_am)
igor@2 299 else:
igor@2 300 show_usage()
igor@2 301 sys.exit(0)
igor@0 302
igor@0 303
igor@0 304
igor@0 305