blob: 73d0d1a6d1508c76bd266210d36140eeefe31514 [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"`
David K. Bainbridgeca68f062016-10-27 11:04:33 -070027 RoleSelectorURL string `default:"" envconfig:"ROLE_SELECTOR_URL"`
28 DefaultRole string `default:"compute-node" envconfig:"DEFAULT_ROLE"`
David K. Bainbridgef0da8732016-06-01 16:15:37 -070029 Script string `default:"do-ansible"`
David K. Bainbridgeca68f062016-10-27 11:04:33 -070030 StorageURL string `default:"memory:" envconfig:"STORAGE_URL"`
31 NumberOfWorkers int `default:"5" envconfig:"NUMBER_OF_WORKERS"`
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070032 LogLevel string `default:"warning" envconfig:"LOG_LEVEL"`
33 LogFormat string `default:"text" envconfig:"LOG_FORMAT"`
David K. Bainbridgef0da8732016-06-01 16:15:37 -070034}
35
36type Context struct {
37 config Config
38 storage Storage
39 workers []Worker
40 dispatcher *Dispatcher
41}
42
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070043var log = logrus.New()
44
David K. Bainbridgef0da8732016-06-01 16:15:37 -070045func main() {
46 context := &Context{}
47
48 err := envconfig.Process("PROVISION", &(context.config))
49 if err != nil {
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070050 log.Fatalf("[ERRO] Unable to parse configuration options : %s", err)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070051 }
52
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070053 switch context.config.LogFormat {
54 case "json":
55 log.Formatter = &logrus.JSONFormatter{}
56 default:
57 log.Formatter = &logrus.TextFormatter{
58 FullTimestamp: true,
59 ForceColors: true,
60 }
61 }
62
63 level, err := logrus.ParseLevel(context.config.LogLevel)
64 if err != nil {
65 level = logrus.WarnLevel
66 }
67 log.Level = level
68
69 log.Infof(`Configuration:
David K. Bainbridgeca68f062016-10-27 11:04:33 -070070 LISTEN: %s
71 PORT: %d
72 ROLE_SELECTION_URL: %s
73 DEFAULT_ROLE: %s
74 SCRIPT: %s
75 STORAGE_URL: %s
76 NUMBER_OF_WORERS: %d
77 LOG_LEVEL: %s
78 LOG_FORMAT: %s`,
David K. Bainbridged86d96d2016-06-01 17:28:46 -070079 context.config.Listen, context.config.Port, context.config.RoleSelectorURL,
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070080 context.config.DefaultRole, context.config.Script, context.config.StorageURL,
David K. Bainbridgeca68f062016-10-27 11:04:33 -070081 context.config.NumberOfWorkers,
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070082 context.config.LogLevel, context.config.LogFormat)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070083
David K. Bainbridge546cdc32016-06-29 15:30:22 -070084 context.storage, err = NewStorage(context.config.StorageURL)
85 if err != nil {
86 log.Fatalf("[error] Unable to connect to specified storage '%s' : %s",
87 context.config.StorageURL, err)
88 }
David K. Bainbridgef0da8732016-06-01 16:15:37 -070089
90 router := mux.NewRouter()
91 router.HandleFunc("/provision/", context.ProvisionRequestHandler).Methods("POST")
92 router.HandleFunc("/provision/", context.ListRequestsHandler).Methods("GET")
93 router.HandleFunc("/provision/{nodeid}", context.QueryStatusHandler).Methods("GET")
David K. Bainbridge068e87d2016-06-30 13:53:19 -070094 router.HandleFunc("/provision/{nodeid}", context.DeleteStatusHandler).Methods("DELETE")
David K. Bainbridgef0da8732016-06-01 16:15:37 -070095 http.Handle("/", router)
96
97 // Start the dispatcher and workers
David K. Bainbridgeca68f062016-10-27 11:04:33 -070098 context.dispatcher = NewDispatcher(context.config.NumberOfWorkers, context.storage)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070099 context.dispatcher.Start()
100
101 http.ListenAndServe(fmt.Sprintf("%s:%d", context.config.Listen, context.config.Port), nil)
102}