David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 1 | // 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. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 18 | "github.com/Sirupsen/logrus" |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 19 | "github.com/gorilla/mux" |
| 20 | "github.com/kelseyhightower/envconfig" |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 21 | "net/http" |
| 22 | ) |
| 23 | |
| 24 | type 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. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 30 | StorageURL string `default:"memory:" envconfig:"storage_url"` |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 31 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL"` |
| 32 | LogFormat string `default:"text" envconfig:"LOG_FORMAT"` |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type Context struct { |
| 36 | config Config |
| 37 | storage Storage |
| 38 | workers []Worker |
| 39 | dispatcher *Dispatcher |
| 40 | } |
| 41 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 42 | var log = logrus.New() |
| 43 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 44 | func main() { |
| 45 | context := &Context{} |
| 46 | |
| 47 | err := envconfig.Process("PROVISION", &(context.config)) |
| 48 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 49 | log.Fatalf("[ERRO] Unable to parse configuration options : %s", err) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 50 | } |
| 51 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 52 | 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. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 69 | Listen: %s |
| 70 | Port: %d |
| 71 | RoleSelectorURL: %s |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 72 | DefaultRole: %s |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 73 | Script: %s |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 74 | StorageURL: %s |
| 75 | Log Level: %s |
| 76 | Log Format: %s`, |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 77 | context.config.Listen, context.config.Port, context.config.RoleSelectorURL, |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 78 | context.config.DefaultRole, context.config.Script, context.config.StorageURL, |
| 79 | context.config.LogLevel, context.config.LogFormat) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 80 | |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 81 | 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. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 86 | |
| 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. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 91 | router.HandleFunc("/provision/{nodeid}", context.DeleteStatusHandler).Methods("DELETE") |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 92 | 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 | } |