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