Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --env cli flag and --openui #1139

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 56 additions & 30 deletions cmd/renterd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -68,8 +69,9 @@ Usage:

var (
cfg = config.Config{
Directory: ".",
Seed: os.Getenv("RENTERD_SEED"),
Directory: ".",
Seed: os.Getenv("RENTERD_SEED"),
AutoOpenWebUI: true,
HTTP: config.HTTP{
Address: build.DefaultAPIAddress,
Password: os.Getenv("RENTERD_API_PASSWORD"),
Expand Down Expand Up @@ -143,15 +145,9 @@ var (
KeypairsV4: nil,
},
}
seed types.PrivateKey
disableStdin bool
)

func check(context string, err error) {
if err != nil {
log.Fatalf("%v: %v", context, err)
}
}

func mustLoadAPIPassword() {
if cfg.HTTP.Password != "" {
return
Expand All @@ -166,25 +162,6 @@ func mustLoadAPIPassword() {
cfg.HTTP.Password = string(pw)
}

func getSeed() types.PrivateKey {
if seed == nil {
phrase := cfg.Seed
if phrase == "" {
fmt.Print("Enter seed: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
check("Could not read seed phrase:", err)
fmt.Println()
phrase = string(pw)
}
var rawSeed [32]byte
if err := wallet.SeedFromPhrase(&rawSeed, phrase); err != nil {
panic(err)
}
seed = wallet.KeyFromSeed(&rawSeed, 0)
}
return seed
}

func mustParseWorkers(workers, password string) {
if workers == "" {
return
Expand Down Expand Up @@ -271,6 +248,8 @@ func main() {
// node
flag.StringVar(&cfg.HTTP.Address, "http", cfg.HTTP.Address, "Address for serving the API")
flag.StringVar(&cfg.Directory, "dir", cfg.Directory, "Directory for storing node state")
flag.BoolVar(&disableStdin, "env", false, "disable stdin prompts for environment variables (default false)")
flag.BoolVar(&cfg.AutoOpenWebUI, "openui", cfg.AutoOpenWebUI, "automatically open the web UI on startup")

// logger
flag.StringVar(&cfg.Log.Level, "log.level", cfg.Log.Level, "Global logger level (debug|info|warn|error). Defaults to 'info' (overrides with RENTERD_LOG_LEVEL)")
Expand Down Expand Up @@ -410,6 +389,30 @@ func main() {
parseEnvVar("RENTERD_LOG_DATABASE_IGNORE_RECORD_NOT_FOUND_ERROR", &cfg.Log.Database.IgnoreRecordNotFoundError)
parseEnvVar("RENTERD_LOG_DATABASE_SLOW_THRESHOLD", &cfg.Log.Database.SlowThreshold)

// check that the API password is set
if cfg.HTTP.Password == "" {
if disableStdin {
stdoutFatalError("API password must be set via environment variable or config file when --env flag is set")
return
}
setAPIPassword()
}

// check that the seed is set
if cfg.Seed == "" {
if disableStdin {
stdoutFatalError("Seed must be set via environment variable or config file when --env flag is set")
return
}
setSeedPhrase()
}

var rawSeed [32]byte
if err := wallet.SeedFromPhrase(&rawSeed, cfg.Seed); err != nil {
log.Fatal("failed to load wallet", zap.Error(err))
}
seed := wallet.KeyFromSeed(&rawSeed, 0)

if cfg.S3.Enabled {
var keyPairsV4 string
parseEnvVar("RENTERD_S3_KEYPAIRS_V4", &keyPairsV4)
Expand Down Expand Up @@ -541,7 +544,7 @@ func main() {

busAddr, busPassword := cfg.Bus.RemoteAddr, cfg.Bus.RemotePassword
if cfg.Bus.RemoteAddr == "" {
b, fn, err := node.NewBus(busCfg, cfg.Directory, getSeed(), logger)
b, fn, err := node.NewBus(busCfg, cfg.Directory, seed, logger)
if err != nil {
logger.Fatal("failed to create bus, err: " + err.Error())
}
Expand All @@ -566,7 +569,7 @@ func main() {
var workers []autopilot.Worker
if len(cfg.Worker.Remotes) == 0 {
if cfg.Worker.Enabled {
w, fn, err := node.NewWorker(cfg.Worker, bc, getSeed(), logger)
w, fn, err := node.NewWorker(cfg.Worker, bc, seed, logger)
if err != nil {
logger.Fatal("failed to create worker: " + err.Error())
}
Expand Down Expand Up @@ -682,6 +685,16 @@ func main() {
}
}

if cfg.AutoOpenWebUI {
time.Sleep(time.Millisecond) // give the web server a chance to start
_, port, err := net.SplitHostPort(l.Addr().String())
if err != nil {
logger.Debug("failed to parse API address", zap.Error(err))
} else if err := openBrowser(fmt.Sprintf("http://127.0.0.1:%s", port)); err != nil {
logger.Debug("failed to open browser", zap.Error(err))
}
}

signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
select {
Expand Down Expand Up @@ -714,6 +727,19 @@ func main() {
os.Exit(exitCode)
}

func openBrowser(url string) error {
switch runtime.GOOS {
case "linux":
return exec.Command("xdg-open", url).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
return exec.Command("open", url).Start()
default:
return fmt.Errorf("unsupported platform %q", runtime.GOOS)
}
}

func runCompatMigrateAutopilotJSONToStore(bc *bus.Client, id, dir string) (err error) {
// check if the file exists
path := filepath.Join(dir, "autopilot.json")
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
type (
// Config contains the configuration for a renterd node
Config struct {
Seed string `yaml:"seed,omitempty"`
Directory string `yaml:"directory,omitempty"`
Seed string `yaml:"seed,omitempty"`
Directory string `yaml:"directory,omitempty"`
AutoOpenWebUI bool `yaml:"autoOpenWebUI,omitempty"`

ShutdownTimeout time.Duration `yaml:"shutdownTimeout,omitempty"`

Expand Down
4 changes: 2 additions & 2 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

if [[ "$BUILD_TAGS" == *'testnet'* ]]; then
exec renterd -http=':9880' -s3.address=':7070' "$@"
exec renterd -env -http=':9880' -s3.address=':7070' "$@"
else
exec renterd -http=':9980' -s3.address=':8080' "$@"
exec renterd -env -http=':9980' -s3.address=':8080' "$@"
fi
Loading