/
main.go
171 lines (160 loc) · 4.3 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
package main
import (
"github.com/qnib/go-wharfie/wharfie"
"github.com/zpatrick/go-config"
"github.com/codegangsta/cli"
"os"
)
var (
dockerSocketFlag = cli.StringFlag{
Name: "docker-host",
Usage: "Docker host to connect to.",
EnvVar: "DOCKER_HOST",
}
dockerCertFlag = cli.StringFlag{
Name: "docker-cert-path",
Usage: "Path to certificates.",
EnvVar: "DOCKER_CERT_PATH",
}
debugFlag = cli.BoolFlag{
Name: "debug",
Usage: "Print proxy requests",
EnvVar: "WHARFIE_DEBUG",
}
bundleFlag = cli.BoolFlag{
Name: "bundle",
Usage: "Use UCP client bundle",
EnvVar: "WHARFIE_BUNDLE",
}
dockerImageFlag = cli.StringFlag{
Name: "docker-image",
Usage: "Docker Image to use for JOB.",
EnvVar: "WHARFIE_DOCKER_IMAGE",
}
jobIdFlag = cli.StringFlag{
Name: "job-id",
Usage: "Job ID.",
EnvVar: "SLURM_JOB_ID",
}
constraintsFlag = cli.StringFlag{
Name: "constraints",
Usage: "Label to limit container on.",
EnvVar: "WHARFIE_CONSTRAINTS",
}
usernameFlag = cli.StringFlag{
Name: "username",
Usage: "username to define home-dir.",
Value: "cluser",
EnvVar: "WHARFIE_USERNAME",
}
userFlag = cli.StringFlag{
Name: "user",
Usage: "UID[:GID] to run container with.",
Value: "1000:1000",
EnvVar: "WHARFIE_USER",
}
homedirFlag = cli.StringFlag{
Name: "homedir",
Usage: "Homedir prefix. Workdir of containers are going to be '${homedir}/${username}'.",
Value: "/home",
EnvVar: "WHARFIE_HOMEDIR",
}
replicaFlag = cli.IntFlag{
Name: "replicas",
Usage: "Service replicas, 0 creates a global service.",
Value: 0,
EnvVar: "WHARFIE_SERVICE_REPLICAS",
}
mountsFlag = cli.StringFlag{
Name: "volumes",
Usage: "Comma separated list of bind-mounts",
EnvVar: "WHARFIE_VOLUMES",
}
nodeListFlag = cli.StringFlag{
Name: "node-list",
Usage: "Comma separated list of nodes (container names)",
EnvVar: "WHARFIE_NODE_LIST",
}
)
func EvalOptions(cfg *config.Config) (po []wharfie.Option) {
dockerSock, _ := cfg.String("docker-host")
po = append(po, wharfie.WithDockerSocket(dockerSock))
dockerImage, _ := cfg.String("docker-image")
po = append(po, wharfie.WithDockerImage(dockerImage))
nodeList, _ := cfg.String("node-list")
po = append(po, wharfie.WithNodeList(nodeList))
jobId, _ := cfg.String("job-id")
po = append(po, wharfie.WithJobId(jobId))
dockerCertPath, _ := cfg.String("docker-cert-path")
po = append(po, wharfie.WithDockerCertPath(dockerCertPath))
debug, _ := cfg.Bool("debug")
po = append(po, wharfie.WithDebugValue(debug))
bundle, _ := cfg.Bool("bundle")
po = append(po, wharfie.WithBundleValue(bundle))
replicas, _ := cfg.Int("replicas")
po = append(po, wharfie.WithReplicas(replicas))
vols, _ := cfg.String("volumes")
po = append(po, wharfie.WithVolumes(vols))
hdir, _ := cfg.String("homedir")
po = append(po, wharfie.WithHomedir(hdir))
uname, _ := cfg.String("username")
po = append(po, wharfie.WithUsername(uname))
user, _ := cfg.String("user")
po = append(po, wharfie.WithUser(user))
constraints , _ := cfg.String("constraints")
po = append(po, wharfie.WithConstraints(constraints))
return
}
func SshTasks(ctx *cli.Context) {
cfg := config.NewConfig([]config.Provider{config.NewCLI(ctx, true)})
po := EvalOptions(cfg)
p := wharfie.New(ctx.App.Version,po...)
p.Ssh(ctx)
}
func StageService(ctx *cli.Context) {
cfg := config.NewConfig([]config.Provider{config.NewCLI(ctx, true)})
po := EvalOptions(cfg)
p := wharfie.New(ctx.App.Version, po...)
p.Stage()
}
func RemoveService(ctx *cli.Context) {
cfg := config.NewConfig([]config.Provider{config.NewCLI(ctx, true)})
po := EvalOptions(cfg)
p := wharfie.New(ctx.App.Version, po...)
p.Remove()
}
func main() {
app := cli.NewApp()
app.Name = "CLI to help mpirun to use docker container."
app.Usage = "go-wharfie [options]"
app.Version = "0.2.0"
app.Flags = []cli.Flag{
debugFlag,
bundleFlag,
dockerSocketFlag,
jobIdFlag,
nodeListFlag,
}
app.Commands = []cli.Command{
{
Name: "stage",
Usage: "Create service and wait for all tasks to be up.",
Action: StageService,
Flags: []cli.Flag{
mountsFlag,
replicaFlag,
usernameFlag,
userFlag,
homedirFlag,
dockerImageFlag,
constraintsFlag,
},
},{
Name: "remove",
Usage: "Remove service and wait for all tasks to be removed.",
Action: RemoveService,
},
}
app.Action = SshTasks
app.Run(os.Args)
}