-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
With this tool we can now change the switch settings for each host interface to move into a specific vlan. Running the tool without `--change` will only check that the values are already correctly set, otherwise it will attempt to make those changes effective on the switch. Change-Id: Ib0b01ca75a83c588169aefef85653d4ed185b684
- Loading branch information
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#!/usr/bin/python3 | ||
|
||
import argparse | ||
import logging | ||
|
||
from quads.config import conf | ||
from quads.model import Host | ||
from quads.tools.juniper_set_port import juniper_set_port | ||
from quads.tools.ssh_helper import SSHHelper | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO, format="%(levelname)s - %(message)s") | ||
|
||
|
||
def verify(_host_name, change=False, nic1=None, nic2=None, nic3=None, nic4=None, nic5=None): | ||
_nics = {"em1": nic1, "em2": nic2, "em3": nic3, "em4": nic4, "em5": nic5} | ||
_host_obj = Host.objects(name=_host_name).first() | ||
if not _host_obj: | ||
logger.error(f"Hostname not found.") | ||
return | ||
|
||
logger.info(f"Host: {_host_obj.name}") | ||
if _host_obj.interfaces: | ||
interfaces = sorted(_host_obj.interfaces, key=lambda k: k["name"]) | ||
for i, interface in enumerate(interfaces): | ||
vlan = _nics.get(interface.name) | ||
if vlan: | ||
ssh_helper = SSHHelper(interface.ip_address, conf["junos_username"]) | ||
|
||
try: | ||
_, old_vlan_out = ssh_helper.run_cmd( | ||
"show configuration interfaces %s" % interface.switch_port | ||
) | ||
old_vlan = old_vlan_out[0].split(";")[0].split()[1] | ||
if old_vlan.startswith("QinQ"): | ||
old_vlan = old_vlan[7:] | ||
except IndexError: | ||
old_vlan = 0 | ||
|
||
try: | ||
_, vlan_member_out = ssh_helper.run_cmd( | ||
"show configuration vlans | display set | match %s.0" | ||
% interface.switch_port | ||
) | ||
vlan_member = vlan_member_out[0].split()[2][4:].strip(",") | ||
except IndexError: | ||
logger.warning( | ||
"Could not determine the previous VLAN member for %s, switch %s, switch port %s " | ||
% ( | ||
interface.name, | ||
interface.ip_address, | ||
interface.switch_port, | ||
) | ||
) | ||
vlan_member = 0 | ||
|
||
ssh_helper.disconnect() | ||
|
||
if int(old_vlan) != int(vlan): | ||
logger.warning( | ||
"Interface %s not using QinQ_vl%s", interface.switch_port, vlan | ||
) | ||
|
||
if int(vlan_member) != int(vlan): | ||
logger.warning( | ||
"Interface %s appears to be a member of VLAN %s, should be %s", | ||
interface.switch_port, | ||
vlan_member, | ||
vlan, | ||
) | ||
|
||
if change: | ||
logger.info(f"Change requested for {interface.name}") | ||
success = juniper_set_port( | ||
interface.ip_address, | ||
interface.switch_port, | ||
vlan_member, | ||
vlan, | ||
) | ||
|
||
if success: | ||
logger.info("Successfully updated switch settings.") | ||
else: | ||
logger.error( | ||
f"There was something wrong updating switch for {interface.name}" | ||
) | ||
else: | ||
logger.info(f"Interface {interface.name} is already configured for vlan{vlan}") | ||
else: | ||
logger.error( | ||
f"The host has no interfaces defined" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Verify switch configs for a cloud or host") | ||
parser.add_argument( | ||
"--host", | ||
dest="host", | ||
type=str, | ||
default=None, | ||
help="Host name to verify switch configuration for.", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--nic1", | ||
dest="nic1", | ||
type=str, | ||
default=None, | ||
help="Nic 1 (EM1).", | ||
) | ||
parser.add_argument( | ||
"--nic2", | ||
dest="nic2", | ||
type=str, | ||
default=None, | ||
help="Nic 2 (EM2).", | ||
) | ||
parser.add_argument( | ||
"--nic3", | ||
dest="nic3", | ||
type=str, | ||
default=None, | ||
help="Nic 3 (EM3).", | ||
) | ||
parser.add_argument( | ||
"--nic4", | ||
dest="nic4", | ||
type=str, | ||
default=None, | ||
help="Nic 4 (EM4).", | ||
) | ||
parser.add_argument( | ||
"--nic5", | ||
dest="nic5", | ||
type=str, | ||
default=None, | ||
help="Nic 5 (EM5).", | ||
) | ||
parser.add_argument( | ||
"--change", dest="change", action="store_true", help="Commit changes on switch." | ||
) | ||
|
||
args = parser.parse_args() | ||
verify(args.host, args.change, args.nic1, args.nic2, args.nic3, args.nic4, args.nic5) |