Made switch config not reconfigure existing teams.

This commit is contained in:
Patrick Fairbank
2014-08-17 17:35:14 -07:00
parent feb03da244
commit e8574821a9
2 changed files with 75 additions and 18 deletions

View File

@@ -29,6 +29,10 @@ var aironetMutex sync.Mutex
// Sets up wireless networks for the given set of teams.
func ConfigureTeamWifi(red1, red2, red3, blue1, blue2, blue3 *Team) error {
// Make sure multiple configurations aren't being set at the same time.
aironetMutex.Lock()
defer aironetMutex.Unlock()
for _, team := range []*Team{red1, red2, red3, blue1, blue2, blue3} {
if team != nil && (len(team.WpaKey) < 8 || len(team.WpaKey) > 63) {
return fmt.Errorf("Invalid WPA key '%s' configured for team %d.", team.WpaKey, team.Id)
@@ -109,10 +113,6 @@ func getSsids() (map[string]int, error) {
// Logs into the Aironet via Telnet and runs the given command in user exec mode. Reads the output and returns
// it as a string.
func runAironetCommand(command string) (string, error) {
// Make sure multiple commands aren't being run at the same time.
aironetMutex.Lock()
defer aironetMutex.Unlock()
// Open a Telnet connection to the AP.
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", eventSettings.ApAddress, aironetTelnetPort))
if err != nil {

View File

@@ -10,6 +10,8 @@ import (
"bytes"
"fmt"
"net"
"regexp"
"strconv"
"sync"
)
@@ -20,28 +22,83 @@ var catalystMutex sync.Mutex
// Sets up wired networks for the given set of teams.
func ConfigureTeamEthernet(red1, red2, red3, blue1, blue2, blue3 *Team) error {
command := setupVlan(red1, red1Vlan) + setupVlan(red2, red2Vlan) + setupVlan(red3, red3Vlan) +
setupVlan(blue1, blue1Vlan) + setupVlan(blue2, blue2Vlan) + setupVlan(blue3, blue3Vlan)
_, err := runCatalystConfigCommand(command)
return err
// Make sure multiple configurations aren't being set at the same time.
catalystMutex.Lock()
defer catalystMutex.Unlock()
// Determine what new team VLANs are needed and build the commands to set them up.
oldTeamVlans, err := getTeamVlans()
if err != nil {
return err
}
addTeamVlansCommand := ""
replaceTeamVlan := func(team *Team, vlan int) {
if team == nil {
return
}
if oldTeamVlans[team.Id] == vlan {
delete(oldTeamVlans, team.Id)
} else {
addTeamVlansCommand += fmt.Sprintf("no access-list 1%d\naccess-list 1%d permit ip "+
"10.%d.%d.0 0.0.0.255 host %s\ninterface Vlan%d\nip address 10.%d.%d.1 255.255.255.0\n", vlan,
vlan, team.Id/100, team.Id%100, eventServerAddress, vlan, team.Id/100, team.Id%100)
}
}
replaceTeamVlan(red1, red1Vlan)
replaceTeamVlan(red2, red2Vlan)
replaceTeamVlan(red3, red3Vlan)
replaceTeamVlan(blue1, blue1Vlan)
replaceTeamVlan(blue2, blue2Vlan)
replaceTeamVlan(blue3, blue3Vlan)
// Build the command to remove the team VLANs that are no longer needed.
removeTeamVlansCommand := ""
for _, vlan := range oldTeamVlans {
removeTeamVlansCommand += fmt.Sprintf("interface Vlan%d\nno ip address\nno access-list 1%d\n", vlan, vlan)
}
command := removeTeamVlansCommand + addTeamVlansCommand
if len(command) > 0 {
_, err = runCatalystConfigCommand(removeTeamVlansCommand + addTeamVlansCommand)
if err != nil {
return err
}
}
return nil
}
func setupVlan(team *Team, vlan int) string {
if team == nil {
return ""
// Returns a map of currently-configured teams to VLANs.
func getTeamVlans() (map[int]int, error) {
// Get the entire config dump.
config, err := runCatalystCommand("show running-config\n")
if err != nil {
return nil, err
}
return fmt.Sprintf("no access-list 1%d\naccess-list 1%d permit ip 10.%d.%d.0 0.0.0.255 host %s\n"+
"interface Vlan%d\nno ip address\nip address 10.%d.%d.1 255.255.255.0\n", vlan, vlan, team.Id/100,
team.Id%100, eventServerAddress, vlan, team.Id/100, team.Id%100)
// Parse out the team IDs and VLANs from the config dump.
re := regexp.MustCompile("(?s)interface Vlan(\\d\\d)\\s+ip address 10\\.(\\d+)\\.(\\d+)\\.1")
teamVlanMatches := re.FindAllStringSubmatch(config, -1)
if teamVlanMatches == nil {
// There are probably no teams currently configured.
return nil, nil
}
// Build the map of team to VLAN.
teamVlans := make(map[int]int)
for _, match := range teamVlanMatches {
team100s, _ := strconv.Atoi(match[2])
team1s, _ := strconv.Atoi(match[3])
team := int(team100s)*100 + team1s
vlan, _ := strconv.Atoi(match[1])
teamVlans[team] = vlan
}
return teamVlans, nil
}
// Logs into the Catalyst via Telnet and runs the given command in user exec mode. Reads the output and
// returns it as a string.
func runCatalystCommand(command string) (string, error) {
// Make sure multiple commands aren't being run at the same time.
catalystMutex.Lock()
defer catalystMutex.Unlock()
// Open a Telnet connection to the switch.
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", eventSettings.SwitchAddress, catalystTelnetPort))
if err != nil {