blob: 6c02c14dd15011340c6419c5cc5087bff0d7193c [file] [log] [blame]
David K. Bainbridgef0da8732016-06-01 16:15:37 -07001package main
2
3import (
4 "fmt"
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -07005 "github.com/Sirupsen/logrus"
David K. Bainbridgef0da8732016-06-01 16:15:37 -07006 "github.com/gorilla/mux"
7 "github.com/kelseyhightower/envconfig"
David K. Bainbridgef0da8732016-06-01 16:15:37 -07008 "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"`
David K. Bainbridge546cdc32016-06-29 15:30:22 -070017 StorageURL string `default:"memory:" envconfig:"storage_url"`
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070018 LogLevel string `default:"warning" envconfig:"LOG_LEVEL"`
19 LogFormat string `default:"text" envconfig:"LOG_FORMAT"`
David K. Bainbridgef0da8732016-06-01 16:15:37 -070020}
21
22type Context struct {
23 config Config
24 storage Storage
25 workers []Worker
26 dispatcher *Dispatcher
27}
28
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070029var log = logrus.New()
30
David K. Bainbridgef0da8732016-06-01 16:15:37 -070031func main() {
32 context := &Context{}
33
34 err := envconfig.Process("PROVISION", &(context.config))
35 if err != nil {
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070036 log.Fatalf("[ERRO] Unable to parse configuration options : %s", err)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070037 }
38
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070039 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. Bainbridgef0da8732016-06-01 16:15:37 -070056 Listen: %s
57 Port: %d
58 RoleSelectorURL: %s
David K. Bainbridged86d96d2016-06-01 17:28:46 -070059 DefaultRole: %s
David K. Bainbridge546cdc32016-06-29 15:30:22 -070060 Script: %s
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070061 StorageURL: %s
62 Log Level: %s
63 Log Format: %s`,
David K. Bainbridged86d96d2016-06-01 17:28:46 -070064 context.config.Listen, context.config.Port, context.config.RoleSelectorURL,
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070065 context.config.DefaultRole, context.config.Script, context.config.StorageURL,
66 context.config.LogLevel, context.config.LogFormat)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070067
David K. Bainbridge546cdc32016-06-29 15:30:22 -070068 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. Bainbridgef0da8732016-06-01 16:15:37 -070073
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. Bainbridge068e87d2016-06-30 13:53:19 -070078 router.HandleFunc("/provision/{nodeid}", context.DeleteStatusHandler).Methods("DELETE")
David K. Bainbridgef0da8732016-06-01 16:15:37 -070079 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}