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 | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 18 | "github.com/Sirupsen/logrus" |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 19 | "github.com/gorilla/mux" |
| 20 | "github.com/kelseyhightower/envconfig" |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 21 | "net/http" |
| 22 | ) |
| 23 | |
| 24 | type Config struct { |
| 25 | Port int `default:"4243"` |
| 26 | Listen string `default:"0.0.0.0"` |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame^] | 27 | RoleSelectorURL string `default:"" envconfig:"ROLE_SELECTOR_URL"` |
| 28 | DefaultRole string `default:"compute-node" envconfig:"DEFAULT_ROLE"` |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 29 | Script string `default:"do-ansible"` |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame^] | 30 | StorageURL string `default:"memory:" envconfig:"STORAGE_URL"` |
| 31 | NumberOfWorkers int `default:"5" envconfig:"NUMBER_OF_WORKERS"` |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 32 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL"` |
| 33 | LogFormat string `default:"text" envconfig:"LOG_FORMAT"` |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | type Context struct { |
| 37 | config Config |
| 38 | storage Storage |
| 39 | workers []Worker |
| 40 | dispatcher *Dispatcher |
| 41 | } |
| 42 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 43 | var log = logrus.New() |
| 44 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 45 | func main() { |
| 46 | context := &Context{} |
| 47 | |
| 48 | err := envconfig.Process("PROVISION", &(context.config)) |
| 49 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 50 | log.Fatalf("[ERRO] Unable to parse configuration options : %s", err) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 51 | } |
| 52 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 53 | switch context.config.LogFormat { |
| 54 | case "json": |
| 55 | log.Formatter = &logrus.JSONFormatter{} |
| 56 | default: |
| 57 | log.Formatter = &logrus.TextFormatter{ |
| 58 | FullTimestamp: true, |
| 59 | ForceColors: true, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | level, err := logrus.ParseLevel(context.config.LogLevel) |
| 64 | if err != nil { |
| 65 | level = logrus.WarnLevel |
| 66 | } |
| 67 | log.Level = level |
| 68 | |
| 69 | log.Infof(`Configuration: |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame^] | 70 | LISTEN: %s |
| 71 | PORT: %d |
| 72 | ROLE_SELECTION_URL: %s |
| 73 | DEFAULT_ROLE: %s |
| 74 | SCRIPT: %s |
| 75 | STORAGE_URL: %s |
| 76 | NUMBER_OF_WORERS: %d |
| 77 | LOG_LEVEL: %s |
| 78 | LOG_FORMAT: %s`, |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 79 | context.config.Listen, context.config.Port, context.config.RoleSelectorURL, |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 80 | context.config.DefaultRole, context.config.Script, context.config.StorageURL, |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame^] | 81 | context.config.NumberOfWorkers, |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 82 | context.config.LogLevel, context.config.LogFormat) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 83 | |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 84 | context.storage, err = NewStorage(context.config.StorageURL) |
| 85 | if err != nil { |
| 86 | log.Fatalf("[error] Unable to connect to specified storage '%s' : %s", |
| 87 | context.config.StorageURL, err) |
| 88 | } |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 89 | |
| 90 | router := mux.NewRouter() |
| 91 | router.HandleFunc("/provision/", context.ProvisionRequestHandler).Methods("POST") |
| 92 | router.HandleFunc("/provision/", context.ListRequestsHandler).Methods("GET") |
| 93 | router.HandleFunc("/provision/{nodeid}", context.QueryStatusHandler).Methods("GET") |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 94 | router.HandleFunc("/provision/{nodeid}", context.DeleteStatusHandler).Methods("DELETE") |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 95 | http.Handle("/", router) |
| 96 | |
| 97 | // Start the dispatcher and workers |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame^] | 98 | context.dispatcher = NewDispatcher(context.config.NumberOfWorkers, context.storage) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 99 | context.dispatcher.Start() |
| 100 | |
| 101 | http.ListenAndServe(fmt.Sprintf("%s:%d", context.config.Listen, context.config.Port), nil) |
| 102 | } |