1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Linux namespace utilities library."""
16 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
19 class Namespaces(object):
20 """Linux namespace utilities."""
24 def create_namespace(self, node, namespace_name):
25 """Create namespace and add the name to the list for later clean-up.
27 :param node: Where to create namespace.
28 :param namespace_name: Name for namespace.
30 :type namespace_name: str
32 cmd = ('ip netns add {0}'.format(namespace_name))
33 exec_cmd_no_error(node, cmd, sudo=True)
34 self._namespaces.append(namespace_name)
37 def attach_interface_to_namespace(node, namespace, interface):
38 """Attach specific interface to namespace.
40 :param node: Node where to execute command.
41 :param namespace: Namespace to execute command on.
42 :param interface: Interface in namespace.
46 :raises RuntimeError: Interface could not be attached.
48 cmd = 'ip link set {0} netns {1}'.format(interface, namespace)
49 (ret_code, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
52 'Could not attach interface, reason:{}'.format(stderr))
53 cmd = 'ip netns exec {} ip link set {} up'.format(
55 (ret_code, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
58 'Could not set interface state, reason:{}'.format(stderr))
61 def create_bridge_for_int_in_namespace(
62 node, namespace, bridge_name, *interfaces):
63 """Setup bridge domain and add interfaces to it.
65 :param node: Node where to execute command.
66 :param namespace: Namespace to execute command on.
67 :param bridge_name: Name of the bridge to be created.
68 :param interfaces: List of interfaces to add to the namespace.
71 :type bridge_name: str
72 :type interfaces: list
74 cmd = 'ip netns exec {} brctl addbr {}'.format(namespace, bridge_name)
75 exec_cmd_no_error(node, cmd, sudo=True)
76 for interface in interfaces:
77 cmd = 'ip netns exec {} brctl addif {} {}'.format(
78 namespace, bridge_name, interface)
79 exec_cmd_no_error(node, cmd, sudo=True)
80 cmd = 'ip netns exec {} ip link set dev {} up'.format(
81 namespace, bridge_name)
82 exec_cmd_no_error(node, cmd, sudo=True)
84 def clean_up_namespaces(self, node):
85 """Remove all old namespaces.
87 :param node: Node where to execute command.
89 :raises RuntimeError: Namespaces could not be cleaned properly.
91 for namespace in self._namespaces:
92 print "Cleaning namespace {}".format(namespace)
93 cmd = 'ip netns delete {}'.format(namespace)
94 (ret_code, _, _) = exec_cmd(node, cmd, timeout=5, sudo=True)
96 raise RuntimeError('Could not delete namespace')