2018-09-20 03:25:58 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
# network.py - PVC client function library, Network fuctions
|
2018-09-20 03:25:58 -04:00
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Joshua M. Boniface <joshua@boniface.me>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import uuid
|
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
import subprocess
|
|
|
|
import difflib
|
|
|
|
import colorama
|
|
|
|
import click
|
|
|
|
import lxml.objectify
|
|
|
|
import configparser
|
|
|
|
import kazoo.client
|
|
|
|
|
2018-10-20 15:28:25 -04:00
|
|
|
import client_lib.ansiprint as ansiprint
|
2018-09-25 01:47:29 -04:00
|
|
|
import client_lib.zkhandler as zkhandler
|
2018-09-23 15:26:20 -04:00
|
|
|
import client_lib.common as common
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Cluster search functions
|
|
|
|
#
|
2018-09-23 15:26:20 -04:00
|
|
|
def getClusterNetworkList(zk_conn):
|
|
|
|
# Get a list of VNIs by listing the children of /networks
|
2018-10-28 18:13:28 -04:00
|
|
|
vni_list = zkhandler.listchildren(zk_conn, '/networks')
|
2018-09-23 15:26:20 -04:00
|
|
|
description_list = []
|
|
|
|
# For each VNI, get the corresponding description from the data
|
|
|
|
for vni in vni_list:
|
2018-10-28 18:13:28 -04:00
|
|
|
description_list.append(zkhandler.readdata(zk_conn, '/networks/{}'.format(vni)))
|
2018-09-23 15:26:20 -04:00
|
|
|
return vni_list, description_list
|
|
|
|
|
|
|
|
def searchClusterByVNI(zk_conn, vni):
|
2018-09-20 03:25:58 -04:00
|
|
|
try:
|
|
|
|
# Get the lists
|
2018-09-23 15:26:20 -04:00
|
|
|
vni_list, description_list = getClusterNetworkList(zk_conn)
|
2018-09-20 03:25:58 -04:00
|
|
|
# We're looking for UUID, so find that element ID
|
2018-09-23 15:26:20 -04:00
|
|
|
index = vni_list.index(vni)
|
2018-09-20 03:25:58 -04:00
|
|
|
# Get the name_list element at that index
|
2018-09-23 15:26:20 -04:00
|
|
|
description = description_list[index]
|
2018-09-20 03:25:58 -04:00
|
|
|
except ValueError:
|
|
|
|
# We didn't find anything
|
|
|
|
return None
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return description
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def searchClusterByDescription(zk_conn, description):
|
2018-09-20 03:25:58 -04:00
|
|
|
try:
|
|
|
|
# Get the lists
|
2018-09-23 15:26:20 -04:00
|
|
|
vni_list, description_list = getClusterNetworkList(zk_conn)
|
2018-09-20 03:25:58 -04:00
|
|
|
# We're looking for name, so find that element ID
|
2018-09-23 15:26:20 -04:00
|
|
|
index = description_list.index(description)
|
2018-09-20 03:25:58 -04:00
|
|
|
# Get the uuid_list element at that index
|
2018-09-23 15:26:20 -04:00
|
|
|
vni = vni_list[index]
|
2018-09-20 03:25:58 -04:00
|
|
|
except ValueError:
|
|
|
|
# We didn't find anything
|
|
|
|
return None
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return vni
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def getNetworkVNI(zk_conn, network):
|
|
|
|
# Validate and obtain alternate passed value
|
|
|
|
if network.isdigit():
|
|
|
|
net_description = searchClusterByVNI(zk_conn, network)
|
|
|
|
net_vni = searchClusterByDescription(zk_conn, net_description)
|
|
|
|
else:
|
|
|
|
net_vni = searchClusterByDescription(zk_conn, network)
|
|
|
|
net_description = searchClusterByVNI(zk_conn, net_vni)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return net_vni
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def getNetworkDescription(zk_conn, network):
|
2018-09-20 03:25:58 -04:00
|
|
|
# Validate and obtain alternate passed value
|
2018-09-23 15:26:20 -04:00
|
|
|
if network.isdigit():
|
|
|
|
net_description = searchClusterByVNI(zk_conn, network)
|
|
|
|
net_vni = searchClusterByDescription(zk_conn, net_description)
|
2018-09-20 03:25:58 -04:00
|
|
|
else:
|
2018-09-23 15:26:20 -04:00
|
|
|
net_vni = searchClusterByDescription(zk_conn, network)
|
|
|
|
net_description = searchClusterByVNI(zk_conn, net_vni)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return net_description
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-02 00:02:23 -04:00
|
|
|
def getNetworkDHCPLeases(zk_conn, vni):
|
2018-11-18 00:55:04 -05:00
|
|
|
# Get a list of DHCP leases by listing the children of /networks/<vni>/dhcp4_leases
|
|
|
|
dhcp4_leases = zkhandler.listchildren(zk_conn, '/networks/{}/dhcp4_leases'.format(vni))
|
|
|
|
return sorted(dhcp4_leases)
|
2018-10-02 00:02:23 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def getNetworkDHCPReservations(zk_conn, vni):
|
2018-11-18 00:55:04 -05:00
|
|
|
# Get a list of DHCP reservations by listing the children of /networks/<vni>/dhcp4_reservations
|
|
|
|
dhcp4_reservations = zkhandler.listchildren(zk_conn, '/networks/{}/dhcp4_reservations'.format(vni))
|
|
|
|
return sorted(dhcp4_reservations)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-17 20:05:22 -04:00
|
|
|
def getNetworkACLs(zk_conn, vni, _direction):
|
2018-10-17 00:23:27 -04:00
|
|
|
# Get the (sorted) list of active ACLs
|
2018-10-17 20:05:22 -04:00
|
|
|
if _direction == 'both':
|
|
|
|
directions = ['in', 'out']
|
|
|
|
else:
|
|
|
|
directions = [_direction]
|
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
full_acl_list = []
|
2018-10-17 20:05:22 -04:00
|
|
|
for direction in directions:
|
|
|
|
unordered_acl_list = zkhandler.listchildren(zk_conn, '/networks/{}/firewall_rules/{}'.format(vni, direction))
|
2018-11-02 00:42:44 -04:00
|
|
|
ordered_acls = dict()
|
2018-10-17 20:05:22 -04:00
|
|
|
for acl in unordered_acl_list:
|
|
|
|
order = zkhandler.readdata(zk_conn, '/networks/{}/firewall_rules/{}/{}/order'.format(vni, direction, acl))
|
|
|
|
ordered_acls[order] = acl
|
2018-10-17 00:23:27 -04:00
|
|
|
|
2018-10-17 20:05:22 -04:00
|
|
|
for order in sorted(ordered_acls.keys()):
|
|
|
|
full_acl_list.append({'direction': direction, 'description': ordered_acls[order]})
|
2018-10-17 00:23:27 -04:00
|
|
|
|
|
|
|
return full_acl_list
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def getNetworkInformation(zk_conn, vni):
|
2018-09-29 02:54:48 -04:00
|
|
|
description = zkhandler.readdata(zk_conn, '/networks/{}'.format(vni))
|
2019-03-15 11:28:49 -04:00
|
|
|
nettype = zkhandler.readdata(zk_conn, '/networks/{}/nettype'.format(vni))
|
2018-09-29 02:54:48 -04:00
|
|
|
domain = zkhandler.readdata(zk_conn, '/networks/{}/domain'.format(vni))
|
2018-11-13 01:22:15 -05:00
|
|
|
ip6_network = zkhandler.readdata(zk_conn, '/networks/{}/ip6_network'.format(vni))
|
|
|
|
ip6_gateway = zkhandler.readdata(zk_conn, '/networks/{}/ip6_gateway'.format(vni))
|
|
|
|
dhcp6_flag = zkhandler.readdata(zk_conn, '/networks/{}/dhcp6_flag'.format(vni))
|
|
|
|
ip4_network = zkhandler.readdata(zk_conn, '/networks/{}/ip4_network'.format(vni))
|
|
|
|
ip4_gateway = zkhandler.readdata(zk_conn, '/networks/{}/ip4_gateway'.format(vni))
|
|
|
|
dhcp4_flag = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_flag'.format(vni))
|
|
|
|
dhcp4_start = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_start'.format(vni))
|
|
|
|
dhcp4_end = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_end'.format(vni))
|
2019-03-15 11:28:49 -04:00
|
|
|
return description, nettype, domain, ip6_network, ip6_gateway, dhcp6_flag, ip4_network, ip4_gateway, dhcp4_flag, dhcp4_start, dhcp4_end
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-02 00:02:23 -04:00
|
|
|
def getDHCPLeaseInformation(zk_conn, vni, mac_address):
|
2018-11-18 00:55:04 -05:00
|
|
|
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_leases/{}/hostname'.format(vni, mac_address))
|
|
|
|
ip4_address = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_leases/{}/ipaddr'.format(vni, mac_address))
|
2018-10-03 19:23:46 -04:00
|
|
|
try:
|
2018-11-18 00:55:04 -05:00
|
|
|
timestamp = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_leases/{}/expiry'.format(vni, mac_address))
|
2018-10-03 19:23:46 -04:00
|
|
|
except:
|
|
|
|
timestamp = 'static'
|
2018-11-13 01:22:15 -05:00
|
|
|
return hostname, ip4_address, mac_address, timestamp
|
2018-09-28 20:31:56 -04:00
|
|
|
|
2018-10-03 23:16:31 -04:00
|
|
|
def getDHCPReservationInformation(zk_conn, vni, mac_address):
|
2018-11-18 00:55:04 -05:00
|
|
|
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_reservations/{}/hostname'.format(vni, mac_address))
|
|
|
|
ip4_address = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_reservations/{}/ipaddr'.format(vni, mac_address))
|
2018-10-03 23:16:31 -04:00
|
|
|
timestamp = 'static'
|
2018-11-13 01:22:15 -05:00
|
|
|
return hostname, ip4_address, mac_address, timestamp
|
2018-10-03 23:16:31 -04:00
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
def getACLInformation(zk_conn, vni, direction, description):
|
|
|
|
order = zkhandler.readdata(zk_conn, '/networks/{}/firewall_rules/{}/{}/order'.format(vni, direction, description))
|
|
|
|
rule = zkhandler.readdata(zk_conn, '/networks/{}/firewall_rules/{}/{}/rule'.format(vni, direction, description))
|
|
|
|
return order, description, rule
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def formatNetworkInformation(zk_conn, vni, long_output):
|
2019-03-15 11:28:49 -04:00
|
|
|
description, nettype, domain, ip6_network, ip6_gateway, dhcp6_flag, ip4_network, ip4_gateway, dhcp4_flag, dhcp4_start, dhcp4_end = getNetworkInformation(zk_conn, vni)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-11-13 01:22:15 -05:00
|
|
|
if dhcp6_flag == "True":
|
|
|
|
dhcp6_flag_colour = ansiprint.green()
|
2018-09-25 13:58:52 -04:00
|
|
|
else:
|
2018-11-13 01:22:15 -05:00
|
|
|
dhcp6_flag_colour = ansiprint.blue()
|
|
|
|
if dhcp4_flag == "True":
|
|
|
|
dhcp4_flag_colour = ansiprint.green()
|
|
|
|
else:
|
|
|
|
dhcp4_flag_colour = ansiprint.blue()
|
2018-10-20 15:28:25 -04:00
|
|
|
colour_off = ansiprint.end()
|
2018-09-25 13:58:52 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
# Format a nice output: do this line-by-line then concat the elements at the end
|
|
|
|
ainformation = []
|
2018-10-20 15:28:25 -04:00
|
|
|
ainformation.append('{}Virtual network information:{}'.format(ansiprint.bold(), ansiprint.end()))
|
2018-09-23 15:26:20 -04:00
|
|
|
ainformation.append('')
|
|
|
|
# Basic information
|
2018-11-13 01:22:15 -05:00
|
|
|
ainformation.append('{}VNI:{} {}'.format(ansiprint.purple(), ansiprint.end(), vni))
|
2019-03-15 11:28:49 -04:00
|
|
|
ainformation.append('{}Type:{} {}'.format(ansiprint.purple(), ansiprint.end(), nettype))
|
2018-11-13 01:22:15 -05:00
|
|
|
ainformation.append('{}Description:{} {}'.format(ansiprint.purple(), ansiprint.end(), description))
|
2019-03-15 11:28:49 -04:00
|
|
|
if nettype == 'managed':
|
|
|
|
ainformation.append('{}Domain:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain))
|
|
|
|
if ip6_network != "None":
|
2018-09-23 15:26:20 -04:00
|
|
|
ainformation.append('')
|
2019-03-15 11:28:49 -04:00
|
|
|
ainformation.append('{}IPv6 network:{} {}'.format(ansiprint.purple(), ansiprint.end(), ip6_network))
|
|
|
|
ainformation.append('{}IPv6 gateway:{} {}'.format(ansiprint.purple(), ansiprint.end(), ip6_gateway))
|
|
|
|
ainformation.append('{}DHCPv6 enabled:{} {}{}{}'.format(ansiprint.purple(), ansiprint.end(), dhcp6_flag_colour, dhcp6_flag, colour_off))
|
|
|
|
if ip4_network != "None":
|
2018-09-23 15:26:20 -04:00
|
|
|
ainformation.append('')
|
2019-03-15 11:28:49 -04:00
|
|
|
ainformation.append('{}IPv4 network:{} {}'.format(ansiprint.purple(), ansiprint.end(), ip4_network))
|
|
|
|
ainformation.append('{}IPv4 gateway:{} {}'.format(ansiprint.purple(), ansiprint.end(), ip4_gateway))
|
|
|
|
ainformation.append('{}DHCPv4 enabled:{} {}{}{}'.format(ansiprint.purple(), ansiprint.end(), dhcp4_flag_colour, dhcp4_flag, colour_off))
|
|
|
|
if dhcp4_flag == "True":
|
|
|
|
ainformation.append('{}DHCPv4 range:{} {} - {}'.format(ansiprint.purple(), ansiprint.end(), dhcp4_start, dhcp4_end))
|
|
|
|
|
|
|
|
if long_output:
|
|
|
|
dhcp4_reservations_list = getNetworkDHCPReservations(zk_conn, vni)
|
|
|
|
if dhcp4_reservations_list:
|
|
|
|
ainformation.append('')
|
|
|
|
ainformation.append('{}Client DHCPv4 reservations:{}'.format(ansiprint.bold(), ansiprint.end()))
|
|
|
|
ainformation.append('')
|
|
|
|
# Only show static reservations in the detailed information
|
|
|
|
dhcp4_reservations_string = formatDHCPLeaseList(zk_conn, vni, dhcp4_reservations_list, reservations=True)
|
|
|
|
for line in dhcp4_reservations_string.split('\n'):
|
|
|
|
ainformation.append(line)
|
|
|
|
|
|
|
|
firewall_rules = zkhandler.listchildren(zk_conn, '/networks/{}/firewall_rules'.format(vni))
|
|
|
|
if firewall_rules:
|
|
|
|
ainformation.append('')
|
|
|
|
ainformation.append('{}Network firewall rules:{}'.format(ansiprint.bold(), ansiprint.end()))
|
|
|
|
ainformation.append('')
|
|
|
|
formatted_firewall_rules = get_list_firewall_rules(zk_conn, vni)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
# Join it all together
|
|
|
|
information = '\n'.join(ainformation)
|
|
|
|
return information
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-28 20:31:56 -04:00
|
|
|
def formatNetworkList(zk_conn, net_list):
|
|
|
|
net_list_output = []
|
2019-03-15 11:28:49 -04:00
|
|
|
nettype = dict()
|
2018-11-02 00:42:44 -04:00
|
|
|
description = dict()
|
|
|
|
domain = dict()
|
2018-11-13 01:22:15 -05:00
|
|
|
v6_flag = dict()
|
|
|
|
v6_flag_colour = dict()
|
|
|
|
ip6_network = dict()
|
|
|
|
ip6_gateway = dict()
|
|
|
|
dhcp6_flag = dict()
|
|
|
|
dhcp6_flag_colour = dict()
|
|
|
|
v4_flag = dict()
|
|
|
|
v4_flag_colour = dict()
|
|
|
|
ip4_network = dict()
|
|
|
|
ip4_gateway = dict()
|
|
|
|
dhcp4_flag = dict()
|
|
|
|
dhcp4_flag_colour = dict()
|
|
|
|
dhcp4_start = dict()
|
|
|
|
dhcp4_end = dict()
|
|
|
|
dhcp4_range = dict()
|
2018-10-20 15:28:25 -04:00
|
|
|
colour_off = ansiprint.end()
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Gather information for printing
|
|
|
|
for net in net_list:
|
|
|
|
# get info
|
2019-03-15 11:28:49 -04:00
|
|
|
description[net], nettype[net], domain[net], ip6_network[net], ip6_gateway[net], dhcp6_flag[net], ip4_network[net], ip4_gateway[net], dhcp4_flag[net], dhcp4_start[net], dhcp4_end[net] = getNetworkInformation(zk_conn, net)
|
2018-11-13 01:22:15 -05:00
|
|
|
|
|
|
|
if ip4_network[net] != "None":
|
|
|
|
v4_flag_colour[net] = ansiprint.green()
|
|
|
|
v4_flag[net] = 'True'
|
|
|
|
else:
|
|
|
|
v4_flag_colour[net] = ansiprint.blue()
|
|
|
|
v4_flag[net] = 'False'
|
|
|
|
|
|
|
|
if ip6_network[net] != "None":
|
|
|
|
v6_flag_colour[net] = ansiprint.green()
|
|
|
|
v6_flag[net] = 'True'
|
|
|
|
else:
|
|
|
|
v6_flag_colour[net] = ansiprint.blue()
|
|
|
|
v6_flag[net] = 'False'
|
2018-09-29 02:54:48 -04:00
|
|
|
|
2018-11-13 01:22:15 -05:00
|
|
|
if dhcp6_flag[net] == "True":
|
|
|
|
dhcp6_flag_colour[net] = ansiprint.green()
|
2018-09-28 20:31:56 -04:00
|
|
|
else:
|
2018-11-13 01:22:15 -05:00
|
|
|
dhcp6_flag_colour[net] = ansiprint.blue()
|
|
|
|
|
|
|
|
if dhcp4_flag[net] == "True":
|
|
|
|
dhcp4_flag_colour[net] = ansiprint.green()
|
|
|
|
dhcp4_range[net] = '{} - {}'.format(dhcp4_start[net], dhcp4_end[net])
|
|
|
|
else:
|
|
|
|
dhcp4_flag_colour[net] = ansiprint.blue()
|
|
|
|
dhcp4_range[net] = 'N/A'
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Determine optimal column widths
|
|
|
|
# Dynamic columns: node_name, hypervisor, migrated
|
|
|
|
net_vni_length = 5
|
2019-03-15 11:28:49 -04:00
|
|
|
net_description_length = 12
|
|
|
|
net_nettype_length = 8
|
|
|
|
net_domain_length = 6
|
2018-11-13 01:22:15 -05:00
|
|
|
net_v6_flag_length = 6
|
|
|
|
net_dhcp6_flag_length = 7
|
|
|
|
net_v4_flag_length = 6
|
|
|
|
net_dhcp4_flag_length = 7
|
2018-09-28 20:31:56 -04:00
|
|
|
for net in net_list:
|
|
|
|
# vni column
|
|
|
|
_net_vni_length = len(net) + 1
|
|
|
|
if _net_vni_length > net_vni_length:
|
|
|
|
net_vni_length = _net_vni_length
|
|
|
|
# description column
|
|
|
|
_net_description_length = len(description[net]) + 1
|
|
|
|
if _net_description_length > net_description_length:
|
|
|
|
net_description_length = _net_description_length
|
2018-09-28 20:42:24 -04:00
|
|
|
# domain column
|
|
|
|
_net_domain_length = len(domain[net]) + 1
|
|
|
|
if _net_domain_length > net_domain_length:
|
|
|
|
net_domain_length = _net_domain_length
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Format the string (header)
|
|
|
|
net_list_output_header = '{bold}\
|
|
|
|
{net_vni: <{net_vni_length}} \
|
|
|
|
{net_description: <{net_description_length}} \
|
2019-03-15 11:28:49 -04:00
|
|
|
{net_nettype: <{net_nettype_length}} \
|
2018-11-13 01:22:15 -05:00
|
|
|
{net_domain: <{net_domain_length}} \
|
|
|
|
{net_v6_flag: <{net_v6_flag_length}} \
|
|
|
|
{net_dhcp6_flag: <{net_dhcp6_flag_length}} \
|
|
|
|
{net_v4_flag: <{net_v4_flag_length}} \
|
|
|
|
{net_dhcp4_flag: <{net_dhcp4_flag_length}} \
|
2018-09-28 20:31:56 -04:00
|
|
|
{end_bold}'.format(
|
2018-10-20 15:28:25 -04:00
|
|
|
bold=ansiprint.bold(),
|
|
|
|
end_bold=ansiprint.end(),
|
2018-09-28 20:31:56 -04:00
|
|
|
net_vni_length=net_vni_length,
|
|
|
|
net_description_length=net_description_length,
|
2019-03-15 11:28:49 -04:00
|
|
|
net_nettype_length=net_nettype_length,
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain_length=net_domain_length,
|
2018-11-13 01:22:15 -05:00
|
|
|
net_v6_flag_length=net_v6_flag_length,
|
|
|
|
net_dhcp6_flag_length=net_dhcp6_flag_length,
|
|
|
|
net_v4_flag_length=net_v4_flag_length,
|
|
|
|
net_dhcp4_flag_length=net_dhcp4_flag_length,
|
2018-09-28 20:31:56 -04:00
|
|
|
net_vni='VNI',
|
|
|
|
net_description='Description',
|
2019-03-15 11:28:49 -04:00
|
|
|
net_nettype='Type',
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain='Domain',
|
2018-11-13 01:22:15 -05:00
|
|
|
net_v6_flag='IPv6',
|
|
|
|
net_dhcp6_flag='DHCPv6',
|
|
|
|
net_v4_flag='IPv4',
|
|
|
|
net_dhcp4_flag='DHCPv4',
|
2018-09-28 20:31:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
for net in net_list:
|
|
|
|
net_list_output.append(
|
|
|
|
'{bold}\
|
|
|
|
{net_vni: <{net_vni_length}} \
|
|
|
|
{net_description: <{net_description_length}} \
|
2019-03-15 11:28:49 -04:00
|
|
|
{net_nettype: <{net_nettype_length}} \
|
2018-11-13 01:22:15 -05:00
|
|
|
{net_domain: <{net_domain_length}} \
|
|
|
|
{v6_flag_colour}{net_v6_flag: <{net_v6_flag_length}}{colour_off} \
|
|
|
|
{dhcp6_flag_colour}{net_dhcp6_flag: <{net_dhcp6_flag_length}}{colour_off} \
|
|
|
|
{v4_flag_colour}{net_v4_flag: <{net_v4_flag_length}}{colour_off} \
|
|
|
|
{dhcp4_flag_colour}{net_dhcp4_flag: <{net_dhcp4_flag_length}}{colour_off} \
|
2018-09-28 20:31:56 -04:00
|
|
|
{end_bold}'.format(
|
|
|
|
bold='',
|
|
|
|
end_bold='',
|
|
|
|
net_vni_length=net_vni_length,
|
|
|
|
net_description_length=net_description_length,
|
2019-03-15 11:28:49 -04:00
|
|
|
net_nettype_length=net_nettype_length,
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain_length=net_domain_length,
|
2018-11-13 01:22:15 -05:00
|
|
|
net_v6_flag_length=net_v6_flag_length,
|
|
|
|
net_dhcp6_flag_length=net_dhcp6_flag_length,
|
|
|
|
net_v4_flag_length=net_v4_flag_length,
|
|
|
|
net_dhcp4_flag_length=net_dhcp4_flag_length,
|
2018-09-28 20:31:56 -04:00
|
|
|
net_vni=net,
|
|
|
|
net_description=description[net],
|
2019-03-15 11:28:49 -04:00
|
|
|
net_nettype=nettype[net],
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain=domain[net],
|
2018-11-13 01:22:15 -05:00
|
|
|
net_v6_flag=v6_flag[net],
|
|
|
|
v6_flag_colour=v6_flag_colour[net],
|
|
|
|
net_dhcp6_flag=dhcp6_flag[net],
|
|
|
|
dhcp6_flag_colour=dhcp6_flag_colour[net],
|
|
|
|
net_v4_flag=v4_flag[net],
|
|
|
|
v4_flag_colour=v4_flag_colour[net],
|
|
|
|
net_dhcp4_flag=dhcp4_flag[net],
|
|
|
|
dhcp4_flag_colour=dhcp4_flag_colour[net],
|
2018-09-28 20:31:56 -04:00
|
|
|
colour_off=colour_off
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
output_string = net_list_output_header + '\n' + '\n'.join(sorted(net_list_output))
|
|
|
|
return output_string
|
|
|
|
|
2018-11-18 00:55:04 -05:00
|
|
|
def formatDHCPLeaseList(zk_conn, vni, dhcp4_leases_list, reservations=False):
|
2018-10-02 00:02:23 -04:00
|
|
|
dhcp_lease_list_output = []
|
2018-11-02 00:42:44 -04:00
|
|
|
hostname = dict()
|
2018-11-13 01:22:15 -05:00
|
|
|
ip4_address = dict()
|
2018-11-02 00:42:44 -04:00
|
|
|
mac_address = dict()
|
|
|
|
timestamp = dict()
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Gather information for printing
|
2018-11-18 00:55:04 -05:00
|
|
|
for dhcp_lease in dhcp4_leases_list:
|
2018-10-03 23:16:31 -04:00
|
|
|
if reservations:
|
2018-11-13 01:22:15 -05:00
|
|
|
hostname[dhcp_lease], ip4_address[dhcp_lease], mac_address[dhcp_lease], timestamp[dhcp_lease] = getDHCPReservationInformation(zk_conn, vni, dhcp_lease)
|
2018-10-03 23:16:31 -04:00
|
|
|
else:
|
2018-11-13 01:22:15 -05:00
|
|
|
hostname[dhcp_lease], ip4_address[dhcp_lease], mac_address[dhcp_lease], timestamp[dhcp_lease] = getDHCPLeaseInformation(zk_conn, vni, dhcp_lease)
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Determine optimal column widths
|
2018-11-02 00:42:44 -04:00
|
|
|
lease_hostname_length = 9
|
2018-11-13 01:22:15 -05:00
|
|
|
lease_ip4_address_length = 11
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_mac_address_length = 13
|
2018-11-02 00:42:44 -04:00
|
|
|
lease_timestamp_length = 13
|
2018-11-18 00:55:04 -05:00
|
|
|
for dhcp_lease in dhcp4_leases_list:
|
2018-09-30 12:43:56 -04:00
|
|
|
# hostname column
|
2018-10-02 00:02:23 -04:00
|
|
|
_lease_hostname_length = len(hostname[dhcp_lease]) + 1
|
|
|
|
if _lease_hostname_length > lease_hostname_length:
|
|
|
|
lease_hostname_length = _lease_hostname_length
|
2018-11-13 01:22:15 -05:00
|
|
|
# ip4_address column
|
|
|
|
_lease_ip4_address_length = len(ip4_address[dhcp_lease]) + 1
|
|
|
|
if _lease_ip4_address_length > lease_ip4_address_length:
|
|
|
|
lease_ip4_address_length = _lease_ip4_address_length
|
2018-10-17 00:23:27 -04:00
|
|
|
# mac_address column
|
2018-10-02 00:02:23 -04:00
|
|
|
_lease_mac_address_length = len(mac_address[dhcp_lease]) + 1
|
|
|
|
if _lease_mac_address_length > lease_mac_address_length:
|
|
|
|
lease_mac_address_length = _lease_mac_address_length
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Format the string (header)
|
2018-10-02 00:02:23 -04:00
|
|
|
dhcp_lease_list_output_header = '{bold}\
|
|
|
|
{lease_hostname: <{lease_hostname_length}} \
|
2018-11-13 01:22:15 -05:00
|
|
|
{lease_ip4_address: <{lease_ip4_address_length}} \
|
2018-10-02 00:02:23 -04:00
|
|
|
{lease_mac_address: <{lease_mac_address_length}} \
|
|
|
|
{lease_timestamp: <{lease_timestamp_length}} \
|
2018-09-28 20:31:56 -04:00
|
|
|
{end_bold}'.format(
|
2018-10-20 15:28:25 -04:00
|
|
|
bold=ansiprint.bold(),
|
|
|
|
end_bold=ansiprint.end(),
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_hostname_length=lease_hostname_length,
|
2018-11-13 01:22:15 -05:00
|
|
|
lease_ip4_address_length=lease_ip4_address_length,
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_mac_address_length=lease_mac_address_length,
|
2018-11-02 00:42:44 -04:00
|
|
|
lease_timestamp_length=lease_timestamp_length,
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_hostname='Hostname',
|
2018-11-13 01:22:15 -05:00
|
|
|
lease_ip4_address='IP Address',
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_mac_address='MAC Address',
|
|
|
|
lease_timestamp='Timestamp'
|
2018-09-28 20:31:56 -04:00
|
|
|
)
|
|
|
|
|
2018-11-18 00:55:04 -05:00
|
|
|
for dhcp_lease in dhcp4_leases_list:
|
2018-10-02 00:02:23 -04:00
|
|
|
dhcp_lease_list_output.append('{bold}\
|
|
|
|
{lease_hostname: <{lease_hostname_length}} \
|
2018-11-13 01:22:15 -05:00
|
|
|
{lease_ip4_address: <{lease_ip4_address_length}} \
|
2018-10-02 00:02:23 -04:00
|
|
|
{lease_mac_address: <{lease_mac_address_length}} \
|
|
|
|
{lease_timestamp: <{lease_timestamp_length}} \
|
2018-09-28 20:31:56 -04:00
|
|
|
{end_bold}'.format(
|
|
|
|
bold='',
|
|
|
|
end_bold='',
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_hostname_length=lease_hostname_length,
|
2018-11-13 01:22:15 -05:00
|
|
|
lease_ip4_address_length=lease_ip4_address_length,
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_mac_address_length=lease_mac_address_length,
|
|
|
|
lease_timestamp_length=12,
|
|
|
|
lease_hostname=hostname[dhcp_lease],
|
2018-11-13 01:22:15 -05:00
|
|
|
lease_ip4_address=ip4_address[dhcp_lease],
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_mac_address=mac_address[dhcp_lease],
|
|
|
|
lease_timestamp=timestamp[dhcp_lease]
|
2018-09-28 20:31:56 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-10-02 00:02:23 -04:00
|
|
|
output_string = dhcp_lease_list_output_header + '\n' + '\n'.join(sorted(dhcp_lease_list_output))
|
2018-09-28 20:31:56 -04:00
|
|
|
return output_string
|
|
|
|
|
2018-10-17 20:05:22 -04:00
|
|
|
def formatACLList(zk_conn, vni, _direction, acl_list):
|
2018-10-17 00:23:27 -04:00
|
|
|
acl_list_output = []
|
2018-11-02 00:42:44 -04:00
|
|
|
direction = dict()
|
|
|
|
order = dict()
|
|
|
|
description = dict()
|
|
|
|
rule = dict()
|
2018-10-17 00:23:27 -04:00
|
|
|
|
2018-10-17 20:05:22 -04:00
|
|
|
if _direction is None:
|
|
|
|
directions = ['in', 'out']
|
|
|
|
else:
|
|
|
|
directions = [_direction]
|
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
# Gather information for printing
|
|
|
|
for acl in acl_list:
|
2018-10-17 20:05:22 -04:00
|
|
|
acld = acl['description']
|
|
|
|
order[acld], description[acld], rule[acld] = getACLInformation(zk_conn, vni, acl['direction'], acl['description'])
|
|
|
|
direction[acld] = acl['direction']
|
2018-10-17 00:23:27 -04:00
|
|
|
|
|
|
|
# Determine optimal column widths
|
2018-11-02 00:42:44 -04:00
|
|
|
acl_direction_length = 10
|
2018-10-17 00:23:27 -04:00
|
|
|
acl_order_length = 6
|
|
|
|
acl_description_length = 12
|
|
|
|
acl_rule_length = 5
|
|
|
|
for acl in acl_list:
|
2018-10-17 20:05:22 -04:00
|
|
|
acld = acl['description']
|
2018-10-17 00:23:27 -04:00
|
|
|
# order column
|
2018-10-17 20:05:22 -04:00
|
|
|
_acl_order_length = len(order[acld]) + 1
|
2018-10-17 00:23:27 -04:00
|
|
|
if _acl_order_length > acl_order_length:
|
|
|
|
acl_order_length = _acl_order_length
|
|
|
|
# description column
|
2018-10-17 20:05:22 -04:00
|
|
|
_acl_description_length = len(description[acld]) + 1
|
2018-10-17 00:23:27 -04:00
|
|
|
if _acl_description_length > acl_description_length:
|
|
|
|
acl_description_length = _acl_description_length
|
|
|
|
# rule column
|
2018-10-17 20:05:22 -04:00
|
|
|
_acl_rule_length = len(rule[acld]) + 1
|
2018-10-17 00:23:27 -04:00
|
|
|
if _acl_rule_length > acl_rule_length:
|
|
|
|
acl_rule_length = _acl_rule_length
|
|
|
|
|
|
|
|
# Format the string (header)
|
|
|
|
acl_list_output_header = '{bold}\
|
2018-11-02 00:42:44 -04:00
|
|
|
{acl_direction: <{acl_direction_length}} \
|
2018-10-17 00:23:27 -04:00
|
|
|
{acl_order: <{acl_order_length}} \
|
|
|
|
{acl_description: <{acl_description_length}} \
|
|
|
|
{acl_rule: <{acl_rule_length}} \
|
|
|
|
{end_bold}'.format(
|
2018-10-20 15:28:25 -04:00
|
|
|
bold=ansiprint.bold(),
|
|
|
|
end_bold=ansiprint.end(),
|
2018-11-02 00:42:44 -04:00
|
|
|
acl_direction_length=acl_direction_length,
|
2018-10-17 00:23:27 -04:00
|
|
|
acl_order_length=acl_order_length,
|
|
|
|
acl_description_length=acl_description_length,
|
|
|
|
acl_rule_length=acl_rule_length,
|
2018-10-17 20:05:22 -04:00
|
|
|
acl_direction='Direction',
|
2018-10-17 00:23:27 -04:00
|
|
|
acl_order='Order',
|
|
|
|
acl_description='Description',
|
|
|
|
acl_rule='Rule',
|
|
|
|
)
|
|
|
|
|
|
|
|
for acl in acl_list:
|
2018-10-17 20:05:22 -04:00
|
|
|
acld = acl['description']
|
2018-10-17 00:23:27 -04:00
|
|
|
acl_list_output.append('{bold}\
|
2018-11-02 00:42:44 -04:00
|
|
|
{acl_direction: <{acl_direction_length}} \
|
2018-10-17 00:23:27 -04:00
|
|
|
{acl_order: <{acl_order_length}} \
|
|
|
|
{acl_description: <{acl_description_length}} \
|
|
|
|
{acl_rule: <{acl_rule_length}} \
|
|
|
|
{end_bold}'.format(
|
|
|
|
bold='',
|
|
|
|
end_bold='',
|
2018-11-02 00:42:44 -04:00
|
|
|
acl_direction_length=acl_direction_length,
|
2018-10-17 00:23:27 -04:00
|
|
|
acl_order_length=acl_order_length,
|
|
|
|
acl_description_length=acl_description_length,
|
|
|
|
acl_rule_length=acl_rule_length,
|
2018-10-17 20:05:22 -04:00
|
|
|
acl_direction=direction[acld],
|
|
|
|
acl_order=order[acld],
|
|
|
|
acl_description=description[acld],
|
|
|
|
acl_rule=rule[acld],
|
2018-10-17 00:23:27 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
output_string = acl_list_output_header + '\n' + '\n'.join(sorted(acl_list_output))
|
|
|
|
return output_string
|
|
|
|
|
2018-09-28 20:31:56 -04:00
|
|
|
def isValidMAC(macaddr):
|
|
|
|
allowed = re.compile(r"""
|
|
|
|
(
|
|
|
|
^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$
|
|
|
|
)
|
|
|
|
""",
|
|
|
|
re.VERBOSE|re.IGNORECASE)
|
|
|
|
|
|
|
|
if allowed.match(macaddr) is None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def isValidIP(ipaddr):
|
2018-11-13 01:22:15 -05:00
|
|
|
ip4_blocks = str(ipaddr).split(".")
|
|
|
|
if len(ip4_blocks) == 4:
|
|
|
|
for block in ip4_blocks:
|
2018-09-28 20:31:56 -04:00
|
|
|
# Check if number is digit, if not checked before calling this function
|
|
|
|
if not block.isdigit():
|
|
|
|
return False
|
|
|
|
tmp = int(block)
|
|
|
|
if 0 > tmp > 255:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
#
|
|
|
|
# Direct functions
|
|
|
|
#
|
2019-03-15 11:28:49 -04:00
|
|
|
def add_network(zk_conn, vni, description, nettype,
|
|
|
|
domain, ip4_network, ip4_gateway, ip6_network, ip6_gateway,
|
2018-11-13 01:22:15 -05:00
|
|
|
dhcp4_flag, dhcp4_start, dhcp4_end):
|
|
|
|
# Ensure start and end DHCP ranges are set if the flag is set
|
|
|
|
if dhcp4_flag and ( not dhcp4_start or not dhcp4_end ):
|
|
|
|
return False, 'ERROR: DHCPv4 start and end addresses are required for a DHCPv4-enabled network.'
|
2018-09-29 02:54:48 -04:00
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
# Check if a network with this VNI or description already exists
|
2018-10-27 15:27:08 -04:00
|
|
|
if zkhandler.exists(zk_conn, '/networks/{}'.format(vni)):
|
2018-09-23 15:26:20 -04:00
|
|
|
return False, 'ERROR: A network with VNI {} already exists!'.format(vni)
|
2018-10-17 00:23:27 -04:00
|
|
|
for network in zkhandler.listchildren(zk_conn, '/networks'):
|
|
|
|
network_description = zkhandler.readdata(zk_conn, '/networks/{}'.format(network))
|
|
|
|
if network_description == description:
|
|
|
|
return False, 'ERROR: A network with description {} already exists!'.format(description)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-11-13 01:22:15 -05:00
|
|
|
# We're generating the default gateway to be ip6_network::1/YY
|
|
|
|
if ip6_network:
|
|
|
|
dhcp6_flag = 'True'
|
|
|
|
if not ip6_gateway:
|
|
|
|
ip6_netpart, ip6_maskpart = ip6_network.split('/')
|
2018-11-14 00:19:43 -05:00
|
|
|
ip6_gateway = '{}1'.format(ip6_netpart)
|
2018-11-13 01:22:15 -05:00
|
|
|
else:
|
|
|
|
dhcp6_flag = 'False'
|
|
|
|
|
2019-06-24 12:59:32 -04:00
|
|
|
if not domain:
|
|
|
|
domain = '{}.local'.format(description)
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
# Add the new network to Zookeeper
|
2018-09-29 02:54:48 -04:00
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/networks/{}'.format(vni): description,
|
2019-03-15 11:28:49 -04:00
|
|
|
'/networks/{}/nettype'.format(vni): nettype,
|
2018-09-29 02:54:48 -04:00
|
|
|
'/networks/{}/domain'.format(vni): domain,
|
2018-11-13 01:22:15 -05:00
|
|
|
'/networks/{}/ip6_network'.format(vni): ip6_network,
|
|
|
|
'/networks/{}/ip6_gateway'.format(vni): ip6_gateway,
|
|
|
|
'/networks/{}/dhcp6_flag'.format(vni): dhcp6_flag,
|
|
|
|
'/networks/{}/ip4_network'.format(vni): ip4_network,
|
|
|
|
'/networks/{}/ip4_gateway'.format(vni): ip4_gateway,
|
|
|
|
'/networks/{}/dhcp4_flag'.format(vni): dhcp4_flag,
|
|
|
|
'/networks/{}/dhcp4_start'.format(vni): dhcp4_start,
|
|
|
|
'/networks/{}/dhcp4_end'.format(vni): dhcp4_end,
|
|
|
|
'/networks/{}/dhcp4_leases'.format(vni): '',
|
|
|
|
'/networks/{}/dhcp4_reservations'.format(vni): '',
|
2018-10-17 00:23:27 -04:00
|
|
|
'/networks/{}/firewall_rules'.format(vni): '',
|
|
|
|
'/networks/{}/firewall_rules/in'.format(vni): '',
|
|
|
|
'/networks/{}/firewall_rules/out'.format(vni): ''
|
2018-09-29 02:54:48 -04:00
|
|
|
})
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return True, 'Network "{}" added successfully!'.format(description)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def modify_network(zk_conn, vni, **parameters):
|
|
|
|
# Add the new network to Zookeeper
|
2018-11-02 00:42:44 -04:00
|
|
|
zk_data = dict()
|
2018-09-23 15:26:20 -04:00
|
|
|
if parameters['description'] != None:
|
2018-09-29 02:54:48 -04:00
|
|
|
zk_data.update({'/networks/{}'.format(vni): parameters['description']})
|
2018-10-03 20:22:42 -04:00
|
|
|
if parameters['domain'] != None:
|
|
|
|
zk_data.update({'/networks/{}/domain'.format(vni): parameters['domain']})
|
2018-11-13 01:22:15 -05:00
|
|
|
if parameters['ip4_network'] != None:
|
|
|
|
zk_data.update({'/networks/{}/ip4_network'.format(vni): parameters['ip4_network']})
|
|
|
|
if parameters['ip4_gateway'] != None:
|
|
|
|
zk_data.update({'/networks/{}/ip4_gateway'.format(vni): parameters['ip4_gateway']})
|
|
|
|
if parameters['ip6_network'] != None:
|
|
|
|
zk_data.update({'/networks/{}/ip6_network'.format(vni): parameters['ip6_network']})
|
|
|
|
if parameters['ip6_network'] != '':
|
|
|
|
zk_data.update({'/networks/{}/dhcp6_flag'.format(vni): 'True'})
|
|
|
|
else:
|
|
|
|
zk_data.update({'/networks/{}/dhcp6_flag'.format(vni): 'False'})
|
|
|
|
if parameters['ip6_gateway'] != None:
|
|
|
|
zk_data.update({'/networks/{}/ip6_gateway'.format(vni): parameters['ip6_gateway']})
|
2018-11-14 00:19:43 -05:00
|
|
|
else:
|
|
|
|
# If we're changing the network, but don't also specify the gateway,
|
|
|
|
# generate a new one automatically
|
|
|
|
if parameters['ip6_network'] != None:
|
|
|
|
ip6_netpart, ip6_maskpart = parameters['ip6_network'].split('/')
|
|
|
|
ip6_gateway = '{}1'.format(ip6_netpart)
|
|
|
|
zk_data.update({'/networks/{}/ip6_gateway'.format(vni): ip6_gateway})
|
2018-09-23 15:26:20 -04:00
|
|
|
if parameters['dhcp_flag'] != None:
|
2018-10-17 00:23:27 -04:00
|
|
|
zk_data.update({'/networks/{}/dhcp_flag'.format(vni): parameters['dhcp_flag']})
|
2018-10-03 20:22:42 -04:00
|
|
|
if parameters['dhcp_start'] != None:
|
|
|
|
zk_data.update({'/networks/{}/dhcp_start'.format(vni): parameters['dhcp_start']})
|
|
|
|
if parameters['dhcp_end'] != None:
|
|
|
|
zk_data.update({'/networks/{}/dhcp_end'.format(vni): parameters['dhcp_end']})
|
2018-09-29 02:54:48 -04:00
|
|
|
|
|
|
|
zkhandler.writedata(zk_conn, zk_data)
|
2019-03-15 11:28:49 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return True, 'Network "{}" modified successfully!'.format(vni)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def remove_network(zk_conn, network):
|
2018-09-20 03:25:58 -04:00
|
|
|
# Validate and obtain alternate passed value
|
2018-09-23 15:26:20 -04:00
|
|
|
vni = getNetworkVNI(zk_conn, network)
|
|
|
|
description = getNetworkDescription(zk_conn, network)
|
|
|
|
if not vni:
|
|
|
|
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
# Delete the configuration
|
2018-10-27 15:27:08 -04:00
|
|
|
zkhandler.deletekey(zk_conn, '/networks/{}'.format(vni))
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return True, 'Network "{}" removed successfully!'.format(description)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-28 20:31:56 -04:00
|
|
|
|
2018-09-30 12:43:56 -04:00
|
|
|
def add_dhcp_reservation(zk_conn, network, ipaddress, macaddress, hostname):
|
2018-09-28 20:31:56 -04:00
|
|
|
# Validate and obtain standard passed value
|
|
|
|
net_vni = getNetworkVNI(zk_conn, network)
|
|
|
|
if net_vni == None:
|
|
|
|
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
|
|
|
|
2018-10-03 23:16:31 -04:00
|
|
|
# Use lowercase MAC format exclusively
|
|
|
|
macaddress = macaddress.lower()
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
if not isValidMAC(macaddress):
|
|
|
|
return False, 'ERROR: MAC address "{}" is not valid! Always use ":" as a separator.'.format(macaddress)
|
|
|
|
|
|
|
|
if not isValidIP(ipaddress):
|
|
|
|
return False, 'ERROR: IP address "{}" is not valid!'.format(macaddress)
|
|
|
|
|
2018-11-18 00:55:04 -05:00
|
|
|
if zkhandler.exists(zk_conn, '/networks/{}/dhcp4_reservations/{}'.format(net_vni, macaddress)):
|
2018-09-30 11:22:25 -04:00
|
|
|
return False, 'ERROR: A reservation with MAC "{}" already exists!'.format(macaddress)
|
2018-09-28 20:31:56 -04:00
|
|
|
|
2018-09-30 12:43:56 -04:00
|
|
|
# Add the new static lease to ZK
|
2018-09-28 20:31:56 -04:00
|
|
|
try:
|
|
|
|
zkhandler.writedata(zk_conn, {
|
2018-11-18 00:55:04 -05:00
|
|
|
'/networks/{}/dhcp4_reservations/{}'.format(net_vni, macaddress): 'static',
|
|
|
|
'/networks/{}/dhcp4_reservations/{}/hostname'.format(net_vni, macaddress): hostname,
|
|
|
|
'/networks/{}/dhcp4_reservations/{}/ipaddr'.format(net_vni, macaddress): ipaddress
|
2018-09-28 20:31:56 -04:00
|
|
|
})
|
|
|
|
except Exception as e:
|
|
|
|
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
|
|
|
|
2018-09-30 12:43:56 -04:00
|
|
|
return True, 'DHCP reservation "{}" added successfully!'.format(macaddress)
|
2018-09-28 20:31:56 -04:00
|
|
|
|
2018-10-03 23:53:33 -04:00
|
|
|
def remove_dhcp_reservation(zk_conn, network, reservation):
|
2018-09-28 20:31:56 -04:00
|
|
|
# Validate and obtain standard passed value
|
|
|
|
net_vni = getNetworkVNI(zk_conn, network)
|
|
|
|
if net_vni == None:
|
|
|
|
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
|
|
|
|
|
|
|
match_description = ''
|
|
|
|
|
|
|
|
# Check if the reservation matches a description, a mac, or an IP address currently in the database
|
2018-11-18 00:55:04 -05:00
|
|
|
dhcp4_reservations_list = getNetworkDHCPReservations(zk_conn, net_vni)
|
|
|
|
for macaddr in dhcp4_reservations_list:
|
|
|
|
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_reservations/{}/hostname'.format(net_vni, macaddr))
|
|
|
|
ipaddress = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_reservations/{}/ipaddr'.format(net_vni, macaddr))
|
2018-10-03 23:53:33 -04:00
|
|
|
if reservation == macaddr or reservation == hostname or reservation == ipaddress:
|
2018-09-30 11:22:25 -04:00
|
|
|
match_description = macaddr
|
2019-03-15 11:28:49 -04:00
|
|
|
|
2018-09-28 20:31:56 -04:00
|
|
|
if not match_description:
|
2018-10-03 23:53:33 -04:00
|
|
|
return False, 'ERROR: No DHCP reservation exists matching "{}"!'.format(reservation)
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Remove the entry from zookeeper
|
|
|
|
try:
|
2018-11-18 00:55:04 -05:00
|
|
|
zkhandler.deletekey(zk_conn, '/networks/{}/dhcp4_reservations/{}'.format(net_vni, match_description))
|
2018-09-28 20:31:56 -04:00
|
|
|
except:
|
|
|
|
return False, 'ERROR: Failed to write to Zookeeper!'
|
|
|
|
|
2018-10-03 23:53:33 -04:00
|
|
|
return True, 'DHCP reservation "{}" removed successfully!'.format(match_description)
|
2018-09-28 20:31:56 -04:00
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
def add_acl(zk_conn, network, direction, description, rule, order):
|
|
|
|
# Validate and obtain standard passed value
|
|
|
|
net_vni = getNetworkVNI(zk_conn, network)
|
|
|
|
if net_vni == None:
|
|
|
|
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
|
|
|
|
|
|
|
# Change direction to something more usable
|
|
|
|
if direction:
|
|
|
|
direction = "in"
|
|
|
|
else:
|
|
|
|
direction = "out"
|
|
|
|
|
2018-10-27 15:27:08 -04:00
|
|
|
if zkhandler.exists(zk_conn, '/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, description)):
|
2018-10-17 00:23:27 -04:00
|
|
|
return False, 'ERROR: A rule with description "{}" already exists!'.format(description)
|
|
|
|
|
|
|
|
# Handle reordering
|
|
|
|
full_acl_list = getNetworkACLs(zk_conn, net_vni, direction)
|
|
|
|
acl_list_length = len(full_acl_list)
|
|
|
|
# Set order to len
|
|
|
|
if order == None or int(order) > acl_list_length:
|
|
|
|
order = acl_list_length
|
|
|
|
# Convert passed-in order to an integer
|
|
|
|
else:
|
|
|
|
order = int(order)
|
2019-03-15 11:28:49 -04:00
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
# Insert into the array at order-1
|
2018-10-17 20:05:22 -04:00
|
|
|
full_acl_list.insert(order, {'direction': direction, 'description': description})
|
2018-10-17 00:23:27 -04:00
|
|
|
|
|
|
|
# Update the existing ordering
|
2018-11-02 00:42:44 -04:00
|
|
|
updated_orders = dict()
|
2018-10-17 00:23:27 -04:00
|
|
|
for idx, acl in enumerate(full_acl_list):
|
2018-10-17 20:05:22 -04:00
|
|
|
if acl['description'] == description:
|
2018-10-17 00:23:27 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
updated_orders[
|
2018-10-17 20:05:22 -04:00
|
|
|
'/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, direction, acl['description'])
|
2018-10-17 00:23:27 -04:00
|
|
|
] = idx
|
|
|
|
|
|
|
|
if updated_orders:
|
2018-10-17 20:05:22 -04:00
|
|
|
try:
|
|
|
|
zkhandler.writedata(zk_conn, updated_orders)
|
|
|
|
except Exception as e:
|
|
|
|
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
2018-10-17 00:23:27 -04:00
|
|
|
|
|
|
|
# Add the new rule
|
|
|
|
try:
|
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, description): '',
|
|
|
|
'/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, direction, description): order,
|
|
|
|
'/networks/{}/firewall_rules/{}/{}/rule'.format(net_vni, direction, description): rule
|
|
|
|
})
|
|
|
|
except Exception as e:
|
|
|
|
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
|
|
|
|
|
|
|
return True, 'Firewall rule "{}" added successfully!'.format(description)
|
|
|
|
|
|
|
|
def remove_acl(zk_conn, network, rule, direction):
|
|
|
|
# Validate and obtain standard passed value
|
|
|
|
net_vni = getNetworkVNI(zk_conn, network)
|
|
|
|
if net_vni == None:
|
|
|
|
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
|
|
|
|
|
|
|
# Change direction to something more usable
|
|
|
|
if direction:
|
|
|
|
direction = "in"
|
|
|
|
else:
|
|
|
|
direction = "out"
|
|
|
|
|
|
|
|
match_description = ''
|
|
|
|
|
|
|
|
# Check if the ACL matches a description currently in the database
|
|
|
|
acl_list = getNetworkACLs(zk_conn, net_vni, direction)
|
|
|
|
for acl in acl_list:
|
2018-10-17 20:05:22 -04:00
|
|
|
if acl['description'] == rule:
|
|
|
|
match_description = acl['description']
|
2019-03-15 11:28:49 -04:00
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
if not match_description:
|
|
|
|
return False, 'ERROR: No firewall rule exists matching description "{}"!'.format(rule)
|
|
|
|
|
|
|
|
# Remove the entry from zookeeper
|
|
|
|
try:
|
2018-10-27 15:27:08 -04:00
|
|
|
zkhandler.deletekey(zk_conn, '/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, match_description))
|
2018-10-17 00:23:27 -04:00
|
|
|
except Exception as e:
|
|
|
|
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
|
|
|
|
|
|
|
# Update the existing ordering
|
2018-10-17 20:05:22 -04:00
|
|
|
updated_acl_list = getNetworkACLs(zk_conn, net_vni, direction)
|
2018-11-02 00:42:44 -04:00
|
|
|
updated_orders = dict()
|
2018-10-17 20:05:22 -04:00
|
|
|
for idx, acl in enumerate(updated_acl_list):
|
2018-10-17 00:23:27 -04:00
|
|
|
updated_orders[
|
2018-10-17 20:05:22 -04:00
|
|
|
'/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, direction, acl['description'])
|
2018-10-17 00:23:27 -04:00
|
|
|
] = idx
|
|
|
|
|
|
|
|
if updated_orders:
|
|
|
|
try:
|
|
|
|
zkhandler.writedata(zk_conn, updated_orders)
|
|
|
|
except Exception as e:
|
|
|
|
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
|
|
|
|
|
|
|
return True, 'Firewall rule "{}" removed successfully!'.format(match_description)
|
|
|
|
|
2018-09-25 01:32:03 -04:00
|
|
|
def get_info(zk_conn, network, long_output):
|
2018-09-20 03:25:58 -04:00
|
|
|
# Validate and obtain alternate passed value
|
2018-09-23 15:26:20 -04:00
|
|
|
net_vni = getNetworkVNI(zk_conn, network)
|
|
|
|
if net_vni == None:
|
|
|
|
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-25 01:32:52 -04:00
|
|
|
information = formatNetworkInformation(zk_conn, net_vni, long_output)
|
2018-09-20 03:25:58 -04:00
|
|
|
click.echo(information)
|
|
|
|
click.echo('')
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return True, ''
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-28 20:31:56 -04:00
|
|
|
def get_list(zk_conn, limit):
|
2018-09-25 01:47:29 -04:00
|
|
|
net_list = []
|
2018-10-27 16:03:01 -04:00
|
|
|
full_net_list = zkhandler.listchildren(zk_conn, '/networks')
|
2018-09-25 02:20:32 -04:00
|
|
|
|
2018-09-25 01:47:29 -04:00
|
|
|
for net in full_net_list:
|
|
|
|
description = zkhandler.readdata(zk_conn, '/networks/{}'.format(net))
|
|
|
|
if limit != None:
|
|
|
|
try:
|
2018-09-25 02:20:32 -04:00
|
|
|
# Implcitly assume fuzzy limits
|
|
|
|
if re.match('\^.*', limit) == None:
|
|
|
|
limit = '.*' + limit
|
|
|
|
if re.match('.*\$', limit) == None:
|
|
|
|
limit = limit + '.*'
|
|
|
|
|
|
|
|
if re.match(limit, net) != None:
|
|
|
|
net_list.append(net)
|
|
|
|
if re.match(limit, description) != None:
|
|
|
|
net_list.append(net)
|
2018-09-25 01:47:29 -04:00
|
|
|
except Exception as e:
|
|
|
|
return False, 'Regex Error: {}'.format(e)
|
2018-09-25 02:20:32 -04:00
|
|
|
else:
|
|
|
|
net_list.append(net)
|
|
|
|
|
2018-09-28 20:31:56 -04:00
|
|
|
output_string = formatNetworkList(zk_conn, net_list)
|
|
|
|
click.echo(output_string)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-28 20:31:56 -04:00
|
|
|
return True, ''
|
2018-09-25 13:58:52 -04:00
|
|
|
|
2018-10-03 19:23:46 -04:00
|
|
|
def get_list_dhcp(zk_conn, network, limit, only_static=False):
|
2018-09-28 20:31:56 -04:00
|
|
|
# Validate and obtain alternate passed value
|
|
|
|
net_vni = getNetworkVNI(zk_conn, network)
|
|
|
|
if net_vni == None:
|
|
|
|
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-03 19:23:46 -04:00
|
|
|
dhcp_list = []
|
|
|
|
|
|
|
|
if only_static:
|
|
|
|
full_dhcp_list = getNetworkDHCPReservations(zk_conn, net_vni)
|
2018-10-03 23:16:31 -04:00
|
|
|
reservations = True
|
2018-10-03 19:23:46 -04:00
|
|
|
else:
|
|
|
|
full_dhcp_list = getNetworkDHCPLeases(zk_conn, net_vni)
|
2018-10-03 23:16:31 -04:00
|
|
|
reservations = False
|
2018-10-02 00:02:23 -04:00
|
|
|
|
|
|
|
if limit:
|
|
|
|
try:
|
|
|
|
# Implcitly assume fuzzy limits
|
|
|
|
if re.match('\^.*', limit) == None:
|
|
|
|
limit = '.*' + limit
|
|
|
|
if re.match('.*\$', limit) == None:
|
|
|
|
limit = limit + '.*'
|
|
|
|
except Exception as e:
|
|
|
|
return False, 'Regex Error: {}'.format(e)
|
2019-03-15 11:28:49 -04:00
|
|
|
|
2018-10-02 00:02:23 -04:00
|
|
|
|
2018-10-03 19:23:46 -04:00
|
|
|
for lease in full_dhcp_list:
|
2018-10-02 00:02:23 -04:00
|
|
|
valid_lease = False
|
2018-10-03 19:23:46 -04:00
|
|
|
if limit:
|
|
|
|
if re.match(limit, lease) != None:
|
|
|
|
valid_lease = True
|
|
|
|
if re.match(limit, lease) != None:
|
2018-10-02 00:14:08 -04:00
|
|
|
valid_lease = True
|
2018-10-03 19:23:46 -04:00
|
|
|
else:
|
|
|
|
valid_lease = True
|
2018-10-02 00:02:23 -04:00
|
|
|
|
|
|
|
if valid_lease:
|
2018-10-03 19:23:46 -04:00
|
|
|
dhcp_list.append(lease)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-03 23:16:31 -04:00
|
|
|
output_string = formatDHCPLeaseList(zk_conn, net_vni, dhcp_list, reservations=reservations)
|
2018-09-28 20:31:56 -04:00
|
|
|
click.echo(output_string)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
return True, ''
|
2018-09-28 20:31:56 -04:00
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
def get_list_acl(zk_conn, network, limit, direction):
|
2018-09-28 20:31:56 -04:00
|
|
|
# Validate and obtain alternate passed value
|
|
|
|
net_vni = getNetworkVNI(zk_conn, network)
|
|
|
|
if net_vni == None:
|
|
|
|
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
|
|
|
|
2018-10-17 00:23:27 -04:00
|
|
|
# Change direction to something more usable
|
2018-10-17 20:05:22 -04:00
|
|
|
if direction is None:
|
|
|
|
direction = "both"
|
|
|
|
elif direction is True:
|
2018-10-17 00:23:27 -04:00
|
|
|
direction = "in"
|
2018-10-17 20:05:22 -04:00
|
|
|
elif direction is False:
|
2018-10-17 00:23:27 -04:00
|
|
|
direction = "out"
|
|
|
|
|
|
|
|
acl_list = []
|
|
|
|
full_acl_list = getNetworkACLs(zk_conn, net_vni, direction)
|
|
|
|
|
|
|
|
if limit:
|
|
|
|
try:
|
|
|
|
# Implcitly assume fuzzy limits
|
|
|
|
if re.match('\^.*', limit) == None:
|
|
|
|
limit = '.*' + limit
|
|
|
|
if re.match('.*\$', limit) == None:
|
|
|
|
limit = limit + '.*'
|
|
|
|
except Exception as e:
|
|
|
|
return False, 'Regex Error: {}'.format(e)
|
|
|
|
|
2018-10-17 20:05:22 -04:00
|
|
|
for acl in full_acl_list:
|
2018-10-17 00:23:27 -04:00
|
|
|
valid_acl = False
|
|
|
|
if limit:
|
2018-10-17 20:05:22 -04:00
|
|
|
if re.match(limit, acl['description']) != None:
|
2018-10-17 00:23:27 -04:00
|
|
|
valid_acl = True
|
|
|
|
else:
|
|
|
|
valid_acl = True
|
|
|
|
|
|
|
|
if valid_acl:
|
|
|
|
acl_list.append(acl)
|
|
|
|
|
|
|
|
output_string = formatACLList(zk_conn, net_vni, direction, acl_list)
|
|
|
|
click.echo(output_string)
|
|
|
|
|
|
|
|
return True, ''
|