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