xen-drbd

diff 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
line diff
     1.1 --- a/xen-drbd.py	Thu Aug 14 22:04:16 2008 +0300
     1.2 +++ b/xen-drbd.py	Mon Sep 15 10:03:24 2008 +0300
     1.3 @@ -1,21 +1,5 @@
     1.4  #!/usr/bin/python
     1.5  
     1.6 -network='od'
     1.7 -debug=2
     1.8 -
     1.9 -import sys,os,imp
    1.10 -from commands import mkarg
    1.11 -
    1.12 -
    1.13 -sys.path.append('/etc/xen')
    1.14 -try:
    1.15 -        exec 'from %s import *' % (network)
    1.16 -except ImportError:
    1.17 -        print "Can't find or interpret module %s with topology description" %(network)
    1.18 -        sys.exit(1)
    1.19 -
    1.20 -xen_drbd_start="/etc/xen/xen-drbd-start"
    1.21 -domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
    1.22  
    1.23  def the_peer_of(node):
    1.24      if node == node1:
    1.25 @@ -24,12 +8,11 @@
    1.26          another_node=node1
    1.27      return another_node
    1.28  
    1.29 -he_is=the_peer_of(i_am)
    1.30  
    1.31  def log_error(error):
    1.32      print error
    1.33  
    1.34 -def run_now(command,node=i_am):
    1.35 +def run_now(command,node):
    1.36      if node == i_am:
    1.37          line=command+" > /dev/stderr 2>&1"
    1.38      else:
    1.39 @@ -43,7 +26,7 @@
    1.40      p.close()
    1.41      return output
    1.42  
    1.43 -def run(command,node=i_am):
    1.44 +def run(command,node):
    1.45      return run_now(command,node)
    1.46      #if node == i_am:
    1.47      #    print command
    1.48 @@ -60,7 +43,7 @@
    1.49              disk.append((disk_description.split(':'))[1])
    1.50      return disk
    1.51  
    1.52 -def set_drbd_primary(domain, node=i_am):
    1.53 +def set_drbd_primary(domain, node):
    1.54      res=""
    1.55      drbd_resources=get_drbd_resources(domain)
    1.56      for drbd in drbd_resources:
    1.57 @@ -68,7 +51,7 @@
    1.58          print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
    1.59      return res
    1.60  
    1.61 -def set_drbd_secondary(domain, node=i_am):
    1.62 +def set_drbd_secondary(domain, node):
    1.63      res=""
    1.64      drbd_resources=get_drbd_resources(domain)
    1.65      for drbd in drbd_resources:
    1.66 @@ -76,7 +59,7 @@
    1.67          print "DRBD resource <%s> on the node <%s> is <%s> now" % (drbd,node,get_drbd_state(drbd,node))
    1.68      return res
    1.69  
    1.70 -def get_domain_id(domain,node=i_am):
    1.71 +def get_domain_id(domain,node):
    1.72      """
    1.73      Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
    1.74      """
    1.75 @@ -87,7 +70,7 @@
    1.76          res = int(res)
    1.77      return res
    1.78  
    1.79 -def get_drbd_id(resource,node=i_am):
    1.80 +def get_drbd_id(resource,node):
    1.81      res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
    1.82      if not res:
    1.83          res = -1
    1.84 @@ -95,19 +78,19 @@
    1.85          res = int(res)
    1.86      return res
    1.87  
    1.88 -def get_drbd_state(resource,node=i_am):
    1.89 +def get_drbd_state(resource,node):
    1.90      res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
    1.91      if not res:
    1.92          res = -1
    1.93      return res
    1.94  
    1.95 -def get_drbd_cstate(resource,node=i_am):
    1.96 +def get_drbd_cstate(resource,node):
    1.97      res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
    1.98      if not res:
    1.99          res = -1
   1.100      return res
   1.101  
   1.102 -def start_domain(domain,node=i_am):
   1.103 +def start_domain(domain,node):
   1.104      if (get_domain_id(domain,i_am) != -1):
   1.105          log_error("Domain %s is running already on the node %s" % (domain,i_am))
   1.106          return -1
   1.107 @@ -119,7 +102,7 @@
   1.108      print set_drbd_primary(domain,node)
   1.109      print run(domain_create_line % domain, node)
   1.110  
   1.111 -def migrate_domain_out(domain,node=i_am):
   1.112 +def migrate_domain_out(domain,node):
   1.113      if (get_domain_id(domain,node) == -1):
   1.114          log_error("Domain %s is not running on the node %s" % (domain,node))
   1.115          return -1
   1.116 @@ -131,44 +114,44 @@
   1.117      set_drbd_secondary(domain,node)
   1.118      print "+ Done"
   1.119      
   1.120 -def migrate_domain_in(domain,node=i_am):
   1.121 +def migrate_domain_in(domain,node):
   1.122      migrate_domain_out(domain,the_peer_of(node))
   1.123  
   1.124  def migrate_domain(domain,node):
   1.125      migrate_domain_in(domain,node)
   1.126  
   1.127 -def running_domains(node=i_am):
   1.128 +def running_domains(node):
   1.129      xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
   1.130      return filter(lambda x: x in xm_domains, domains)
   1.131  
   1.132 -def migrate_out_all(node=i_am):
   1.133 +def migrate_out_all(node):
   1.134      for domain in running_domains(node):
   1.135          migrate_domain_out(domain,node)
   1.136  
   1.137 -def migrate_in_all(node=i_am):
   1.138 +def migrate_in_all(node):
   1.139      for domain in running_domains(the_peer_of(node)):
   1.140          migrate_domain_in(domain,node)
   1.141  
   1.142 -def start_all(node=i_am):
   1.143 +def start_all(node):
   1.144      for domain in domains:
   1.145          if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   1.146              start_domain(domain,node)
   1.147  
   1.148 -def start_my_domains(node=i_am):
   1.149 +def start_my_domains(node):
   1.150      for domain in domain_home[node]:
   1.151          if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   1.152              start_domain(domain,node)
   1.153  
   1.154 -def migrate_my_domains_home(node=i_am):
   1.155 +def migrate_my_domains_home(node):
   1.156      for domain in domain_home[node]:
   1.157          if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
   1.158              migrate_domain_in(domain,node)
   1.159  
   1.160 -def migrate_and_start_my_domains(node=i_am):
   1.161 +def migrate_and_start_my_domains(node):
   1.162      migrate_my_domains_home(node)
   1.163      start_my_domains(node)
   1.164      
   1.165 -def migrate_and_start_all(node=i_am):
   1.166 +def migrate_and_start_all(node):
   1.167      migrate_my_domains_home(node)
   1.168      start_my_domains(node)
   1.169      migrate_my_domains_home(the_peer_of(node))
   1.170 @@ -196,7 +179,7 @@
   1.171  def show_usage():
   1.172      print """
   1.173  Usage:
   1.174 -    xen-drbd command [domain]
   1.175 +    xen-drbd [options] command [domain]
   1.176  
   1.177  Commands:
   1.178      start domain
   1.179 @@ -212,6 +195,12 @@
   1.180      migrate-and-start-all
   1.181  
   1.182      dump-config domain
   1.183 +
   1.184 +Options:
   1.185 +    --help         (-h)   -- show usage information
   1.186 +    --network=name (-n)   -- the network descriptions is in the file name.py (default: network.py)
   1.187 +    
   1.188 +
   1.189      """
   1.190  
   1.191  def test():
   1.192 @@ -234,41 +223,79 @@
   1.193  #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
   1.194  #sys.exit(0)
   1.195  
   1.196 -if len(sys.argv) == 1:
   1.197 +network='network'
   1.198 +import sys,os,imp,getopt
   1.199 +from commands import mkarg
   1.200 +
   1.201 +try:
   1.202 +    opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
   1.203 +except getopt.GetoptError, err:
   1.204 +    # print help information and exit:
   1.205 +    print str(err) # will print something like "option -a not recognized"
   1.206 +    usage()
   1.207 +    sys.exit(2)
   1.208 +
   1.209 +for o, a in opts:
   1.210 +    if o in ("-h", "--help"):
   1.211 +        show_usage()
   1.212 +        sys.exit()
   1.213 +    elif o in ("-n", "--network"):
   1.214 +        network = a
   1.215 +    else:
   1.216 +        assert False, "unhandled option"
   1.217 +
   1.218 +
   1.219 +debug=2
   1.220 +
   1.221 +
   1.222 +
   1.223 +sys.path.append('/etc/xen')
   1.224 +try:
   1.225 +        exec 'from %s import *' % (network)
   1.226 +except ImportError:
   1.227 +        print "Can't find or interpret module <%s> with topology description" %(network)
   1.228 +        sys.exit(1)
   1.229 +
   1.230 +xen_drbd_start="/etc/xen/xen-drbd-start"
   1.231 +domain_create_line="xm create "+xen_drbd_start+" network="+network+" domain=%s"
   1.232 +
   1.233 +if len(args) == 0:
   1.234      show_usage()
   1.235      sys.exit(0)
   1.236  
   1.237 -command=sys.argv[1]
   1.238 -if len(sys.argv) == 3:
   1.239 -    domain=sys.argv[2]
   1.240 +he_is=the_peer_of(i_am)
   1.241 +
   1.242 +command=args[0]
   1.243 +if len(args) == 2:
   1.244 +    domain=args[1]
   1.245      if command == 'start':
   1.246 -        start_domain(domain)
   1.247 +        start_domain(domain, i_am)
   1.248      elif command == 'migrate-out':
   1.249 -        migrate_domain_out(domain)
   1.250 +        migrate_domain_out(domain, i_am)
   1.251      elif command == 'migrate-in':
   1.252 -        migrate_domain_in(domain)
   1.253 +        migrate_domain_in(domain, i_am)
   1.254      elif command == 'dump-config':
   1.255          dump_config(domain)
   1.256      else:
   1.257          show_usage()
   1.258          sys.exit(0)
   1.259 -elif len(sys.argv) == 2:
   1.260 +elif len(args) == 1:
   1.261      if command == 'start-all':
   1.262 -        start_all()
   1.263 +        start_all(i_am)
   1.264      elif command == 'start-my-domains':
   1.265 -        start_my_domains()
   1.266 +        start_my_domains(i_am)
   1.267      elif command == 'migrate-out-all':
   1.268 -        migrate_out_all()
   1.269 +        migrate_out_all(i_am)
   1.270      elif command == 'migrate-in-all':
   1.271 -        migrate_in_all()
   1.272 +        migrate_in_all(i_am)
   1.273      elif command == 'migrate-my-domains-home':
   1.274 -        migrate_my_domains_home()
   1.275 +        migrate_my_domains_home(i_am)
   1.276      elif command == 'migrate-and-start-my-domains':
   1.277 -        migrate_and_start_my_domains()
   1.278 +        migrate_and_start_my_domains(i_am)
   1.279      elif command == 'migrate-and-start-all':
   1.280 -        migrate_and_start_all()
   1.281 +        migrate_and_start_all(i_am)
   1.282      elif command == 'list':
   1.283 -        print running_domains()
   1.284 +        print running_domains(i_am)
   1.285      else:
   1.286          show_usage()
   1.287          sys.exit(0)