Skip to content

Commit 79fac5d

Browse files
committed
cmd/catalyst: boot from a temporary mist config file
1 parent 2a6aeba commit 79fac5d

File tree

5 files changed

+110
-54
lines changed

5 files changed

+110
-54
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ box-dev: scripts
219219
-v $$(realpath config):/etc/livepeer:ro \
220220
-v $$(realpath ./coredumps):$$(realpath ./coredumps) \
221221
-e CORE_DUMP_DIR=$$(realpath ./coredumps) \
222+
-e CATALYST_CONFIG=/etc/livepeer/full-stack.yaml \
223+
-e CATALYST_DOWNLOAD=false \
222224
$(shell for line in $$(cat .env 2>/dev/null || echo ''); do printf -- "-e $$line "; done) \
223225
--rm \
224226
-it \

cmd/catalyst/catalyst.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"os"
5+
"os/exec"
76
"syscall"
87

9-
"github.com/icza/dyno"
10-
"github.com/livepeer/catalyst/cmd/downloader/cli"
8+
"github.com/livepeer/catalyst/cmd/catalyst/cli"
9+
"github.com/livepeer/catalyst/cmd/catalyst/config"
1110
"github.com/livepeer/catalyst/cmd/downloader/downloader"
1211
"github.com/livepeer/catalyst/cmd/downloader/types"
1312
glog "github.com/magicsong/color-glog"
14-
"gopkg.in/yaml.v3"
1513
)
1614

1715
var Version = "undefined"
@@ -25,27 +23,41 @@ func main() {
2523
glog.Fatalf("error parsing cli flags: %s", err)
2624
return
2725
}
28-
err = downloader.Run(cliFlags)
26+
if cliFlags.Download {
27+
err = downloader.Run(cliFlags)
28+
if err != nil {
29+
glog.Fatalf("error running downloader: %s", err)
30+
}
31+
}
32+
err = execNext(cliFlags)
2933
if err != nil {
30-
glog.Fatalf("error running downloader: %s", err)
34+
glog.Fatalf("error executing MistController: %s", err)
3135
}
32-
execNext(cliFlags)
3336
}
3437

