-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
193 lines (155 loc) · 4.83 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"math/rand"
"os"
"os/user"
"path"
"github.com/go-redis/redis/v8"
"github.com/Donders-Institute/dynamore-feature-extraction-runner/util"
log "github.com/Donders-Institute/tg-toolset-golang/pkg/logger"
)
var (
rdb *redis.Client
optRedisURL *string
optRedisChannel *string
optRunnerUser *string
optSSHKeyDir *string
optsJobQue *string
optsJobReq *string
verbose *bool
// list of submission hosts (port number is necessary) of the HPC cluster.
hpcSubmitHosts = []string{
"mentat001.dccn.nl:22",
"mentat002.dccn.nl:22",
"mentat003.dccn.nl:22",
"mentat004.dccn.nl:22",
"mentat005.dccn.nl:22",
}
)
func usage() {
fmt.Printf("\nUsage: %s [OPTIONS]\n", os.Args[0])
fmt.Printf("\nOPTIONS:\n")
flag.PrintDefaults()
}
func init() {
u, err := user.Current()
if err != nil {
log.Fatalf("%s", err)
}
// load env variables for default values
defaultRedisURL := os.Getenv("REDIS_URL")
if defaultRedisURL == "" {
defaultRedisURL = "redis://localhost:6379/0"
}
defaultRedisChannel := os.Getenv("REDIS_PAYLOAD_CHANNEL")
if defaultRedisChannel == "" {
defaultRedisChannel = "dynamore_feature_extraction"
}
defaultExecUser := os.Getenv("EXEC_USER")
if defaultExecUser == "" {
defaultExecUser = u.Username
}
defaultSSHKeyDir := os.Getenv("SSH_KEY_DIR")
if defaultSSHKeyDir == "" {
defaultSSHKeyDir = path.Join(u.HomeDir, ".ssh", "dfe_runner")
}
defaultJobReq := os.Getenv("TORQUE_JOB_REQUIREMENT")
if defaultJobReq == "" {
defaultJobReq = "walltime=1:00:00,mem=4gb"
}
// parse commandline arguments
optRedisURL = flag.String("d", defaultRedisURL, "set endpoint `url` of the Redis server.")
optRedisChannel = flag.String("c", defaultRedisChannel, "set redis `channel` for feature-extraction payloads.")
optRunnerUser = flag.String("u", defaultExecUser, "run feature-extraction process/job as the `user`.")
optSSHKeyDir = flag.String("k", defaultSSHKeyDir, "`path` in which the the SSH pub/priv keys are created.")
optsJobReq = flag.String("l", defaultJobReq, "specify the HPC torque job `requirement`.")
optsJobQue = flag.String("q", os.Getenv("TORQUE_JOB_QUEUE"), "specify the HPC torque job `queue`.")
verbose = flag.Bool("v", false, "show debug messages.")
flag.Usage = usage
flag.Parse()
// create dir for ssh keys
if err := os.MkdirAll(*optSSHKeyDir, 0700); err != nil {
log.Fatalf("%s", err)
}
// config logger
cfg := log.Configuration{
EnableConsole: true,
ConsoleJSONFormat: false,
ConsoleLevel: log.Info,
}
if *verbose {
cfg.ConsoleLevel = log.Debug
}
// initialize logger
log.NewLogger(cfg, log.InstanceLogrusLogger)
// initiate connection to redis server
opt, err := redis.ParseURL(*optRedisURL)
if err != nil {
log.Fatalf("%s", err)
}
rdb = redis.NewClient(opt)
}
// main function
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
chanPayload := rdb.Subscribe(ctx, *optRedisChannel)
defer func() {
chanPayload.Close()
cancel()
}()
if err := serve(ctx, chanPayload); err != nil {
log.Fatalf("%s", err)
}
}
// serve runs indefinitely and listens to incoming payload message from the redis.
func serve(ctx context.Context, payloads *redis.PubSub) error {
// provision SSH when sshPrivKey does not exist.
sshPrivKey := path.Join(*optSSHKeyDir, "id_rsa")
sshPubKey := path.Join(*optSSHKeyDir, "id_rsa.pub")
if _, err := os.Stat(sshPrivKey); os.IsNotExist(err) {
if err := util.GenerateRSAKeyPair(sshPrivKey, sshPubKey); err != nil {
return fmt.Errorf("cannot initiate RSA keys for ssh connection: %s", err)
}
if err := util.AddAuthorizedPublicKey(*optRunnerUser, sshPubKey); err != nil {
return fmt.Errorf("cannot update authorized_keys: %s", err)
}
}
ch := payloads.Channel()
for {
select {
case <-ctx.Done():
return nil
case m := <-ch:
go runPayload(m, sshPrivKey)
}
}
}
func runPayload(m *redis.Message, sshPrivKey string) {
log.Infof("payload: %s", m.Payload)
// unmarshal redis message to payload struct
p := util.Payload{}
json.Unmarshal([]byte(m.Payload), &p)
// submit payload in one of the following methods:
// TODO: make method switch configurable
//
// method 1: run singularity on local server.
// _, err := p.Run(*optRunnerUser)
//
// method 2: submit job to run singularity using direct qsub.
// The server is the submit host.
// jid, err := p.Submit(*optRunnerUser, *optJobReq, *optJobQue)
//
// method 3: submit job to run singularity via a remote submit host.
// SSH keypair auth required.
submitHost := hpcSubmitHosts[rand.Int()%len(hpcSubmitHosts)]
jid, err := p.SSHSubmit(*optRunnerUser, *optsJobReq, *optsJobQue, submitHost, sshPrivKey)
if err != nil {
log.Errorf("[%s] cannot submit payload: %s", p, err)
}
log.Infof("[%s] payload submitted as job %s", p, jid)
}