-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
160 lines (138 loc) · 5.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
//go:generate bin/go-bindata -nometadata -prefix assets -o assets.go assets/... README.md
//go:generate bin/go-bindata -nometadata -debug -pkg dassets -prefix assets -o dassets/assets.go assets/... README.md
//go:generate ./version.sh
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/Mayo-QIN/grunt/dassets"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/mux"
yaml "gopkg.in/yaml.v2"
)
type SMTP struct {
From string
Username string
Password string
Server string
Port int
}
type Config struct {
Services []*Service `json:"services"`
SlicerServices []*SlicerService `yaml:"cli" json:"-"`
ServiceMap map[string]*Service `json:"-"`
Mail SMTP `json:"mail"`
Name string `json:"name"`
Server string `json:"server"`
Directory string `json:"working_directory"`
ConfigDirectory string `json:"config_directory" yaml:"configDirectory"`
WarnLevel int `json:"warn_level" yaml:"warnLevel"`
CriticalLevel int `json:"critical_level" yaml:"criticalLevel"`
CleanupTimeInMinutes int `json:"cleanup_time_in_minutes" yaml:"cleanupTimeInMinutes"`
}
var config Config
var consulHost string
var consulPort int
var advertisedHost string = ""
var advertisedPort int = 9901
var debug bool
func main() {
log.Printf("Starting grunt")
log.Printf("\tVersion: %v", Version)
log.Printf("\tFull: %v", FullVersion)
log.Printf("\tBuild Date: %v", BuildTimestamp)
log.Printf("\tHash: %v", Hash)
var port int
flag.IntVar(&port, "p", 9901, "specify port to use. defaults to 9901.")
flag.StringVar(&consulHost, "consul", "", "specify Consul host. defaults to none. Also set by CONSUL_HOST or CONSULT_PORT_8500_TCP_ADDR environment variable")
flag.IntVar(&consulPort, "consul-port", 0, "specify Consul port to use. defaults to 0. Also set through the CONSULT_HOST or CONSUL_PORT_8500_TCP_PORT environment variable set by Docker")
flag.StringVar(&advertisedHost, "advertised", "", "specify Advertised host. defaults to none. Also set through the ADVERTISED_HOST environment variable.")
flag.IntVar(&advertisedPort, "advertised-port", 0, "specify Advertised port to use. defaults to 0. Also set through the ADVERTISED_PORT environment variable.")
flag.BoolVar(&debug, "d", false, "Debug")
// Set config defaults
config.ServiceMap = make(map[string]*Service)
config.WarnLevel = 3
config.CriticalLevel = 5
config.Mail.Port = 25
config.CleanupTimeInMinutes = 120
flag.Parse()
if len(flag.Args()) < 1 {
log.Fatal("Usage: grunt gruntfile.yml")
}
gruntfile := flag.Arg(0)
data, err := ioutil.ReadFile(gruntfile)
if err != nil {
log.Fatalf("Error reading %v: %v", gruntfile, err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Fatalf("Error in YML parsing: %v", err)
}
for _, ss := range config.SlicerServices {
s, err := CreateService(ss.Executable)
if err != nil {
log.Fatalf("Error constructing Slicer CLI: %v", err)
}
s.EndPoint = ss.EndPoint
config.Services = append(config.Services, s)
}
// Read all the files in the config directory
if config.ConfigDirectory != "" {
log.Printf("load configurations from %v", config.ConfigDirectory)
loadServices(config.ConfigDirectory)
}
// Start up all the services
for _, service := range config.Services {
service.setup()
config.ServiceMap[service.EndPoint] = service
log.Printf("\tservice available: %v\n", service.EndPoint)
}
if config.Mail.Port == 0 {
config.Mail.Port = 25
}
if config.Mail.From == "" {
config.Mail.From = "noreply@example.com"
}
if config.Directory == "" {
config.Directory, err = ioutil.TempDir("", "grunt")
if err != nil {
log.Fatalf("Failed to make working directory(%v): %v", config.Directory, err.Error())
}
}
err = os.MkdirAll(config.Directory, 0755)
if err != nil {
log.Fatalf("Failed to make working directory(%v): %v", config.Directory, err.Error())
}
// Expose the endpoints
r := mux.NewRouter()
r.HandleFunc("/rest/service", GetServices).Methods("GET")
r.HandleFunc("/rest/service/{id}", GetService).Methods("GET")
r.HandleFunc("/rest/service/{id}", StartService).Methods("POST")
r.HandleFunc("/rest/job/{id}", GetJob).Methods("GET")
r.HandleFunc("/rest/job/wait/{id}", WaitForJob).Methods("GET")
r.HandleFunc("/rest/job/{id}/file/{filename}", GetJobFile).Methods("GET")
r.HandleFunc("/rest/job/{id}/zip", GetJobZip).Methods("GET")
r.HandleFunc("/job/{id}", JobDetail).Methods("GET")
r.HandleFunc("/service/{id}", ServiceDetail).Methods("GET")
r.HandleFunc("/health", GetHealth).Methods("GET")
r.HandleFunc("/{name}", ExecTemplate).Methods("GET")
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/grunt.html", http.StatusFound)
})
if debug {
r.PathPrefix("/static").Handler(http.FileServer(&assetfs.AssetFS{Asset: dassets.Asset, AssetDir: dassets.AssetDir, AssetInfo: dassets.AssetInfo}))
} else {
r.PathPrefix("/static").Handler(http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo}))
}
http.Handle("/", r)
// Register the main grunt services
c := ConfigD{Name: config.Name, Services: config.Services}
registerConfigWithConsul(&c)
log.Printf("Finished jobs will cleanup %d minutes after completion", config.CleanupTimeInMinutes)
log.Printf("Starting grunt on http://localhost:%v", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}