David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/gorilla/mux" |
| 6 | "github.com/kelseyhightower/envconfig" |
| 7 | "log" |
| 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"` |
| 17 | } |
| 18 | |
| 19 | type Context struct { |
| 20 | config Config |
| 21 | storage Storage |
| 22 | workers []Worker |
| 23 | dispatcher *Dispatcher |
| 24 | } |
| 25 | |
| 26 | func main() { |
| 27 | context := &Context{} |
| 28 | |
| 29 | err := envconfig.Process("PROVISION", &(context.config)) |
| 30 | if err != nil { |
| 31 | log.Fatalf("[error] Unable to parse configuration options : %s", err) |
| 32 | } |
| 33 | |
| 34 | log.Printf(`Configuration: |
| 35 | Listen: %s |
| 36 | Port: %d |
| 37 | RoleSelectorURL: %s |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 38 | DefaultRole: %s |
| 39 | Script: %s`, |
| 40 | context.config.Listen, context.config.Port, context.config.RoleSelectorURL, |
| 41 | context.config.DefaultRole, context.config.Script) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 42 | |
| 43 | context.storage = NewMemoryStorage() |
| 44 | |
| 45 | router := mux.NewRouter() |
| 46 | router.HandleFunc("/provision/", context.ProvisionRequestHandler).Methods("POST") |
| 47 | router.HandleFunc("/provision/", context.ListRequestsHandler).Methods("GET") |
| 48 | router.HandleFunc("/provision/{nodeid}", context.QueryStatusHandler).Methods("GET") |
| 49 | http.Handle("/", router) |
| 50 | |
| 51 | // Start the dispatcher and workers |
| 52 | context.dispatcher = NewDispatcher(5, context.storage) |
| 53 | context.dispatcher.Start() |
| 54 | |
| 55 | http.ListenAndServe(fmt.Sprintf("%s:%d", context.config.Listen, context.config.Port), nil) |
| 56 | } |