Example #1
0
 def vm(self, ip):
     """
     list vm related rules
     :param ip: vm ip
     :return:
     """
     debug("Try to show vm rules, ip=%s\n" % ip)
     port_id = get_port_id_from_ip(ip)
     debug('The port id is %s\n' % port_id)
     if not port_id:
         warn('No port id is found for ip=%s\n' % ip)
         return
     output(color_str('## IP = %s, port = %s\n' % (ip, port_id), 'r'))
     br_port = find_br_ports(port_id)
     if not br_port:
         warn('No br port is found for ip=%s\n' % ip)
         return
     debug('The br port is %s\n' % br_port)
     rules_dic = self._query_port_rules(br_port)
     if rules_dic:
         output(color_str( _format_str_iptables_rule_ % (
             'PKTS', 'SOURCE', 'DESTINATION', 'PROT', 'OTHER'), 'b'))
         for r in rules_dic:
             if rules_dic[r]:
                 output('%s:\n' % r)
                 self._fmt_show_rules(rules_dic[r])
Example #2
0
 def do_set(self, arg):
     """
     <bridge> set
     Set the default bridge
     """
     if not arg:
         output('Argument is missed\n')
     elif not br_exists(arg):
         output('The bridge does not exist.\n '
                'You can check available bridges using show\n')
     else:
         self.prompt = \
             color_str(PROMPT_KW[:-2] + ':%s> ' % color_str('b', arg), 'g')
         self.bridge = arg
         output('Set the default bridge to %s.\n' % self.bridge)
Example #3
0
 def __init__(self, stdin=sys.stdin, foreground=True):
     self.bridge = None  # default bridge
     self.ipt = IPtables()
     if foreground:
         output('EasyOVS %s, type help for information\n' % VERSION)
         self.prompt = color_str(PROMPT_KW, 'g')
         self.stdin = stdin
         self.in_poller = poll()
         self.in_poller.register(stdin)
         Cmd.__init__(self)
         output("***\n Welcome to EasyOVS,"
                "type help to see available commands.\n***\n")
         info('*** Starting CLI:\n')
         debug("==Loading credentials==\n")
         debug("auth_url = %s\n" % os.getenv('OS_AUTH_URL') or
               cfg.CONF.OS.auth_url)
         debug("username = %s\n" % os.getenv('OS_USERNAME') or
               cfg.CONF.OS.username)
         passwd = os.getenv('OS_PASSWORD') or cfg.CONF.OS.password
         passwd = passwd[:len(passwd)/4] + "****" + passwd[-len(passwd)/4:]
         debug("password = %s\n" % passwd)
         debug("tenant_name = %s\n" % os.getenv('OS_TENANT_NAME') or
               cfg.CONF.OS.tenant_name)
         while True:
             try:
                 #if self.isatty():
                 #quietRun( 'stty sane' )
                 self.cmdloop()
                 break
             except KeyboardInterrupt:
                 info('\nInterrupt\n')
Example #4
0
def show_port_info(keywords):
    """
    :param keyword: might be the ip address or substring of the id string
    :return: related port
    """
    for keyword in keywords.replace(',', ' ').split():
        port = \
            neutron_handler.query_port_by_id(keyword) or \
            neutron_handler.query_port_by_ip(keyword)
        if port:
            output(color_str('## port_id = %s\n' % (port.get('id')), 'b'))
            for k in port:
                output('%s: %s\n' % (k, port.get(k)))
        else:
            output('%s\n' % (color_str('No port is found, please '
                                       'check your tenant info', 'r')))
Example #5
0
def br_show(bridge_name):
    """
    Show information of a given bridges.
    """
    ovs_ports = ovs_lib.OVSBridge(bridge_name).get_ports()
    if not ovs_ports:
        return
    neutron_ports = neutron_handler.get_neutron_ports()
    debug('get neutron_ports\n')
    content = []
    mac_ip_show = False
    for intf in ovs_ports:  # e.g., qvo-xxx, int-br-eth0, qr-xxx, tapxxx
        port, tag, intf_type = \
            ovs_ports[intf]['port'], ovs_ports[intf]['vlan'], ovs_ports[
                intf]['type']
        if neutron_ports and intf[3:] in neutron_ports:
            p = neutron_ports[intf[3:]]
            vm_ips = ','.join(map(lambda x: x.get('ip_address'),
                                  p['fixed_ips']))
            vm_mac = p.get('mac_address')
            mac_ip_show = True
        else:
            vm_ips, vm_mac = '', ''
        content.append((intf, port, tag, intf_type, vm_ips, vm_mac))
        # output('%-20s%-8s%-16s%-24s%-8s\n' %(intf,port,vmIP,vmMac,tag))
    content.sort(key=lambda x: x[1])  # sort by port
    content.sort(key=lambda x: x[4])  # sort by vm_ip
    content.sort(key=lambda x: x[3])  # sort by type
    output(color_str('%-20s%-12s%-8s%-12s'
                     % ('Intf', 'Port', 'Vlan', 'Type'), 'r'))
    if mac_ip_show:
        output(color_str('%-16s%-24s\n' % ('vmIP', 'vmMAC'), 'r'))
    else:
        output('\n')
    i = 0
    for _ in content:
        #color = ['w','g'][i%2]
        color = 'b'
        output(color_str('%-20s%-12s%-8s%-12s'
                         % (_[0], _[1], _[2], _[3]), color))
        if mac_ip_show:
            output(color_str('%-16s%-24s\n' % (_[4], _[5]), color))
        else:
            output('\n')
        i += 1
Example #6
0
 def show(self):
     """
     Print all rules in this chain
     :return:
     """
     output(color_str("chain=%s, policy=%s\n" % (self.name, self.policy),
                      'b'))
     for r in self.rules:
         r.show()
Example #7
0
 def show(self, chain=None):
     """
     Get rules from this table
     :param chain:
     :return:
     """
     output(color_str("===table=%s===\n" % self.name, 'r'))
     for cn in self.chains:
         if not chain or cn.upper() == chain.upper():
             self.chains[cn].show()
Example #8
0
 def do_exit(self, _arg):
     """
     Go up one level in the command mode structure. If
     already at the top level, exit from the command line
     interface and log out.
     """
     if self.bridge:
         self.bridge = None
         self.prompt = color_str(PROMPT_KW, 'g')
     else:
         return self.do_quit(_arg)
Example #9
0
 def fmt_output(self):
     if self.packet > 0:
         result = \
             color_str(self._format_str_ % (self.id, self.table,
                                            self.packet,
                                            self.priority,
                                            compress_mac_str(self.match),
                                            self.actions), 'b')
     else:
         result = \
             self._format_str_ \
             % (self.id, self.table, self.packet,
                self.priority, compress_mac_str(self.match), self.actions)
     output(result)
Example #10
0
 def banner_output():
     output(color_str('%-3s%-4s%-10s%-6s%-60s%-20s\n'
            % ('ID', 'TAB', 'PKT', 'PRI', 'MATCH', 'ACT'), 'g'))