blob: 559e23cae2fecfd124e06cd51c22fee682339949 [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"`
David K. Bainbridge546cdc32016-06-29 15:30:22 -070017 StorageURL string `default:"memory:" envconfig:"storage_url"`
David K. Bainbridgef0da8732016-06-01 16:15:37 -070018}
19
20type Context struct {
21 config Config
22 storage Storage
23 workers []Worker
24 dispatcher *Dispatcher
25}
26
27func 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. Bainbridged86d96d2016-06-01 17:28:46 -070039 DefaultRole: %s
David K. Bainbridge546cdc32016-06-29 15:30:22 -070040 Script: %s
41 StorageURL: %s`,
David K. Bainbridged86d96d2016-06-01 17:28:46 -070042 context.config.Listen, context.config.Port, context.config.RoleSelectorURL,
David K. Bainbridge546cdc32016-06-29 15:30:22 -070043 context.config.DefaultRole, context.config.Script, context.config.StorageURL)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070044
David K. Bainbridge546cdc32016-06-29 15:30:22 -070045 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. Bainbridgef0da8732016-06-01 16:15:37 -070050
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}