xen-drbd

changeset 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 44efea4507ac
children 30671fe7fd79
files init.d-xen-drbd xen-drbd-install.py xen-drbd.py
line diff
     1.1 --- a/init.d-xen-drbd	Sat Aug 30 11:14:50 2008 +0300
     1.2 +++ b/init.d-xen-drbd	Mon Sep 15 10:03:24 2008 +0300
     1.3 @@ -1,19 +1,19 @@
     1.4  #!/bin/sh
     1.5  
     1.6  XEN_DRBD_PATH='/usr/bin'
     1.7 -TOPOLOGY_NAME='xen-drbd'
     1.8 +TOPOLOGY_NAME='network'
     1.9  START_ACTION='nothing'
    1.10  STOP_ACTION='nothing'
    1.11  [ -e /etc/default/xen-drbd ] && . /etc/default/xen-drbd
    1.12  
    1.13  case $1 in
    1.14  		start)
    1.15 -			$XEN_DRBD_PATH/xen-drbd-install ${TOPOLOGY_NAME} make-links | sh -s
    1.16 +			$XEN_DRBD_PATH/xen-drbd-install --network=${TOPOLOGY_NAME} make-links | sh -s
    1.17  			if [ -z "$START_ACTION" ] || echo $START_ACTION | grep -qix nothing 
    1.18  			then
    1.19  				true
    1.20  			else
    1.21 -				$XEN_DRBD_PATH/xen-drbd $START_ACTION
    1.22 +				$XEN_DRBD_PATH/xen-drbd --network=${TOPOLOGY_NAME} $START_ACTION
    1.23  			fi
    1.24  		;;
    1.25  		stop)
    1.26 @@ -21,7 +21,7 @@
    1.27  			then
    1.28  				true
    1.29  			else
    1.30 -				$XEN_DRBD_PATH/xen-drbd $STOP_ACTION
    1.31 +				$XEN_DRBD_PATH/xen-drbd --network=${TOPOLOGY_NAME} $STOP_ACTION
    1.32  			fi
    1.33  		;;
    1.34  		*)
     2.1 --- a/xen-drbd-install.py	Sat Aug 30 11:14:50 2008 +0300
     2.2 +++ b/xen-drbd-install.py	Mon Sep 15 10:03:24 2008 +0300
     2.3 @@ -1,13 +1,13 @@
     2.4  #!/usr/bin/python
     2.5  
     2.6 -import sys,re
     2.7 +import sys,re,getopt
     2.8  sys.path.append('/etc/xen')
     2.9  
    2.10  def show_usage():
    2.11      print """
    2.12  Usage:
    2.13 -    xen-drbd-install <network> <command>            (to view)
    2.14 -    xen-drbd-install <network> <command> | sh -s    (to run)
    2.15 +    xen-drbd-install [options] <command>            (to view)
    2.16 +    xen-drbd-install [options] <command> | sh -s    (to run)
    2.17  
    2.18      <network> is the name of the file, which contains network description
    2.19  
    2.20 @@ -16,20 +16,43 @@
    2.21      make-domains
    2.22      make-bridges
    2.23      make-links
    2.24 +
    2.25 +Options:
    2.26 +    --help         (-h)   -- show usage information
    2.27 +    --network=name (-n)   -- the network descriptions is in the file name.py (default: network.py)
    2.28  """
    2.29  
    2.30 -if len(sys.argv) != 3:
    2.31 +
    2.32 +
    2.33 +network='network'
    2.34 +try:
    2.35 +    opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
    2.36 +except getopt.GetoptError, err:
    2.37 +    # print help information and exit:
    2.38 +    print str(err) # will print something like "option -a not recognized"
    2.39 +    usage()
    2.40 +    sys.exit(2)
    2.41 +
    2.42 +for o, a in opts:
    2.43 +    if o in ("-h", "--help"):
    2.44 +        show_usage()
    2.45 +        sys.exit()
    2.46 +    elif o in ("-n", "--network"):
    2.47 +        network = a
    2.48 +    else:
    2.49 +        assert False, "unhandled option"
    2.50 +
    2.51 +if len(args) != 1:
    2.52      show_usage()
    2.53      sys.exit(1)
    2.54  
    2.55 -
    2.56 -network=sys.argv[1]
    2.57  try:
    2.58      exec 'from %s import * ' % (network)
    2.59  except ImportError:
    2.60 -    print "Can't find or interpret module %s with topology description" %(network)
    2.61 +    print "Can't find or interpret module <%s> with topology description" %(network)
    2.62      sys.exit(1)
    2.63  
    2.64 +
    2.65  drbd_base_port=7790
    2.66  meta_disk='/dev/%s/meta'%lvm_vg_name
    2.67  drbd_device={}
    2.68 @@ -83,7 +106,7 @@
    2.69  
    2.70      for dom in domains:
    2.71          for disk in disk_table[dom]:
    2.72 -            drbd_number=int(re.compile('drbd').sub('',disk.split(':')[0]))
    2.73 +            drbd_number=int(re.compile('drbd').sub('',(disk.split(':')[0]).split('=')[0] ))
    2.74              resource=disk.split(':')[1]
    2.75              disk="/dev/%s/%s" % (lvm_vg_name,resource)
    2.76  
    2.77 @@ -345,8 +368,8 @@
    2.78  
    2.79  #make_all_system()
    2.80  
    2.81 -if len(sys.argv) > 1:
    2.82 -    command = sys.argv[2]
    2.83 +if len(args) > 0:
    2.84 +    command = args[0]
    2.85      if command == 'make-drbd':
    2.86          make_drbd()
    2.87      elif command == 'make-domains':
     3.1 --- a/xen-drbd.py	Sat Aug 30 11:14:50 2008 +0300
     3.2 +++ b/xen-drbd.py	Mon Sep 15 10:03:24 2008 +0300
     3.3 @@ -1,21 +1,5 @@
     3.4  #!/usr/bin/python
     3.5  
     3.6 -network='od'
     3.7 -debug=2
     3.8 -
     3.9 -import sys,os,imp
    3.10 -from commands import mkarg
    3.11 -
    3.12 -
    3.13 -sys.path.append('/etc/xen')
    3.14 -try:
    3.15 -        exec 'from %s import *' % (network)
    3.16 -except ImportError:
    3.17 -        print "Can't find or interpret module %s with topology description" %(network)
    3.18 -        sys.exit(1)
    3.19 -
    3.20 -xen_drbd_start="/etc/xen/xen-drbd-start"
    3.21 -domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
    3.22  
    3.23  def the_peer_of(node):
    3.24      if node == node1:
    3.25 @@ -24,12 +8,11 @@
    3.26          another_node=node1
    3.27      return another_node
    3.28  
    3.29 -he_is=the_peer_of(i_am)
    3.30  
    3.31  def log_error(error):
    3.32      print error
    3.33  
    3.34 -def run_now(command,node=i_am):
    3.35 +def run_now(command,node):
    3.36      if node == i_am:
    3.37          line=command+" > /dev/stderr 2>&1"
    3.38      else:
    3.39 @@ -43,7 +26,7 @@
    3.40      p.close()
    3.41      return output
    3.42  
    3.43 -def run(command,node=i_am):
    3.44 +def run(command,node):
    3.45      return run_now(command,node)
    3.46      #if node == i_am:
    3.47      #    print command
    3.48 @@ -60,7 +43,7 @@
    3.49              disk.append((disk_description.split(':'))[1])
    3.50      return disk
    3.51  
    3.52 -def set_drbd_primary(domain, node=i_am):
    3.53 +def set_drbd_primary(domain, node):
    3.54      res=""
    3.55      drbd_resources=get_drbd_resources(domain)
    3.56      for drbd in drbd_resources:
    3.57 @@ -68,7 +51,7 @@
    3.58          print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
    3.59      return res
    3.60  
    3.61 -def set_drbd_secondary(domain, node=i_am):
    3.62 +def set_drbd_secondary(domain, node):
    3.63      res=""
    3.64      drbd_resources=get_drbd_resources(domain)
    3.65      for drbd in drbd_resources:
    3.66 @@ -76,7 +59,7 @@
    3.67          print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
    3.68      return res
    3.69  
    3.70 -def get_domain_id(domain,node=i_am):
    3.71 +def get_domain_id(domain,node):
    3.72      """
    3.73      Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
    3.74      """
    3.75 @@ -87,7 +70,7 @@
    3.76          res = int(res)
    3.77      return res
    3.78  
    3.79 -def get_drbd_id(resource,node=i_am):
    3.80 +def get_drbd_id(resource,node):
    3.81      res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
    3.82      if not res:
    3.83          res = -1
    3.84 @@ -95,19 +78,19 @@
    3.85          res = int(res)
    3.86      return res
    3.87  
    3.88 -def get_drbd_state(resource,node=i_am):
    3.89 +def get_drbd_state(resource,node):
    3.90      res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
    3.91      if not res:
    3.92          res = -1
    3.93      return res
    3.94  
    3.95 -def get_drbd_cstate(resource,node=i_am):
    3.96 +def get_drbd_cstate(resource,node):
    3.97      res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
    3.98      if not res:
    3.99          res = -1
   3.100      return res
   3.101  
   3.102 -def start_domain(domain,node=i_am):
   3.103 +def start_domain(domain,node):
   3.104      if (get_domain_id(domain,i_am) != -1):
   3.105          log_error("Domain %s is running already on the node %s" % (domain,i_am))
   3.106          return -1
   3.107 @@ -119,7 +102,7 @@
   3.108      print set_drbd_primary(domain,node)
   3.109      print run(domain_create_line % domain, node)
   3.110  
   3.111 -def migrate_domain_out(domain,node=i_am):
   3.112 +def migrate_domain_out(domain,node):
   3.113      if (get_domain_id(domain,node) == -1):
   3.114          log_error("Domain %s is not running on the node %s" % (domain,node))
   3.115          return -1
   3.116 @@ -131,44 +114,44 @@
   3.117      set_drbd_secondary(domain,node)
   3.118      print "+ Done"
   3.119      
   3.120 -def migrate_domain_in(domain,node=i_am):
   3.121 +def migrate_domain_in(domain,node):
   3.122      migrate_domain_out(domain,the_peer_of(node))
   3.123  
   3.124  def migrate_domain(domain,node):
   3.125      migrate_domain_in(domain,node)
   3.126  
   3.127 -def running_domains(node=i_am):
   3.128 +def running_domains(node):
   3.129      xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
   3.130      return filter(lambda x: x in xm_domains, domains)
   3.131  
   3.132 -def migrate_out_all(node=i_am):
   3.133 +def migrate_out_all(node):
   3.134      for domain in running_domains(node):
   3.135          migrate_domain_out(domain,node)
   3.136  
   3.137 -def migrate_in_all(node=i_am):
   3.138 +def migrate_in_all(node):
   3.139      for domain in running_domains(the_peer_of(node)):
   3.140          migrate_domain_in(domain,node)
   3.141  
   3.142 -def start_all(node=i_am):
   3.143 +def start_all(node):
   3.144      for domain in domains:
   3.145          if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   3.146              start_domain(domain,node)
   3.147  
   3.148 -def start_my_domains(node=i_am):
   3.149 +def start_my_domains(node):
   3.150      for domain in domain_home[node]:
   3.151          if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   3.152              start_domain(domain,node)
   3.153  
   3.154 -def migrate_my_domains_home(node=i_am):
   3.155 +def migrate_my_domains_home(node):
   3.156      for domain in domain_home[node]:
   3.157          if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
   3.158              migrate_domain_in(domain,node)
   3.159  
   3.160 -def migrate_and_start_my_domains(node=i_am):
   3.161 +def migrate_and_start_my_domains(node):
   3.162      migrate_my_domains_home(node)
   3.163      start_my_domains(node)
   3.164      
   3.165 -def migrate_and_start_all(node=i_am):
   3.166 +def migrate_and_start_all(node):
   3.167      migrate_my_domains_home(node)
   3.168      start_my_domains(node)
   3.169      migrate_my_domains_home(the_peer_of(node))
   3.170 @@ -196,7 +179,7 @@
   3.171  def show_usage():
   3.172      print """
   3.173  Usage:
   3.174 -    xen-drbd command [domain]
   3.175 +    xen-drbd [options] command [domain]
   3.176  
   3.177  Commands:
   3.178      start domain
   3.179 @@ -212,6 +195,12 @@
   3.180      migrate-and-start-all
   3.181  
   3.182      dump-config domain
   3.183 +
   3.184 +Options:
   3.185 +    --help         (-h)   -- show usage information
   3.186 +    --network=name (-n)   -- the network descriptions is in the file name.py (default: network.py)
   3.187 +    
   3.188 +
   3.189      """
   3.190  
   3.191  def test():
   3.192 @@ -234,41 +223,79 @@
   3.193  #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
   3.194  #sys.exit(0)
   3.195  
   3.196 -if len(sys.argv) == 1:
   3.197 +network='network'
   3.198 +import sys,os,imp,getopt
   3.199 +from commands import mkarg
   3.200 +
   3.201 +try:
   3.202 +    opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
   3.203 +except getopt.GetoptError, err:
   3.204 +    # print help information and exit:
   3.205 +    print str(err) # will print something like "option -a not recognized"
   3.206 +    usage()
   3.207 +    sys.exit(2)
   3.208 +
   3.209 +for o, a in opts:
   3.210 +    if o in ("-h", "--help"):
   3.211 +        show_usage()
   3.212 +        sys.exit()
   3.213 +    elif o in ("-n", "--network"):
   3.214 +        network = a
   3.215 +    else:
   3.216 +        assert False, "unhandled option"
   3.217 +
   3.218 +
   3.219 +debug=2
   3.220 +
   3.221 +
   3.222 +
   3.223 +sys.path.append('/etc/xen')
   3.224 +try:
   3.225 +        exec 'from %s import *' % (network)
   3.226 +except ImportError:
   3.227 +        print "Can't find or interpret module <%s> with topology description" %(network)
   3.228 +        sys.exit(1)
   3.229 +
   3.230 +xen_drbd_start="/etc/xen/xen-drbd-start"
   3.231 +domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
   3.232 +
   3.233 +if len(args) == 0:
   3.234      show_usage()
   3.235      sys.exit(0)
   3.236  
   3.237 -command=sys.argv[1]
   3.238 -if len(sys.argv) == 3:
   3.239 -    domain=sys.argv[2]
   3.240 +he_is=the_peer_of(i_am)
   3.241 +
   3.242 +command=args[0]
   3.243 +if len(args) == 2:
   3.244 +    domain=args[1]
   3.245      if command == 'start':
   3.246 -        start_domain(domain)
   3.247 +        start_domain(domain, i_am)
   3.248      elif command == 'migrate-out':
   3.249 -        migrate_domain_out(domain)
   3.250 +        migrate_domain_out(domain, i_am)
   3.251      elif command == 'migrate-in':
   3.252 -        migrate_domain_in(domain)
   3.253 +        migrate_domain_in(domain, i_am)
   3.254      elif command == 'dump-config':
   3.255          dump_config(domain)
   3.256      else:
   3.257          show_usage()
   3.258          sys.exit(0)
   3.259 -elif len(sys.argv) == 2:
   3.260 +elif len(args) == 1:
   3.261      if command == 'start-all':
   3.262 -        start_all()
   3.263 +        start_all(i_am)
   3.264      elif command == 'start-my-domains':
   3.265 -        start_my_domains()
   3.266 +        start_my_domains(i_am)
   3.267      elif command == 'migrate-out-all':
   3.268 -        migrate_out_all()
   3.269 +        migrate_out_all(i_am)
   3.270      elif command == 'migrate-in-all':
   3.271 -        migrate_in_all()
   3.272 +        migrate_in_all(i_am)
   3.273      elif command == 'migrate-my-domains-home':
   3.274 -        migrate_my_domains_home()
   3.275 +        migrate_my_domains_home(i_am)
   3.276      elif command == 'migrate-and-start-my-domains':
   3.277 -        migrate_and_start_my_domains()
   3.278 +        migrate_and_start_my_domains(i_am)
   3.279      elif command == 'migrate-and-start-all':
   3.280 -        migrate_and_start_all()
   3.281 +        migrate_and_start_all(i_am)
   3.282      elif command == 'list':
   3.283 -        print running_domains()
   3.284 +        print running_domains(i_am)
   3.285      else:
   3.286          show_usage()
   3.287          sys.exit(0)