/
main.go
208 lines (170 loc) · 7.16 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright (C) 2003-2011 Institute for Systems Biology
Seattle, Washington, USA.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"flag"
"github.com/codeforsystemsbiology/rest.go"
"github.com/codeforsystemsbiology/verboselogger.go"
"github.com/dlintw/goconf"
"labix.org/v1/mgo"
"net/http"
"net/url"
"os"
)
var logger *log4go.VerboseLogger
//parse args and start as master, scribe, addama proxy or worker
func main() {
var configurationFile string
var isMaster bool
var isScribe bool
var isAddama bool
flag.BoolVar(&isMaster, "m", false, "Start as master node.")
flag.BoolVar(&isScribe, "s", false, "Start as scribe node.")
flag.BoolVar(&isAddama, "a", false, "Start as addama node.")
flag.StringVar(&configurationFile, "config", "golem.config", "A configuration file for golem services")
flag.Parse()
configFile, err := goconf.ReadConfigFile(configurationFile)
if err != nil {
panic(err)
}
GlobalLogger(configFile)
GlobalTls(configFile)
SubIOBufferSize("default", configFile)
GoMaxProc("default", configFile)
ConBufferSize("default", configFile)
StartHtmlHandler(configFile)
if isMaster {
StartMaster(configFile)
} else if isScribe {
StartScribe(configFile)
} else if isAddama {
StartAddama(configFile)
} else {
StartWorker(configFile)
}
}
// sets global logger based on verbosity level in configuration
// optional parameter: default.verbose (defaults to true if not present or incorrectly set)
func GlobalLogger(configFile *goconf.ConfigFile) {
verbose, err := configFile.GetBool("default", "verbose")
logger = log4go.NewVerboseLogger(verbose, nil, "")
if err != nil {
logger.Warn(err)
verbose = true
}
logger.Printf("verbose set [%v]", verbose)
}
// starts master service based on the given configuration file
// required parameters: default.hostname, default.password
// optional parameters: master.buffersize
func StartMaster(configFile *goconf.ConfigFile) {
SubIOBufferSize("master", configFile)
GoMaxProc("master", configFile)
ConBufferSize("master", configFile)
IOMOnitors(configFile)
hostname := GetRequiredString(configFile, "default", "hostname")
password := GetRequiredString(configFile, "default", "password")
m := NewMaster()
rest.Resource("jobs", MasterJobController{m, password})
rest.Resource("nodes", MasterNodeController{m, password})
rest.ResourceContentType("jobs", "application/json")
rest.ResourceContentType("nodes", "application/json")
ListenAndServeTLSorNot(hostname)
}
// starts scribe service based on the given configuration file
// required parameters: default.hostname, default.password, scribe.target, mgodb.server, mgodb.store, mgodb.jobcollection, mgodb.taskcollection
func StartScribe(configFile *goconf.ConfigFile) {
MongoLogger(configFile)
GoMaxProc("scribe", configFile)
hostname := GetRequiredString(configFile, "default", "hostname")
apikey := GetRequiredString(configFile, "default", "password")
target := GetRequiredString(configFile, "scribe", "target")
dbhost := GetRequiredString(configFile, "mgodb", "server")
dbstore := GetRequiredString(configFile, "mgodb", "store")
url, err := url.Parse(target)
if err != nil {
panic(err)
}
go LaunchScribe(NewMongoJobStore(dbhost, dbstore), target, apikey)
rest.Resource("jobs", ScribeJobController{NewMongoJobStore(dbhost, dbstore), url, apikey})
rest.ResourceContentType("jobs", "application/json")
rest.Resource("nodes", ProxyNodeController{url, apikey})
rest.ResourceContentType("nodes", "application/json")
rest.Resource("cluster", ScribeClusterController{NewMongoJobStore(dbhost, dbstore), url})
rest.ResourceContentType("cluster", "application/json")
var numberOfSeconds int = 10
if pollingSecs, oops := configFile.GetInt("scribe", "clusterStatsPollingInSeconds"); oops == nil {
numberOfSeconds = pollingSecs
}
logger.Printf("polling for cluster stats every %d secs", numberOfSeconds)
go MonitorClusterStats(NewMongoJobStore(dbhost, dbstore), target, int64(numberOfSeconds))
ListenAndServeTLSorNot(hostname)
}
func MongoLogger(configFile *goconf.ConfigFile) {
verbose, err := configFile.GetBool("mgodb", "verbose")
if err != nil {
logger.Warn(err)
verbose = false
}
logger.Printf("mongo logger verbose set [%v]", verbose)
if verbose {
mgo.SetLogger(logger)
mgo.SetDebug(verbose)
}
}
// starts Addama proxy (http://addama.org) based on the given configuration file
// required parameters: default.hostname, default.password, addama.target, addama.connectionFile, addama.host, addama.service, addama.uri, addama.label
func StartAddama(configFile *goconf.ConfigFile) {
hostname := GetRequiredString(configFile, "default", "hostname")
addamaConn := AddamaConnection{
target: GetRequiredString(configFile, "addama", "target"),
connectionFile: GetRequiredString(configFile, "addama", "connectionFile"),
serviceHost: GetRequiredString(configFile, "addama", "host"),
serviceName: GetRequiredString(configFile, "addama", "service"),
uri: GetRequiredString(configFile, "addama", "uri"),
label: GetRequiredString(configFile, "addama", "label"),
apikey: GetRequiredString(configFile, "default", "password")}
http.Handle("/", NewAddamaProxy(addamaConn))
ListenAndServeTLSorNot(hostname)
}
// starts worker based on the given configuration file
// required parameters: worker.masterhost
// optional parameters: worker.processes
func StartWorker(configFile *goconf.ConfigFile) {
GoMaxProc("worker", configFile)
ConBufferSize("worker", configFile)
processes, err := configFile.GetInt("worker", "processes")
if err != nil {
logger.Warn(err)
processes = 3
}
masterhost := GetRequiredString(configFile, "worker", "masterhost")
logger.Printf("StartWorker() [%v, %d]", masterhost, processes)
RunNode(processes, masterhost)
}
// starts http handlers for HTML content based on the given configuration file
// optional parameters: default.contentDirectory (location of html content to be served at https://example.com/ or https://example.com/html/index.html
func StartHtmlHandler(configFile *goconf.ConfigFile) {
if contentDir, _ := configFile.GetString("default", "contentDirectory"); contentDir != "" {
logger.Printf("StartHtmlHandler(): serving HTML content from [%v]", contentDir)
http.Handle("/html/", http.StripPrefix("/html/", http.FileServer(http.Dir(contentDir))))
http.Handle("/", http.RedirectHandler("/html/index.html", http.StatusTemporaryRedirect))
wd, err := os.Getwd()
if err == nil {
http.Handle("/output/", http.StripPrefix("/output/", http.FileServer(http.Dir(wd))))
}
}
}