ia64/xen-unstable
changeset 1681:5f8c3e557ac1
bitkeeper revision 1.1041.9.1 (40eac387W39rUFRFKTwvCfPBOF7Aow)
Convert most 'xm create' command-line switches to variables.
Convert most 'xm create' command-line switches to variables.
author | mjw@wray-m-3.hpl.hp.com |
---|---|
date | Tue Jul 06 15:21:43 2004 +0000 (2004-07-06) |
parents | 35e97fc65a8b |
children | 74a48e39e4be |
files | tools/python/xen/util/ip.py tools/python/xen/xm/create.py tools/python/xen/xm/opts.py |
line diff
1.1 --- a/tools/python/xen/util/ip.py Tue Jul 06 13:00:49 2004 +0000 1.2 +++ b/tools/python/xen/util/ip.py Tue Jul 06 15:21:43 2004 +0000 1.3 @@ -3,7 +3,7 @@ import re 1.4 import socket 1.5 import struct 1.6 1.7 -def readlines(fd): 1.8 +def _readlines(fd): 1.9 """Version of readlines safe against EINTR. 1.10 """ 1.11 import errno 1.12 @@ -21,7 +21,7 @@ def readlines(fd): 1.13 lines.append(line) 1.14 return lines 1.15 1.16 -def readline(fd): 1.17 +def _readline(fd): 1.18 """Version of readline safe against EINTR. 1.19 """ 1.20 while 1: 1.21 @@ -42,11 +42,14 @@ as it may have been moved onto the bridg 1.22 NBE_BRIDGE = 'nbe-br' 1.23 1.24 def get_current_ipaddr(dev='eth0'): 1.25 - """Return a string containing the primary IP address for the given 1.26 - network interface (default 'eth0'). 1.27 + """Get the primary IP address for the given network interface. 1.28 + 1.29 + dev network interface (default eth0) 1.30 + 1.31 + returns interface address as a string 1.32 """ 1.33 fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' ) 1.34 - lines = readlines(fd) 1.35 + lines = _readlines(fd) 1.36 for line in lines: 1.37 m = re.search( '^\s+inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*', 1.38 line ) 1.39 @@ -57,11 +60,14 @@ def get_current_ipaddr(dev='eth0'): 1.40 return None 1.41 1.42 def get_current_ipmask(dev='eth0'): 1.43 - """Return a string containing the primary IP netmask for the given 1.44 - network interface (default 'eth0'). 1.45 + """Get the primary IP netmask for a network interface. 1.46 + 1.47 + dev network interface (default eth0) 1.48 + 1.49 + returns interface netmask as a string 1.50 """ 1.51 fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' ) 1.52 - lines = readlines(fd) 1.53 + lines = _readlines(fd) 1.54 for line in lines: 1.55 m = re.search( '^.+Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*', 1.56 line ) 1.57 @@ -72,11 +78,14 @@ def get_current_ipmask(dev='eth0'): 1.58 return None 1.59 1.60 def get_current_ipgw(dev='eth0'): 1.61 - """Return a string containing the IP gateway for the given 1.62 - network interface (default 'eth0'). 1.63 + """Get the IP gateway for a network interface. 1.64 + 1.65 + dev network interface (default eth0) 1.66 + 1.67 + returns gateway address as a string 1.68 """ 1.69 fd = os.popen( '/sbin/route -n' ) 1.70 - lines = readlines(fd) 1.71 + lines = _readlines(fd) 1.72 for line in lines: 1.73 m = re.search( '^\S+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' + 1.74 '\s+\S+\s+\S*G.*' + dev + '.*', line ) 1.75 @@ -88,24 +97,46 @@ def get_current_ipgw(dev='eth0'): 1.76 1.77 def inet_aton(addr): 1.78 """Convert an IP addr in IPv4 dot notation into an int. 1.79 + 1.80 + addr IP address as a string 1.81 + 1.82 + returns integer 1.83 """ 1.84 b = socket.inet_aton(addr) 1.85 return struct.unpack('!I', b)[0] 1.86 1.87 def inet_ntoa(n): 1.88 """Convert an int into an IP addr in IPv4 dot notation. 1.89 + 1.90 + n IP address 1.91 + 1.92 + returns string 1.93 """ 1.94 b = struct.pack('!I', n) 1.95 return socket.inet_ntoa(b) 1.96 1.97 def add_offset_to_ip(addr, offset): 1.98 """Add a numerical offset to an IP addr in IPv4 dot notation. 1.99 + 1.100 + addr IP address 1.101 + offset offset to add 1.102 + 1.103 + returns new address 1.104 """ 1.105 n = inet_aton(addr) 1.106 n += offset 1.107 return inet_ntoa(n) 1.108 1.109 def check_subnet( ip, network, netmask ): 1.110 + """Check if an IP address is in the subnet defined by 1.111 + a network address and mask'. 1.112 + 1.113 + ip IP adress 1.114 + network network address 1.115 + netmask network mask 1.116 + 1.117 + returns 1 if it is in the subnet, 0 if not 1.118 + """ 1.119 n_ip = inet_aton(ip) 1.120 n_net = inet_aton(network) 1.121 n_mask = inet_aton(netmask)
2.1 --- a/tools/python/xen/xm/create.py Tue Jul 06 13:00:49 2004 +0000 2.2 +++ b/tools/python/xen/xm/create.py Tue Jul 06 15:21:43 2004 +0000 2.3 @@ -13,7 +13,7 @@ from xen.util import console_client 2.4 2.5 from xen.xm.opts import * 2.6 2.7 -gopts = Opts(use="""[options] 2.8 +gopts = Opts(use="""[options] [vars] 2.9 2.10 Create a domain. 2.11 2.12 @@ -66,54 +66,49 @@ gopts.opt('load', short='L', val='FILE', 2.13 fn=set_value, default=None, 2.14 use='Domain saved state to load.') 2.15 2.16 -#gopts.opt('define', short='D', val='VAR=VAL', 2.17 -# fn=set_var, default=None, 2.18 -# use="""Set a variable before loading defaults, e.g. '-D vmid=3' 2.19 -# to set vmid. May be repeated to set more than one variable.""") 2.20 - 2.21 gopts.opt('dryrun', short='n', 2.22 fn=set_true, default=0, 2.23 use="""Dry run - print the config but don't create the domain. 2.24 The defaults file is loaded and the SXP configuration is created and printed. 2.25 """) 2.26 2.27 -gopts.opt('name', short='N', val='NAME', 2.28 - fn=set_value, default=None, 2.29 - use="Domain name.") 2.30 - 2.31 gopts.opt('console_autoconnect', short='c', 2.32 fn=set_true, default=0, 2.33 use="Connect to console after domain is created.") 2.34 2.35 -gopts.opt('kernel', short='k', val='FILE', 2.36 +gopts.var('name', val='NAME', 2.37 + fn=set_value, default=None, 2.38 + use="Domain name.") 2.39 + 2.40 +gopts.var('kernel', val='FILE', 2.41 fn=set_value, default=None, 2.42 use="Path to kernel image.") 2.43 2.44 -gopts.opt('ramdisk', short='r', val='FILE', 2.45 +gopts.var('ramdisk', val='FILE', 2.46 fn=set_value, default='', 2.47 use="Path to ramdisk.") 2.48 2.49 -gopts.opt('builder', short='b', val='FUNCTION', 2.50 +gopts.var('builder', val='FUNCTION', 2.51 fn=set_value, default='linux', 2.52 use="Function to use to build the domain.") 2.53 2.54 -gopts.opt('memory', short='m', val='MEMORY', 2.55 +gopts.var('memory', val='MEMORY', 2.56 fn=set_value, default=128, 2.57 use="Domain memory in MB.") 2.58 2.59 -gopts.opt('autorestart', 2.60 - fn=set_true, default=0, 2.61 +gopts.var('autorestart', val='no|yes', 2.62 + fn=set_bool, default=0, 2.63 use="Whether to restart the domain on exit.") 2.64 2.65 -gopts.opt('blkif', 2.66 - fn=set_true, default=0, 2.67 +gopts.var('blkif', val='no|yes', 2.68 + fn=set_bool, default=0, 2.69 use="Make the domain a block device backend.") 2.70 2.71 -gopts.opt('netif', 2.72 - fn=set_true, default=0, 2.73 +gopts.var('netif', val='no|yes', 2.74 + fn=set_bool, default=0, 2.75 use="Make the domain a network interface backend.") 2.76 2.77 -gopts.opt('disk', short='d', val='phy:DEV,VDEV,MODE', 2.78 +gopts.var('disk', val='phy:DEV,VDEV,MODE', 2.79 fn=append_value, default=[], 2.80 use="""Add a disk device to a domain. The physical device is DEV, 2.81 which is exported to the domain as VDEV. The disk is read-only if MODE 2.82 @@ -121,18 +116,18 @@ gopts.opt('disk', short='d', val='phy:DE 2.83 The option may be repeated to add more than one disk. 2.84 """) 2.85 2.86 -gopts.opt('pci', val='BUS,DEV,FUNC', 2.87 +gopts.var('pci', val='BUS,DEV,FUNC', 2.88 fn=append_value, default=[], 2.89 use="""Add a PCI device to a domain, using given params (in hex). 2.90 For example '-pci c0,02,1a'. 2.91 The option may be repeated to add more than one pci device. 2.92 """) 2.93 2.94 -gopts.opt('ipaddr', short='i', val="IPADDR", 2.95 +gopts.var('ipaddr', val="IPADDR", 2.96 fn=append_value, default=[], 2.97 use="Add an IP address to the domain.") 2.98 2.99 -gopts.opt('vif', val="mac=MAC,bridge=BRIDGE", 2.100 +gopts.var('vif', val="mac=MAC,bridge=BRIDGE", 2.101 fn=append_value, default=[], 2.102 use="""Add a network interface with the given MAC address and bridge. 2.103 If mac is not specified a random MAC address is used. 2.104 @@ -141,7 +136,7 @@ gopts.opt('vif', val="mac=MAC,bridge=BRI 2.105 Specifying vifs will increase the number of interfaces as needed. 2.106 """) 2.107 2.108 -gopts.opt('nics', val="NUM", 2.109 +gopts.var('nics', val="NUM", 2.110 fn=set_int, default=1, 2.111 use="""Set the number of network interfaces. 2.112 Use the vif option to define interface parameters, otherwise 2.113 @@ -149,44 +144,44 @@ gopts.opt('nics', val="NUM", 2.114 number of interfaces as needed. 2.115 """) 2.116 2.117 -gopts.opt('root', short='R', val='DEVICE', 2.118 +gopts.var('root', val='DEVICE', 2.119 fn=set_value, default='', 2.120 use="""Set the root= parameter on the kernel command line. 2.121 Use a device, e.g. /dev/sda1, or /dev/nfs for NFS root.""") 2.122 2.123 -gopts.opt('extra', short='E', val="ARGS", 2.124 +gopts.var('extra', val="ARGS", 2.125 fn=set_value, default='', 2.126 use="Set extra arguments to append to the kernel command line.") 2.127 2.128 -gopts.opt('ip', short='I', val='IPADDR', 2.129 +gopts.var('ip', val='IPADDR', 2.130 fn=set_value, default='', 2.131 use="Set the kernel IP interface address.") 2.132 2.133 -gopts.opt('gateway', val="IPADDR", 2.134 +gopts.var('gateway', val="IPADDR", 2.135 fn=set_value, default='', 2.136 use="Set the kernel IP gateway.") 2.137 2.138 -gopts.opt('netmask', val="MASK", 2.139 +gopts.var('netmask', val="MASK", 2.140 fn=set_value, default = '', 2.141 use="Set the kernel IP netmask.") 2.142 2.143 -gopts.opt('hostname', val="NAME", 2.144 +gopts.var('hostname', val="NAME", 2.145 fn=set_value, default='', 2.146 use="Set the kernel IP hostname.") 2.147 2.148 -gopts.opt('interface', val="INTF", 2.149 +gopts.var('interface', val="INTF", 2.150 fn=set_value, default="eth0", 2.151 use="Set the kernel IP interface name.") 2.152 2.153 -gopts.opt('dhcp', val="off|dhcp", 2.154 +gopts.var('dhcp', val="off|dhcp", 2.155 fn=set_value, default='off', 2.156 use="Set the kernel dhcp option.") 2.157 2.158 -gopts.opt('nfs_server', val="IPADDR", 2.159 +gopts.var('nfs_server', val="IPADDR", 2.160 fn=set_value, default=None, 2.161 use="Set the address of the NFS server for NFS root.") 2.162 2.163 -gopts.opt('nfs_root', val="PATH", 2.164 +gopts.var('nfs_root', val="PATH", 2.165 fn=set_value, default=None, 2.166 use="Set the path of the root NFS directory.") 2.167 2.168 @@ -315,7 +310,7 @@ def preprocess_vifs(opts): 2.169 d = {} 2.170 a = vif.split(',') 2.171 for b in a: 2.172 - (k, v) = b.strip().split('=') 2.173 + (k, v) = b.strip().split('=', 1) 2.174 k = k.strip() 2.175 v = v.strip() 2.176 if k not in ['mac', 'bridge']: 2.177 @@ -392,7 +387,7 @@ def main(argv): 2.178 # Process remaining args as config variables. 2.179 for arg in args: 2.180 if '=' in arg: 2.181 - (var, val) = arg.strip().split('=') 2.182 + (var, val) = arg.strip().split('=', 1) 2.183 gopts.setvar(var.strip(), val.strip()) 2.184 if opts.vals.config: 2.185 pass
3.1 --- a/tools/python/xen/xm/opts.py Tue Jul 06 13:00:49 2004 +0000 3.2 +++ b/tools/python/xen/xm/opts.py Tue Jul 06 15:21:43 2004 +0000 3.3 @@ -126,6 +126,42 @@ class Opt: 3.4 """ 3.5 return self.specified_opt 3.6 3.7 +class OptVar(Opt): 3.8 + """An individual option variable. 3.9 + """ 3.10 + def __init__(self, opts, name, 3.11 + val=None, fn=None, use=None, default=None): 3.12 + """Create an option. 3.13 + 3.14 + opts parent options object 3.15 + name name of the field it controls 3.16 + val string used to print option args in help. 3.17 + If val is not specified the option has no arg. 3.18 + fn function to call when the option is specified. 3.19 + use usage (help) string 3.20 + default default value if not specified on command-line 3.21 + """ 3.22 + if val is None: 3.23 + val = name.upper() 3.24 + Opt.__init__(self, opts, name, val=val, fn=fn, use=use, default=default) 3.25 + self.optkeys = [] 3.26 + self.optkeys.append(self.long) 3.27 + 3.28 + def short_opt(self): 3.29 + return None 3.30 + 3.31 + def long_opt(self): 3.32 + return None 3.33 + 3.34 + def show(self): 3.35 + print '%s=%s' %(self.optkeys[0], self.val) 3.36 + print 3.37 + if self.use: 3.38 + print '\t', 3.39 + print self.use 3.40 + if self.val: 3.41 + print '\tDefault', self.default or 'None' 3.42 + 3.43 class OptVals: 3.44 """Class to hold option values. 3.45 """ 3.46 @@ -134,6 +170,13 @@ class OptVals: 3.47 class Opts: 3.48 """Container for options. 3.49 """ 3.50 + 3.51 + imports = ["import sys", 3.52 + "import os", 3.53 + "import os.path", 3.54 + "from xen.util.ip import *", 3.55 + ] 3.56 + 3.57 def __init__(self, use=None): 3.58 """Options constructor. 3.59 3.60 @@ -168,6 +211,12 @@ class Opts: 3.61 self.options_map[name] = x 3.62 return x 3.63 3.64 + def var(self, name, **args): 3.65 + x = OptVar(self, name, **args) 3.66 + self.options.append(x) 3.67 + self.options_map[name] = x 3.68 + return x 3.69 + 3.70 def setvar(self, var, val): 3.71 """Set a default script variable. 3.72 """ 3.73 @@ -232,7 +281,14 @@ class Opts: 3.74 else: 3.75 print >>sys.stderr, "Error: Unknown option:", k 3.76 self.usage() 3.77 - return args 3.78 + xargs = [] 3.79 + for arg in args: 3.80 + (k, v) = arg.split('=', 1) 3.81 + for opt in self.options: 3.82 + if opt.specify(k, v): break 3.83 + else: 3.84 + xargs.append(arg) 3.85 + return xargs 3.86 3.87 def short_opts(self): 3.88 """Get short options specifier for getopt. 3.89 @@ -257,6 +313,7 @@ class Opts: 3.90 def usage(self): 3.91 print 'Usage: ', self.argv[0], self.use or 'OPTIONS' 3.92 for opt in self.options: 3.93 + print 3.94 opt.show() 3.95 3.96 def load_defaults(self, help=0): 3.97 @@ -287,21 +344,22 @@ class Opts: 3.98 globals = {} 3.99 locals = {} 3.100 locals.update(self.vars) 3.101 - cmd = '\n'.join(["import sys", 3.102 - "import os", 3.103 - "import os.path", 3.104 - "from xen.xm.help import Vars", 3.105 - "from xen.util import ip", 3.106 - "xm_file = '%s'" % defaults, 3.107 - "xm_help = %d" % help, 3.108 - "xm_vars = Vars(xm_file, xm_help, locals())", 3.109 - ]) 3.110 + cmd = '\n'.join(self.imports + 3.111 + [ "from xen.xm.help import Vars", 3.112 + "xm_file = '%s'" % defaults, 3.113 + "xm_help = %d" % help, 3.114 + "xm_vars = Vars(xm_file, xm_help, locals())" 3.115 + ]) 3.116 exec cmd in globals, locals 3.117 try: 3.118 execfile(defaults, globals, locals) 3.119 except: 3.120 if not help: raise 3.121 - if help: return 3.122 + if help: 3.123 + print 'The following imports are done automatically:' 3.124 + for x in self.imports: 3.125 + print x 3.126 + return 3.127 # Extract the values set by the script and set the corresponding 3.128 # options, if not set on the command line. 3.129 vtypes = [ types.StringType, 3.130 @@ -322,6 +380,17 @@ def set_false(opt, k, v): 3.131 """Set an option false.""" 3.132 opt.set(0) 3.133 3.134 +def set_bool(opt, k, v): 3.135 + """Set a boolean option. 3.136 + """ 3.137 + if v in ['yes']: 3.138 + opt.set(1) 3.139 + elif v in ['no']: 3.140 + opt.set(0) 3.141 + else: 3.142 + opt.opts.err('Invalid value:' +v) 3.143 + 3.144 + 3.145 def set_value(opt, k, v): 3.146 """Set an option to a valoue.""" 3.147 opt.set(v) 3.148 @@ -341,6 +410,6 @@ def append_value(opt, k, v): 3.149 def set_var(opt, k, v): 3.150 """Set a default script variable. 3.151 """ 3.152 - (var, val) = v.strip().split('=') 3.153 + (var, val) = v.strip().split('=', 1) 3.154 opt.opts.setvar(var.strip(), val.strip()) 3.155