Skip to content

Commit b42c44b

Browse files
committed
[runner] Revamp main.go
* Migrate to urfave/cli/v3 * Merge cmd.go into main.go * Pass ctx to Server.Run()
1 parent eb59354 commit b42c44b

File tree

5 files changed

+89
-107
lines changed

5 files changed

+89
-107
lines changed

runner/cmd/runner/cmd.go

Lines changed: 0 additions & 79 deletions
This file was deleted.

runner/cmd/runner/main.go

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,94 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
_ "net/http/pprof"
87
"os"
98
"path/filepath"
109

1110
"github.com/sirupsen/logrus"
11+
"github.com/urfave/cli/v3"
1212

1313
"github.com/dstackai/dstack/runner/consts"
1414
"github.com/dstackai/dstack/runner/internal/log"
1515
"github.com/dstackai/dstack/runner/internal/runner/api"
1616
)
1717

18+
// Version is a build-time variable. The value is overridden by ldflags.
19+
var Version string
20+
1821
func main() {
19-
App()
22+
os.Exit(mainInner())
23+
}
24+
25+
func mainInner() int {
26+
var tempDir string
27+
var homeDir string
28+
var httpPort int
29+
var sshPort int
30+
var logLevel int
31+
32+
cmd := &cli.Command{
33+
Name: "dstack-runner",
34+
Usage: "configure and start dstack-runner",
35+
Version: Version,
36+
Flags: []cli.Flag{
37+
&cli.IntFlag{
38+
Name: "log-level",
39+
Value: 2,
40+
DefaultText: "4 (Info)",
41+
Usage: "log verbosity level: 2 (Error), 3 (Warning), 4 (Info), 5 (Debug), 6 (Trace)",
42+
Destination: &logLevel,
43+
},
44+
},
45+
Commands: []*cli.Command{
46+
{
47+
Name: "start",
48+
Usage: "Start dstack-runner",
49+
Flags: []cli.Flag{
50+
&cli.StringFlag{
51+
Name: "temp-dir",
52+
Usage: "Temporary directory for logs and other files",
53+
Value: consts.RunnerTempDir,
54+
Destination: &tempDir,
55+
TakesFile: true,
56+
},
57+
&cli.StringFlag{
58+
Name: "home-dir",
59+
Usage: "HomeDir directory for credentials and $HOME",
60+
Value: consts.RunnerHomeDir,
61+
Destination: &homeDir,
62+
TakesFile: true,
63+
},
64+
&cli.IntFlag{
65+
Name: "http-port",
66+
Usage: "Set a http port",
67+
Value: consts.RunnerHTTPPort,
68+
Destination: &httpPort,
69+
},
70+
&cli.IntFlag{
71+
Name: "ssh-port",
72+
Usage: "Set the ssh port",
73+
Value: consts.RunnerSSHPort,
74+
Destination: &sshPort,
75+
},
76+
},
77+
Action: func(cxt context.Context, cmd *cli.Command) error {
78+
return start(cxt, tempDir, homeDir, httpPort, sshPort, logLevel, Version)
79+
},
80+
},
81+
},
82+
}
83+
84+
ctx := context.Background()
85+
86+
if err := cmd.Run(ctx, os.Args); err != nil {
87+
log.Error(ctx, err.Error())
88+
return 1
89+
}
90+
91+
return 0
2092
}
2193

