blob: 590470b690063165a73d1e3113ab43efc7dc7707 [file] [log] [blame]
David K. Bainbridgedf9df632016-07-07 18:47:46 -07001// Copyright 2016 Open Networking Laboratory
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
David K. Bainbridgef0da8732016-06-01 16:15:37 -070014package main
15
16import (
17 "fmt"
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070018 "github.com/Sirupsen/logrus"
David K. Bainbridgef0da8732016-06-01 16:15:37 -070019 "github.com/gorilla/mux"
20 "github.com/kelseyhightower/envconfig"
David K. Bainbridgef0da8732016-06-01 16:15:37 -070021 "net/http"
22)
23
24type Config struct {
25 Port int `default:"4243"`
26 Listen string `default:"0.0.0.0"`
27 RoleSelectorURL string `default:"" envconfig:"role_selector_url"`
28 DefaultRole string `default:"compute-node" envconfig:"default_role"`
29 Script string `default:"do-ansible"`
David K. Bainbridge546cdc32016-06-29 15:30:22 -070030 StorageURL string `default:"memory:" envconfig:"storage_url"`
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070031 LogLevel string `default:"warning" envconfig:"LOG_LEVEL"`
32 LogFormat string `default:"text" envconfig:"LOG_FORMAT"`
David K. Bainbridgef0da8732016-06-01 16:15:37 -070033}
34
35type Context struct {
36 config Config
37 storage Storage
38 workers []Worker
39 dispatcher *Dispatcher
40}
41
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070042var log = logrus.New()
43
David K. Bainbridgef0da8732016-06-01 16:15:37 -070044func main() {
45 context := &Context{}
46
47 err := envconfig.Process("PROVISION", &(context.config))
48 if err != nil {
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070049 log.Fatalf("[ERRO] Unable to parse configuration options : %s", err)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070050 }
51
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070052 switch context.config.LogFormat {
53 case "json":
54 log.Formatter = &logrus.JSONFormatter{}
55 default:
56 log.Formatter = &logrus.TextFormatter{
57 FullTimestamp: true,
58 ForceColors: true,
59 }
60 }
61
62 level, err := logrus.ParseLevel(context.config.LogLevel)
63 if err != nil {
64 level = logrus.WarnLevel
65 }
66 log.Level = level
67
68 log.Infof(`Configuration:
David K. Bainbridgef0da8732016-06-01 16:15:37 -070069 Listen: %s
70 Port: %d
71 RoleSelectorURL: %s
David K. Bainbridged86d96d2016-06-01 17:28:46 -070072 DefaultRole: %s
David K. Bainbridge546cdc32016-06-29 15:30:22 -070073 Script: %s
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070074 StorageURL: %s
75 Log Level: %s
76 Log Format: %s`,
David K. Bainbridged86d96d2016-06-01 17:28:46 -070077 context.config.Listen, context.config.Port, context.config.RoleSelectorURL,
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070078 context.config.DefaultRole, context.config.Script, context.config.StorageURL,
79 context.config.LogLevel, context.config.LogFormat)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070080
David K. Bainbridge546cdc32016-06-29 15:30:22 -070081 context.storage, err = NewStorage(context.config.StorageURL)
82 if err != nil {
83 log.Fatalf("[error] Unable to connect to specified storage '%s' : %s",
84 context.config.StorageURL, err)
85 }
David K. Bainbridgef0da8732016-06-01 16:15:37 -070086
87 router := mux.NewRouter()
88 router.HandleFunc("/provision/", context.ProvisionRequestHandler).Methods("POST")
89 router.HandleFunc("/provision/", context.ListRequestsHandler).Methods("GET")
90 router.HandleFunc("/provision/{nodeid}", context.QueryStatusHandler).Methods("GET")
David K. Bainbridge068e87d2016-06-30 13:53:19 -070091 router.HandleFunc("/provision/{nodeid}", context.DeleteStatusHandler).Methods("DELETE")
David K. Bainbridgef0da8732016-06-01 16:15:37 -070092 http.Handle("/", router)
93
94 // Start the dispatcher and workers
95 context.dispatcher = NewDispatcher(5, context.storage)
96 context.dispatcher.Start()
97
98 http.ListenAndServe(fmt.Sprintf("%s:%d", context.config.Listen, context.config.Port), nil)
99}