xen-drbd

diff xen-drbd.py @ 4:0b0a886abd08

+ xen-drbd fix
author igor
date Fri Sep 28 16:32:50 2007 +0300 (2007-09-28)
parents d6f7c53b085d
children 1c7442c793a1
line diff
     1.1 --- a/xen-drbd.py	Thu Sep 27 23:27:58 2007 +0300
     1.2 +++ b/xen-drbd.py	Fri Sep 28 16:32:50 2007 +0300
     1.3 @@ -2,7 +2,7 @@
     1.4  
     1.5  network='eb'
     1.6  
     1.7 -import sys,os
     1.8 +import sys,os,imp
     1.9  from commands import mkarg
    1.10  
    1.11  
    1.12 @@ -13,226 +13,248 @@
    1.13          print "Can't find or interpret module %s with topology description" %(network)
    1.14          sys.exit(1)
    1.15  
    1.16 -domain_create_line="xm create xen-drbd-start network="+network+" domain=%s"
    1.17 +xen_drbd_start="xen-drbd-start.py"
    1.18 +domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
    1.19  
    1.20  def the_peer_of(node):
    1.21 -	if node == node1:
    1.22 -		another_node=node2
    1.23 -	else:
    1.24 -		another_node=node1
    1.25 -	return another_node
    1.26 +    if node == node1:
    1.27 +        another_node=node2
    1.28 +    else:
    1.29 +        another_node=node1
    1.30 +    return another_node
    1.31  
    1.32  he_is=the_peer_of(i_am)
    1.33  
    1.34  def log_error(error):
    1.35 -	print error
    1.36 +    print error
    1.37  
    1.38  def run_now(command,node=i_am):
    1.39 -	if node == i_am:
    1.40 -		line=command+" > /dev/stderr"
    1.41 -	else:
    1.42 -		line="ssh %s %s < /dev/null > /dev/stderr"  % (node,mkarg(command))
    1.43 -	(p, child_stdout, child_stderr) = os.popen3(line)
    1.44 -	output = child_stderr.read()
    1.45 -	#p = os.popen(line)
    1.46 -	#output = p.read()
    1.47 -	p.close()
    1.48 -	return output
    1.49 +    if node == i_am:
    1.50 +        line=command+" > /dev/stderr"
    1.51 +    else:
    1.52 +        line="ssh %s %s < /dev/null > /dev/stderr"  % (node,mkarg(command))
    1.53 +    (p, child_stdout, child_stderr) = os.popen3(line)
    1.54 +    output = child_stderr.read()
    1.55 +    #p = os.popen(line)
    1.56 +    #output = p.read()
    1.57 +    p.close()
    1.58 +    return output
    1.59  
    1.60  def run(command,node=i_am):
    1.61 -	run_now(command,node)
    1.62 -#	if node == i_am:
    1.63 -#		print command
    1.64 -#	else:
    1.65 -#		print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
    1.66 +    run_now(command,node)
    1.67 +#   if node == i_am:
    1.68 +#       print command
    1.69 +#   else:
    1.70 +#       print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
    1.71  
    1.72  
    1.73  def get_drbd_resources(domain):
    1.74 -	disk=[]
    1.75 -	for disk_description in disk_table[domain]:
    1.76 -		if disk_description.find(":") == -1:
    1.77 -			disk.append(domain)
    1.78 -		else:
    1.79 -			disk.append((disk_description.split(':'))[0])
    1.80 -	return disk
    1.81 -	
    1.82 +    disk=[]
    1.83 +    for disk_description in disk_table[domain]:
    1.84 +        if disk_description.find(":") == -1:
    1.85 +            disk.append(domain)
    1.86 +        else:
    1.87 +            disk.append((disk_description.split(':'))[0])
    1.88 +    return disk
    1.89 +    
    1.90  
    1.91  def set_drbd_primary(domain, node=i_am):
    1.92 -	drbd_resources=get_drbd_resources(domain)
    1.93 -	for drbd in drbd_resources:
    1.94 -		run("drbdadm primary %s"%(drbd),node)
    1.95 +    drbd_resources=get_drbd_resources(domain)
    1.96 +    for drbd in drbd_resources:
    1.97 +        run("drbdadm primary %s"%(drbd),node)
    1.98  
    1.99  def set_drbd_secondary(domain, node=i_am):
   1.100 -	drbd_resources=get_drbd_resources(domain)
   1.101 -	for drbd in drbd_resources:
   1.102 -		run("drbdadm secondary %s"%(drbd),node)
   1.103 +    drbd_resources=get_drbd_resources(domain)
   1.104 +    for drbd in drbd_resources:
   1.105 +        run("drbdadm secondary %s"%(drbd),node)
   1.106  
   1.107  def get_domain_id(domain,node=i_am):
   1.108 -	"""
   1.109 -	Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
   1.110 -	"""
   1.111 -	res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
   1.112 -	if not res:
   1.113 -		res = -1
   1.114 -	else:
   1.115 -		res = int(res)
   1.116 -	return res
   1.117 +    """
   1.118 +    Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
   1.119 +    """
   1.120 +    res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
   1.121 +    if not res:
   1.122 +        res = -1
   1.123 +    else:
   1.124 +        res = int(res)
   1.125 +    return res
   1.126  
   1.127  def get_drbd_id(resource,node=i_am):
   1.128 -	res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
   1.129 -	if not res:
   1.130 -		res = -1
   1.131 -	else:
   1.132 -		res = int(res)
   1.133 -	return res
   1.134 +    res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
   1.135 +    if not res:
   1.136 +        res = -1
   1.137 +    else:
   1.138 +        res = int(res)
   1.139 +    return res
   1.140  
   1.141  def get_drbd_state(resource,node=i_am):
   1.142 -	res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
   1.143 -	if not res:
   1.144 -		res = -1
   1.145 -	return res
   1.146 +    res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
   1.147 +    if not res:
   1.148 +        res = -1
   1.149 +    return res
   1.150  
   1.151  def get_drbd_cstate(resource,node=i_am):
   1.152 -	res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
   1.153 -	if not res:
   1.154 -		res = -1
   1.155 -	return res
   1.156 -
   1.157 +    res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
   1.158 +    if not res:
   1.159 +        res = -1
   1.160 +    return res
   1.161  
   1.162  def start_domain(domain,node=i_am):
   1.163 -	if (get_domain_id(domain,i_am) != -1):
   1.164 -		log_error("Domain %s is running already on the node %s" % (domain,i_am))
   1.165 -		return -1
   1.166 -	if (get_domain_id(domain,he_is) != -1):
   1.167 -		log_error("Domain %s is running already on the node %s" % (domain,he_is))
   1.168 -		return -1
   1.169 -	another_node=the_peer_of(node)
   1.170 -	set_drbd_secondary(domain,another_node)
   1.171 -	set_drbd_primary(domain,node)
   1.172 -	run(domain_create_line % domain)
   1.173 +    if (get_domain_id(domain,i_am) != -1):
   1.174 +        log_error("Domain %s is running already on the node %s" % (domain,i_am))
   1.175 +        return -1
   1.176 +    if (get_domain_id(domain,he_is) != -1):
   1.177 +        log_error("Domain %s is running already on the node %s" % (domain,he_is))
   1.178 +        return -1
   1.179 +    another_node=the_peer_of(node)
   1.180 +    set_drbd_secondary(domain,another_node)
   1.181 +    set_drbd_primary(domain,node)
   1.182 +    run(domain_create_line % domain)
   1.183  
   1.184  def migrate_domain_out(domain,node=i_am):
   1.185 -	if (get_domain_id(domain,node) == -1):
   1.186 -		log_error("Domain %s is not running on the node %s" % (domain,node))
   1.187 -		return -1
   1.188 -	another_node=the_peer_of(node)
   1.189 -	print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
   1.190 -	set_drbd_primary(domain,another_node)
   1.191 -	run("xm migrate %s %s --live" % (domain,another_node),node)
   1.192 -	run("sleep 2")
   1.193 -	set_drbd_secondary(domain,node)
   1.194 -	print "+ Done"
   1.195 -	
   1.196 +    if (get_domain_id(domain,node) == -1):
   1.197 +        log_error("Domain %s is not running on the node %s" % (domain,node))
   1.198 +        return -1
   1.199 +    another_node=the_peer_of(node)
   1.200 +    print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
   1.201 +    set_drbd_primary(domain,another_node)
   1.202 +    run("xm migrate %s %s --live" % (domain,another_node),node)
   1.203 +    run("sleep 2")
   1.204 +    set_drbd_secondary(domain,node)
   1.205 +    print "+ Done"
   1.206 +    
   1.207  def migrate_domain_in(domain,node=i_am):
   1.208 -	migrate_domain_out(domain,the_peer_of(node))
   1.209 +    migrate_domain_out(domain,the_peer_of(node))
   1.210  
   1.211  def migrate_domain(domain,node):
   1.212 -	migrate_domain_in(domain,node)
   1.213 +    migrate_domain_in(domain,node)
   1.214  
   1.215  def running_domains(node=i_am):
   1.216 -	xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
   1.217 -	return filter(lambda x: x in xm_domains, domains)
   1.218 +    xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
   1.219 +    return filter(lambda x: x in xm_domains, domains)
   1.220  
   1.221  def migrate_all_out(node=i_am):
   1.222 -	for domain in running_domains(node):
   1.223 -		migrate_domain_out(domain,node)
   1.224 +    for domain in running_domains(node):
   1.225 +        migrate_domain_out(domain,node)
   1.226  
   1.227  def migrate_all_in(node=i_am):
   1.228 -	for domain in running_domains(the_peer_of(node)):
   1.229 -		migrate_domain_in(domain,node)
   1.230 +    for domain in running_domains(the_peer_of(node)):
   1.231 +        migrate_domain_in(domain,node)
   1.232  
   1.233  def start_all(node=i_am):
   1.234 -	for domain in domains:
   1.235 -		if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   1.236 -			start_domain(domain,node)
   1.237 +    for domain in domains:
   1.238 +        if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   1.239 +            start_domain(domain,node)
   1.240  
   1.241  def start_my_domains(node=i_am):
   1.242 -	for domain in domain_home[node]:
   1.243 -		if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   1.244 -			start_domain(domain,node)
   1.245 +    for domain in domain_home[node]:
   1.246 +        if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   1.247 +            start_domain(domain,node)
   1.248  
   1.249  def migrate_my_domains_home(node=i_am):
   1.250 -	for domain in domain_home[node]:
   1.251 -		if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
   1.252 -			migrate_domain_in(domain,node)
   1.253 -	
   1.254 +    for domain in domain_home[node]:
   1.255 +        if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
   1.256 +            migrate_domain_in(domain,node)
   1.257 +    
   1.258  def migrate_and_start_all(node=i_am):
   1.259 -	migrate_my_domains_home(node)
   1.260 -	start_my_domains(node)
   1.261 +    migrate_my_domains_home(node)
   1.262 +    start_my_domains(node)
   1.263 +
   1.264 +def do_import(name, source):
   1.265 +   module = imp.new_module(name)
   1.266 +   sys.modules[name] = module
   1.267 +   exec source in vars(module)
   1.268 +   return module
   1.269 +
   1.270 +def load_file(file):
   1.271 +    f=open(file)
   1.272 +    result=f.read()
   1.273 +    return result
   1.274 +
   1.275 +def dump_config(domain):
   1.276 +    xen_domain=load_file(xen_drbd_start)
   1.277 +    xen_domain="domain=\"%s\"\n" % (domain) + xen_domain
   1.278 +    xen_domain="network=\"%s\"\n" % (network) + xen_domain
   1.279 +    do_import('xen_domain_module',xen_domain)
   1.280 +    import xen_domain_module
   1.281 +    xen_domain_module.print_config()
   1.282  
   1.283  def show_usage():
   1.284 -	print """
   1.285 +    print """
   1.286  Usage:
   1.287 -	xen-drbd command [domain]
   1.288 +    xen-drbd command [domain]
   1.289  
   1.290  Commands:
   1.291 -	start domain
   1.292 -	start-all
   1.293 -	start-my-domains
   1.294 +    start domain
   1.295 +    start-all
   1.296 +    start-my-domains
   1.297  
   1.298 -	migrate-out domain
   1.299 -	migrate-in domain
   1.300 -	migrate-all-out
   1.301 -	migrate-all-in
   1.302 -	migrate-my-domains-home
   1.303 +    migrate-out domain
   1.304 +    migrate-in domain
   1.305 +    migrate-all-out
   1.306 +    migrate-all-in
   1.307 +    migrate-my-domains-home
   1.308 +    migrate-and-start-all
   1.309  
   1.310 -	migrate-and-start-all
   1.311 -	"""
   1.312 +    dump-config domain
   1.313 +    """
   1.314  
   1.315  def test():
   1.316 -	print "get_drbd_resources(samba)=",get_drbd_resources("samba")
   1.317 -	print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
   1.318 -	print "get_domain_id(samba)=",get_domain_id("samba")
   1.319 -	print "get_drbd_id(samba)=",get_drbd_id("samba")
   1.320 -	print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
   1.321 -	print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
   1.322 -	print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
   1.323 -	print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
   1.324 -	print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
   1.325 -	print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
   1.326 -	print "start_domain(samba)"
   1.327 -	start_domain("samba")
   1.328 -	print "migrate_domain_out(samba)"
   1.329 -	migrate_domain_in("samba")
   1.330 +    print "get_drbd_resources(samba)=",get_drbd_resources("samba")
   1.331 +    print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
   1.332 +    print "get_domain_id(samba)=",get_domain_id("samba")
   1.333 +    print "get_drbd_id(samba)=",get_drbd_id("samba")
   1.334 +    print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
   1.335 +    print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
   1.336 +    print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
   1.337 +    print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
   1.338 +    print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
   1.339 +    print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
   1.340 +    print "start_domain(samba)"
   1.341 +    start_domain("samba")
   1.342 +    print "migrate_domain_out(samba)"
   1.343 +    migrate_domain_in("samba")
   1.344  
   1.345  
   1.346  #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
   1.347  #sys.exit(0)
   1.348  
   1.349  if len(sys.argv) == 1:
   1.350 -	show_usage()
   1.351 -	sys.exit(0)
   1.352 +    show_usage()
   1.353 +    sys.exit(0)
   1.354  
   1.355  command=sys.argv[1]
   1.356  if len(sys.argv) == 3:
   1.357 -	domain=sys.argv[2]
   1.358 -	if command == 'start':
   1.359 -		start_domain(domain)
   1.360 -	elif command == 'migrate-out':
   1.361 -		migrate_domain_out(domain)
   1.362 -	elif command == 'migrate-in':
   1.363 -		migrate_domain_in(domain)
   1.364 -	else:
   1.365 -		show_usage()
   1.366 -		sys.exit(0)
   1.367 +    domain=sys.argv[2]
   1.368 +    if command == 'start':
   1.369 +        start_domain(domain)
   1.370 +    elif command == 'migrate-out':
   1.371 +        migrate_domain_out(domain)
   1.372 +    elif command == 'migrate-in':
   1.373 +        migrate_domain_in(domain)
   1.374 +    elif command == 'dump-config':
   1.375 +        dump_config(domain)
   1.376 +    else:
   1.377 +        show_usage()
   1.378 +        sys.exit(0)
   1.379  elif len(sys.argv) == 2:
   1.380 -	if command == 'start-all':
   1.381 -		start_all()
   1.382 -	elif command == 'start-my-domains':
   1.383 -		start_my_domains()
   1.384 -	elif command == 'migrate-all-out':
   1.385 -		migrate_all_out()
   1.386 -	elif command == 'migrate-all-in':
   1.387 -		migrate_all_in()
   1.388 -	elif command == 'migrate-my-domains-home':
   1.389 -		migrate_my_domains_home()
   1.390 -	elif command == 'migrate-and-start-all':
   1.391 -		migrate_and_start_all()
   1.392 -	elif command == 'list':
   1.393 -		print running_domains()
   1.394 -	else:
   1.395 -		show_usage()
   1.396 -		sys.exit(0)
   1.397 +    if command == 'start-all':
   1.398 +        start_all()
   1.399 +    elif command == 'start-my-domains':
   1.400 +        start_my_domains()
   1.401 +    elif command == 'migrate-all-out':
   1.402 +        migrate_all_out()
   1.403 +    elif command == 'migrate-all-in':
   1.404 +        migrate_all_in()
   1.405 +    elif command == 'migrate-my-domains-home':
   1.406 +        migrate_my_domains_home()
   1.407 +    elif command == 'migrate-and-start-all':
   1.408 +        migrate_and_start_all()
   1.409 +    elif command == 'list':
   1.410 +        print running_domains()
   1.411 +    else:
   1.412 +        show_usage()
   1.413 +        sys.exit(0)
   1.414  
   1.415  
   1.416