22-
func start(tempDir string, homeDir string, httpPort int, sshPort int, logLevel int, version string) error {
94+
func start(ctx context.Context, tempDir string, homeDir string, httpPort int, sshPort int, logLevel int, version string) error {
2395
if err := os.MkdirAll(tempDir, 0o755); err != nil {
2496
return fmt.Errorf("create temp directory: %w", err)
2597
}
@@ -31,20 +103,20 @@ func start(tempDir string, homeDir string, httpPort int, sshPort int, logLevel i
31103
defer func() {
32104
closeErr := defaultLogFile.Close()
33105
if closeErr != nil {
34-
log.Error(context.TODO(), "Failed to close default log file", "err", closeErr)
106+
log.Error(ctx, "Failed to close default log file", "err", closeErr)
35107
}
36108
}()
37109

38110
log.DefaultEntry.Logger.SetOutput(io.MultiWriter(os.Stdout, defaultLogFile))
39111
log.DefaultEntry.Logger.SetLevel(logrus.Level(logLevel))
40112

41-
server, err := api.NewServer(context.TODO(), tempDir, homeDir, fmt.Sprintf(":%d", httpPort), sshPort, version)
113+
server, err := api.NewServer(ctx, tempDir, homeDir, fmt.Sprintf(":%d", httpPort), sshPort, version)
42114
if err != nil {
43115
return fmt.Errorf("create server: %w", err)
44116
}
45117

46-
log.Trace(context.TODO(), "Starting API server", "port", httpPort)
47-
if err := server.Run(); err != nil {
118+
log.Trace(ctx, "Starting API server", "port", httpPort)
119+
if err := server.Run(ctx); err != nil {
48120
return fmt.Errorf("server failed: %w", err)
49121
}
50122

runner/go.mod

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ require (
2020
github.com/shirou/gopsutil/v4 v4.24.11
2121
github.com/sirupsen/logrus v1.9.3
2222
github.com/stretchr/testify v1.11.1
23-
github.com/urfave/cli/v2 v2.27.7
2423
github.com/urfave/cli/v3 v3.6.1
2524
golang.org/x/crypto v0.22.0
2625
golang.org/x/sys v0.26.0
@@ -33,7 +32,6 @@ require (
3332
github.com/bits-and-blooms/bitset v1.22.0 // indirect
3433
github.com/cloudflare/circl v1.3.7 // indirect
3534
github.com/containerd/log v0.1.0 // indirect
36-
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
3735
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
3836
github.com/davecgh/go-spew v1.1.1 // indirect
3937
github.com/distribution/reference v0.6.0 // indirect
@@ -62,15 +60,13 @@ require (
6260
github.com/pkg/errors v0.9.1 // indirect
6361
github.com/pmezard/go-difflib v1.0.0 // indirect
6462
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
65-
github.com/russross/blackfriday/v2 v2.1.0 // indirect
6663
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
6764
github.com/skeema/knownhosts v1.2.2 // indirect
6865
github.com/tidwall/btree v1.7.0 // indirect
6966
github.com/tklauser/go-sysconf v0.3.12 // indirect
7067
github.com/tklauser/numcpus v0.6.1 // indirect
7168
github.com/ulikunitz/xz v0.5.12 // indirect
7269
github.com/xanzy/ssh-agent v0.3.3 // indirect
73-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
7470
github.com/yusufpapurcu/wmi v1.2.4 // indirect
7571
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0 // indirect
7672
go.opentelemetry.io/otel v1.25.0 // indirect

runner/go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ github.com/codeclysm/extract/v4 v4.0.0 h1:H87LFsUNaJTu2e/8p/oiuiUsOK/TaPQ5wxsjPn
3434
github.com/codeclysm/extract/v4 v4.0.0/go.mod h1:SFju1lj6as7FvUgalpSct7torJE0zttbJUWtryPRG6s=
3535
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
3636
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
37-
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
38-
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
3937
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
4038
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
4139
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
@@ -155,8 +153,6 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
155153
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
156154
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
157155
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
158-
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
159-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
160156
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
161157
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
162158
github.com/shirou/gopsutil/v4 v4.24.11 h1:WaU9xqGFKvFfsUv94SXcUPD7rCkU0vr/asVdQOBZNj8=
@@ -185,14 +181,10 @@ github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+F
185181
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
186182
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
187183
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
188-
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
189-
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
190184
github.com/urfave/cli/v3 v3.6.1 h1:j8Qq8NyUawj/7rTYdBGrxcH7A/j7/G8Q5LhWEW4G3Mo=
191185
github.com/urfave/cli/v3 v3.6.1/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
192186
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
193187
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
194-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
195-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
196188
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
197189
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
198190
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

runner/internal/runner/api/server.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"net/http"
7+
_ "net/http/pprof"
78
"os"
89
"os/signal"
910
"syscall"
@@ -80,21 +81,21 @@ func NewServer(ctx context.Context, tempDir string, homeDir string, address stri
8081
return s, nil
8182
}
8283

83-
func (s *Server) Run() error {
84-
signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGQUIT}
84+
func (s *Server) Run(ctx context.Context) error {
85+
signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT}
8586
signalCh := make(chan os.Signal, 1)
8687

8788
go func() {
8889
if err := s.srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
89-
log.Error(context.TODO(), "Server failed", "err", err)
90+
log.Error(ctx, "Server failed", "err", err)
9091
}
9192
}()
92-
defer func() { _ = s.srv.Shutdown(context.TODO()) }()
93+
defer func() { _ = s.srv.Shutdown(ctx) }()
9394

9495
select {
9596
case <-s.jobBarrierCh: // job started
9697
case <-time.After(s.submitWaitDuration):
97-
log.Error(context.TODO(), "Job didn't start in time, shutting down")
98+
log.Error(ctx, "Job didn't start in time, shutting down")
9899
return errors.New("no job submitted")
99100
}
100101

@@ -103,10 +104,10 @@ func (s *Server) Run() error {
103104
signal.Notify(signalCh, signals...)
104105
select {
105106
case <-signalCh:
106-
log.Error(context.TODO(), "Received interrupt signal, shutting down")
107+
log.Error(ctx, "Received interrupt signal, shutting down")
107108
s.stop()
108109
case <-s.jobBarrierCh:
109-
log.Info(context.TODO(), "Job finished, shutting down")
110+
log.Info(ctx, "Job finished, shutting down")
110111
}
111112
close(s.shutdownCh)
112113
signal.Reset(signals...)
@@ -123,9 +124,9 @@ loop:
123124
for _, ch := range logsToWait {
124125
select {
125126
case <-ch.ch:
126-
log.Info(context.TODO(), "Logs streaming finished", "endpoint", ch.name)
127+
log.Info(ctx, "Logs streaming finished", "endpoint", ch.name)
127128
case <-waitLogsDone:
128-
log.Error(context.TODO(), "Logs streaming didn't finish in time")
129+
log.Error(ctx, "Logs streaming didn't finish in time")
129130
break loop // break the loop, not the select
130131
}
131132
}

0 commit comments

Comments
 (0)