From 185955d8b66c87518f12128e638dcbfc0faf5df7 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 2 Mar 2015 19:11:52 -0500 Subject: [PATCH] Import content from project-config patch --- README.rst | 2 +- babel.cfg | 1 - openstack-common.conf | 6 - process-config-drive/__init__.py | 19 --- .../__init__.py | 0 process_config_drive/cmd.py | 160 ++++++++++++++++++ process_config_drive/tests/__init__.py | 0 .../tests/base.py | 2 +- .../tests/test_process_config_drive.py | 6 +- requirements.txt | 6 - setup.cfg | 8 +- setup.py | 2 +- 12 files changed, 172 insertions(+), 40 deletions(-) delete mode 100644 babel.cfg delete mode 100644 openstack-common.conf delete mode 100644 process-config-drive/__init__.py rename {process-config-drive/tests => process_config_drive}/__init__.py (100%) create mode 100644 process_config_drive/cmd.py create mode 100644 process_config_drive/tests/__init__.py rename {process-config-drive => process_config_drive}/tests/base.py (93%) rename process-config-drive/tests/test_process-config-drive.py => process_config_drive/tests/test_process_config_drive.py (87%) delete mode 100644 requirements.txt diff --git a/README.rst b/README.rst index 6da9ad0..82312be 100644 --- a/README.rst +++ b/README.rst @@ -12,4 +12,4 @@ Simple program to write static config from config-drive Features -------- -* TODO \ No newline at end of file +* TODO diff --git a/babel.cfg b/babel.cfg deleted file mode 100644 index efceab8..0000000 --- a/babel.cfg +++ /dev/null @@ -1 +0,0 @@ -[python: **.py] diff --git a/openstack-common.conf b/openstack-common.conf deleted file mode 100644 index 50adb39..0000000 --- a/openstack-common.conf +++ /dev/null @@ -1,6 +0,0 @@ -[DEFAULT] - -# The list of modules to copy from oslo-incubator.git - -# The base module to hold the copy of openstack.common -base=process-config-drive \ No newline at end of file diff --git a/process-config-drive/__init__.py b/process-config-drive/__init__.py deleted file mode 100644 index e8921e2..0000000 --- a/process-config-drive/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -# -*- coding: utf-8 -*- - -# 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 -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import pbr.version - - -__version__ = pbr.version.VersionInfo( - 'process-config-drive').version_string() \ No newline at end of file diff --git a/process-config-drive/tests/__init__.py b/process_config_drive/__init__.py similarity index 100% rename from process-config-drive/tests/__init__.py rename to process_config_drive/__init__.py diff --git a/process_config_drive/cmd.py b/process_config_drive/cmd.py new file mode 100644 index 0000000..51cbd40 --- /dev/null +++ b/process_config_drive/cmd.py @@ -0,0 +1,160 @@ +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# 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 a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import json +import platform +import sys + +post_up = " post-up route add -net {net} netmask {mask} gw {gw} || true\n" +pre_down = " pre-down route del -net {net} netmask {mask} gw {gw} || true\n" + + +def _write_rh_interface(name, interface): + files_to_write = dict() + results = """# Automatically generated, do not edit +DEVICE={name} +BOOTPROTO=static +HWADDR={hwaddr} +IPADDR={ip_address} +NETMASK={netmask} +ONBOOT=yes +NM_CONTROLLED=no +""".format( + name=name, + hwaddr=interface['mac_address'], + ip_address=interface['ip_address'], + netmask=interface['netmask'], + + ) + routes = [] + for route in interface['routes']: + if route['network'] == '0.0.0.0' and route['netmask'] == '0.0.0.0': + results += "DEFROUTE=yes\n" + results += "GATEWAY={gw}\n".format(gw=route['gateway']) + else: + routes.append(dict( + net=route['network'], mask=route['netmask'], + gw=route['gateway'])) + + if routes: + route_content = "" + for x in range(0, len(routes)): + route_content += "ADDRESS{x}={net}\n".format(x=x, **routes[x]) + route_content += "NETMASK{x}={mask}\n".format(x=x, **routes[x]) + route_content += "GATEWAY{x}={gw}\n".format(x=x, **routes[x]) + files_to_write['/etc/sysconfig/network-scripts/route-{name}'.format( + name=name)] = route_content + files_to_write['/etc/sysconfig/network-scripts/ifcfg-{name}'.format( + name=name)] = results + return files_to_write + + +def write_redhat_interfaces(interfaces): + files_to_write = dict() + for iname, interface in interfaces.items(): + if interface['type'] != 'ipv6': + interface_name = interface['id'].replace('network', 'eth') + files_to_write.update( + _write_rh_interface(interface_name, interface)) + return files_to_write + + +def write_debian_interfaces(interfaces): + results = "" + for iname, interface in interfaces.items(): + link_type = "inet" + if interface['type'] == 'ipv6': + link_type = "inet6" + interface_name = interface['id'].replace('network', 'eth') + results += "auto {0}\n".format(interface_name) + results += "iface {name} {link_type} static\n".format( + name=interface_name, link_type=link_type) + results += " address {0}\n".format(interface['ip_address']) + results += " netmask {0}\n".format(interface['netmask']) + for route in interface['routes']: + if route['network'] == '0.0.0.0' and route['netmask'] == '0.0.0.0': + results += " gateway {0}\n".format(route['gateway']) + else: + results += post_up.format( + net=route['network'], mask=route['netmask'], + gw=route['gateway']) + results += pre_down.format( + net=route['network'], mask=route['netmask'], + gw=route['gateway']) + return {'/etc/network/interfaces': results} + + +def write_dns_info(dns_servers): + results = "" + for server in dns_servers: + results += "nameserver {0}\n".format(server) + return {'/etc/resolv.conf': results} + + +def write_network_info(net, args): + + dns_servers = [ + f['address'] for f in net['services'] if f['type'] == 'dns' + ] + + interfaces = {} + + for network in net['networks']: + interfaces[network['link']] = network + for link in net['links']: + interfaces[link['id']]['mac_address'] = link['ethernet_mac_address'] + + distro = args.distro + if not distro: + distro = platform.dist()[0].lower() + if distro in ('debian', 'ubuntu'): + files_to_write = write_debian_interfaces(interfaces) + elif distro in ('redhat', 'centos', 'fedora', 'suse', 'opensuse'): + files_to_write = write_redhat_interfaces(interfaces) + files_to_write.update(write_dns_info(dns_servers)) + for k, v in files_to_write.items(): + if args.noop: + print("### Write {0}".format(k)) + print(v) + else: + with open(k, 'w') as outfile: + outfile.write(v) + + +def main(): + parser = argparse.ArgumentParser(description="Static network config") + parser.add_argument( + '-n', '--noop', action='store_true', help='Do not write files') + parser.add_argument( + '--distro', dest='distro', default=None, + help='Override detected distro') + args = parser.parse_args() + + meta_data = json.load(open('/mnt/config/openstack/latest/meta_data.json')) + with open('/root/.ssh/authorized_keys', 'a') as keys: + for (name, key) in meta_data['public_keys'].items(): + keys.write("# Injected key {name} by keypair extension\n".format( + name=name)) + keys.write(key) + keys.write('\n') + + v = json.load(open('/mnt/config/openstack/latest/vendor_data.json')) + if 'network_info' in v: + write_network_info(v['network_info'], args) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/process_config_drive/tests/__init__.py b/process_config_drive/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/process-config-drive/tests/base.py b/process_config_drive/tests/base.py similarity index 93% rename from process-config-drive/tests/base.py rename to process_config_drive/tests/base.py index 185fd6f..1c30cdb 100644 --- a/process-config-drive/tests/base.py +++ b/process_config_drive/tests/base.py @@ -20,4 +20,4 @@ from oslotest import base class TestCase(base.BaseTestCase): - """Test case base class for all unit tests.""" \ No newline at end of file + """Test case base class for all unit tests.""" diff --git a/process-config-drive/tests/test_process-config-drive.py b/process_config_drive/tests/test_process_config_drive.py similarity index 87% rename from process-config-drive/tests/test_process-config-drive.py rename to process_config_drive/tests/test_process_config_drive.py index a2ac7b8..972e28c 100644 --- a/process-config-drive/tests/test_process-config-drive.py +++ b/process_config_drive/tests/test_process_config_drive.py @@ -19,10 +19,10 @@ test_process-config-drive Tests for `process-config-drive` module. """ -from process-config-drive.tests import base +from process_config_drive.tests import base -class TestProcess-config-drive(base.TestCase): +class TestProcessConfigDrive(base.TestCase): def test_something(self): - pass \ No newline at end of file + pass diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index bc7131e..0000000 --- a/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -# The order of packages is significant, because pip processes them in the order -# of appearance. Changing the order has an impact on the overall integration -# process, which may cause wedges in the gate later. - -pbr>=0.6,!=0.7,<1.0 -Babel>=1.3 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 6349666..e624841 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,7 +22,11 @@ classifier = [files] packages = - process-config-drive + process_config_drive + +[entry_points] +console_scripts = + process-config-drive = process_config_drive.cmd:main [build_sphinx] source-dir = doc/source @@ -44,4 +48,4 @@ input_file = process-config-drive/locale/process-config-drive.pot [extract_messages] keywords = _ gettext ngettext l_ lazy_gettext mapping_file = babel.cfg -output_file = process-config-drive/locale/process-config-drive.pot \ No newline at end of file +output_file = process-config-drive/locale/process-config-drive.pot diff --git a/setup.py b/setup.py index 7eeb36b..70c2b3f 100755 --- a/setup.py +++ b/setup.py @@ -19,4 +19,4 @@ import setuptools setuptools.setup( setup_requires=['pbr'], - pbr=True) \ No newline at end of file + pbr=True)