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-09-23 15:26:20 -04:00
|
|
|
import client_lib.ansiiprint as ansiiprint
|
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
|
|
|
|
vni_list = zk_conn.get_children('/networks')
|
|
|
|
description_list = []
|
|
|
|
# For each VNI, get the corresponding description from the data
|
|
|
|
for vni in vni_list:
|
|
|
|
description_list.append(zk_conn.get('/networks/{}'.format(vni))[0].decode('ascii'))
|
|
|
|
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):
|
|
|
|
# Get a list of DHCP leases by listing the children of /networks/<vni>/dhcp_leases
|
|
|
|
dhcp_leases = zk_conn.get_children('/networks/{}/dhcp_leases'.format(vni))
|
|
|
|
return sorted(dhcp_leases)
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def getNetworkDHCPReservations(zk_conn, vni):
|
2018-10-03 19:23:46 -04:00
|
|
|
# Get a list of DHCP reservations by listing the children of /networks/<vni>/dhcp_reservations
|
|
|
|
dhcp_reservations = zk_conn.get_children('/networks/{}/dhcp_reservations'.format(vni))
|
2018-09-30 11:22:25 -04:00
|
|
|
return sorted(dhcp_reservations)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def getNetworkFirewallRules(zk_conn, vni):
|
2018-09-28 20:31:56 -04:00
|
|
|
firewall_rules = zk_conn.get_children('/networks/{}/firewall_rules'.format(vni))
|
2018-09-23 15:26:20 -04:00
|
|
|
return None
|
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))
|
|
|
|
domain = zkhandler.readdata(zk_conn, '/networks/{}/domain'.format(vni))
|
|
|
|
ip_network = zkhandler.readdata(zk_conn, '/networks/{}/ip_network'.format(vni))
|
|
|
|
ip_gateway = zkhandler.readdata(zk_conn, '/networks/{}/ip_gateway'.format(vni))
|
|
|
|
dhcp_flag = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_flag'.format(vni))
|
|
|
|
dhcp_start = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_start'.format(vni))
|
|
|
|
dhcp_end = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_end'.format(vni))
|
|
|
|
return description, domain, ip_network, ip_gateway, dhcp_flag, dhcp_start, dhcp_end
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-02 00:02:23 -04:00
|
|
|
def getDHCPLeaseInformation(zk_conn, vni, mac_address):
|
|
|
|
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/hostname'.format(vni, mac_address))
|
|
|
|
ip_address = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/ipaddr'.format(vni, mac_address))
|
2018-10-03 19:23:46 -04:00
|
|
|
try:
|
|
|
|
timestamp = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/expiry'.format(vni, mac_address))
|
|
|
|
except:
|
|
|
|
timestamp = 'static'
|
2018-10-02 00:02:23 -04:00
|
|
|
return hostname, ip_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):
|
|
|
|
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_reservations/{}/hostname'.format(vni, mac_address))
|
|
|
|
ip_address = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_reservations/{}/ipaddr'.format(vni, mac_address))
|
|
|
|
timestamp = 'static'
|
|
|
|
return hostname, ip_address, mac_address, timestamp
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
def formatNetworkInformation(zk_conn, vni, long_output):
|
2018-09-29 02:54:48 -04:00
|
|
|
description, domain, ip_network, ip_gateway, dhcp_flag, dhcp_start, dhcp_end = getNetworkInformation(zk_conn, vni)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-29 02:54:48 -04:00
|
|
|
if dhcp_flag == "True":
|
2018-09-25 13:58:52 -04:00
|
|
|
dhcp_flag_colour = ansiiprint.green()
|
|
|
|
else:
|
|
|
|
dhcp_flag_colour = ansiiprint.blue()
|
|
|
|
colour_off = ansiiprint.end()
|
|
|
|
|
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 = []
|
|
|
|
ainformation.append('{}Virtual network information:{}'.format(ansiiprint.bold(), ansiiprint.end()))
|
|
|
|
ainformation.append('')
|
|
|
|
# Basic information
|
2018-09-29 02:56:36 -04:00
|
|
|
ainformation.append('{}VNI:{} {}'.format(ansiiprint.purple(), ansiiprint.end(), vni))
|
|
|
|
ainformation.append('{}Description:{} {}'.format(ansiiprint.purple(), ansiiprint.end(), description))
|
|
|
|
ainformation.append('{}Domain:{} {}'.format(ansiiprint.purple(), ansiiprint.end(), domain))
|
|
|
|
ainformation.append('{}IP network:{} {}'.format(ansiiprint.purple(), ansiiprint.end(), ip_network))
|
|
|
|
ainformation.append('{}IP gateway:{} {}'.format(ansiiprint.purple(), ansiiprint.end(), ip_gateway))
|
|
|
|
ainformation.append('{}DHCP enabled:{} {}{}{}'.format(ansiiprint.purple(), ansiiprint.end(), dhcp_flag_colour, dhcp_flag, colour_off))
|
2018-09-29 02:54:48 -04:00
|
|
|
if dhcp_flag == "True":
|
2018-09-29 02:56:36 -04:00
|
|
|
ainformation.append('{}DHCP range:{} {} - {}'.format(ansiiprint.purple(), ansiiprint.end(), dhcp_start, dhcp_end))
|
2018-09-23 15:26:20 -04:00
|
|
|
|
|
|
|
if long_output:
|
2018-10-02 00:02:23 -04:00
|
|
|
dhcp_reservations_list = getNetworkDHCPReservations(zk_conn, vni)
|
2018-09-28 20:31:56 -04:00
|
|
|
if dhcp_reservations_list:
|
2018-09-23 15:26:20 -04:00
|
|
|
ainformation.append('')
|
|
|
|
ainformation.append('{}Client DHCP reservations:{}'.format(ansiiprint.bold(), ansiiprint.end()))
|
|
|
|
ainformation.append('')
|
2018-10-02 00:02:23 -04:00
|
|
|
# Only show static reservations in the detailed information
|
2018-10-03 23:16:31 -04:00
|
|
|
dhcp_reservations_string = formatDHCPLeaseList(zk_conn, vni, dhcp_reservations_list, reservations=True)
|
2018-09-28 20:31:56 -04:00
|
|
|
for line in dhcp_reservations_string.split('\n'):
|
|
|
|
ainformation.append(line)
|
2018-09-30 11:22:25 -04:00
|
|
|
|
2018-09-28 20:31:56 -04:00
|
|
|
firewall_rules = zk_conn.get_children('/networks/{}/firewall_rules'.format(vni))
|
2018-09-23 15:26:20 -04:00
|
|
|
if firewall_rules:
|
|
|
|
ainformation.append('')
|
|
|
|
ainformation.append('{}Network firewall rules:{}'.format(ansiiprint.bold(), ansiiprint.end()))
|
|
|
|
ainformation.append('')
|
2018-09-28 20:31:56 -04:00
|
|
|
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 = []
|
|
|
|
description = {}
|
2018-09-28 20:42:24 -04:00
|
|
|
domain = {}
|
2018-09-28 20:31:56 -04:00
|
|
|
ip_network = {}
|
|
|
|
ip_gateway = {}
|
|
|
|
dhcp_flag = {}
|
|
|
|
dhcp_flag_colour = {}
|
2018-09-29 02:54:48 -04:00
|
|
|
dhcp_start = {}
|
|
|
|
dhcp_end = {}
|
|
|
|
dhcp_range = {}
|
2018-09-28 20:31:56 -04:00
|
|
|
colour_off = ansiiprint.end()
|
|
|
|
|
|
|
|
# Gather information for printing
|
|
|
|
for net in net_list:
|
|
|
|
# get info
|
2018-09-29 02:54:48 -04:00
|
|
|
description[net], domain[net], ip_network[net], ip_gateway[net], dhcp_flag[net], dhcp_start[net], dhcp_end[net] = getNetworkInformation(zk_conn, net)
|
|
|
|
|
|
|
|
if dhcp_flag[net] == "True":
|
2018-09-28 20:31:56 -04:00
|
|
|
dhcp_flag_colour[net] = ansiiprint.green()
|
2018-09-29 02:54:48 -04:00
|
|
|
dhcp_range[net] = '{} - {}'.format(dhcp_start[net], dhcp_end[net])
|
2018-09-28 20:31:56 -04:00
|
|
|
else:
|
|
|
|
dhcp_flag_colour[net] = ansiiprint.blue()
|
2018-09-29 02:54:48 -04:00
|
|
|
dhcp_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
|
|
|
|
net_description_length = 13
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain_length = 8
|
2018-09-28 20:31:56 -04:00
|
|
|
net_ip_network_length = 12
|
|
|
|
net_ip_gateway_length = 9
|
2018-09-29 02:54:48 -04:00
|
|
|
net_dhcp_range_length = 12
|
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
|
|
|
# ip_network column
|
|
|
|
_net_ip_network_length = len(ip_network[net]) + 1
|
|
|
|
if _net_ip_network_length > net_ip_network_length:
|
|
|
|
net_ip_network_length = _net_ip_network_length
|
|
|
|
# ip_gateway column
|
|
|
|
_net_ip_gateway_length = len(ip_gateway[net]) + 1
|
|
|
|
if _net_ip_gateway_length > net_ip_gateway_length:
|
|
|
|
net_ip_gateway_length = _net_ip_gateway_length
|
2018-09-29 02:54:48 -04:00
|
|
|
# dhcp_range column
|
|
|
|
_net_dhcp_range_length = len(dhcp_range[net]) + 1
|
|
|
|
if _net_dhcp_range_length > net_dhcp_range_length:
|
|
|
|
net_dhcp_range_length = _net_dhcp_range_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}} \
|
2018-09-28 20:42:24 -04:00
|
|
|
{net_domain: <{net_domain_length}} \
|
2018-09-28 20:31:56 -04:00
|
|
|
{net_ip_network: <{net_ip_network_length}} \
|
|
|
|
{net_ip_gateway: <{net_ip_gateway_length}} \
|
2018-09-29 02:54:48 -04:00
|
|
|
{net_dhcp_flag: <6} \
|
|
|
|
{net_dhcp_range: <{net_dhcp_range_length}} \
|
2018-09-28 20:31:56 -04:00
|
|
|
{end_bold}'.format(
|
|
|
|
bold=ansiiprint.bold(),
|
|
|
|
end_bold=ansiiprint.end(),
|
|
|
|
net_vni_length=net_vni_length,
|
|
|
|
net_description_length=net_description_length,
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain_length=net_domain_length,
|
2018-09-28 20:31:56 -04:00
|
|
|
net_ip_network_length=net_ip_network_length,
|
|
|
|
net_ip_gateway_length=net_ip_gateway_length,
|
2018-09-29 02:54:48 -04:00
|
|
|
net_dhcp_range_length=net_dhcp_range_length,
|
2018-09-28 20:31:56 -04:00
|
|
|
net_vni='VNI',
|
|
|
|
net_description='Description',
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain='Domain',
|
2018-09-28 20:31:56 -04:00
|
|
|
net_ip_network='Network',
|
|
|
|
net_ip_gateway='Gateway',
|
2018-09-29 02:54:48 -04:00
|
|
|
net_dhcp_flag='DHCP',
|
|
|
|
net_dhcp_range='Range',
|
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}} \
|
2018-09-28 20:42:24 -04:00
|
|
|
{net_domain: <{net_domain_length}} \
|
2018-09-28 20:31:56 -04:00
|
|
|
{net_ip_network: <{net_ip_network_length}} \
|
|
|
|
{net_ip_gateway: <{net_ip_gateway_length}} \
|
2018-09-29 02:54:48 -04:00
|
|
|
{dhcp_flag_colour}{net_dhcp_flag: <6}{colour_off} \
|
|
|
|
{net_dhcp_range: <{net_dhcp_range_length}} \
|
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,
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain_length=net_domain_length,
|
2018-09-28 20:31:56 -04:00
|
|
|
net_ip_network_length=net_ip_network_length,
|
|
|
|
net_ip_gateway_length=net_ip_gateway_length,
|
2018-09-29 02:54:48 -04:00
|
|
|
net_dhcp_range_length=net_dhcp_range_length,
|
2018-09-28 20:31:56 -04:00
|
|
|
net_vni=net,
|
|
|
|
net_description=description[net],
|
2018-09-28 20:42:24 -04:00
|
|
|
net_domain=domain[net],
|
2018-09-28 20:31:56 -04:00
|
|
|
net_ip_network=ip_network[net],
|
|
|
|
net_ip_gateway=ip_gateway[net],
|
|
|
|
net_dhcp_flag=dhcp_flag[net],
|
2018-09-29 02:54:48 -04:00
|
|
|
net_dhcp_range=dhcp_range[net],
|
2018-09-28 20:31:56 -04:00
|
|
|
dhcp_flag_colour=dhcp_flag_colour[net],
|
|
|
|
colour_off=colour_off
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
output_string = net_list_output_header + '\n' + '\n'.join(sorted(net_list_output))
|
|
|
|
return output_string
|
|
|
|
|
2018-10-03 23:16:31 -04:00
|
|
|
def formatDHCPLeaseList(zk_conn, vni, dhcp_leases_list, reservations=False):
|
2018-10-02 00:02:23 -04:00
|
|
|
dhcp_lease_list_output = []
|
2018-09-30 12:43:56 -04:00
|
|
|
hostname = {}
|
2018-09-28 20:31:56 -04:00
|
|
|
ip_address = {}
|
|
|
|
mac_address = {}
|
2018-10-02 00:02:23 -04:00
|
|
|
timestamp = {}
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Gather information for printing
|
2018-10-02 00:02:23 -04:00
|
|
|
for dhcp_lease in dhcp_leases_list:
|
2018-10-03 23:16:31 -04:00
|
|
|
if reservations:
|
|
|
|
hostname[dhcp_lease], ip_address[dhcp_lease], mac_address[dhcp_lease], timestamp[dhcp_lease] = getDHCPReservationInformation(zk_conn, vni, dhcp_lease)
|
|
|
|
else:
|
|
|
|
hostname[dhcp_lease], ip_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-10-02 00:02:23 -04:00
|
|
|
lease_hostname_length = 13
|
|
|
|
lease_ip_address_length = 11
|
|
|
|
lease_mac_address_length = 13
|
|
|
|
for dhcp_lease in dhcp_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-09-28 20:31:56 -04:00
|
|
|
# ip_network column
|
2018-10-02 00:02:23 -04:00
|
|
|
_lease_ip_address_length = len(ip_address[dhcp_lease]) + 1
|
|
|
|
if _lease_ip_address_length > lease_ip_address_length:
|
|
|
|
lease_ip_address_length = _lease_ip_address_length
|
2018-09-28 20:31:56 -04:00
|
|
|
# ip_gateway 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}} \
|
|
|
|
{lease_ip_address: <{lease_ip_address_length}} \
|
|
|
|
{lease_mac_address: <{lease_mac_address_length}} \
|
|
|
|
{lease_timestamp: <{lease_timestamp_length}} \
|
2018-09-28 20:31:56 -04:00
|
|
|
{end_bold}'.format(
|
|
|
|
bold=ansiiprint.bold(),
|
|
|
|
end_bold=ansiiprint.end(),
|
2018-10-02 00:02:23 -04:00
|
|
|
lease_hostname_length=lease_hostname_length,
|
|
|
|
lease_ip_address_length=lease_ip_address_length,
|
|
|
|
lease_mac_address_length=lease_mac_address_length,
|
|
|
|
lease_timestamp_length=12,
|
|
|
|
lease_hostname='Hostname',
|
|
|
|
lease_ip_address='IP Address',
|
|
|
|
lease_mac_address='MAC Address',
|
|
|
|
lease_timestamp='Timestamp'
|
2018-09-28 20:31:56 -04:00
|
|
|
)
|
|
|
|
|
2018-10-02 00:02:23 -04:00
|
|
|
for dhcp_lease in dhcp_leases_list:
|
|
|
|
dhcp_lease_list_output.append('{bold}\
|
|
|
|
{lease_hostname: <{lease_hostname_length}} \
|
|
|
|
{lease_ip_address: <{lease_ip_address_length}} \
|
|
|
|
{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,
|
|
|
|
lease_ip_address_length=lease_ip_address_length,
|
|
|
|
lease_mac_address_length=lease_mac_address_length,
|
|
|
|
lease_timestamp_length=12,
|
|
|
|
lease_hostname=hostname[dhcp_lease],
|
|
|
|
lease_ip_address=ip_address[dhcp_lease],
|
|
|
|
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
|
|
|
|
|
|
|
|
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):
|
|
|
|
ip_blocks = str(ipaddr).split(".")
|
|
|
|
if len(ip_blocks) == 4:
|
|
|
|
for block in ip_blocks:
|
|
|
|
# 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
|
|
|
|
#
|
2018-09-29 02:54:48 -04:00
|
|
|
def add_network(zk_conn, vni, description, domain, ip_network, ip_gateway, dhcp_flag, dhcp_start, dhcp_end):
|
2018-09-23 15:26:20 -04:00
|
|
|
if description == '':
|
|
|
|
description = vni
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-09-29 02:54:48 -04:00
|
|
|
if dhcp_flag and ( not dhcp_start or not dhcp_end ):
|
|
|
|
return False, 'ERROR: DHCP start and end addresses are required for a DHCP-enabled network.'
|
|
|
|
|
2018-09-23 15:26:20 -04:00
|
|
|
# Check if a network with this VNI already exists
|
|
|
|
if zk_conn.exists('/networks/{}'.format(vni)):
|
|
|
|
return False, 'ERROR: A network with VNI {} already exists!'.format(vni)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
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,
|
|
|
|
'/networks/{}/domain'.format(vni): domain,
|
|
|
|
'/networks/{}/ip_network'.format(vni): ip_network,
|
|
|
|
'/networks/{}/ip_gateway'.format(vni): ip_gateway,
|
|
|
|
'/networks/{}/dhcp_flag'.format(vni): str(dhcp_flag),
|
|
|
|
'/networks/{}/dhcp_start'.format(vni): dhcp_start,
|
|
|
|
'/networks/{}/dhcp_end'.format(vni): dhcp_end,
|
2018-09-30 12:43:56 -04:00
|
|
|
'/networks/{}/dhcp_leases'.format(vni): '',
|
2018-10-03 19:23:46 -04:00
|
|
|
'/networks/{}/dhcp_reservations'.format(vni): '',
|
2018-09-29 02:54:48 -04:00
|
|
|
'/networks/{}/firewall_rules'.format(vni): ''
|
|
|
|
})
|
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
|
|
|
|
transaction = zk_conn.transaction()
|
2018-09-29 02:54:48 -04:00
|
|
|
zk_data = {}
|
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-09-23 15:26:20 -04:00
|
|
|
if parameters['ip_network'] != None:
|
2018-09-29 02:54:48 -04:00
|
|
|
zk_data.update({'/networks/{}/ip_network'.format(vni): parameters['ip_network']})
|
2018-09-23 15:26:20 -04:00
|
|
|
if parameters['ip_gateway'] != None:
|
2018-09-29 02:54:48 -04:00
|
|
|
zk_data.update({'/networks/{}/ip_gateway'.format(vni): parameters['ip_gateway']})
|
2018-09-23 15:26:20 -04:00
|
|
|
if parameters['dhcp_flag'] != None:
|
2018-09-29 02:54:48 -04:00
|
|
|
zk_data.update({'/networks/{}/dhcp_flag'.format(vni): str(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)
|
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-09-20 03:25:58 -04:00
|
|
|
try:
|
2018-09-23 15:26:20 -04:00
|
|
|
zk_conn.delete('/networks/{}'.format(vni), recursive=True)
|
2018-09-20 03:25:58 -04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
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-10-03 23:16:31 -04:00
|
|
|
if zk_conn.exists('/networks/{}/dhcp_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-10-03 23:16:31 -04:00
|
|
|
'/networks/{}/dhcp_reservations/{}'.format(net_vni, macaddress): 'static',
|
|
|
|
'/networks/{}/dhcp_reservations/{}/hostname'.format(net_vni, macaddress): hostname,
|
|
|
|
'/networks/{}/dhcp_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-02 00:02:23 -04:00
|
|
|
def remove_dhcp_lease(zk_conn, network, lease):
|
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-10-02 00:02:23 -04:00
|
|
|
dhcp_leases_list = zk_conn.get_children('/networks/{}/dhcp_leases'.format(net_vni))
|
|
|
|
for macaddr in dhcp_leases_list:
|
2018-09-30 12:43:56 -04:00
|
|
|
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/hostname'.format(net_vni, macaddr))
|
2018-09-30 11:22:25 -04:00
|
|
|
ipaddress = zkhandler.readdata(zk_conn, '/networks/{}/dhcp_leases/{}/ipaddr'.format(net_vni, macaddr))
|
2018-10-02 00:02:23 -04:00
|
|
|
if lease == macaddr or lease == hostname or lease == ipaddress:
|
2018-09-30 11:22:25 -04:00
|
|
|
match_description = macaddr
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
if not match_description:
|
2018-10-02 00:02:23 -04:00
|
|
|
return False, 'ERROR: No DHCP lease exists matching "{}"!'.format(reservation)
|
2018-09-28 20:31:56 -04:00
|
|
|
|
|
|
|
# Remove the entry from zookeeper
|
|
|
|
try:
|
2018-09-30 11:22:25 -04:00
|
|
|
zk_conn.delete('/networks/{}/dhcp_leases/{}'.format(net_vni, match_description), recursive=True)
|
2018-09-28 20:31:56 -04:00
|
|
|
except:
|
|
|
|
return False, 'ERROR: Failed to write to Zookeeper!'
|
|
|
|
|
2018-10-02 00:02:23 -04:00
|
|
|
return True, 'DHCP lease "{}" removed successfully!'.format(match_description)
|
2018-09-28 20:31:56 -04:00
|
|
|
|
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 = []
|
|
|
|
full_net_list = zk_conn.get_children('/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)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def get_list_firewall_rules(zk_conn, network):
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
firewall_rules = getNetworkFirewallRules(zk_conn, net_vni)
|
|
|
|
return firewall_rules
|