David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Laboratory |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 17 | "bytes" |
| 18 | "encoding/json" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 19 | "fmt" |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 20 | "github.com/Sirupsen/logrus" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 21 | "github.com/kelseyhightower/envconfig" |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 22 | "net/http" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 23 | "time" |
| 24 | ) |
| 25 | |
| 26 | type Config struct { |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 27 | VendorsURL string `default:"file:///switchq/vendors.json" envconfig:"vendors_url"` |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 28 | AddressURL string `default:"file:///switchq/dhcp_harvest.inc" envconfig:"address_url"` |
| 29 | PollInterval string `default:"1m" envconfig:"poll_interval"` |
| 30 | ProvisionTTL string `default:"1h" envconfig:"provision_ttl"` |
| 31 | ProvisionURL string `default:"" envconfig:"provision_url"` |
| 32 | RoleSelectorURL string `default:"" envconfig:"role_selector_url"` |
| 33 | DefaultRole string `default:"fabric-switch" envconfig:"default_role"` |
| 34 | Script string `default:"do-ansible"` |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 35 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL"` |
| 36 | LogFormat string `default:"text" envconfig:"LOG_FORMAT"` |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 37 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 38 | vendors Vendors |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 39 | addressSource AddressSource |
| 40 | interval time.Duration |
| 41 | ttl time.Duration |
| 42 | } |
| 43 | |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 44 | const ( |
| 45 | Pending TaskStatus = iota |
| 46 | Running |
| 47 | Complete |
| 48 | Failed |
| 49 | ) |
| 50 | |
| 51 | type RequestInfo struct { |
| 52 | Id string `json:"id"` |
| 53 | Name string `json:"name"` |
| 54 | Ip string `json:"ip"` |
| 55 | Mac string `json:"mac"` |
| 56 | RoleSelector string `json:"role_selector"` |
| 57 | Role string `json:"role"` |
| 58 | Script string `json:"script"` |
| 59 | } |
| 60 | |
| 61 | type TaskStatus uint8 |
| 62 | |
| 63 | type WorkRequest struct { |
| 64 | Info *RequestInfo |
| 65 | Script string |
| 66 | Role string |
| 67 | } |
| 68 | |
| 69 | type StatusMsg struct { |
| 70 | Request *WorkRequest `json:"request"` |
| 71 | Worker int `json:"worker"` |
| 72 | Status TaskStatus `json:"status"` |
| 73 | Message string `json:"message"` |
| 74 | Timestamp int64 `json:"timestamp"` |
| 75 | } |
| 76 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 77 | func checkError(err error, msg string, args ...interface{}) { |
| 78 | if err != nil { |
| 79 | log.Fatalf(msg, args...) |
| 80 | } |
| 81 | } |
| 82 | |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 83 | func (c *Config) getProvisionedState(rec AddressRec) (*StatusMsg, error) { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 84 | log.Debugf("Fetching provisioned state of device '%s' (%s, %s)", |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 85 | rec.Name, rec.IP, rec.MAC) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 86 | resp, err := http.Get(c.ProvisionURL + rec.MAC) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 87 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 88 | log.Errorf("Error while retrieving provisioning state for device '%s (%s, %s)' : %s", |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 89 | rec.Name, rec.IP, rec.MAC, err) |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 90 | return nil, err |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 91 | } |
| 92 | if resp.StatusCode != 404 && int(resp.StatusCode/100) != 2 { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 93 | log.Errorf("Error while retrieving provisioning state for device '%s (%s, %s)' : %s", |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 94 | rec.Name, rec.IP, rec.MAC, resp.Status) |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 95 | return nil, fmt.Errorf(resp.Status) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 96 | } |
| 97 | defer resp.Body.Close() |
| 98 | if resp.StatusCode != 404 { |
| 99 | decoder := json.NewDecoder(resp.Body) |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 100 | var status StatusMsg |
| 101 | err = decoder.Decode(&status) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 102 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 103 | log.Errorf("Unmarshal provisioning service response for device '%s (%s, %s)' : %s", |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 104 | rec.Name, rec.IP, rec.MAC, err) |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 105 | return nil, err |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 106 | } |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 107 | return &status, nil |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 108 | } |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 109 | |
| 110 | // If we end up here that means that no record was found in the provisioning, so return |
| 111 | // a status of -1, w/o an error |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 112 | return nil, nil |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | func (c *Config) provision(rec AddressRec) error { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 116 | log.Infof("POSTing to '%s' for provisioning of '%s (%s)'", c.ProvisionURL, rec.Name, rec.MAC) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 117 | data := map[string]string{ |
| 118 | "id": rec.MAC, |
| 119 | "name": rec.Name, |
| 120 | "ip": rec.IP, |
| 121 | "mac": rec.MAC, |
| 122 | } |
| 123 | if c.RoleSelectorURL != "" { |
| 124 | data["role_selector"] = c.RoleSelectorURL |
| 125 | } |
| 126 | if c.DefaultRole != "" { |
| 127 | data["role"] = c.DefaultRole |
| 128 | } |
| 129 | if c.Script != "" { |
| 130 | data["script"] = c.Script |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 131 | } |
| 132 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 133 | hc := http.Client{} |
| 134 | var b []byte |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 135 | b, err := json.Marshal(data) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 136 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 137 | log.Errorf("Unable to marshal provisioning data : %s", err) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 138 | return err |
| 139 | } |
| 140 | req, err := http.NewRequest("POST", c.ProvisionURL, bytes.NewReader(b)) |
| 141 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 142 | log.Errorf("Unable to construct POST request to provisioner : %s", err) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 143 | return err |
| 144 | } |
| 145 | |
| 146 | req.Header.Add("Content-Type", "application/json") |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 147 | resp, err := hc.Do(req) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 148 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 149 | log.Errorf("Unable to POST request to provisioner : %s", err) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 150 | return err |
| 151 | } |
| 152 | |
| 153 | defer resp.Body.Close() |
| 154 | if resp.StatusCode != http.StatusAccepted { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 155 | log.Errorf("Provisioning request not accepted by provisioner : %s", resp.Status) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 156 | return err |
| 157 | } |
| 158 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 159 | return nil |
| 160 | } |
| 161 | |
| 162 | func (c *Config) processRecord(rec AddressRec) error { |
| 163 | ok, err := c.vendors.Switchq(rec.MAC) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 164 | if err != nil { |
| 165 | return fmt.Errorf("unable to determine ventor of MAC '%s' (%s)", rec.MAC, err) |
| 166 | } |
| 167 | |
| 168 | if !ok { |
| 169 | // Not something we care about |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 170 | log.Debugf("host with IP '%s' and MAC '%s' and named '%s' not a known switch type", |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 171 | rec.IP, rec.MAC, rec.Name) |
| 172 | return nil |
| 173 | } |
| 174 | |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 175 | // Verify if the provision status of the node is complete, if in an error state then TTL means |
| 176 | // nothing |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 177 | state, err := c.getProvisionedState(rec) |
| 178 | if state != nil { |
| 179 | switch state.Status { |
| 180 | case Pending, Running: // Pending or Running |
| 181 | log.Debugf("device '%s' (%s, %s) is being provisioned", |
| 182 | rec.Name, rec.IP, rec.MAC) |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 183 | return nil |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 184 | case Complete: // Complete |
| 185 | log.Debugf("device '%s' (%s, %s) has completed provisioning", |
| 186 | rec.Name, rec.IP, rec.MAC) |
| 187 | case Failed: // Failed |
| 188 | log.Debugf("device '%s' (%s, %s) failed last provisioning with message '%s', reattempt", |
| 189 | rec.Name, rec.IP, rec.MAC, state.Message) |
David K. Bainbridge | 98bbc04 | 2016-08-22 17:35:28 -0700 | [diff] [blame] | 190 | state = nil |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 191 | default: // Unknown state |
| 192 | log.Debugf("device '%s' (%s, %s) has unknown provisioning state '%d', will provision", |
| 193 | rec.Name, rec.IP, rec.MAC, state.Status) |
David K. Bainbridge | 98bbc04 | 2016-08-22 17:35:28 -0700 | [diff] [blame] | 194 | state = nil |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 195 | } |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 196 | } else { |
| 197 | log.Debugf("device '%s' (%s, %s) has no provisioning record", |
| 198 | rec.Name, rec.IP, rec.MAC) |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 199 | } |
| 200 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 201 | // If TTL is 0 then we will only provision a switch once. |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 202 | if state == nil || (c.ttl > 0 && time.Since(time.Unix(state.Timestamp, 0)) > c.ttl) { |
| 203 | if state != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 204 | log.Debugf("device '%s' (%s, %s) TTL expired, reprovisioning", |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 205 | rec.Name, rec.IP, rec.MAC) |
| 206 | } |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 207 | c.provision(rec) |
| 208 | } else if c.ttl == 0 { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 209 | log.Debugf("device '%s' (%s, %s) has completed its one time provisioning, with a TTL set to %s", |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 210 | rec.Name, rec.IP, rec.MAC, c.ProvisionTTL) |
| 211 | } else { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 212 | log.Debugf("device '%s' (%s, %s) has completed provisioning within the specified TTL of %s", |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 213 | rec.Name, rec.IP, rec.MAC, c.ProvisionTTL) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 214 | } |
| 215 | return nil |
| 216 | } |
| 217 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 218 | var log = logrus.New() |
| 219 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 220 | func main() { |
| 221 | |
| 222 | var err error |
| 223 | config := Config{} |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 224 | err = envconfig.Process("SWITCHQ", &config) |
| 225 | if err != nil { |
| 226 | log.Fatalf("Unable to parse configuration options : %s", err) |
| 227 | } |
| 228 | |
| 229 | switch config.LogFormat { |
| 230 | case "json": |
| 231 | log.Formatter = &logrus.JSONFormatter{} |
| 232 | default: |
| 233 | log.Formatter = &logrus.TextFormatter{ |
| 234 | FullTimestamp: true, |
| 235 | ForceColors: true, |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | level, err := logrus.ParseLevel(config.LogLevel) |
| 240 | if err != nil { |
| 241 | level = logrus.WarnLevel |
| 242 | } |
| 243 | log.Level = level |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 244 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 245 | config.vendors, err = NewVendors(config.VendorsURL) |
| 246 | checkError(err, "Unable to create known vendors list from specified URL '%s' : %s", config.VendorsURL, err) |
| 247 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 248 | config.addressSource, err = NewAddressSource(config.AddressURL) |
| 249 | checkError(err, "Unable to create required address source for specified URL '%s' : %s", config.AddressURL, err) |
| 250 | |
| 251 | config.interval, err = time.ParseDuration(config.PollInterval) |
| 252 | checkError(err, "Unable to parse specified poll interface '%s' : %s", config.PollInterval, err) |
| 253 | |
| 254 | config.ttl, err = time.ParseDuration(config.ProvisionTTL) |
| 255 | checkError(err, "Unable to parse specified provision TTL value of '%s' : %s", config.ProvisionTTL, err) |
| 256 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 257 | log.Infof(`Configuration: |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 258 | Vendors URL: %s |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 259 | Poll Interval: %s |
| 260 | Address Source: %s |
| 261 | Provision TTL: %s |
| 262 | Provision URL: %s |
| 263 | Role Selector URL: %s |
| 264 | Default Role: %s |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 265 | Script: %s |
| 266 | Log Level: %s |
| 267 | Log Format: %s`, |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 268 | config.VendorsURL, config.PollInterval, config.AddressURL, config.ProvisionTTL, |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 269 | config.ProvisionURL, config.RoleSelectorURL, config.DefaultRole, config.Script, |
| 270 | config.LogLevel, config.LogFormat) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 271 | |
| 272 | // We use two methods to attempt to find the MAC (hardware) address associated with an IP. The first |
| 273 | // is to look in the table. The second is to send an ARP packet. |
| 274 | for { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 275 | log.Infof("Checking for switches @ %s", time.Now()) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 276 | addresses, err := config.addressSource.GetAddresses() |
| 277 | |
| 278 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 279 | log.Errorf("unable to read addresses from address source : %s", err) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 280 | } else { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 281 | log.Infof("Queried %d addresses from address source", len(addresses)) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 282 | |
| 283 | for _, rec := range addresses { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 284 | log.Debugf("Processing %s(%s, %s)", rec.Name, rec.IP, rec.MAC) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 285 | if err := config.processRecord(rec); err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 286 | log.Errorf("Error when processing IP '%s' : %s", rec.IP, err) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | time.Sleep(config.interval) |
| 292 | } |
| 293 | } |