Commit 309d4db7 authored by Vincent Pelletier's avatar Vincent Pelletier

Reduce try..except scope.

parent 6e2f0a69
...@@ -859,130 +859,126 @@ class Parser(OptionParser): ...@@ -859,130 +859,126 @@ class Parser(OptionParser):
return options, args[0] return options, args[0]
def run(config): def run(config):
try: # Define the computer
# Define the computer if config.input_definition_file:
if config.input_definition_file: filepath = os.path.abspath(config.input_definition_file)
filepath = os.path.abspath(config.input_definition_file) config.logger.info('Using definition file %r' % filepath)
config.logger.info('Using definition file %r' % filepath) computer_definition = ConfigParser.RawConfigParser()
computer_definition = ConfigParser.RawConfigParser() computer_definition.read(filepath)
computer_definition.read(filepath) bridge = None
bridge = None address = None
address = None netmask = None
netmask = None if computer_definition.has_option('computer', 'address'):
if computer_definition.has_option('computer', 'address'): address, netmask = computer_definition.get('computer', 'address').split('/')
address, netmask = computer_definition.get('computer', 'address').split('/') if config.alter_network and config.bridge_name is not None \
if config.alter_network and config.bridge_name is not None \ and config.ipv4_local_network is not None:
and config.ipv4_local_network is not None: bridge = Bridge(config.bridge_name, config.ipv4_local_network,
bridge = Bridge(config.bridge_name, config.ipv4_local_network, config.ipv6_interface)
computer = Computer(
reference=config.computer_id,
bridge=bridge,
addr=address,
netmask=netmask,
ipv6_interface=config.ipv6_interface
)
partition_list = []
for partition_number in range(int(config.partition_amount)):
section = 'partition_%s' % partition_number
user = User(computer_definition.get(section, 'user'))
address_list = []
for a in computer_definition.get(section, 'address').split():
address, netmask = a.split('/')
address_list.append(dict(addr=address, netmask=netmask))
tap = Tap(computer_definition.get(section, 'network_interface'))
partition_list.append(Partition(reference=computer_definition.get(section, 'pathname'),
path=os.path.join(config.instance_root, computer_definition.get(section, 'pathname')),
user=user,
address_list=address_list,
tap=tap,
))
computer.partition_list = partition_list
else:
# no definition file, figure out computer
if os.path.exists(config.computer_xml):
config.logger.info('Loading previous computer data from %r' % config.computer_xml)
computer = Computer.load(config.computer_xml, reference=config.computer_id, ipv6_interface=config.ipv6_interface)
# Connect to the bridge interface defined by the configuration
computer.bridge = Bridge(config.bridge_name, config.ipv4_local_network,
config.ipv6_interface) config.ipv6_interface)
computer = Computer(
reference=config.computer_id,
bridge=bridge,
addr=address,
netmask=netmask,
ipv6_interface=config.ipv6_interface
)
partition_list = []
for partition_number in range(int(config.partition_amount)):
section = 'partition_%s' % partition_number
user = User(computer_definition.get(section, 'user'))
address_list = []
for a in computer_definition.get(section, 'address').split():
address, netmask = a.split('/')
address_list.append(dict(addr=address, netmask=netmask))
tap = Tap(computer_definition.get(section, 'network_interface'))
partition_list.append(Partition(reference=computer_definition.get(section, 'pathname'),
path=os.path.join(config.instance_root, computer_definition.get(section, 'pathname')),
user=user,
address_list=address_list,
tap=tap,
))
computer.partition_list = partition_list
else: else:
# no definition file, figure out computer # If no pre-existent configuration found, creating a new computer object
if os.path.exists(config.computer_xml): config.logger.warning('Creating new data computer with id %r' % config.computer_id)
config.logger.info('Loading previous computer data from %r' % config.computer_xml) computer = Computer(
computer = Computer.load(config.computer_xml, reference=config.computer_id, ipv6_interface=config.ipv6_interface) reference=config.computer_id,
# Connect to the bridge interface defined by the configuration bridge=Bridge(config.bridge_name, config.ipv4_local_network,
computer.bridge = Bridge(config.bridge_name, config.ipv4_local_network, config.ipv6_interface),
config.ipv6_interface) addr=None,
else: netmask=None,
# If no pre-existent configuration found, creating a new computer object ipv6_interface=config.ipv6_interface
config.logger.warning('Creating new data computer with id %r' % config.computer_id) )
computer = Computer(
reference=config.computer_id, partition_amount = int(config.partition_amount)
bridge=Bridge(config.bridge_name, config.ipv4_local_network, existing_partition_amount = len(computer.partition_list)
config.ipv6_interface), if existing_partition_amount > partition_amount:
addr=None, raise ValueError('Requested amount of computer partitions (%s) is lower '
netmask=None, 'then already configured (%s), cannot continue' % (partition_amount,
ipv6_interface=config.ipv6_interface len(computer.partition_list)))
)
config.logger.info('Adding %s new partitions' %
partition_amount = int(config.partition_amount) (partition_amount-existing_partition_amount))
existing_partition_amount = len(computer.partition_list) for nb_iter in range(existing_partition_amount, partition_amount):
if existing_partition_amount > partition_amount: # add new ones
raise ValueError('Requested amount of computer partitions (%s) is lower ' user = User("%s%s" % (config.user_base_name, nb_iter))
'then already configured (%s), cannot continue' % (partition_amount,
len(computer.partition_list))) tap = Tap("%s%s" % (config.tap_base_name, nb_iter))
config.logger.info('Adding %s new partitions' % path = os.path.join(config.instance_root, "%s%s" % (
(partition_amount-existing_partition_amount)) config.partition_base_name, nb_iter))
for nb_iter in range(existing_partition_amount, partition_amount): computer.partition_list.append(
# add new ones Partition(
user = User("%s%s" % (config.user_base_name, nb_iter)) reference="%s%s" % (config.partition_base_name, nb_iter),
path=path,
tap = Tap("%s%s" % (config.tap_base_name, nb_iter)) user=user,
address_list=None,
path = os.path.join(config.instance_root, "%s%s" % ( tap=tap,
config.partition_base_name, nb_iter)) ))
computer.partition_list.append(
Partition( computer.instance_root = config.instance_root
reference="%s%s" % (config.partition_base_name, nb_iter), computer.software_root = config.software_root
path=path, config.logger.info('Updating computer')
user=user, address = computer.getAddress()
address_list=None, computer.address = address['addr']
tap=tap, computer.netmask = address['netmask']
))
if config.output_definition_file:
computer.instance_root = config.instance_root computer_definition = ConfigParser.RawConfigParser()
computer.software_root = config.software_root computer_definition.add_section('computer')
config.logger.info('Updating computer') if computer.address is not None and computer.netmask is not None:
address = computer.getAddress() computer_definition.set('computer', 'address', '/'.join([computer.address, computer.netmask]))
computer.address = address['addr'] partition_number = 0
computer.netmask = address['netmask'] for partition in computer.partition_list:
section = 'partition_%s' % partition_number
if config.output_definition_file: computer_definition.add_section(section)
computer_definition = ConfigParser.RawConfigParser() address_list = []
computer_definition.add_section('computer') for address in partition.address_list:
if computer.address is not None and computer.netmask is not None: address_list.append('/'.join([address['addr'], address['netmask']]))
computer_definition.set('computer', 'address', '/'.join([computer.address, computer.netmask])) computer_definition.set(section, 'address', ' '.join(address_list))
partition_number = 0 computer_definition.set(section, 'user', partition.user.name)
for partition in computer.partition_list: computer_definition.set(section, 'user', partition.user.name)
section = 'partition_%s' % partition_number computer_definition.set(section, 'network_interface', partition.tap.name)
computer_definition.add_section(section) computer_definition.set(section, 'pathname', partition.reference)
address_list = [] partition_number += 1
for address in partition.address_list: filepath = os.path.abspath(config.output_definition_file)
address_list.append('/'.join([address['addr'], address['netmask']])) computer_definition.write(open(filepath, 'w'))
computer_definition.set(section, 'address', ' '.join(address_list)) config.logger.info('Stored computer definition in %r' % filepath)
computer_definition.set(section, 'user', partition.user.name) computer.construct(alter_user=config.alter_user,
computer_definition.set(section, 'user', partition.user.name) alter_network=config.alter_network)
computer_definition.set(section, 'network_interface', partition.tap.name)
computer_definition.set(section, 'pathname', partition.reference) # Dumping and sending to the erp5 the current configuration
partition_number += 1 if not config.dry_run:
filepath = os.path.abspath(config.output_definition_file) computer.dump(config.computer_xml)
computer_definition.write(open(filepath, 'w')) config.logger.info('Posting information to %r' % config.master_url)
config.logger.info('Stored computer definition in %r' % filepath) computer.send(config)
computer.construct(alter_user=config.alter_user,
alter_network=config.alter_network)
# Dumping and sending to the erp5 the current configuration
if not config.dry_run:
computer.dump(config.computer_xml)
config.logger.info('Posting information to %r' % config.master_url)
computer.send(config)
except:
config.logger.exception('Uncaught exception:')
raise
class Config: class Config:
def checkRequiredBinary(self, binary_list): def checkRequiredBinary(self, binary_list):
...@@ -1104,39 +1100,44 @@ def main(*args): ...@@ -1104,39 +1100,44 @@ def main(*args):
real_callAndRead = callAndRead real_callAndRead = callAndRead
usage = "usage: %s [options] CONFIGURATION_FILE" % sys.argv[0] usage = "usage: %s [options] CONFIGURATION_FILE" % sys.argv[0]
# Parse arguments
options, configuration_file_path = Parser(usage=usage).check_args(args)
config = Config()
try: try:
# Parse arguments
options, configuration_file_path = Parser(usage=usage).check_args(args)
config = Config()
config.setConfig(options, configuration_file_path) config.setConfig(options, configuration_file_path)
os = OS(config)
if config.dry_run:
def dry_callAndRead(argument_list, raise_on_error=True):
if argument_list == ['brctl', 'show']:
return real_callAndRead(argument_list, raise_on_error)
else:
return 0, ''
callAndRead = dry_callAndRead
real_addSystemAddress = Bridge._addSystemAddress
def fake_addSystemAddress(*args, **kw):
real_addSystemAddress(*args, **kw)
# Fake success
return True
Bridge._addSystemAddress = fake_addSystemAddress
def fake_getpwnam(user):
class result:
pw_uid = 12345
pw_gid = 54321
return result
pwd.getpwnam = fake_getpwnam
else:
dry_callAndRead = real_callAndRead
if config.verbose:
def logging_callAndRead(argument_list, raise_on_error=True):
config.logger.debug(' '.join(argument_list))
return dry_callAndRead(argument_list, raise_on_error)
callAndRead = logging_callAndRead
run(config)
except UsageError, err: except UsageError, err:
print >>sys.stderr, err.msg print >>sys.stderr, err.msg
print >>sys.stderr, "For help use --help" print >>sys.stderr, "For help use --help"
sys.exit(1)
os = OS(config)
if config.dry_run:
def dry_callAndRead(argument_list, raise_on_error=True):
if argument_list == ['brctl', 'show']:
return real_callAndRead(argument_list, raise_on_error)
else:
return 0, ''
callAndRead = dry_callAndRead
real_addSystemAddress = Bridge._addSystemAddress
def fake_addSystemAddress(*args, **kw):
real_addSystemAddress(*args, **kw)
# Fake success
return True
Bridge._addSystemAddress = fake_addSystemAddress
def fake_getpwnam(user):
class result:
pw_uid = 12345
pw_gid = 54321
return result
pwd.getpwnam = fake_getpwnam
else:
dry_callAndRead = real_callAndRead
if config.verbose:
def logging_callAndRead(argument_list, raise_on_error=True):
config.logger.debug(' '.join(argument_list))
return dry_callAndRead(argument_list, raise_on_error)
callAndRead = logging_callAndRead
try:
run(config)
except:
config.logger.exception('Uncaught exception:')
raise
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment