blob: 362c06d153fcbc84585494254afb341679b22a7e [file] [log] [blame]
David K. Bainbridgef0da8732016-06-01 16:15:37 -07001package main
2
3import (
4 "fmt"
5 "github.com/gorilla/mux"
6 "github.com/kelseyhightower/envconfig"
7 "log"
8 "net/http"
9)
10
11type 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
19type Context struct {
20 config Config
21 storage Storage
22 workers []Worker
23 dispatcher *Dispatcher
24}
25
26func 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. Bainbridged86d96d2016-06-01 17:28:46 -070038 DefaultRole: %s
39 Script: %s`,
40 context.config.Listen, context.config.Port, context.config.RoleSelectorURL,
41 context.config.DefaultRole, context.config.Script)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070042
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}