/
singularity.go
315 lines (267 loc) · 8.29 KB
/
singularity.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cli
import (
"errors"
"fmt"
"os"
"os/user"
"path"
"strings"
"text/template"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/sylabs/singularity/internal/pkg/buildcfg"
"github.com/sylabs/singularity/internal/pkg/sylog"
"github.com/sylabs/singularity/internal/pkg/util/auth"
"github.com/sylabs/singularity/src/docs"
)
// Global variables for singularity CLI
var (
debug bool
silent bool
verbose bool
quiet bool
)
var (
// TokenFile holds the path to the sylabs auth token file
defaultTokenFile, tokenFile string
// authToken holds the sylabs auth token
authToken, authWarning string
)
const (
envPrefix = "SINGULARITY_"
)
func init() {
SingularityCmd.Flags().SetInterspersed(false)
SingularityCmd.PersistentFlags().SetInterspersed(false)
templateFuncs := template.FuncMap{
"TraverseParentsUses": TraverseParentsUses,
}
cobra.AddTemplateFuncs(templateFuncs)
SingularityCmd.SetHelpTemplate(docs.HelpTemplate)
SingularityCmd.SetUsageTemplate(docs.UseTemplate)
usr, err := user.Current()
if err != nil {
sylog.Fatalf("Couldn't determine user home directory: %v", err)
}
defaultTokenFile = path.Join(usr.HomeDir, ".singularity", "sylabs-token")
SingularityCmd.Flags().BoolVarP(&debug, "debug", "d", false, "print debugging information (highest verbosity)")
SingularityCmd.Flags().BoolVarP(&silent, "silent", "s", false, "only print errors")
SingularityCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "suppress normal output")
SingularityCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print additional information")
SingularityCmd.Flags().StringVarP(&tokenFile, "tokenfile", "t", defaultTokenFile, "path to the file holding your sylabs authentication token")
VersionCmd.Flags().SetInterspersed(false)
SingularityCmd.AddCommand(VersionCmd)
}
func setSylogMessageLevel(cmd *cobra.Command, args []string) {
var level int
if debug {
level = 5
} else if verbose {
level = 4
} else if quiet {
level = -1
} else if silent {
level = -3
} else {
level = 1
}
sylog.SetLevel(level)
}
// SingularityCmd is the base command when called without any subcommands
var SingularityCmd = &cobra.Command{
TraverseChildren: true,
DisableFlagsInUseLine: true,
PersistentPreRun: persistentPreRun,
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("Invalid command")
},
Use: docs.SingularityUse,
Version: buildcfg.PACKAGE_VERSION,
Short: docs.SingularityShort,
Long: docs.SingularityLong,
Example: docs.SingularityExample,
SilenceErrors: true,
}
// ExecuteSingularity adds all child commands to the root command and sets
// flags appropriately. This is called by main.main(). It only needs to happen
// once to the root command (singularity).
func ExecuteSingularity() {
defaultEnv := "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
// backup user PATH
userEnv := strings.Join([]string{os.Getenv("PATH"), defaultEnv}, ":")
os.Setenv("USER_PATH", userEnv)
os.Setenv("PATH", defaultEnv)
if err := SingularityCmd.Execute(); err != nil {
os.Exit(1)
}
}
// TraverseParentsUses walks the parent commands and outputs a properly formatted use string
func TraverseParentsUses(cmd *cobra.Command) string {
if cmd.HasParent() {
return TraverseParentsUses(cmd.Parent()) + cmd.Use + " "
}
return cmd.Use + " "
}
// VersionCmd displays installed singularity version
var VersionCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(buildcfg.PACKAGE_VERSION)
},
Use: "version",
Short: "Show application version",
}
func updateFlagsFromEnv(cmd *cobra.Command) {
cmd.Flags().VisitAll(handleEnv)
}
func handleEnv(flag *pflag.Flag) {
envKeys, ok := flag.Annotations["envkey"]
if !ok {
return
}
for _, key := range envKeys {
val, set := os.LookupEnv(envPrefix + key)
if !set {
continue
}
updateFn := flagEnvFuncs[flag.Name]
updateFn(flag, val)
}
}
func persistentPreRun(cmd *cobra.Command, args []string) {
setSylogMessageLevel(cmd, args)
updateFlagsFromEnv(cmd)
}
// sylabsToken process the authentication Token
// priority default_file < env < file_flag
func sylabsToken(cmd *cobra.Command, args []string) {
if val := os.Getenv("SYLABS_TOKEN"); val != "" {
authToken = val
}
if tokenFile != defaultTokenFile {
authToken, authWarning = auth.ReadToken(tokenFile)
}
if authToken == "" {
authToken, authWarning = auth.ReadToken(defaultTokenFile)
}
if authToken == "" && authWarning == auth.WarningTokenFileNotFound {
sylog.Warningf("%v : Only pulls of public images will succeed", authWarning)
}
}
// envAppend combines command line and environment var into a single argument
func envAppend(flag *pflag.Flag, envvar string) {
if err := flag.Value.Set(envvar); err != nil {
sylog.Warningf("Unable to set %s to environment variable value %s", flag.Name, envvar)
} else {
flag.Changed = true
sylog.Debugf("Update flag Value to: %s", flag.Value)
}
}
// envBool sets a bool flag if the CLI option is unset and env var is set
func envBool(flag *pflag.Flag, envvar string) {
if flag.Changed == true || envvar == "" {
return
}
if err := flag.Value.Set(envvar); err != nil {
sylog.Debugf("Unable to set flag %s to value %s: %s", flag.Name, envvar, err)
if err := flag.Value.Set("true"); err != nil {
sylog.Warningf("Unable to set flag %s to value %s: %s", flag.Name, envvar, err)
return
}
}
flag.Changed = true
sylog.Debugf("Set %s Value to: %s", flag.Name, flag.Value)
}
// envStringNSlice writes to a string or slice flag if CLI option/argument
// string is unset and env var is set
func envStringNSlice(flag *pflag.Flag, envvar string) {
if flag.Changed == true {
return
}
if err := flag.Value.Set(envvar); err != nil {
sylog.Warningf("Unable to set flag %s to value %s: %s", flag.Name, envvar, err)
return
}
flag.Changed = true
sylog.Debugf("Set %s Value to: %s", flag.Name, flag.Value)
}
type envHandle func(*pflag.Flag, string)
// map of functions to use to bind flags to environment variables
var flagEnvFuncs = map[string]envHandle{
// action flags
"bind": envAppend,
"home": envStringNSlice,
"overlay": envStringNSlice,
"scratch": envStringNSlice,
"workdir": envStringNSlice,
"shell": envStringNSlice,
"pwd": envStringNSlice,
"hostname": envStringNSlice,
"network": envStringNSlice,
"network-args": envStringNSlice,
"dns": envStringNSlice,
"containlibs": envStringNSlice,
"security": envStringNSlice,
"apply-cgroups": envStringNSlice,
"app": envStringNSlice,
"boot": envBool,
"fakeroot": envBool,
"cleanenv": envBool,
"contain": envBool,
"containall": envBool,
"nv": envBool,
"no-nv": envBool,
"writable": envBool,
"writable-tmpfs": envBool,
"no-home": envBool,
"no-init": envBool,
"pid": envBool,
"ipc": envBool,
"net": envBool,
"uts": envBool,
"userns": envBool,
"keep-privs": envBool,
"no-privs": envBool,
"add-caps": envStringNSlice,
"drop-caps": envStringNSlice,
"allow-setuid": envBool,
// build flags
"sandbox": envBool,
"section": envStringNSlice,
"json": envBool,
"name": envStringNSlice,
// "writable": envBool, // set above for now
"force": envBool,
"update": envBool,
"notest": envBool,
"remote": envBool,
"detached": envBool,
"builder": envStringNSlice,
"library": envStringNSlice,
"nohttps": envBool,
"tmpdir": envStringNSlice,
"docker-username": envStringNSlice,
"docker-password": envStringNSlice,
"docker-login": envBool,
// capability flags (and others)
"user": envStringNSlice,
"group": envStringNSlice,
"desc": envBool,
"all": envBool,
// instance flags
"signal": envStringNSlice,
// keys flags
"secret": envBool,
"url": envStringNSlice,
// inspect flags
"labels": envBool,
"deffile": envBool,
"runscript": envBool,
"test": envBool,
"environment": envBool,
"helpfile": envBool,
}