xen-drbd

changeset 2:afe3c0992ec3

fixes related to topology file format
author igor
date Fri Sep 28 15:39:51 2007 +0300 (2007-09-28)
parents d6f7c53b085d
children 3ce02620180c
files xen-drbd-install.py xen-drbd.py
line diff
     1.1 --- a/xen-drbd-install.py	Thu Sep 27 23:27:58 2007 +0300
     1.2 +++ b/xen-drbd-install.py	Fri Sep 28 15:39:51 2007 +0300
     1.3 @@ -4,139 +4,151 @@
     1.4  
     1.5  network=sys.argv[1]
     1.6  try:
     1.7 -	exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \
     1.8 -		  ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \
     1.9 -		  debian_release, debian_mirror, \
    1.10 -		  bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network)
    1.11 +    exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \
    1.12 +        ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \
    1.13 +        debian_release, debian_mirror, \
    1.14 +        bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network)
    1.15  except:
    1.16 -	print "Can't find or interpret module %s with topology description" %(network)
    1.17 -	sys.exit(1)
    1.18 +    print "Can't find or interpret module %s with topology description" %(network)
    1.19 +    sys.exit(1)
    1.20  
    1.21  drbd_base_port=7790
    1.22  meta_disk='/dev/%s/meta'%lvm_vg_name
    1.23 -drbd_devices=[]
    1.24 +drbd_device={}
    1.25  
    1.26 -def make_lvm(do_print=1):
    1.27 -		if do_print:
    1.28 -				print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name)
    1.29 -		for dom in domains:
    1.30 -			for disk in disk_table[dom]:
    1.31 -				if disk.find(":") == -1:
    1.32 -					if do_print:
    1.33 -						print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
    1.34 -					drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
    1.35 -				else:
    1.36 -					if do_print:
    1.37 -						print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
    1.38 -					drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
    1.39 +def make_drbd_hash():
    1.40 +    for dom in domains:
    1.41 +        for disk in disk_table[dom]:
    1.42 +            if disk.find(":") == -1:
    1.43 +                drbd_device[disk]=dom
    1.44 +            else:
    1.45 +                drbd_device[disk.split(':')[0]]=disk.split(':')[1]
    1.46 +
    1.47 +def make_lvm():
    1.48 +    if do_print:
    1.49 +        print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name)
    1.50 +    for dom in domains:
    1.51 +        for disk in disk_table[dom]:
    1.52 +            if disk.find(":") == -1:
    1.53 +                #if do_print:
    1.54 +                #    print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
    1.55 +                #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
    1.56 +                drbd_device[disk]=dom
    1.57 +            else:
    1.58 +                drbd_device[disk.split(':')[0]]=disk.split(':')[0]
    1.59 +                if do_print:
    1.60 +                    print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
    1.61 +                drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
    1.62  
    1.63  
    1.64  def make_drbdconf():
    1.65 -		print "cat <<DRBD > /etc/drbd.conf"
    1.66 -		i=0
    1.67 -		for drbd in drbd_devices:
    1.68 -			resource=drbd[0]
    1.69 -			disk=drbd[1]
    1.70 -			drbd_number=i
    1.71 -			i+=1
    1.72 -			print """
    1.73 +    print "cat <<DRBD > /etc/drbd.conf"
    1.74 +    i=0
    1.75 +    for drbd in drbd_devices:
    1.76 +        resource=drbd[0]
    1.77 +        disk=drbd[1]
    1.78 +        drbd_number=i
    1.79 +        i+=1
    1.80 +        print """
    1.81  resource %s {
    1.82 -	protocol C;
    1.83 -	net { 
    1.84 -		allow-two-primaries;
    1.85 -	 	after-sb-0pri discard-least-changes;
    1.86 -	  	after-sb-1pri call-pri-lost-after-sb;
    1.87 -	   	after-sb-2pri call-pri-lost-after-sb;
    1.88 -	}
    1.89 -	syncer { 
    1.90 -		rate 5M; 
    1.91 -	}
    1.92 -	on %s 
    1.93 -	{
    1.94 -		device /dev/drbd%s;
    1.95 -  		disk %s;
    1.96 -		address %s:%s;
    1.97 -	    meta-disk %s[%s];
    1.98 -	}
    1.99 -	on %s 
   1.100 -	{
   1.101 -		device /dev/drbd%s;
   1.102 -  		disk %s;
   1.103 -		address %s:%s;
   1.104 -	    meta-disk %s[%s];
   1.105 -	}
   1.106 +    protocol C;
   1.107 +    net { 
   1.108 +        allow-two-primaries;
   1.109 +        after-sb-0pri discard-least-changes;
   1.110 +        after-sb-1pri call-pri-lost-after-sb;
   1.111 +        after-sb-2pri call-pri-lost-after-sb;
   1.112 +    }
   1.113 +    syncer { 
   1.114 +        rate 5M; 
   1.115 +    }
   1.116 +    on %s 
   1.117 +    {
   1.118 +        device /dev/drbd%s;
   1.119 +        disk %s;
   1.120 +        address %s:%s;
   1.121 +        meta-disk %s[%s];
   1.122 +    }
   1.123 +    on %s 
   1.124 +    {
   1.125 +        device /dev/drbd%s;
   1.126 +        disk %s;
   1.127 +        address %s:%s;
   1.128 +        meta-disk %s[%s];
   1.129 +    }
   1.130  }
   1.131  
   1.132  """ % (resource, node1, drbd_number, disk, node1_ip, i+drbd_base_port, meta_disk, drbd_number, node2, drbd_number, disk, node2_ip, i+drbd_base_port, meta_disk, drbd_number )
   1.133 -		print "DRBD"
   1.134 +        print "DRBD"
   1.135  
   1.136  
   1.137 -def make_drbd_links():
   1.138 -	make_lvm(0)
   1.139 -	print "mkdir -p /dev/drbd"
   1.140 -	i=0
   1.141 -	for drbd in drbd_devices:
   1.142 -		drbd_number=i
   1.143 -		i+=1
   1.144 -		resource=drbd[0]
   1.145 -		disk=drbd[1]
   1.146 -		print "ln -sf /dev/drbd%s /dev/drbd/%s"	%(drbd_number, resource)
   1.147 +def make_links():
   1.148 +
   1.149 +
   1.150 +    # drbd_devices={
   1.151 +    #   'dev' : 'resource',
   1.152 +    # }
   1.153 +
   1.154 +    make_drbd_hash()
   1.155 +    print "mkdir -p /dev/drbd"
   1.156 +    for dev in drbd_device.keys():
   1.157 +        resource=drbd_device[dev]
   1.158 +        print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
   1.159  
   1.160  def make_fs():
   1.161 -	for dom in domains:
   1.162 -		print "mkfs.ext3 /dev/drbd/"+dom
   1.163 +    for dom in domains:
   1.164 +        print "mkfs.ext3 /dev/drbd/"+dom
   1.165  
   1.166  
   1.167  def make_mount():
   1.168 -	print "mkdir -p /domU"
   1.169 -	for dom in domains:
   1.170 -		print "mkdir -p /domU/%s" % (dom)
   1.171 -		print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
   1.172 +    print "mkdir -p /domU"
   1.173 +    for dom in domains:
   1.174 +        print "mkdir -p /domU/%s" % (dom)
   1.175 +        print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
   1.176  
   1.177  def make_umount():
   1.178 -	for dom in domains:
   1.179 -		print "umount /domU/%s" % (dom)
   1.180 +    for dom in domains:
   1.181 +        print "umount /domU/%s" % (dom)
   1.182  
   1.183  def make_dns():
   1.184 -	i=1
   1.185 -	for dom in domains:
   1.186 -		print "%s A %s.%s" % (dom, ip_network, i)
   1.187 -		i+=1
   1.188 +    i=1
   1.189 +    for dom in domains:
   1.190 +        print "%s A %s.%s" % (dom, ip_network, i)
   1.191 +        i+=1
   1.192  
   1.193  def make_dns_reverse():
   1.194 -	i=1
   1.195 -	for dom in domains:
   1.196 -		print "%s A %s.%s." % (i, dom, domain_name)
   1.197 -		i+=1
   1.198 +    i=1
   1.199 +    for dom in domains:
   1.200 +        print "%s A %s.%s." % (i, dom, domain_name)
   1.201 +        i+=1
   1.202  
   1.203  def make_install_debian():
   1.204 -	print """
   1.205 +    print """
   1.206  debootstrap %s /domU/%s %s
   1.207  chroot /domU/%s apt-get install -y --force-yes %s
   1.208 -	""" % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
   1.209 +    """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
   1.210  
   1.211  def make_rsync():
   1.212 -	for dom in domains[1:]:
   1.213 -		print """
   1.214 +    for dom in domains[1:]:
   1.215 +        print """
   1.216  rsync -a /domU/%s/ /domU/%s/
   1.217 -		""" % (domains[0], dom)
   1.218 +        """ % (domains[0], dom)
   1.219  
   1.220  def make_etcfstab():
   1.221 -	i=0
   1.222 -	for dom in domains:
   1.223 -		i+=1
   1.224 -		print """
   1.225 +    i=0
   1.226 +    for dom in domains:
   1.227 +        i+=1
   1.228 +        print """
   1.229  cat <<FSTAB > /domU/%s/etc/fstab
   1.230  proc                /proc           proc    defaults                    0       0
   1.231  /dev/hda1           /               ext3    defaults,errors=remount-ro  0       1
   1.232  FSTAB
   1.233 -		""" % (dom)
   1.234 +        """ % (dom)
   1.235  
   1.236  def make_etcnetworkinterfaces():
   1.237 -	i=0
   1.238 -	for dom in domains:
   1.239 -		i+=1
   1.240 -		print """
   1.241 +    i=0
   1.242 +    for dom in domains:
   1.243 +        i+=1
   1.244 +        print """
   1.245  cat <<INTERFACES > /domU/%s/etc/network/interfaces
   1.246  auto lo eth0
   1.247  iface lo inet loopback
   1.248 @@ -150,31 +162,31 @@
   1.249      dns-nameservers %s
   1.250      dns-search %s
   1.251  INTERFACES
   1.252 -		""" % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
   1.253 +        """ % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
   1.254  
   1.255  def make_etcresolvconf():
   1.256 -	for dom in domains:
   1.257 -		print """
   1.258 +    for dom in domains:
   1.259 +        print """
   1.260  cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
   1.261  search %s
   1.262  nameserver %s
   1.263  RESOLVCONF
   1.264 -		""" % (dom,domain_name,ip_nameserver)
   1.265 +        """ % (dom,domain_name,ip_nameserver)
   1.266  
   1.267  def make_etchostname():
   1.268 -	for dom in domains:
   1.269 -		print """
   1.270 +    for dom in domains:
   1.271 +        print """
   1.272  cat <<HOSTNAME > /domU/%s/etc/hostname
   1.273  %s
   1.274  HOSTNAME
   1.275 -		""" % (dom,dom)
   1.276 +        """ % (dom,dom)
   1.277  
   1.278  
   1.279  def make_etchosts():
   1.280 -	i=0
   1.281 -	for dom in domains:
   1.282 -		i+=1
   1.283 -		print """
   1.284 +    i=0
   1.285 +    for dom in domains:
   1.286 +        i+=1
   1.287 +        print """
   1.288  cat <<HOSTS > /domU/%s/etc/hosts
   1.289  127.0.0.1       localhost
   1.290  192.168.1.%s   %s.%s  %s
   1.291 @@ -186,88 +198,100 @@
   1.292  ff02::2 ip6-allrouters
   1.293  ff02::3 ip6-allhosts
   1.294  HOSTS
   1.295 -		""" % (dom,i,dom,domain_name,dom)
   1.296 +        """ % (dom,i,dom,domain_name,dom)
   1.297  
   1.298  def make_rootssh():
   1.299 -	for dom in domains:
   1.300 -		print """
   1.301 +    for dom in domains:
   1.302 +        print """
   1.303  mkdir -p /domU/%s/root/.ssh/
   1.304  cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
   1.305  chmod 600 /domU/%s/root/.ssh/authorized_keys
   1.306 -		"""  % (dom,dom,dom)
   1.307 +        """  % (dom,dom,dom)
   1.308  
   1.309  def make_l3rc():
   1.310 -	for dom in domains:
   1.311 -		print """
   1.312 -		cat <<EOF > /domU/%s/root/.l3rc
   1.313 +    for dom in domains:
   1.314 +        print """
   1.315 +        cat <<EOF > /domU/%s/root/.l3rc
   1.316  l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
   1.317  EOF
   1.318 -		"""  % (dom,dom,domain_name)
   1.319 +        """  % (dom,dom,domain_name)
   1.320  
   1.321  
   1.322  def make_etc():
   1.323 -		make_etcfstab()
   1.324 -		make_etcnetworkinterfaces()
   1.325 -		make_etcresolvconf()
   1.326 -		make_etchosts()
   1.327 -		make_etchostname()
   1.328 -		make_rootssh()
   1.329 -		make_l3rc()
   1.330 +    make_etcfstab()
   1.331 +    make_etcnetworkinterfaces()
   1.332 +    make_etcresolvconf()
   1.333 +    make_etchosts()
   1.334 +    make_etchostname()
   1.335 +    make_rootssh()
   1.336 +    make_l3rc()
   1.337  
   1.338  def make_all_system():
   1.339 -	make_drbd_links()
   1.340 -	make_fs()
   1.341 -	make_mount()
   1.342 -	make_install_debian()
   1.343 -	make_rsync()
   1.344 -	make_etc()
   1.345 -	make_umount()
   1.346 +    make_drbd_links()
   1.347 +    make_fs()
   1.348 +    make_mount()
   1.349 +    make_install_debian()
   1.350 +    make_rsync()
   1.351 +    make_etc()
   1.352 +    make_umount()
   1.353  
   1.354  def get_management_ip():
   1.355 -	return management_ip
   1.356 +    return management_ip
   1.357  
   1.358  def get_default_gateway():
   1.359 -	return management_gw
   1.360 +    return management_gw
   1.361  
   1.362  def make_bridges():
   1.363 -	ip=get_management_ip()
   1.364 -	gw=get_default_gateway()
   1.365 -	print "ifconfig %s 0.0.0.0" % (trunk)
   1.366 -	for bridge in bridges:
   1.367 -		vlan=vlans[bridges.index(bridge)]
   1.368 -		print "vconfig add %s %s" % (trunk,vlan)
   1.369 -		print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
   1.370 -	print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
   1.371 -	if management_gw:
   1.372 -			print "route add default gw %s" % (management_gw)
   1.373 +    ip=get_management_ip()
   1.374 +    gw=get_default_gateway()
   1.375 +    print "ifconfig %s 0.0.0.0" % (trunk)
   1.376 +    
   1.377 +    # tagged interfaces
   1.378 +    for bridge in bridges:
   1.379 +        vlan=vlans[bridges.index(bridge)]
   1.380 +        if vlan == 'tagged': 
   1.381 +            print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
   1.382 +            print "ip link set %s up" % (trunk)
   1.383 +
   1.384 +    # untagged interfaces
   1.385 +    for bridge in bridges:
   1.386 +        vlan=vlans[bridges.index(bridge)]
   1.387 +        if vlan != 'tagged': 
   1.388 +            print "vconfig add %s %s" % (trunk,vlan)
   1.389 +            print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
   1.390 +            print "ip link set %s.%s up" % (trunk,vlan)
   1.391 +
   1.392 +    print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
   1.393 +    if management_gw:
   1.394 +            print "route add default gw %s" % (management_gw)
   1.395  
   1.396  
   1.397  def show_usage():
   1.398 -	print """
   1.399 +    print """
   1.400  Usage:
   1.401 -	xen-drbd-install <network> <command>  			(to view)
   1.402 -	xen-drbd-install <network> <command> | sh -s 	(to run)
   1.403 +    xen-drbd-install <network> <command>            (to view)
   1.404 +    xen-drbd-install <network> <command> | sh -s    (to run)
   1.405  
   1.406 -	<network> is a name of the file, which contains network description
   1.407 +    <network> is a name of the file, which contains network description
   1.408  
   1.409  Commands:
   1.410 -	make-all
   1.411 -	make-bridges
   1.412 -	make-links
   1.413 +    make-all
   1.414 +    make-bridges
   1.415 +    make-links
   1.416  """
   1.417  
   1.418  #make_all_system()
   1.419  
   1.420  if len(sys.argv) > 1:
   1.421 -	command = sys.argv[2]
   1.422 -	if command == 'make-all':
   1.423 -		make_all_system()
   1.424 -	if command == 'make-bridges':	
   1.425 -		make_bridges()
   1.426 -	elif command == 'make-links':	
   1.427 -		make_drbd_links()
   1.428 -	else:
   1.429 -		show_usage()
   1.430 +    command = sys.argv[2]
   1.431 +    if command == 'make-all':
   1.432 +        make_all_system()
   1.433 +    if command == 'make-bridges':   
   1.434 +        make_bridges()
   1.435 +    elif command == 'make-links':   
   1.436 +        make_links()
   1.437 +    else:
   1.438 +        show_usage()
   1.439  else:
   1.440 -	show_usage()
   1.441 +    show_usage()
   1.442  
     2.1 --- a/xen-drbd.py	Thu Sep 27 23:27:58 2007 +0300
     2.2 +++ b/xen-drbd.py	Fri Sep 28 15:39:51 2007 +0300
     2.3 @@ -2,7 +2,7 @@
     2.4  
     2.5  network='eb'
     2.6  
     2.7 -import sys,os
     2.8 +import sys,os,imp
     2.9  from commands import mkarg
    2.10  
    2.11  
    2.12 @@ -13,226 +13,248 @@
    2.13          print "Can't find or interpret module %s with topology description" %(network)
    2.14          sys.exit(1)
    2.15  
    2.16 -domain_create_line="xm create xen-drbd-start network="+network+" domain=%s"
    2.17 +xen_drbd_start="xen-drbd-start.py"
    2.18 +domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
    2.19  
    2.20  def the_peer_of(node):
    2.21 -	if node == node1:
    2.22 -		another_node=node2
    2.23 -	else:
    2.24 -		another_node=node1
    2.25 -	return another_node
    2.26 +    if node == node1:
    2.27 +        another_node=node2
    2.28 +    else:
    2.29 +        another_node=node1
    2.30 +    return another_node
    2.31  
    2.32  he_is=the_peer_of(i_am)
    2.33  
    2.34  def log_error(error):
    2.35 -	print error
    2.36 +    print error
    2.37  
    2.38  def run_now(command,node=i_am):
    2.39 -	if node == i_am:
    2.40 -		line=command+" > /dev/stderr"
    2.41 -	else:
    2.42 -		line="ssh %s %s < /dev/null > /dev/stderr"  % (node,mkarg(command))
    2.43 -	(p, child_stdout, child_stderr) = os.popen3(line)
    2.44 -	output = child_stderr.read()
    2.45 -	#p = os.popen(line)
    2.46 -	#output = p.read()
    2.47 -	p.close()
    2.48 -	return output
    2.49 +    if node == i_am:
    2.50 +        line=command+" > /dev/stderr"
    2.51 +    else:
    2.52 +        line="ssh %s %s < /dev/null > /dev/stderr"  % (node,mkarg(command))
    2.53 +    (p, child_stdout, child_stderr) = os.popen3(line)
    2.54 +    output = child_stderr.read()
    2.55 +    #p = os.popen(line)
    2.56 +    #output = p.read()
    2.57 +    p.close()
    2.58 +    return output
    2.59  
    2.60  def run(command,node=i_am):
    2.61 -	run_now(command,node)
    2.62 -#	if node == i_am:
    2.63 -#		print command
    2.64 -#	else:
    2.65 -#		print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
    2.66 +    run_now(command,node)
    2.67 +#   if node == i_am:
    2.68 +#       print command
    2.69 +#   else:
    2.70 +#       print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
    2.71  
    2.72  
    2.73  def get_drbd_resources(domain):
    2.74 -	disk=[]
    2.75 -	for disk_description in disk_table[domain]:
    2.76 -		if disk_description.find(":") == -1:
    2.77 -			disk.append(domain)
    2.78 -		else:
    2.79 -			disk.append((disk_description.split(':'))[0])
    2.80 -	return disk
    2.81 -	
    2.82 +    disk=[]
    2.83 +    for disk_description in disk_table[domain]:
    2.84 +        if disk_description.find(":") == -1:
    2.85 +            disk.append(domain)
    2.86 +        else:
    2.87 +            disk.append((disk_description.split(':'))[0])
    2.88 +    return disk
    2.89 +    
    2.90  
    2.91  def set_drbd_primary(domain, node=i_am):
    2.92 -	drbd_resources=get_drbd_resources(domain)
    2.93 -	for drbd in drbd_resources:
    2.94 -		run("drbdadm primary %s"%(drbd),node)
    2.95 +    drbd_resources=get_drbd_resources(domain)
    2.96 +    for drbd in drbd_resources:
    2.97 +        run("drbdadm primary %s"%(drbd),node)
    2.98  
    2.99  def set_drbd_secondary(domain, node=i_am):
   2.100 -	drbd_resources=get_drbd_resources(domain)
   2.101 -	for drbd in drbd_resources:
   2.102 -		run("drbdadm secondary %s"%(drbd),node)
   2.103 +    drbd_resources=get_drbd_resources(domain)
   2.104 +    for drbd in drbd_resources:
   2.105 +        run("drbdadm secondary %s"%(drbd),node)
   2.106  
   2.107  def get_domain_id(domain,node=i_am):
   2.108 -	"""
   2.109 -	Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
   2.110 -	"""
   2.111 -	res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
   2.112 -	if not res:
   2.113 -		res = -1
   2.114 -	else:
   2.115 -		res = int(res)
   2.116 -	return res
   2.117 +    """
   2.118 +    Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
   2.119 +    """
   2.120 +    res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
   2.121 +    if not res:
   2.122 +        res = -1
   2.123 +    else:
   2.124 +        res = int(res)
   2.125 +    return res
   2.126  
   2.127  def get_drbd_id(resource,node=i_am):
   2.128 -	res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
   2.129 -	if not res:
   2.130 -		res = -1
   2.131 -	else:
   2.132 -		res = int(res)
   2.133 -	return res
   2.134 +    res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
   2.135 +    if not res:
   2.136 +        res = -1
   2.137 +    else:
   2.138 +        res = int(res)
   2.139 +    return res
   2.140  
   2.141  def get_drbd_state(resource,node=i_am):
   2.142 -	res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
   2.143 -	if not res:
   2.144 -		res = -1
   2.145 -	return res
   2.146 +    res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
   2.147 +    if not res:
   2.148 +        res = -1
   2.149 +    return res
   2.150  
   2.151  def get_drbd_cstate(resource,node=i_am):
   2.152 -	res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
   2.153 -	if not res:
   2.154 -		res = -1
   2.155 -	return res
   2.156 -
   2.157 +    res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
   2.158 +    if not res:
   2.159 +        res = -1
   2.160 +    return res
   2.161  
   2.162  def start_domain(domain,node=i_am):
   2.163 -	if (get_domain_id(domain,i_am) != -1):
   2.164 -		log_error("Domain %s is running already on the node %s" % (domain,i_am))
   2.165 -		return -1
   2.166 -	if (get_domain_id(domain,he_is) != -1):
   2.167 -		log_error("Domain %s is running already on the node %s" % (domain,he_is))
   2.168 -		return -1
   2.169 -	another_node=the_peer_of(node)
   2.170 -	set_drbd_secondary(domain,another_node)
   2.171 -	set_drbd_primary(domain,node)
   2.172 -	run(domain_create_line % domain)
   2.173 +    if (get_domain_id(domain,i_am) != -1):
   2.174 +        log_error("Domain %s is running already on the node %s" % (domain,i_am))
   2.175 +        return -1
   2.176 +    if (get_domain_id(domain,he_is) != -1):
   2.177 +        log_error("Domain %s is running already on the node %s" % (domain,he_is))
   2.178 +        return -1
   2.179 +    another_node=the_peer_of(node)
   2.180 +    set_drbd_secondary(domain,another_node)
   2.181 +    set_drbd_primary(domain,node)
   2.182 +    run(domain_create_line % domain)
   2.183  
   2.184  def migrate_domain_out(domain,node=i_am):
   2.185 -	if (get_domain_id(domain,node) == -1):
   2.186 -		log_error("Domain %s is not running on the node %s" % (domain,node))
   2.187 -		return -1
   2.188 -	another_node=the_peer_of(node)
   2.189 -	print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
   2.190 -	set_drbd_primary(domain,another_node)
   2.191 -	run("xm migrate %s %s --live" % (domain,another_node),node)
   2.192 -	run("sleep 2")
   2.193 -	set_drbd_secondary(domain,node)
   2.194 -	print "+ Done"
   2.195 -	
   2.196 +    if (get_domain_id(domain,node) == -1):
   2.197 +        log_error("Domain %s is not running on the node %s" % (domain,node))
   2.198 +        return -1
   2.199 +    another_node=the_peer_of(node)
   2.200 +    print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
   2.201 +    set_drbd_primary(domain,another_node)
   2.202 +    run("xm migrate %s %s --live" % (domain,another_node),node)
   2.203 +    run("sleep 2")
   2.204 +    set_drbd_secondary(domain,node)
   2.205 +    print "+ Done"
   2.206 +    
   2.207  def migrate_domain_in(domain,node=i_am):
   2.208 -	migrate_domain_out(domain,the_peer_of(node))
   2.209 +    migrate_domain_out(domain,the_peer_of(node))
   2.210  
   2.211  def migrate_domain(domain,node):
   2.212 -	migrate_domain_in(domain,node)
   2.213 +    migrate_domain_in(domain,node)
   2.214  
   2.215  def running_domains(node=i_am):
   2.216 -	xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
   2.217 -	return filter(lambda x: x in xm_domains, domains)
   2.218 +    xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
   2.219 +    return filter(lambda x: x in xm_domains, domains)
   2.220  
   2.221  def migrate_all_out(node=i_am):
   2.222 -	for domain in running_domains(node):
   2.223 -		migrate_domain_out(domain,node)
   2.224 +    for domain in running_domains(node):
   2.225 +        migrate_domain_out(domain,node)
   2.226  
   2.227  def migrate_all_in(node=i_am):
   2.228 -	for domain in running_domains(the_peer_of(node)):
   2.229 -		migrate_domain_in(domain,node)
   2.230 +    for domain in running_domains(the_peer_of(node)):
   2.231 +        migrate_domain_in(domain,node)
   2.232  
   2.233  def start_all(node=i_am):
   2.234 -	for domain in domains:
   2.235 -		if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   2.236 -			start_domain(domain,node)
   2.237 +    for domain in domains:
   2.238 +        if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   2.239 +            start_domain(domain,node)
   2.240  
   2.241  def start_my_domains(node=i_am):
   2.242 -	for domain in domain_home[node]:
   2.243 -		if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   2.244 -			start_domain(domain,node)
   2.245 +    for domain in domain_home[node]:
   2.246 +        if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   2.247 +            start_domain(domain,node)
   2.248  
   2.249  def migrate_my_domains_home(node=i_am):
   2.250 -	for domain in domain_home[node]:
   2.251 -		if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
   2.252 -			migrate_domain_in(domain,node)
   2.253 -	
   2.254 +    for domain in domain_home[node]:
   2.255 +        if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
   2.256 +            migrate_domain_in(domain,node)
   2.257 +    
   2.258  def migrate_and_start_all(node=i_am):
   2.259 -	migrate_my_domains_home(node)
   2.260 -	start_my_domains(node)
   2.261 +    migrate_my_domains_home(node)
   2.262 +    start_my_domains(node)
   2.263 +
   2.264 +def do_import(name, source):
   2.265 +   module = imp.new_module(name)
   2.266 +   sys.modules[name] = module
   2.267 +   exec source in vars(module)
   2.268 +   return module
   2.269 +
   2.270 +def load_file(file):
   2.271 +    f=open(file)
   2.272 +    result=f.read()
   2.273 +    return result
   2.274 +
   2.275 +def dump_config(domain):
   2.276 +    xen_domain=load_file(xen_drbd_start)
   2.277 +    xen_domain="domain=\"%s\"\n" % (domain) + xen_domain
   2.278 +    xen_domain="network=\"%s\"\n" % (network) + xen_domain
   2.279 +    do_import('xen_domain_module',xen_domain)
   2.280 +    import xen_domain_module
   2.281 +    xen_domain_module.print_config()
   2.282  
   2.283  def show_usage():
   2.284 -	print """
   2.285 +    print """
   2.286  Usage:
   2.287 -	xen-drbd command [domain]
   2.288 +    xen-drbd command [domain]
   2.289  
   2.290  Commands:
   2.291 -	start domain
   2.292 -	start-all
   2.293 -	start-my-domains
   2.294 +    start domain
   2.295 +    start-all
   2.296 +    start-my-domains
   2.297  
   2.298 -	migrate-out domain
   2.299 -	migrate-in domain
   2.300 -	migrate-all-out
   2.301 -	migrate-all-in
   2.302 -	migrate-my-domains-home
   2.303 +    migrate-out domain
   2.304 +    migrate-in domain
   2.305 +    migrate-all-out
   2.306 +    migrate-all-in
   2.307 +    migrate-my-domains-home
   2.308 +    migrate-and-start-all
   2.309  
   2.310 -	migrate-and-start-all
   2.311 -	"""
   2.312 +    dump-config domain
   2.313 +    """
   2.314  
   2.315  def test():
   2.316 -	print "get_drbd_resources(samba)=",get_drbd_resources("samba")
   2.317 -	print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
   2.318 -	print "get_domain_id(samba)=",get_domain_id("samba")
   2.319 -	print "get_drbd_id(samba)=",get_drbd_id("samba")
   2.320 -	print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
   2.321 -	print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
   2.322 -	print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
   2.323 -	print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
   2.324 -	print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
   2.325 -	print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
   2.326 -	print "start_domain(samba)"
   2.327 -	start_domain("samba")
   2.328 -	print "migrate_domain_out(samba)"
   2.329 -	migrate_domain_in("samba")
   2.330 +    print "get_drbd_resources(samba)=",get_drbd_resources("samba")
   2.331 +    print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
   2.332 +    print "get_domain_id(samba)=",get_domain_id("samba")
   2.333 +    print "get_drbd_id(samba)=",get_drbd_id("samba")
   2.334 +    print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
   2.335 +    print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
   2.336 +    print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
   2.337 +    print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
   2.338 +    print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
   2.339 +    print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
   2.340 +    print "start_domain(samba)"
   2.341 +    start_domain("samba")
   2.342 +    print "migrate_domain_out(samba)"
   2.343 +    migrate_domain_in("samba")
   2.344  
   2.345  
   2.346  #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
   2.347  #sys.exit(0)
   2.348  
   2.349  if len(sys.argv) == 1:
   2.350 -	show_usage()
   2.351 -	sys.exit(0)
   2.352 +    show_usage()
   2.353 +    sys.exit(0)
   2.354  
   2.355  command=sys.argv[1]
   2.356  if len(sys.argv) == 3:
   2.357 -	domain=sys.argv[2]
   2.358 -	if command == 'start':
   2.359 -		start_domain(domain)
   2.360 -	elif command == 'migrate-out':
   2.361 -		migrate_domain_out(domain)
   2.362 -	elif command == 'migrate-in':
   2.363 -		migrate_domain_in(domain)
   2.364 -	else:
   2.365 -		show_usage()
   2.366 -		sys.exit(0)
   2.367 +    domain=sys.argv[2]
   2.368 +    if command == 'start':
   2.369 +        start_domain(domain)
   2.370 +    elif command == 'migrate-out':
   2.371 +        migrate_domain_out(domain)
   2.372 +    elif command == 'migrate-in':
   2.373 +        migrate_domain_in(domain)
   2.374 +    elif command == 'dump-config':
   2.375 +        dump_config(domain)
   2.376 +    else:
   2.377 +        show_usage()
   2.378 +        sys.exit(0)
   2.379  elif len(sys.argv) == 2:
   2.380 -	if command == 'start-all':
   2.381 -		start_all()
   2.382 -	elif command == 'start-my-domains':
   2.383 -		start_my_domains()
   2.384 -	elif command == 'migrate-all-out':
   2.385 -		migrate_all_out()
   2.386 -	elif command == 'migrate-all-in':
   2.387 -		migrate_all_in()
   2.388 -	elif command == 'migrate-my-domains-home':
   2.389 -		migrate_my_domains_home()
   2.390 -	elif command == 'migrate-and-start-all':
   2.391 -		migrate_and_start_all()
   2.392 -	elif command == 'list':
   2.393 -		print running_domains()
   2.394 -	else:
   2.395 -		show_usage()
   2.396 -		sys.exit(0)
   2.397 +    if command == 'start-all':
   2.398 +        start_all()
   2.399 +    elif command == 'start-my-domains':
   2.400 +        start_my_domains()
   2.401 +    elif command == 'migrate-all-out':
   2.402 +        migrate_all_out()
   2.403 +    elif command == 'migrate-all-in':
   2.404 +        migrate_all_in()
   2.405 +    elif command == 'migrate-my-domains-home':
   2.406 +        migrate_my_domains_home()
   2.407 +    elif command == 'migrate-and-start-all':
   2.408 +        migrate_and_start_all()
   2.409 +    elif command == 'list':
   2.410 +        print running_domains()
   2.411 +    else:
   2.412 +        show_usage()
   2.413 +        sys.exit(0)
   2.414  
   2.415  
   2.416