Import content from project-config patch

This commit is contained in:
Monty Taylor 2015-03-02 19:11:52 -05:00
parent f9300492eb
commit 185955d8b6
12 changed files with 172 additions and 40 deletions

View File

@ -12,4 +12,4 @@ Simple program to write static config from config-drive
Features
--------
* TODO
* TODO

View File

@ -1 +0,0 @@
[python: **.py]

View File

@ -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

View File

@ -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()

160
process_config_drive/cmd.py Normal file
View File

@ -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())

View File

View File

@ -20,4 +20,4 @@ from oslotest import base
class TestCase(base.BaseTestCase):
"""Test case base class for all unit tests."""
"""Test case base class for all unit tests."""

View File

@ -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
pass

View File

@ -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

View File

@ -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
output_file = process-config-drive/locale/process-config-drive.pot

View File

@ -19,4 +19,4 @@ import setuptools
setuptools.setup(
setup_requires=['pbr'],
pbr=True)
pbr=True)