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