Exemplo n.º 1
0
 def teardown_link(self, interface):
     """ip link
     """
     cmd = ['ip', 'link', 'delete', interface]
     stdcode, stdout = agent_utils.execute(cmd, root=True)
     if stdcode == 0:
         return agent_utils.make_response(code=stdcode)
     # execute failed.
     message = stdout.pop(0)
     return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 2
0
 def teardown_link(self, interface):
     """ip link
     """
     cmd = ['ip', 'link', 'delete', interface]
     stdcode, stdout = agent_utils.execute(cmd, root=True)
     if stdcode == 0:
         return agent_utils.make_response(code=stdcode)
     # execute failed.
     message = stdout.pop(0)
     return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 3
0
    def setup_link(self, interface, cidr):
        """Setup a link.

        ip addr add dev interface
        ip link  set dev interface up
        """
        # clear old ipaddr in interface
        cmd = ["ip", "addr", "flush", "dev", interface]
        agent_utils.execute(cmd, root=True)
        ip = IPNetwork(cidr)
        cmd = ["ip", "addr", "add", cidr, "broadcast", str(ip.broadcast), "dev", interface]
        stdcode, stdout = agent_utils.execute(cmd, root=True)
        if stdcode == 0:
            cmd = ["ip", "link", "set", "dev", interface, "up"]
            stdcode, stdout = agent_utils.execute(cmd, root=True)
            if stdcode == 0:
                return agent_utils.make_response(code=stdcode)
        # execute failed.
        message = stdout.pop(0)
        return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 4
0
    def setup_link(self, interface, cidr):
        """Setup a link.

        ip addr add dev interface
        ip link  set dev interface up
        """
        # clear old ipaddr in interface
        cmd = ['ip', 'addr', 'flush', 'dev', interface]
        agent_utils.execute(cmd, root=True)
        ip = IPNetwork(cidr)
        cmd = ['ip', 'addr', 'add', cidr, 'broadcast',
               str(ip.broadcast), 'dev', interface]
        stdcode, stdout = agent_utils.execute(cmd, root=True)
        if stdcode == 0:
            cmd = ['ip', 'link', 'set', 'dev', interface, 'up']
            stdcode, stdout = agent_utils.execute(cmd, root=True)
            if stdcode == 0:
                return agent_utils.make_response(code=stdcode)
        # execute failed.
        message = stdout.pop(0)
        return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 5
0
    def setup_link(self, interface, cidr):
        """Setup a link.

        ip addr add dev interface
        ip link  set dev interface up
        """
        # clear old ipaddr in interface
        cmd = ['ip', 'addr', 'flush', 'dev', interface]
        agent_utils.execute(cmd, root=True)
        ip = IPNetwork(cidr)
        cmd = [
            'ip', 'addr', 'add', cidr, 'broadcast',
            str(ip.broadcast), 'dev', interface
        ]
        stdcode, stdout = agent_utils.execute(cmd, root=True)
        if stdcode == 0:
            cmd = ['ip', 'link', 'set', 'dev', interface, 'up']
            stdcode, stdout = agent_utils.execute(cmd, root=True)
            if stdcode == 0:
                return agent_utils.make_response(code=stdcode)
        # execute failed.
        message = stdout.pop(0)
        return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 6
0
    def add_vlan_to_interface(self, interface, vlan_id):
        """Add vlan interface.

        ip link add link eth0 name eth0.10 type vlan id 10
        """
        subif = "%s.%s" % (interface, vlan_id)
        vlan_id = "%s" % vlan_id
        cmd = ["ip", "link", "add", "link", interface, "name", subif, "type", "vlan", "id", vlan_id]
        stdcode, stdout = agent_utils.execute(cmd, root=True)
        if stdcode == 0:
            return agent_utils.make_response(code=stdcode)
        # execute failed.
        message = stdout.pop(0)
        return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 7
0
    def add_vlan_to_interface(self, interface, vlan_id):
        """Add vlan interface.

        ip link add link eth0 name eth0.10 type vlan id 10
        """
        subif = '%s.%s' % (interface, vlan_id)
        vlan_id = '%s' % vlan_id
        cmd = ['ip', 'link', 'add', 'link', interface, 'name',
               subif, 'type', 'vlan', 'id', vlan_id]
        stdcode, stdout = agent_utils.execute(cmd, root=True)
        if stdcode == 0:
            return agent_utils.make_response(code=stdcode)
        # execute failed.
        message = stdout.pop(0)
        return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 8
