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 | 47996d6 | 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 | 47996d6 | 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 | 47996d6 | 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 | 47996d6 | 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 | 47996d6 | 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 | 47996d6 | 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 | 47996d6 | 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 | 47996d6 | 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 | 47996d6 | 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 | 47996d6 | 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) |
| 190 | default: // Unknown state |
| 191 | log.Debugf("device '%s' (%s, %s) has unknown provisioning state '%d', will provision", |
| 192 | rec.Name, rec.IP, rec.MAC, state.Status) |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 193 | } |
David K. Bainbridge | 47996d6 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 194 | } else { |
| 195 | log.Debugf("device '%s' (%s, %s) has no provisioning record", |
| 196 | rec.Name, rec.IP, rec.MAC) |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 197 | } |
| 198 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 199 | // If TTL is 0 then we will only provision a switch once. |
David K. Bainbridge | 47996d6 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 200 | if state == nil || (c.ttl > 0 && time.Since(time.Unix(state.Timestamp, 0)) > c.ttl) { |
| 201 | if state != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 202 | log.Debugf("device '%s' (%s, %s) TTL expired, reprovisioning", |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 203 | rec.Name, rec.IP, rec.MAC) |
| 204 | } |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 205 | c.provision(rec) |
| 206 | } else if c.ttl == 0 { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 207 | 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] | 208 | rec.Name, rec.IP, rec.MAC, c.ProvisionTTL) |
| 209 | } else { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 210 | 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] | 211 | rec.Name, rec.IP, rec.MAC, c.ProvisionTTL) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 212 | } |
| 213 | return nil |
| 214 | } |
| 215 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 216 | var log = logrus.New() |
| 217 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 218 | func main() { |
| 219 | |
| 220 | var err error |
| 221 | config := Config{} |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 222 | err = envconfig.Process("SWITCHQ", &config) |
| 223 | if err != nil { |
| 224 | log.Fatalf("Unable to parse configuration options : %s", err) |
| 225 | } |
| 226 | |
| 227 | switch config.LogFormat { |
| 228 | case "json": |
| 229 | log.Formatter = &logrus.JSONFormatter{} |
| 230 | default: |
| 231 | log.Formatter = &logrus.TextFormatter{ |
| 232 | FullTimestamp: true, |
| 233 | ForceColors: true, |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | level, err := logrus.ParseLevel(config.LogLevel) |
| 238 | if err != nil { |
| 239 | level = logrus.WarnLevel |
| 240 | } |
| 241 | log.Level = level |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 242 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 243 | config.vendors, err = NewVendors(config.VendorsURL) |
| 244 | checkError(err, "Unable to create known vendors list from specified URL '%s' : %s", config.VendorsURL, err) |
| 245 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 246 | config.addressSource, err = NewAddressSource(config.AddressURL) |
| 247 | checkError(err, "Unable to create required address source for specified URL '%s' : %s", config.AddressURL, err) |
| 248 | |
| 249 | config.interval, err = time.ParseDuration(config.PollInterval) |
| 250 | checkError(err, "Unable to parse specified poll interface '%s' : %s", config.PollInterval, err) |
| 251 | |
| 252 | config.ttl, err = time.ParseDuration(config.ProvisionTTL) |
| 253 | checkError(err, "Unable to parse specified provision TTL value of '%s' : %s", config.ProvisionTTL, err) |
| 254 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 255 | log.Infof(`Configuration: |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 256 | Vendors URL: %s |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 257 | Poll Interval: %s |
| 258 | Address Source: %s |
| 259 | Provision TTL: %s |
| 260 | Provision URL: %s |
| 261 | Role Selector URL: %s |
| 262 | Default Role: %s |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 263 | Script: %s |
| 264 | Log Level: %s |
| 265 | Log Format: %s`, |
David K. Bainbridge | 47996d6 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 266 | config.VendorsURL, config.PollInterval, config.AddressURL, config.ProvisionTTL, |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 267 | config.ProvisionURL, config.RoleSelectorURL, config.DefaultRole, config.Script, |
| 268 | config.LogLevel, config.LogFormat) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 269 | |
| 270 | // We use two methods to attempt to find the MAC (hardware) address associated with an IP. The first |
| 271 | // is to look in the table. The second is to send an ARP packet. |
| 272 | for { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 273 | log.Infof("Checking for switches @ %s", time.Now()) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 274 | addresses, err := config.addressSource.GetAddresses() |
| 275 | |
| 276 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 277 | log.Errorf("unable to read addresses from address source : %s", err) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 278 | } else { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 279 | log.Infof("Queried %d addresses from address source", len(addresses)) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 280 | |
| 281 | for _, rec := range addresses { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 282 | 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] | 283 | if err := config.processRecord(rec); err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 284 | log.Errorf("Error when processing IP '%s' : %s", rec.IP, err) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | time.Sleep(config.interval) |
| 290 | } |
| 291 | } |