Get rid of the static network topology file.

Now it will be auto generated based on the expected interfaces.

Change-Id: Ia9d29435b5687b4519b88dcdcf55b4f96ef563fd
Depends-On: Ic7cbdaa562301949483a0215b421f69099ae4d79
This commit is contained in:
Proskurin Kirill 2016-07-01 15:31:51 +02:00
parent 1881d944a2
commit 05c6e5aa0d
3 changed files with 111 additions and 19 deletions

View File

@ -4,13 +4,13 @@ import logging
import os
import pwd
import signal
import socket
import subprocess
import sys
import time
import etcd
import jinja2
import netifaces
import six
from six.moves.urllib import parse
import yaml
@ -18,7 +18,7 @@ import yaml
ENV = 'default'
GLOBALS_PATH = '/etc/mcp/globals/globals.yaml'
NETWORK_TOPOLOGY_FILE = "/etc/mcp/globals/network_topology.yaml"
META_FILE = "/etc/mcp/meta/meta.yaml"
WORKFLOW_PATH_TEMPLATE = '/etc/mcp/role/%s.yaml'
FILES_DIR = '/etc/mcp/files'
@ -30,6 +30,60 @@ LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
def get_ip_address(iface):
"""Get IP address of the interface connected to the network.
If there is no such an interface, then localhost is returned.
"""
if iface not in netifaces.interfaces():
LOG.warning("Can't find interface '%s' in the host list of interfaces",
iface)
return '127.0.0.1'
address_family = netifaces.AF_INET
if address_family not in netifaces.ifaddresses(iface):
LOG.warning("Interface '%s' doesnt configured with ipv4 address",
iface)
return '127.0.0.1'
for ifaddress in netifaces.ifaddresses(iface)[address_family]:
if 'addr' in ifaddress:
return ifaddress['addr']
else:
LOG.warning("Can't find ip addr for interface '%s'", iface)
return '127.0.0.1'
def create_network_topology(meta_info, variables):
"""Create a network topology config.
These config could be used in jinja2 templates to fetch needed variables
Example:
{{ network_topology["private"]["address"] }}
{{ network_topology["public"]["iface"] }}
"""
network_info = {}
if meta_info["host-net"]:
LOG.debug("Found 'host-net' flag, trying to fetch host network")
priv_iface = variables["private_interface"]
pub_iface = variables["public_interface"]
network_info = {"private": {"iface": priv_iface,
"address": get_ip_address(priv_iface)},
"public": {"iface": pub_iface,
"address": get_ip_address(pub_iface)}}
else:
LOG.debug("Can't find 'host-net' flag, fetching ip only from eth0")
network_info = {"private": {"iface": "eth0",
"address": get_ip_address("eth0")},
"public": {"iface": "eth0",
"address": get_ip_address("eth0")}}
LOG.debug("Network information\n%s", yaml.dump(network_info))
return network_info
def etcd_path(*path):
return os.path.join('/mcp', ENV, 'status', 'global', *path)
@ -183,22 +237,14 @@ def main():
LOG.info("Getting global variables from %s", GLOBALS_PATH)
with open(GLOBALS_PATH) as f:
variables = yaml.load(f)
with open(META_FILE) as f:
meta_info = yaml.load(f)
variables['role_name'] = role_name
LOG.debug('Global variables:\n%s', variables)
LOG.info("Getting network topology from %s", NETWORK_TOPOLOGY_FILE)
with open(NETWORK_TOPOLOGY_FILE) as f:
topology = yaml.load(f)
if topology:
hostname = socket.gethostname()
if topology.get("network", {}).get(hostname):
network_info = topology["network"][hostname]
else:
ip = socket.gethostbyname(socket.gethostname())
LOG.debug("Can't find hostname '%s' in network topology file",
socket.gethostname())
network_info = {"private": {"iface": "eth0", "address": ip}}
LOG.debug("Network information\n%s", yaml.dump(network_info))
variables["network_topology"] = network_info
LOG.debug("Getting meta info from %s", META_FILE)
LOG.debug("Creating network topology configuration")
variables["network_topology"] = create_network_topology(meta_info,
variables)
workflow_path = WORKFLOW_PATH_TEMPLATE % role_name
LOG.info("Getting workflow from %s", workflow_path)

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
"""
test_ms_ext_config
----------------------------------
@ -19,10 +20,54 @@ test_ms_ext_config
Tests for `ms_ext_config` module.
"""
import mock
from ms_ext_config import start_script
from ms_ext_config.tests import base
class TestMs_ext_config(base.TestCase):
def test_something(self):
pass
def setUp(self):
super(TestMs_ext_config, self).setUp()
self.private_iface = 'eth0'
self.public_iface = 'eth1'
@mock.patch('netifaces.interfaces')
@mock.patch('netifaces.ifaddresses')
def test_get_ip_address_iface_wrong(self, m_ifaddresses, m_interfaces):
m_interfaces.return_value = ['eth10', 'eth99']
r_value = start_script.get_ip_address(self.private_iface)
self.assertEqual('127.0.0.1', r_value)
self.assertEqual(1, len(m_interfaces.mock_calls))
self.assertEqual(0, len(m_ifaddresses.mock_calls))
@mock.patch('netifaces.interfaces')
@mock.patch('netifaces.ifaddresses')
def test_get_ip_address_address_family_wrong(self, m_ifaddresses,
m_interfaces):
m_interfaces.return_value = ['eth0', 'eth99']
m_ifaddresses.return_value = {3: [{"addr": "8.8.8.8"}]}
r_value = start_script.get_ip_address(self.private_iface)
self.assertEqual('127.0.0.1', r_value)
self.assertEqual(1, len(m_interfaces.mock_calls))
self.assertEqual(1, len(m_ifaddresses.mock_calls))
@mock.patch('netifaces.interfaces')
@mock.patch('netifaces.ifaddresses')
def test_get_ip_address_address_wrong(self, m_ifaddresses, m_interfaces):
m_interfaces.return_value = ['eth0', 'eth99']
m_ifaddresses.return_value = {2: [{"notaddr": "8.8.8.8"}]}
r_value = start_script.get_ip_address(self.private_iface)
self.assertEqual('127.0.0.1', r_value)
self.assertEqual(1, len(m_interfaces.mock_calls))
self.assertEqual(2, len(m_ifaddresses.mock_calls))
@mock.patch('netifaces.interfaces')
@mock.patch('netifaces.ifaddresses')
def test_get_ip_address_address_good(self, m_ifaddresses, m_interfaces):
m_interfaces.return_value = ['eth0', 'eth99']
m_ifaddresses.return_value = {2: [{"addr": "8.8.8.8"}]}
r_value = start_script.get_ip_address(self.private_iface)
self.assertEqual('8.8.8.8', r_value)
self.assertEqual(1, len(m_interfaces.mock_calls))
self.assertEqual(2, len(m_ifaddresses.mock_calls))

View File

@ -6,4 +6,5 @@ pbr>=1.6
Jinja2>=2.8 # BSD License (3 clause)
python-etcd
PyYAML>=3.1.0 # MIT
six>=1.9.0 # MIT
six>=1.9.0 # MIT
netifaces>=0.10.4 # MIT