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 | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 21 | "github.com/gorilla/mux" |
| 22 | maas "github.com/juju/gomaasapi" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 23 | "github.com/kelseyhightower/envconfig" |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 24 | "net/http" |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 25 | "sync" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 26 | "time" |
| 27 | ) |
| 28 | |
| 29 | type Config struct { |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 30 | VendorsURL string `default:"file:///switchq/vendors.json" envconfig:"vendors_url"` |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 31 | AddressURL string `default:"file:///switchq/dhcp_harvest.inc" envconfig:"address_url"` |
| 32 | PollInterval string `default:"1m" envconfig:"poll_interval"` |
| 33 | ProvisionTTL string `default:"1h" envconfig:"provision_ttl"` |
| 34 | ProvisionURL string `default:"" envconfig:"provision_url"` |
| 35 | RoleSelectorURL string `default:"" envconfig:"role_selector_url"` |
| 36 | DefaultRole string `default:"fabric-switch" envconfig:"default_role"` |
| 37 | Script string `default:"do-ansible"` |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 38 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL"` |
| 39 | LogFormat string `default:"text" envconfig:"LOG_FORMAT"` |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 40 | Listen string `default:""` |
| 41 | Port int `default:"4244"` |
| 42 | MaasURL string `default:"http://localhost/MAAS" envconfig:"MAAS_URL"` |
| 43 | MaasKey string `default:"" envconfig:"MAAS_API_KEY"` |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 44 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 45 | vendors Vendors |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 46 | addressSource AddressSource |
| 47 | interval time.Duration |
| 48 | ttl time.Duration |
| 49 | } |
| 50 | |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 51 | const ( |
| 52 | Pending TaskStatus = iota |
| 53 | Running |
| 54 | Complete |
| 55 | Failed |
| 56 | ) |
| 57 | |
| 58 | type RequestInfo struct { |
| 59 | Id string `json:"id"` |
| 60 | Name string `json:"name"` |
| 61 | Ip string `json:"ip"` |
| 62 | Mac string `json:"mac"` |
| 63 | RoleSelector string `json:"role_selector"` |
| 64 | Role string `json:"role"` |
| 65 | Script string `json:"script"` |
| 66 | } |
| 67 | |
| 68 | type TaskStatus uint8 |
| 69 | |
| 70 | type WorkRequest struct { |
| 71 | Info *RequestInfo |
| 72 | Script string |
| 73 | Role string |
| 74 | } |
| 75 | |
| 76 | type StatusMsg struct { |
| 77 | Request *WorkRequest `json:"request"` |
| 78 | Worker int `json:"worker"` |
| 79 | Status TaskStatus `json:"status"` |
| 80 | Message string `json:"message"` |
| 81 | Timestamp int64 `json:"timestamp"` |
| 82 | } |
| 83 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 84 | type AppContext struct { |
| 85 | config Config |
| 86 | |
| 87 | maasClient *maas.MAASObject |
| 88 | pushChan chan []AddressRec |
| 89 | mutex sync.RWMutex |
| 90 | nextList []AddressRec |
| 91 | publishList []AddressRec |
| 92 | } |
| 93 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 94 | func checkError(err error, msg string, args ...interface{}) { |
| 95 | if err != nil { |
| 96 | log.Fatalf(msg, args...) |
| 97 | } |
| 98 | } |
| 99 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 100 | func (c *AppContext) getProvisionedState(rec AddressRec) (*StatusMsg, error) { |
| 101 | if len(c.config.ProvisionURL) == 0 { |
| 102 | log.Warnf("Unable to fetch provisioning state of device '%s' (%s, %s) as no URL for the provisioner was specified", |
| 103 | rec.Name, rec.IP, rec.MAC) |
| 104 | return nil, fmt.Errorf("No URL for provisioner specified") |
| 105 | } |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 106 | log.Debugf("Fetching provisioned state of device '%s' (%s, %s)", |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 107 | rec.Name, rec.IP, rec.MAC) |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 108 | resp, err := http.Get(c.config.ProvisionURL + rec.MAC) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 109 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 110 | 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] | 111 | rec.Name, rec.IP, rec.MAC, err) |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 112 | return nil, err |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 113 | } |
| 114 | if resp.StatusCode != 404 && int(resp.StatusCode/100) != 2 { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 115 | 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] | 116 | rec.Name, rec.IP, rec.MAC, resp.Status) |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 117 | return nil, fmt.Errorf(resp.Status) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 118 | } |
| 119 | defer resp.Body.Close() |
| 120 | if resp.StatusCode != 404 { |
| 121 | decoder := json.NewDecoder(resp.Body) |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 122 | var status StatusMsg |
| 123 | err = decoder.Decode(&status) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 124 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 125 | 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] | 126 | rec.Name, rec.IP, rec.MAC, err) |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 127 | return nil, err |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 128 | } |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 129 | return &status, nil |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 130 | } |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 131 | |
| 132 | // If we end up here that means that no record was found in the provisioning, so return |
| 133 | // a status of -1, w/o an error |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 134 | return nil, nil |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 135 | } |
| 136 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 137 | func (c *AppContext) provision(rec AddressRec) error { |
| 138 | if len(c.config.ProvisionURL) == 0 { |
| 139 | log.Warnf("Unable to POST to provisioner for device '%s' (%s, %s) as no URL for the provisioner was specified", |
| 140 | rec.Name, rec.IP, rec.MAC) |
| 141 | return fmt.Errorf("No URL for provisioner specified") |
| 142 | } |
| 143 | log.Infof("POSTing to '%s' for provisioning of '%s (%s)'", c.config.ProvisionURL, rec.Name, rec.MAC) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 144 | data := map[string]string{ |
| 145 | "id": rec.MAC, |
| 146 | "name": rec.Name, |
| 147 | "ip": rec.IP, |
| 148 | "mac": rec.MAC, |
| 149 | } |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 150 | if c.config.RoleSelectorURL != "" { |
| 151 | data["role_selector"] = c.config.RoleSelectorURL |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 152 | } |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 153 | if c.config.DefaultRole != "" { |
| 154 | data["role"] = c.config.DefaultRole |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 155 | } |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 156 | if c.config.Script != "" { |
| 157 | data["script"] = c.config.Script |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 158 | } |
| 159 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 160 | hc := http.Client{} |
| 161 | var b []byte |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 162 | b, err := json.Marshal(data) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 163 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 164 | log.Errorf("Unable to marshal provisioning data : %s", err) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 165 | return err |
| 166 | } |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 167 | req, err := http.NewRequest("POST", c.config.ProvisionURL, bytes.NewReader(b)) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 168 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 169 | log.Errorf("Unable to construct POST request to provisioner : %s", err) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 170 | return err |
| 171 | } |
| 172 | |
| 173 | req.Header.Add("Content-Type", "application/json") |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 174 | resp, err := hc.Do(req) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 175 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 176 | log.Errorf("Unable to POST request to provisioner : %s", err) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 177 | return err |
| 178 | } |
| 179 | |
| 180 | defer resp.Body.Close() |
| 181 | if resp.StatusCode != http.StatusAccepted { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 182 | log.Errorf("Provisioning request not accepted by provisioner : %s", resp.Status) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 183 | return err |
| 184 | } |
| 185 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 186 | return nil |
| 187 | } |
| 188 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 189 | func (c *AppContext) processRecord(rec AddressRec) error { |
| 190 | ok, err := c.config.vendors.Switchq(rec.MAC) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 191 | if err != nil { |
| 192 | return fmt.Errorf("unable to determine ventor of MAC '%s' (%s)", rec.MAC, err) |
| 193 | } |
| 194 | |
| 195 | if !ok { |
| 196 | // Not something we care about |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 197 | 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] | 198 | rec.IP, rec.MAC, rec.Name) |
| 199 | return nil |
| 200 | } |
| 201 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 202 | // Add this IP information to our list of known switches |
| 203 | c.nextList = append(c.nextList, rec) |
| 204 | |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 205 | // Verify if the provision status of the node is complete, if in an error state then TTL means |
| 206 | // nothing |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 207 | state, err := c.getProvisionedState(rec) |
| 208 | if state != nil { |
| 209 | switch state.Status { |
| 210 | case Pending, Running: // Pending or Running |
| 211 | log.Debugf("device '%s' (%s, %s) is being provisioned", |
| 212 | rec.Name, rec.IP, rec.MAC) |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 213 | return nil |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 214 | case Complete: // Complete |
| 215 | log.Debugf("device '%s' (%s, %s) has completed provisioning", |
| 216 | rec.Name, rec.IP, rec.MAC) |
| 217 | case Failed: // Failed |
| 218 | log.Debugf("device '%s' (%s, %s) failed last provisioning with message '%s', reattempt", |
| 219 | rec.Name, rec.IP, rec.MAC, state.Message) |
David K. Bainbridge | 98bbc04 | 2016-08-22 17:35:28 -0700 | [diff] [blame] | 220 | state = nil |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 221 | default: // Unknown state |
| 222 | log.Debugf("device '%s' (%s, %s) has unknown provisioning state '%d', will provision", |
| 223 | rec.Name, rec.IP, rec.MAC, state.Status) |
David K. Bainbridge | 98bbc04 | 2016-08-22 17:35:28 -0700 | [diff] [blame] | 224 | state = nil |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 225 | } |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 226 | } else { |
| 227 | log.Debugf("device '%s' (%s, %s) has no provisioning record", |
| 228 | rec.Name, rec.IP, rec.MAC) |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 229 | } |
| 230 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 231 | // If TTL is 0 then we will only provision a switch once. |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 232 | if state == nil || (c.config.ttl > 0 && time.Since(time.Unix(state.Timestamp, 0)) > c.config.ttl) { |
David K. Bainbridge | 52f2954 | 2016-07-27 22:28:15 -0700 | [diff] [blame] | 233 | if state != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 234 | log.Debugf("device '%s' (%s, %s) TTL expired, reprovisioning", |
David K. Bainbridge | c809ef7 | 2016-06-22 21:18:07 -0700 | [diff] [blame] | 235 | rec.Name, rec.IP, rec.MAC) |
| 236 | } |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 237 | c.provision(rec) |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 238 | } else if c.config.ttl == 0 { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 239 | log.Debugf("device '%s' (%s, %s) has completed its one time provisioning, with a TTL set to %s", |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 240 | rec.Name, rec.IP, rec.MAC, c.config.ProvisionTTL) |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 241 | } else { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 242 | log.Debugf("device '%s' (%s, %s) has completed provisioning within the specified TTL of %s", |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 243 | rec.Name, rec.IP, rec.MAC, c.config.ProvisionTTL) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 244 | } |
| 245 | return nil |
| 246 | } |
| 247 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 248 | func (c *AppContext) processLoop() { |
| 249 | // We use two methods to attempt to find the MAC (hardware) address associated with an IP. The first |
| 250 | // is to look in the table. The second is to send an ARP packet. |
| 251 | for { |
| 252 | log.Infof("Checking for switches @ %s", time.Now()) |
| 253 | addresses, err := c.config.addressSource.GetAddresses() |
| 254 | |
| 255 | if err != nil { |
| 256 | log.Errorf("unable to read addresses from address source : %s", err) |
| 257 | } else { |
| 258 | log.Infof("Queried %d addresses from address source", len(addresses)) |
| 259 | |
| 260 | c.nextList = make([]AddressRec, 0, len(addresses)) |
| 261 | for _, rec := range addresses { |
| 262 | log.Debugf("Processing %s(%s, %s)", rec.Name, rec.IP, rec.MAC) |
| 263 | if err := c.processRecord(rec); err != nil { |
| 264 | log.Errorf("Error when processing IP '%s' : %s", rec.IP, err) |
| 265 | } |
| 266 | } |
| 267 | c.mutex.Lock() |
| 268 | c.publishList = c.nextList |
| 269 | c.nextList = nil |
| 270 | c.mutex.Unlock() |
| 271 | c.pushChan <- c.publishList |
| 272 | } |
| 273 | |
| 274 | time.Sleep(c.config.interval) |
| 275 | } |
| 276 | } |
| 277 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 278 | var log = logrus.New() |
| 279 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 280 | func main() { |
| 281 | |
| 282 | var err error |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 283 | context := &AppContext{} |
| 284 | err = envconfig.Process("SWITCHQ", &context.config) |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 285 | if err != nil { |
| 286 | log.Fatalf("Unable to parse configuration options : %s", err) |
| 287 | } |
| 288 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 289 | switch context.config.LogFormat { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 290 | case "json": |
| 291 | log.Formatter = &logrus.JSONFormatter{} |
| 292 | default: |
| 293 | log.Formatter = &logrus.TextFormatter{ |
| 294 | FullTimestamp: true, |
| 295 | ForceColors: true, |
| 296 | } |
| 297 | } |
| 298 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 299 | level, err := logrus.ParseLevel(context.config.LogLevel) |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 300 | if err != nil { |
| 301 | level = logrus.WarnLevel |
| 302 | } |
| 303 | log.Level = level |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 304 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 305 | log.Infof(`Configuration: |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 306 | Vendors URL: %s |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 307 | Poll Interval: %s |
| 308 | Address Source: %s |
| 309 | Provision TTL: %s |
| 310 | Provision URL: %s |
| 311 | Role Selector URL: %s |
| 312 | Default Role: %s |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 313 | Script: %s |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 314 | API Listen IP: %s |
| 315 | API Listen Port: %d |
| 316 | MAAS URL: %s |
| 317 | MAAS APIKEY: %s |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 318 | Log Level: %s |
| 319 | Log Format: %s`, |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 320 | context.config.VendorsURL, context.config.PollInterval, context.config.AddressURL, context.config.ProvisionTTL, |
| 321 | context.config.ProvisionURL, context.config.RoleSelectorURL, context.config.DefaultRole, context.config.Script, |
| 322 | context.config.Listen, context.config.Port, context.config.MaasURL, context.config.MaasKey, |
| 323 | context.config.LogLevel, context.config.LogFormat) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 324 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 325 | context.config.vendors, err = NewVendors(context.config.VendorsURL) |
| 326 | checkError(err, "Unable to create known vendors list from specified URL '%s' : %s", context.config.VendorsURL, err) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 327 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 328 | context.config.addressSource, err = NewAddressSource(context.config.AddressURL) |
| 329 | checkError(err, "Unable to create required address source for specified URL '%s' : %s", context.config.AddressURL, err) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 330 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 331 | context.config.interval, err = time.ParseDuration(context.config.PollInterval) |
| 332 | checkError(err, "Unable to parse specified poll interface '%s' : %s", context.config.PollInterval, err) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 333 | |
David K. Bainbridge | 3569d62 | 2016-09-16 08:40:54 -0700 | [diff] [blame^] | 334 | context.config.ttl, err = time.ParseDuration(context.config.ProvisionTTL) |
| 335 | checkError(err, "Unable to parse specified provision TTL value of '%s' : %s", context.config.ProvisionTTL, err) |
| 336 | |
| 337 | if len(context.config.MaasURL) > 0 { |
| 338 | |
| 339 | // Attempt to connect to MAAS |
| 340 | authClient, err := maas.NewAuthenticatedClient(context.config.MaasURL, context.config.MaasKey, "1.0") |
| 341 | checkError(err, "Unable to connect to MAAS at '%s' : %s", context.config.MaasURL, err) |
| 342 | |
| 343 | context.maasClient = maas.NewMAAS(*authClient) |
| 344 | } |
| 345 | |
| 346 | context.pushChan = make(chan []AddressRec, 1) |
| 347 | |
| 348 | go context.processLoop() |
| 349 | go context.syncToMaas(context.pushChan) |
| 350 | |
| 351 | router := mux.NewRouter() |
| 352 | router.HandleFunc("/switch/", context.ListSwitchesHandler).Methods("GET") |
| 353 | http.Handle("/", router) |
| 354 | log.Infof("Listening for HTTP request on '%s:%d'", context.config.Listen, context.config.Port) |
| 355 | err = http.ListenAndServe(fmt.Sprintf("%s:%d", context.config.Listen, context.config.Port), nil) |
| 356 | if err != nil { |
| 357 | checkError(err, "Error while attempting to listen to REST requests on '%s:%d' : %s", |
| 358 | context.config.Listen, context.config.Port, err) |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 359 | } |
| 360 | } |