@@ -8,10 +8,10 @@ import (
8
8
"strings"
9
9
"syscall"
10
10
11
- log "github.com/sirupsen/logrus"
12
11
"github.com/urfave/cli/v2"
13
12
"golang.org/x/sys/unix"
14
13
14
+ "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
15
15
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/runtime"
16
16
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/toolkit"
17
17
)
@@ -51,12 +51,40 @@ func (o options) toolkitRoot() string {
51
51
var Version = "development"
52
52
53
53
func main () {
54
- remainingArgs , root , err := ParseArgs (os .Args )
54
+ logger := logger .New ()
55
+
56
+ remainingArgs , root , err := ParseArgs (logger , os .Args )
55
57
if err != nil {
56
- log .Errorf ("Error: unable to parse arguments: %v" , err )
58
+ logger .Errorf ("Error: unable to parse arguments: %v" , err )
59
+ os .Exit (1 )
60
+ }
61
+
62
+ c := new (logger , root )
63
+
64
+ // Run the CLI
65
+ logger .Infof ("Starting %v" , c .Name )
66
+ if err := c .Run (remainingArgs ); err != nil {
67
+ logger .Errorf ("error running nvidia-toolkit: %v" , err )
57
68
os .Exit (1 )
58
69
}
59
70
71
+ logger .Infof ("Completed %v" , c .Name )
72
+ }
73
+
74
+ type app struct {
75
+ logger logger.Interface
76
+ defaultRoot string
77
+ }
78
+
79
+ func new (logger logger.Interface , defaultRoot string ) * cli.App {
80
+ a := app {
81
+ logger : logger ,
82
+ defaultRoot : defaultRoot ,
83
+ }
84
+ return a .build ()
85
+ }
86
+
87
+ func (a app ) build () * cli.App {
60
88
options := options {
61
89
toolkitOptions : toolkit.Options {},
62
90
}
@@ -68,10 +96,10 @@ func main() {
68
96
c .Description = "DESTINATION points to the host path underneath which the nvidia-container-toolkit should be installed.\n It will be installed at ${DESTINATION}/toolkit"
69
97
c .Version = Version
70
98
c .Before = func (ctx * cli.Context ) error {
71
- return validateFlags (ctx , & options )
99
+ return a . validateFlags (ctx , & options )
72
100
}
73
101
c .Action = func (ctx * cli.Context ) error {
74
- return Run (ctx , & options )
102
+ return a . Run (ctx , & options )
75
103
}
76
104
77
105
// Setup flags for the CLI
@@ -102,7 +130,7 @@ func main() {
102
130
},
103
131
& cli.StringFlag {
104
132
Name : "root" ,
105
- Value : root ,
133
+ Value : a . defaultRoot ,
106
134
Usage : "the folder where the NVIDIA Container Toolkit is to be installed. It will be installed to `ROOT`/toolkit" ,
107
135
Destination : & options .root ,
108
136
EnvVars : []string {"ROOT" },
@@ -119,17 +147,10 @@ func main() {
119
147
c .Flags = append (c .Flags , toolkit .Flags (& options .toolkitOptions )... )
120
148
c .Flags = append (c .Flags , runtime .Flags (& options .runtimeOptions )... )
121
149
122
- // Run the CLI
123
- log .Infof ("Starting %v" , c .Name )
124
- if err := c .Run (remainingArgs ); err != nil {
125
- log .Errorf ("error running nvidia-toolkit: %v" , err )
126
- os .Exit (1 )
127
- }
128
-
129
- log .Infof ("Completed %v" , c .Name )
150
+ return c
130
151
}
131
152
132
- func validateFlags (_ * cli.Context , o * options ) error {
153
+ func ( a * app ) validateFlags (_ * cli.Context , o * options ) error {
133
154
if o .root == "" {
134
155
return fmt .Errorf ("the install root must be specified" )
135
156
}
@@ -139,6 +160,7 @@ func validateFlags(_ *cli.Context, o *options) error {
139
160
if filepath .Base (o .pidFile ) != toolkitPidFilename {
140
161
return fmt .Errorf ("invalid toolkit.pid path %v" , o .pidFile )
141
162
}
163
+
142
164
if err := toolkit .ValidateOptions (& o .toolkitOptions , o .toolkitRoot ()); err != nil {
143
165
return err
144
166
}
@@ -149,12 +171,12 @@ func validateFlags(_ *cli.Context, o *options) error {
149
171
}
150
172
151
173
// Run runs the core logic of the CLI
152
- func Run (c * cli.Context , o * options ) error {
153
- err := initialize (o .pidFile )
174
+ func ( a * app ) Run (c * cli.Context , o * options ) error {
175
+ err := a . initialize (o .pidFile )
154
176
if err != nil {
155
177
return fmt .Errorf ("unable to initialize: %v" , err )
156
178
}
157
- defer shutdown (o .pidFile )
179
+ defer a . shutdown (o .pidFile )
158
180
159
181
if len (o .toolkitOptions .ContainerRuntimeRuntimes .Value ()) == 0 {
160
182
lowlevelRuntimePaths , err := runtime .GetLowlevelRuntimePaths (& o .runtimeOptions , o .runtime )
@@ -176,7 +198,7 @@ func Run(c *cli.Context, o *options) error {
176
198
}
177
199
178
200
if ! o .noDaemon {
179
- err = waitForSignal ()
201
+ err = a . waitForSignal ()
180
202
if err != nil {
181
203
return fmt .Errorf ("unable to wait for signal: %v" , err )
182
204
}
@@ -192,8 +214,8 @@ func Run(c *cli.Context, o *options) error {
192
214
193
215
// ParseArgs checks if a single positional argument was defined and extracts this the root.
194
216
// If no positional arguments are defined, it is assumed that the root is specified as a flag.
195
- func ParseArgs (args []string ) ([]string , string , error ) {
196
- log .Infof ("Parsing arguments" )
217
+ func ParseArgs (logger logger. Interface , args []string ) ([]string , string , error ) {
218
+ logger .Infof ("Parsing arguments" )
197
219
198
220
if len (args ) < 2 {
199
221
return args , "" , nil
@@ -218,8 +240,8 @@ func ParseArgs(args []string) ([]string, string, error) {
218
240
return nil , "" , fmt .Errorf ("unexpected positional argument(s) %v" , args [2 :lastPositionalArg + 1 ])
219
241
}
220
242
221
- func initialize (pidFile string ) error {
222
- log .Infof ("Initializing" )
243
+ func ( a * app ) initialize (pidFile string ) error {
244
+ a . logger .Infof ("Initializing" )
223
245
224
246
if dir := filepath .Dir (pidFile ); dir != "" {
225
247
err := os .MkdirAll (dir , 0755 )
@@ -235,8 +257,8 @@ func initialize(pidFile string) error {
235
257
236
258
err = unix .Flock (int (f .Fd ()), unix .LOCK_EX | unix .LOCK_NB )
237
259
if err != nil {
238
- log .Warningf ("Unable to get exclusive lock on '%v'" , pidFile )
239
- log .Warningf ("This normally means an instance of the NVIDIA toolkit Container is already running, aborting" )
260
+ a . logger .Warningf ("Unable to get exclusive lock on '%v'" , pidFile )
261
+ a . logger .Warningf ("This normally means an instance of the NVIDIA toolkit Container is already running, aborting" )
240
262
return fmt .Errorf ("unable to get flock on pidfile: %v" , err )
241
263
}
242
264
@@ -253,27 +275,27 @@ func initialize(pidFile string) error {
253
275
case <- waitingForSignal :
254
276
signalReceived <- true
255
277
default :
256
- log .Infof ("Signal received, exiting early" )
257
- shutdown (pidFile )
278
+ a . logger .Infof ("Signal received, exiting early" )
279
+ a . shutdown (pidFile )
258
280
os .Exit (0 )
259
281
}
260
282
}()
261
283
262
284
return nil
263
285
}
264
286
265
- func waitForSignal () error {
266
- log .Infof ("Waiting for signal" )
287
+ func ( a * app ) waitForSignal () error {
288
+ a . logger .Infof ("Waiting for signal" )
267
289
waitingForSignal <- true
268
290
<- signalReceived
269
291
return nil
270
292
}
271
293
272
- func shutdown (pidFile string ) {
273
- log .Infof ("Shutting Down" )
294
+ func ( a * app ) shutdown (pidFile string ) {
295
+ a . logger .Infof ("Shutting Down" )
274
296
275
297
err := os .Remove (pidFile )
276
298
if err != nil {
277
- log .Warningf ("Unable to remove pidfile: %v" , err )
299
+ a . logger .Warningf ("Unable to remove pidfile: %v" , err )
278
300
}
279
301
}
0 commit comments