3538
// Done! Move on to the provided next application, if it exists.
36-
func execNext(cliFlags types.CliFlags) {
37-
if len(cliFlags.ExecCommand) == 0 {
38-
// Nothing to do.
39-
return
39+
func execNext(cliFlags types.CliFlags) error {
40+
jsonBytes, err := config.HandleConfigStack(cliFlags.ConfigStack)
41+
if err != nil {
42+
return err
43+
}
44+
f, err := os.CreateTemp("", "catalyst-generated-*.json")
45+
if err != nil {
46+
return err
47+
}
48+
_, err = f.Write(jsonBytes)
49+
if err != nil {
50+
return err
4051
}
41-
configStr, err := handleConfigFile("/home/iameli/code/catalyst/config/full-stack.yaml")
52+
glog.Infof("downloader complete, now we will exec %v", cliFlags.MistController)
53+
binary, err := exec.LookPath(cliFlags.MistController)
4254
if err != nil {
43-
panic(err)
55+
return err
4456
}
45-
panic(configStr)
46-
glog.Infof("downloader complete, now we will exec %v", cliFlags.ExecCommand)
47-
execErr := syscall.Exec(cliFlags.ExecCommand[0], cliFlags.ExecCommand, os.Environ())
57+
args := []string{binary, "-c", f.Name()}
58+
execErr := syscall.Exec(binary, args, os.Environ())
4859
if execErr != nil {
4960
glog.Fatalf("error running next command: %s", execErr)
5061
}
62+
return nil
5163
}

cmd/catalyst/cli/cli.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cli
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
downloaderCli "github.com/livepeer/catalyst/cmd/downloader/cli"
9+
"github.com/livepeer/catalyst/cmd/downloader/constants"
10+
"github.com/livepeer/catalyst/cmd/downloader/types"
11+
"github.com/peterbourgon/ff/v3"
12+
)
13+
14+
// GetCliFlags reads command-line arguments and generates a struct
15+
// with useful values set after parsing the same.
16+
func GetCliFlags(buildFlags types.BuildFlags) (types.CliFlags, error) {
17+
cliFlags := types.CliFlags{}
18+
flag.Set("logtostderr", "true")
19+
vFlag := flag.Lookup("v")
20+
fs := flag.NewFlagSet(constants.AppName, flag.ExitOnError)
21+
22+
downloaderCli.AddDownloaderFlags(fs, &cliFlags)
23+
24+
fs.StringVar(&cliFlags.MistController, "mist-controller", "MistController", "Path to MistController binary to exec when done")
25+
fs.StringVar(&cliFlags.ConfigStack, "config", "/etc/livepeer/catalyst.yaml", "Path to multiple Catalyst config files to use. Can contain multiple entries e.g. /conf1:/conf2")
26+
27+
version := fs.Bool("version", false, "Get version information")
28+
29+
if *version {
30+
fmt.Printf("catalyst version: %s\n", buildFlags.Version)
31+
os.Exit(0)
32+
}
33+
34+
ff.Parse(
35+
fs, os.Args[1:],
36+
ff.WithConfigFileParser(ff.PlainParser),
37+
ff.WithEnvVarPrefix("CATALYST"),
38+
ff.WithEnvVarSplit(","),
39+
)
40+
flag.CommandLine.Parse(nil)
41+
vFlag.Value.Set(cliFlags.Verbosity)
42+
43+
err := downloaderCli.ValidateFlags(&cliFlags)
44+
if err != nil {
45+
return cliFlags, err
46+
}
47+
return cliFlags, err
48+
}

cmd/downloader/cli/cli.go

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cli
22

33
import (
4-
"errors"
54
"flag"
65
"fmt"
76
"net/url"
@@ -15,7 +14,7 @@ import (
1514
"github.com/peterbourgon/ff/v3"
1615
)
1716

18-
func validateFlags(flags *types.CliFlags) error {
17+
func ValidateFlags(flags *types.CliFlags) error {
1918
if !utils.IsSupportedPlatformArch(flags.Platform, flags.Architecture) {
2019
return fmt.Errorf(
2120
"invalid combination of platform+architecture detected: %s+%s",
@@ -30,8 +29,6 @@ func validateFlags(flags *types.CliFlags) error {
3029
}
3130
if manifestURL.Scheme == "https" {
3231
flags.ManifestURL = true
33-
} else if len(flags.ExecCommand) == 0 {
34-
return errors.New("invalid path/url to manifest file")
3532
}
3633
}
3734
if info, err := os.Stat(flags.DownloadPath); !(err == nil && info.IsDir()) {
@@ -47,38 +44,12 @@ func validateFlags(flags *types.CliFlags) error {
4744
// with useful values set after parsing the same.
4845
func GetCliFlags(buildFlags types.BuildFlags) (types.CliFlags, error) {
4946
cliFlags := types.CliFlags{}
50-
args := []string{}
51-
// Handle post-exec string
52-
for i, arg := range os.Args[1:] {
53-
if arg == "--" {
54-
cliFlags.ExecCommand = os.Args[i+2:]
55-
break
56-
}
57-
args = append(args, arg)
58-
}
47+
5948
flag.Set("logtostderr", "true")
6049
vFlag := flag.Lookup("v")
6150
fs := flag.NewFlagSet(constants.AppName, flag.ExitOnError)
6251

63-
goos := runtime.GOOS
64-
if os.Getenv("GOOS") != "" {
65-
goos = os.Getenv("GOOS")
66-
}
67-
68-
goarch := runtime.GOARCH
69-
if os.Getenv("GOARCH") != "" {
70-
goarch = os.Getenv("GOARCH")
71-
}
72-
73-
fs.StringVar(&cliFlags.Verbosity, "v", "3", "Log verbosity. Integer value from 0 to 9")
74-
fs.StringVar(&cliFlags.Platform, "platform", goos, "One of linux/windows/darwin")
75-
fs.StringVar(&cliFlags.Architecture, "architecture", goarch, "System architecture (amd64/arm64)")
76-
fs.StringVar(&cliFlags.DownloadPath, "path", fmt.Sprintf(".%sbin", string(os.PathSeparator)), "Path to store binaries")
77-
fs.StringVar(&cliFlags.ManifestFile, "manifest", "manifest.yaml", "Path (or URL) to manifest yaml file")
78-
fs.BoolVar(&cliFlags.SkipDownloaded, "skip-downloaded", false, "Skip already downloaded archive (if found)")
79-
fs.BoolVar(&cliFlags.Cleanup, "cleanup", true, "Cleanup downloaded archives after extraction")
80-
fs.BoolVar(&cliFlags.UpdateManifest, "update-manifest", false, "Update the manifest file commit shas from releases prior to downloading")
81-
fs.BoolVar(&cliFlags.Download, "download", true, "Actually do a download. Only useful for -update-manifest=true -download=false")
52+
AddDownloaderFlags(fs, &cliFlags)
8253

8354
version := fs.Bool("version", false, "Get version information")
8455

@@ -88,18 +59,40 @@ func GetCliFlags(buildFlags types.BuildFlags) (types.CliFlags, error) {
8859
}
8960

9061
ff.Parse(
91-
fs, args,
92-
ff.WithConfigFileFlag("config"),
62+
fs, os.Args[1:],
9363
ff.WithConfigFileParser(ff.PlainParser),
9464
ff.WithEnvVarPrefix("CATALYST_DOWNLOADER"),
9565
ff.WithEnvVarSplit(","),
9666
)
9767
flag.CommandLine.Parse(nil)
9868
vFlag.Value.Set(cliFlags.Verbosity)
9969

100-
err := validateFlags(&cliFlags)
70+
err := ValidateFlags(&cliFlags)
10171
if err != nil {
10272
glog.Fatal(err)
10373
}
10474
return cliFlags, err
10575
}
76+
77+
// Populate a provided flagset with downloader flags
78+
func AddDownloaderFlags(fs *flag.FlagSet, cliFlags *types.CliFlags) {
79+
goos := runtime.GOOS
80+
if os.Getenv("GOOS") != "" {
81+
goos = os.Getenv("GOOS")
82+
}
83+
84+
goarch := runtime.GOARCH
85+
if os.Getenv("GOARCH") != "" {
86+
goarch = os.Getenv("GOARCH")
87+
}
88+
89+
fs.StringVar(&cliFlags.Verbosity, "v", "3", "Log verbosity. Integer value from 0 to 9")
90+
fs.StringVar(&cliFlags.Platform, "platform", goos, "One of linux/windows/darwin")
91+
fs.StringVar(&cliFlags.Architecture, "architecture", goarch, "System architecture (amd64/arm64)")
92+
fs.StringVar(&cliFlags.DownloadPath, "path", fmt.Sprintf(".%sbin", string(os.PathSeparator)), "Path to store binaries")
93+
fs.StringVar(&cliFlags.ManifestFile, "manifest", "manifest.yaml", "Path (or URL) to manifest yaml file")
94+
fs.BoolVar(&cliFlags.SkipDownloaded, "skip-downloaded", false, "Skip already downloaded archive (if found)")
95+
fs.BoolVar(&cliFlags.Cleanup, "cleanup", true, "Cleanup downloaded archives after extraction")
96+
fs.BoolVar(&cliFlags.UpdateManifest, "update-manifest", false, "Update the manifest file commit shas from releases prior to downloading")
97+
fs.BoolVar(&cliFlags.Download, "download", true, "Actually do a download. Only useful for -update-manifest=true -download=false")
98+
}

cmd/downloader/types/types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ type CliFlags struct {
4343
Architecture string
4444
ManifestFile string
4545
Verbosity string
46-
ExecCommand []string
4746

48-
ManifestURL bool
47+
ManifestURL bool
48+
MistController string
49+
ConfigStack string
4950
}
5051

5152
type DownloadStrategy struct {

0 commit comments

Comments
 (0)