blob: aeb237ac4b631e0ca6cc7bbf078fc15433e82d2c [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
38 DefaultRole: %s`,
39 context.config.Listen, context.config.Port, context.config.RoleSelectorURL, context.config.DefaultRole)
40
41 context.storage = NewMemoryStorage()
42
43 router := mux.NewRouter()
44 router.HandleFunc("/provision/", context.ProvisionRequestHandler).Methods("POST")
45 router.HandleFunc("/provision/", context.ListRequestsHandler).Methods("GET")
46 router.HandleFunc("/provision/{nodeid}", context.QueryStatusHandler).Methods("GET")
47 http.Handle("/", router)
48
49 // Start the dispatcher and workers
50 context.dispatcher = NewDispatcher(5, context.storage)
51 context.dispatcher.Start()
52
53 http.ListenAndServe(fmt.Sprintf("%s:%d", context.config.Listen, context.config.Port), nil)
54}