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