0
    def add_vlan_to_interface(self, interface, vlan_id):
        """Add vlan interface.

        ip link add link eth0 name eth0.10 type vlan id 10
        """
        subif = '%s.%s' % (interface, vlan_id)
        vlan_id = '%s' % vlan_id
        cmd = [
            'ip', 'link', 'add', 'link', interface, 'name', subif, 'type',
            'vlan', 'id', vlan_id
        ]
        stdcode, stdout = agent_utils.execute(cmd, root=True)
        if stdcode == 0:
            return agent_utils.make_response(code=stdcode)
        # execute failed.
        message = stdout.pop(0)
        return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 9
0
    def check_ports_on_br(self, bridge="br-ex", ports=["eth3"]):
        """Check ports exist on bridge.

        ovs-vsctl list-ports bridge
        """
        LOG.info("RPC: check_ports_on_br bridge: %s, ports: %s" % (bridge, ports))
        cmd = ["ovs-vsctl", "list-ports", bridge]
        stdcode, stdout = agent_utils.execute(cmd, root=True)
        data = dict()
        if stdcode == 0:
            for port in ports:
                if port in stdout:
                    data[port] = True
                    stdout.remove(port)
                else:
                    data[port] = False
            return agent_utils.make_response(code=stdcode, data=data)
        # execute failed.
        message = stdout.pop(0)
        return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 10
0
    def check_ports_on_br(self, bridge='br-ex', ports=['eth3']):
        """Check ports exist on bridge.

        ovs-vsctl list-ports bridge
        """
        LOG.info("RPC: check_ports_on_br bridge: %s, ports: %s" %
                 (bridge, ports))
        cmd = ['ovs-vsctl', 'list-ports', bridge]
        stdcode, stdout = agent_utils.execute(cmd, root=True)
        data = dict()
        if stdcode == 0:
            for port in ports:
                if port in stdout:
                    data[port] = True
                    stdout.remove(port)
                else:
                    data[port] = False
            return agent_utils.make_response(code=stdcode, data=data)
        # execute failed.
        message = stdout.pop(0)
        return agent_utils.make_response(code=stdcode, message=message)
Exemplo n.º 11
0
    def ping(self, ips, boardcast=False, count=2, timeout=2, interface=None):
        """Ping host or broadcast.

        ping host -c 2 -W 2
        """
        cmd = ['ping', '-c', str(count), '-W', str(timeout)]
        True if not interface else cmd.extend(['-I', interface])
        True if not boardcast else cmd.append('-b')
        # Batch create subprocess
        data = dict()
        try:
            for ip in ips:
                stdcode, stdout = agent_utils.execute(cmd + [ip])
                if stdcode:
                    data[ip] = 100
                else:
                    pattern = r',\s([0-9]+)%\spacket\sloss'
                    data[ip] = re.search(pattern, stdout[-2]).groups()[0]
            return agent_utils.make_response(code=0, data=data)
        except Exception as e:
            message = e.message
            return agent_utils.make_response(code=1, message=message)
Exemplo n.º 12
0
    def ping(self, ips, boardcast=False, count=2, timeout=2, interface=None):
        """Ping host or broadcast.

        ping host -c 2 -W 2
        """
        cmd = ["ping", "-c", str(count), "-W", str(timeout)]
        True if not interface else cmd.extend(["-I", interface])
        True if not boardcast else cmd.append("-b")
        # Batch create subprocess
        data = dict()
        try:
            for ip in ips:
                stdcode, stdout = agent_utils.execute(cmd + [ip])
                if stdcode:
                    data[ip] = 100
                else:
                    pattern = r",\s([0-9]+)%\spacket\sloss"
                    data[ip] = re.search(pattern, stdout[-2]).groups()[0]
            return agent_utils.make_response(code=0, data=data)
        except Exception as e:
            message = e.message
            return agent_utils.make_response(code=1, message=message)
Exemplo n.º 13
0
 def test_execute(self):
     expected = "%s\n" % self.test_file
     code, result = utils.execute(["ls", self.test_file])
     self.assertEqual(0, code)
     self.assertEqual(result.pop(), expected.strip('\n'))
Exemplo n.º 14
0
 def test_execute(self):
     expected = "%s\n" % self.test_file
     code, result = utils.execute(["ls", self.test_file])
     self.assertEqual(0, code)
     self.assertEqual(result.pop(), expected.strip("\n"))