// 2019 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. // To build this app, run the following command: // CGO_ENABLED=0 GOARCH=amd64 go build -a -installsuffix cgo -o bin/change_dc change_dc.go // Package uglyservers is used to generate a listing of complex server port mappings and // and port-mapping issues. package uglyservers import ( "encoding/json" "fmt" "net/url" "os" "sort" "strconv" "strings" "github.rackspace.com/DCM/tools" "github.com/gookit/color" "github.com/jedib0t/go-pretty/v6/table" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/gomigrate/shared" "github.rackspace.com/gorax/clients/dcxAutohost" "github.rackspace.com/gorax/clients/mongodbAPI" "github.rackspace.com/gorax/clients/unipede" "github.rackspace.com/gorax/gokeys" ) var portDetailsTypes = map[string]bool{ "AggExNet": true, "PrivateNet": true, } type ( // Client is an initializer struct for the ugly_servers package // that contains all the client API pkgs needed for this package. Client struct { dcx *dcxAutohost.Client uni *unipede.Client mgo *mongodbAPI.Client identityToken string } devMaps struct { Complex []tools.Device NonComplex []tools.Device } // VlanMaps type is used to map vlan/port data to a switch vlanMaps struct { Mode string `json:"mode"` NativeVlan int `json:"native_vlan"` Vlans []string `json:"vlans"` } ) func initCfg(cfg gokeys.Settings) *Client { // Pull a core token from the users gokeys keychain cKey, err := cfg.Core(false) if err != nil { logrus.Fatal(err) } ssoKey, err := cfg.SSO() if err != nil { logrus.Fatal(err) } // Pull a Identity token from the users gokeys keychain iKey, err := cfg.Identity(false) if err != nil { logrus.Fatal(err) } u, err := unipede.New(ssoKey.Username, ssoKey.Password, "") if err != nil { logrus.Fatal(err) } dcx, err := dcxAutohost.NewWithToken(iKey.Password, false) if err != nil { logrus.Fatal(err) } return &Client{ dcx: dcx, uni: u, mgo: mongodbAPI.New(mongodbAPI.WithToken(cKey.Password), "migration", false), identityToken: iKey.Password, } } // pluginFunc is the main entry point function and will take a move-group to determine servers or net-devices // that are assign a port configuration that could possibly be a complicated assignment and require // manual review. func pluginFunc(cfg gokeys.Settings) { c := initCfg(cfg) if len(*argMoveGroups) == 0 { logrus.Fatalf("unable to find move-groups in %s", *argMoveGroups) } // Determine the mongo collection we should be querying based on the move-group // name. collection := shared.GetCollection((*argMoveGroups)[0]) if collection == "" { logrus.Fatalf("unable to parse collection name from %s", (*argMoveGroups)[0]) } for _, moveGroup := range *argMoveGroups { moveGroup = strings.ToUpper(moveGroup) // Pull a list of network devices ONLY for us to run through the failover_device call. accountList, err := c.getDeviceList(moveGroup, collection) if err != nil { logrus.Errorf("unable to pull devices for %s::%s", moveGroup, err) } var portMapDevices []int // Get a list of device numbers to get port-maps on for _, devs := range accountList { for _, d := range devs.Complex { portMapDevices = append(portMapDevices, d.ID) } for _, d := range devs.NonComplex { portMapDevices = append(portMapDevices, d.ID) } } if *argVerbose { logrus.Info("retrieving portmap data ...") } portMaps, err := c.GetPortMaps(portMapDevices) if err != nil { logrus.Fatalf("uglyservers:Run:getPortMaps:%s", err) return } logrus.Infof("Auditing %d devices for >2 ExNet, >1 PNet/LocalNet, or unmapped ports:", len(portMapDevices)) for acct, devices := range accountList { var items []tools.Device var vlanMaps = make(map[string]vlanMaps) for _, device := range devices.Complex { if _, exist := portMaps[device.ID]; !exist { logrus.Errorf("%s::no port-map data found", device.ID) continue } if portMaps[device.ID].Error != "" { logrus.Errorf("%d::error fetching port-map data [%s]", device.ID, portMaps[device.ID].Error) continue } for _, port := range device.Environment.Current.PortsParsed { if _, ok := portDetailsTypes[port.PortType]; ok { if *argVerbose { logrus.Infof("retrieving vlan data for %s[%d].%s ...", port.SwitchName, port.PortNumber, port.DataCenter) } vlans, err := c.getVlansForPort(fmt.Sprintf("%s.%s", port.SwitchName, port.DataCenter), port.PortNumber) if err != nil { logrus.Errorf("%s::unable to get port vlan data::%s", device.ID, err) } vlanMaps[fmt.Sprintf("%s[%d]", port.SwitchName, port.PortNumber)] = vlans } } items = append(items, device) } var errs []error // Check all servers for unmapped ports for _, device := range devices.NonComplex { if _, exist := portMaps[device.ID]; !exist { logrus.Errorf("%d::no port-map data found", device.ID) continue } if portMaps[device.ID].Error != "" { logrus.Errorf("%d::error fetching port-map data [%s]", device.ID, portMaps[device.ID].Error) continue } if portMaps[device.ID].UnmappedMigrationPorts != "" || portMaps[device.ID].UnmappedSourcePorts != "" { items = append(items, device) } if errs = c.PortMapAudit(portMaps[device.ID]); len(errs) != 0 { items = append(items, device) } } if len(items) != 0 { c.prettyPrint(acct, items, portMaps, vlanMaps) } } } } // prettyPrint func will take a structured type of response data and render a table // output to stdout. func (c *Client) prettyPrint(account int, devices []tools.Device, portMaps map[int]*dcxAutohost.DevicePortMap, vlanMaps map[string]vlanMaps) { t := table.NewWriter() t.SetOutputMirror(os.Stdout) t.AppendHeader(table.Row{fmt.Sprintf("ACCOUNT: %d", account), "Source Port", "Migration Port", "Vlans"}) for _, d := range shared.SortDevices(devices) { var src []string var dst []string var vlans []string errs := c.PortMapAudit(portMaps[d.ID]) for _, p := range shared.SortPortMaps(portMaps[d.ID].PortMaps) { src = append(src, fmt.Sprintf("%s.%s[%d] (%s)", p.Source.Switch.Number, portMaps[d.ID].SourceDC, p.Source.PortNumber, p.Source.SwitchPortType)) if _, ok := vlanMaps[fmt.Sprintf("%s[%d]", p.Source.Switch.Number, p.Source.PortNumber)]; ok { v := vlanMaps[fmt.Sprintf("%s[%d]", p.Source.Switch.Number, p.Source.PortNumber)] vlanStr := strings.Join(v.Vlans, ",") if len(v.Vlans) > 5 { vlanStr = strings.Join(v.Vlans[:5], ",") vlanStr += "..." } vlans = append(vlans, fmt.Sprintf("%s:%s", v.Mode, vlanStr)) } else { vlans = append(vlans, "") } dst = append(dst, fmt.Sprintf("%s.%s[%d] (%s)", p.Migration.Switch.Number, portMaps[d.ID].MigrationDC, p.Migration.PortNumber, p.Migration.SwitchPortType)) } id := strconv.Itoa(d.ID) if portMaps[d.ID].UnmappedSourcePorts != "" || portMaps[d.ID].UnmappedMigrationPorts != "" { id += color.Red.Sprintf("\nUNMAPPED PORTS!") } if len(errs) != 0 { id += color.Yellow.Sprintf("\nMAPPING ERRORS!") } if portMaps[d.ID].UnmappedSourcePorts != "" { src = append(src, color.Red.Sprintf(strings.Replace(portMaps[d.ID].UnmappedSourcePorts, ",", "\n", -1))) } if portMaps[d.ID].UnmappedMigrationPorts != "" { dst = append(dst, color.Red.Sprintf(strings.Replace(portMaps[d.ID].UnmappedMigrationPorts, ",", "\n", -1))) } for _, e := range errs { src = append(src, color.Yellow.Sprint(e.Error())) } t.AppendRow(table.Row{id, strings.Join(src, "\n"), strings.Join(dst, "\n"), strings.Join(vlans, "\n")}) } table.StyleDefault.Options.SeparateRows = true t.SetStyle(table.StyleDefault) t.Render() } // getDeviceList will call out to Mongo to pull a list of devices within a move-group // and filter out just network devices that are targeted in shutdown. func (c *Client) getDeviceList(moveGroup, collection string) (map[int]devMaps, error) { if *argVerbose { logrus.Info("retrieving devices from DB ...") } var resp = make(map[int]devMaps) // Initiate the call out to the migration/Find resource byt, err := c.mgo.Find( &mongodbAPI.FindQuery{ Query: map[string]interface{}{ "move_group": moveGroup, "deleted": map[string]bool{ "$ne": true, }, "environment.current.switch_ports_parsed": map[string]interface{}{ "$nin": []interface{}{"", nil}, }, }, Projection: []string{ "move_group", "device.make_model", "environment.current.switch_ports_parsed", "account.number", "account.name", }, }, collection) if err != nil { return resp, fmt.Errorf("mongodbAPI.Find:%s", err) } // Decode the json response back var payload struct { Result []tools.Device `json:"result"` } jd := json.NewDecoder(strings.NewReader(string(byt))) if err = jd.Decode(&payload); err != nil { return resp, err } cnt := 0 for _, d := range payload.Result { if len(d.Environment.Current.PortsParsed) == 0 { continue } cnt++ if _, ok := resp[d.Account.Number]; !ok { resp[d.Account.Number] = devMaps{ Complex: make([]tools.Device, 0), NonComplex: make([]tools.Device, 0), } } var ( privNet = 0 exNet = 0 ) for _, p := range d.Environment.Current.PortsParsed { switch strings.ToLower(p.PortType) { case "exnet": exNet = exNet + 1 case "aggexnet": exNet = exNet + 1 case "privatenet": privNet = privNet + 1 case "localnet": privNet = privNet + 1 } } temp := resp[d.Account.Number] if exNet > 2 || privNet > 0 { temp.Complex = append(temp.Complex, d) } else { temp.NonComplex = append(temp.NonComplex, d) } resp[d.Account.Number] = temp } if *argVerbose { logrus.Infof("found %d devices with ports in DB", cnt) } return resp, nil } // GetPortMaps method is a helper function to retrieve port-maps from a list of devices and store the // results in a key: value pair with the device number being the key. func (c *Client) GetPortMaps(devices []int) (map[int]*dcxAutohost.DevicePortMap, error) { var resp = make(map[int]*dcxAutohost.DevicePortMap) for _, d := range devices { pm, err := shared.PortMapsForDevice(d, c.identityToken) if err != nil { logrus.Errorf("[%d] GetPortMaps:%s", d, err) continue } resp[d] = &pm } return resp, nil } func (c *Client) getVlansForPort(switchName string, portID int) (vlanMaps, error) { result := vlanMaps{} items, err := c.uni.DeviceVlanPortView(url.Values{ "filter": []string{fmt.Sprintf("port_id:=%d", portID)}, }, switchName, nil) if err != nil { return result, fmt.Errorf("deviceVlanPortView:%s", err) } if len(items.Items) == 0 { result.Mode = color.LightRed.Sprint("Error") result.Vlans = append(result.Vlans, "Interface not found") return result, nil } for _, item := range items.Items { result.Mode = item.Mode result.NativeVlan = item.NativeVlan result.Vlans = append(result.Vlans, strconv.Itoa(item.Vlan)) } return result, nil } // PortMapData type contains all the data we will need // to make port-mapping error decisions type PortMapData struct { DRACCount int AggExCount int AggExMaps map[string][]AggExMap SNetCount int } // AggExMap struct is a breakdown of the mapped switchport // for a given source port. type AggExMap struct { Port int MappedSwitch string MappedPort int } // PortMapAudit will perform the logic to determine if there is a port-map // issue. func (c *Client) PortMapAudit(pMap *dcxAutohost.DevicePortMap) []error { var ( errs = make([]error, 0) pmd = PortMapData{AggExMaps: make(map[string][]AggExMap)} ) idInt, err := strconv.Atoi(pMap.Device) if err != nil { errs = append(errs, err) return errs } dMig, err := c.dcx.MigrationsByDevice(idInt) if err != nil { errs = append(errs, err) return errs } // We need to loop through the port-maps first to gather things that we need regarding all // the port maps. // We are going to do a contains instead of key: value map here because we don't want // minor differences of service-type names to break our audit. for _, p := range pMap.PortMaps { if shared.IContains(p.Migration.SwitchPortType, "DRAC") { pmd.DRACCount++ } if shared.IContains(p.Migration.SwitchPortType, "ExNet") { if _, ok := pmd.AggExMaps[p.Source.Switch.Number]; !ok { pmd.AggExMaps[p.Source.Switch.Number] = make([]AggExMap, 0) } pmd.AggExMaps[p.Source.Switch.Number] = append(pmd.AggExMaps[p.Source.Switch.Number], AggExMap{ Port: p.Source.PortNumber, MappedSwitch: p.Migration.Switch.Number, MappedPort: p.Migration.PortNumber, }) //nolint:scopelint sort.SliceStable(pmd.AggExMaps[p.Source.Switch.Number], func(i, j int) bool { return pmd.AggExMaps[p.Source.Switch.Number][i].Port < pmd.AggExMaps[p.Source.Switch.Number][j].Port }) pmd.AggExCount++ } if shared.IContains(p.Migration.SwitchPortType, "Service") { pmd.SNetCount++ } } for _, p := range pMap.PortMaps { // Check for rules that would require manual auditing. Basically things we haven't or can't // automate yet. if len(pmd.AggExMaps) > 2 { //errs = append(errs, fmt.Errorf("3+ Source ExNet switches. Audit mappings manually")) break } // If ExNet and only one assigned, confirm it assigned in-cab // AggExCount == 2 isn't needed here except for readability when I have to look at this // pile of junk later. if shared.IContains(p.Migration.SwitchPortType, "ExNet") && pmd.AggExCount == 2 { // If the source switch is the in-cab switch, check that the migration switch is // also in the migration in-cab switch if strings.HasPrefix(p.Source.Switch.Number, dMig.Current.Container) { if !strings.HasPrefix(p.Migration.Switch.Number, dMig.Migration.Container) { errs = append(errs, errInCab(p, dMig.Migration.Container)) } } } for srcSwitch, maps := range pmd.AggExMaps { iter := 0 for k, v := range maps { // The source maps are already sorted lowest to highest. // If the mapped port is higher than the previous mapped port // return an error because the migration mapping should also be lowest to highest. logrus.Debugf("Checking map for %s[%d] => %s[%d]", srcSwitch, v.Port, v.MappedSwitch, v.MappedPort) if v.MappedPort < maps[iter].MappedPort { errs = append(errs, fmt.Errorf("error:lowest-to-highest map %s[%d] => %s[%d]", srcSwitch, v.Port, v.MappedSwitch, v.MappedPort)) break } iter = k } } } return errs } func errInCab(p *dcxAutohost.PortMapPair, container string) error { return fmt.Errorf("%s[%d] => %s[%d] (%s) mapping not in %s", p.Source.Switch.Number, p.Source.PortNumber, p.Migration.Switch.Number, p.Migration.PortNumber, p.Migration.SwitchPortType, container) }