From 103b87aa82cfd298b664fe1314e987bd953ec20d Mon Sep 17 00:00:00 2001 From: Frederic Lepied Date: Thu, 5 Feb 2015 22:17:42 +0100 Subject: Allow to specify the root directory of the filesystem Change-Id: Ice1b8d17804cf7a0aafd308945a5e960fe927bbc --- os_net_config/__init__.py | 5 +++-- os_net_config/cli.py | 30 +++++++++++++++++++----------- os_net_config/impl_eni.py | 16 ++++++++-------- os_net_config/impl_ifcfg.py | 14 +++++++------- os_net_config/tests/test_cli.py | 9 +++++++++ os_net_config/tests/test_impl_eni.py | 2 +- 6 files changed, 47 insertions(+), 29 deletions(-) diff --git a/os_net_config/__init__.py b/os_net_config/__init__.py index c33e5d1..641ee3b 100644 --- a/os_net_config/__init__.py +++ b/os_net_config/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2014 Red Hat, Inc. +# Copyright 2014-2015 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -33,9 +33,10 @@ class NotImplemented(Exception): class NetConfig(object): """Configure network interfaces using the ifcfg format.""" - def __init__(self, noop=False): + def __init__(self, noop=False, root_dir=''): self.noop = noop self.log_prefix = "NOOP: " if noop else "" + self.root_dir = root_dir def add_object(self, obj): """Convenience method to add any type of object to the network config. diff --git a/os_net_config/cli.py b/os_net_config/cli.py index aeccb26..c2491a2 100644 --- a/os_net_config/cli.py +++ b/os_net_config/cli.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2014 Red Hat, Inc. +# Copyright 2014-2015 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -45,6 +45,9 @@ def parse_opts(argv): help="""The provider to use.""" """One of: ifcfg, eni, iproute.""", default=None) + parser.add_argument('-r', '--root-dir', metavar='ROOT_DIR', + help="""The root directory of the filesystem.""", + default='') parser.add_argument( '-d', '--debug', dest="debug", @@ -119,19 +122,24 @@ def main(argv=sys.argv): provider = None if opts.provider: if opts.provider == 'ifcfg': - provider = impl_ifcfg.IfcfgNetConfig(noop=opts.noop) + provider = impl_ifcfg.IfcfgNetConfig(noop=opts.noop, + root_dir=opts.root_dir) elif opts.provider == 'eni': - provider = impl_eni.ENINetConfig(noop=opts.noop) + provider = impl_eni.ENINetConfig(noop=opts.noop, + root_dir=opts.root_dir) elif opts.provider == 'iproute': - provider = impl_iproute.IPRouteNetConfig(noop=opts.noop) + provider = impl_iproute.IPRouteNetConfig(noop=opts.noop, + root_dir=opts.root_dir) else: logger.error('Invalid provider specified.') return 1 else: - if os.path.exists('/etc/sysconfig/network-scripts/'): - provider = impl_ifcfg.IfcfgNetConfig(noop=opts.noop) - elif os.path.exists('/etc/network/'): - provider = impl_eni.ENINetConfig(noop=opts.noop) + if os.path.exists('%s/etc/sysconfig/network-scripts/' % opts.root_dir): + provider = impl_ifcfg.IfcfgNetConfig(noop=opts.noop, + root_dir=opts.root_dir) + elif os.path.exists('%s/etc/network/' % opts.root_dir): + provider = impl_eni.ENINetConfig(noop=opts.noop, + root_dir=opts.root_dir) else: logger.error('Unable to set provider for this operating system.') return 1 @@ -172,9 +180,9 @@ def main(argv=sys.argv): activate=not opts.no_activate) if opts.noop: for location, data in files_changed.iteritems(): - print "File: %s\n" % location - print data - print "----" + print("File: %s\n" % location) + print(data) + print("----") return 0 diff --git a/os_net_config/impl_eni.py b/os_net_config/impl_eni.py index 3115e04..7eb69f3 100644 --- a/os_net_config/impl_eni.py +++ b/os_net_config/impl_eni.py @@ -1,6 +1,6 @@ # -*- Coding: utf-8 -*- -# Copyright 2014 Red Hat, Inc. +# Copyright 2014-2015 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -26,8 +26,8 @@ logger = logging.getLogger(__name__) # TODO(?): should move to interfaces.d -def _network_config_path(): - return "/etc/network/interfaces" +def _network_config_path(prefix=''): + return prefix + "/etc/network/interfaces" class ENINetConfig(os_net_config.NetConfig): @@ -37,8 +37,8 @@ class ENINetConfig(os_net_config.NetConfig): /etc/network/interfaces format. """ - def __init__(self, noop=False): - super(ENINetConfig, self).__init__(noop) + def __init__(self, noop=False, root_dir=''): + super(ENINetConfig, self).__init__(noop, root_dir) self.interfaces = {} self.routes = {} self.bridges = {} @@ -214,7 +214,7 @@ class ENINetConfig(os_net_config.NetConfig): iface_data += (route_data or '') new_config += iface_data - if (utils.diff(_network_config_path(), new_config)): + if utils.diff(_network_config_path(self.root_dir), new_config): if activate: for interface in self.interfaces.keys(): self.ifdown(interface) @@ -222,7 +222,7 @@ class ENINetConfig(os_net_config.NetConfig): for bridge in self.bridges.keys(): self.ifdown(bridge, iftype='bridge') - self.write_config(_network_config_path(), new_config) + self.write_config(_network_config_path(self.root_dir), new_config) if activate: for bridge in self.bridges.keys(): @@ -233,4 +233,4 @@ class ENINetConfig(os_net_config.NetConfig): else: logger.info('No interface changes are required.') - return {"/etc/network/interfaces": new_config} + return {_network_config_path(self.root_dir): new_config} diff --git a/os_net_config/impl_ifcfg.py b/os_net_config/impl_ifcfg.py index 0fe8cc5..99273bc 100644 --- a/os_net_config/impl_ifcfg.py +++ b/os_net_config/impl_ifcfg.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2014 Red Hat, Inc. +# Copyright 2014-2015 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -45,8 +45,8 @@ def cleanup_pattern(): class IfcfgNetConfig(os_net_config.NetConfig): """Configure network interfaces using the ifcfg format.""" - def __init__(self, noop=False): - super(IfcfgNetConfig, self).__init__(noop) + def __init__(self, noop=False, root_dir=''): + super(IfcfgNetConfig, self).__init__(noop, root_dir) self.interface_data = {} self.route_data = {} self.bridge_data = {} @@ -250,8 +250,8 @@ class IfcfgNetConfig(os_net_config.NetConfig): for interface_name, iface_data in self.interface_data.iteritems(): route_data = self.route_data.get(interface_name, '') - interface_path = ifcfg_config_path(interface_name) - route_path = route_config_path(interface_name) + interface_path = self.root_dir + ifcfg_config_path(interface_name) + route_path = self.root_dir + route_config_path(interface_name) all_file_names.append(interface_path) all_file_names.append(route_path) if (utils.diff(interface_path, iface_data) or @@ -265,8 +265,8 @@ class IfcfgNetConfig(os_net_config.NetConfig): for bridge_name, bridge_data in self.bridge_data.iteritems(): route_data = self.route_data.get(bridge_name, '') - bridge_path = bridge_config_path(bridge_name) - bridge_route_path = route_config_path(bridge_name) + bridge_path = self.root_dir + bridge_config_path(bridge_name) + bridge_route_path = self.root_dir + route_config_path(bridge_name) all_file_names.append(bridge_path) all_file_names.append(bridge_route_path) if (utils.diff(bridge_path, bridge_data) or diff --git a/os_net_config/tests/test_cli.py b/os_net_config/tests/test_cli.py index d3de720..310a527 100644 --- a/os_net_config/tests/test_cli.py +++ b/os_net_config/tests/test_cli.py @@ -112,3 +112,12 @@ class TestCli(base.TestCase): for dev in sanity_devices: self.assertIn(dev, stdout_yaml) self.assertEqual(stdout_yaml, stdout_json) + + def test_bridge_noop_rootfs(self): + for provider in ('ifcfg', 'eni'): + bond_yaml = os.path.join(SAMPLE_BASE, 'bridge_dhcp.yaml') + stdout_yaml, stderr = self.run_cli('ARG0 --provider=%s --noop ' + '--root-dir=/rootfs ' + '-c %s' % (provider, bond_yaml)) + self.assertEqual('', stderr) + self.assertIn('File: /rootfs/', stdout_yaml) diff --git a/os_net_config/tests/test_impl_eni.py b/os_net_config/tests/test_impl_eni.py index e5a712e..fe49c3e 100644 --- a/os_net_config/tests/test_impl_eni.py +++ b/os_net_config/tests/test_impl_eni.py @@ -209,7 +209,7 @@ class TestENINetConfigApply(base.TestCase): self.temp_config_file = tempfile.NamedTemporaryFile() self.ifup_interface_names = [] - def test_config_path(): + def test_config_path(prefix): return self.temp_config_file.name self.stubs.Set(impl_eni, '_network_config_path', test_config_path) -- cgit 1.2.3-korg