refactor: make seed/password public to be called, and create a chan to be checked with Ready to see if the renter is ready

This commit is contained in:
Derrick Hammer 2023-05-04 04:12:26 -04:00
parent 5ad91ad263
commit 7bb7edb7d9
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 32 additions and 9 deletions

View File

@ -47,7 +47,10 @@ var (
// fetched once, then cached // fetched once, then cached
apiPassword *string apiPassword *string
apiAddr *string
seed *types.PrivateKey seed *types.PrivateKey
ready = make(chan bool)
readyFired = false
) )
func check(context string, err error) { func check(context string, err error) {
@ -56,7 +59,7 @@ func check(context string, err error) {
} }
} }
func getAPIPassword() string { func GetAPIPassword() string {
if apiPassword == nil { if apiPassword == nil {
pw := os.Getenv("RENTERD_API_PASSWORD") pw := os.Getenv("RENTERD_API_PASSWORD")
if pw != "" { if pw != "" {
@ -76,7 +79,7 @@ func getAPIPassword() string {
return *apiPassword return *apiPassword
} }
func getSeed() types.PrivateKey { func GetSeed() types.PrivateKey {
if seed == nil { if seed == nil {
phrase := os.Getenv("RENTERD_SEED") phrase := os.Getenv("RENTERD_SEED")
if phrase != "" { if phrase != "" {
@ -156,12 +159,14 @@ func Main() {
apiPassword string apiPassword string
node.WorkerConfig node.WorkerConfig
} }
workerCfg.ContractLockTimeout = 30 * time.Second
var autopilotCfg struct { var autopilotCfg struct {
enabled bool enabled bool
node.AutopilotConfig node.AutopilotConfig
} }
apiAddr := flag.String("http", build.DefaultAPIAddress, "address to serve API on") apiAddr = flag.String("http", build.DefaultAPIAddress, "address to serve API on")
tracingEnabled := flag.Bool("tracing-enabled", false, "Enables tracing through OpenTelemetry. If RENTERD_TRACING_ENABLED is set, it overwrites the CLI flag's value. Tracing can be configured using the standard OpenTelemetry environment variables. https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/protocol/exporter.md") tracingEnabled := flag.Bool("tracing-enabled", false, "Enables tracing through OpenTelemetry. If RENTERD_TRACING_ENABLED is set, it overwrites the CLI flag's value. Tracing can be configured using the standard OpenTelemetry environment variables. https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/protocol/exporter.md")
tracingServiceInstanceId := flag.String("tracing-service-instance-id", "cluster", "ID of the service instance used for tracing. If RENTERD_TRACING_SERVICE_INSTANCE_ID is set, it overwrites the CLI flag's value.") tracingServiceInstanceId := flag.String("tracing-service-instance-id", "cluster", "ID of the service instance used for tracing. If RENTERD_TRACING_SERVICE_INSTANCE_ID is set, it overwrites the CLI flag's value.")
dir := flag.String("dir", ".", "directory to store node state in") dir := flag.String("dir", ".", "directory to store node state in")
@ -179,7 +184,8 @@ func Main() {
flag.DurationVar(&workerCfg.SessionTTL, "worker.sessionTTL", 2*time.Minute, "the time a host session is valid for before reconnecting") flag.DurationVar(&workerCfg.SessionTTL, "worker.sessionTTL", 2*time.Minute, "the time a host session is valid for before reconnecting")
flag.DurationVar(&workerCfg.DownloadSectorTimeout, "worker.downloadSectorTimeout", 3*time.Second, "timeout applied to sector downloads when downloading a slab") flag.DurationVar(&workerCfg.DownloadSectorTimeout, "worker.downloadSectorTimeout", 3*time.Second, "timeout applied to sector downloads when downloading a slab")
flag.DurationVar(&workerCfg.UploadSectorTimeout, "worker.uploadSectorTimeout", 5*time.Second, "timeout applied to sector uploads when uploading a slab") flag.DurationVar(&workerCfg.UploadSectorTimeout, "worker.uploadSectorTimeout", 5*time.Second, "timeout applied to sector uploads when uploading a slab")
flag.IntVar(&workerCfg.UploadMaxOverdrive, "worker.uploadMaxOverdrive", 5, "maximum number of active overdrive workers when uploading a slab") flag.Uint64Var(&workerCfg.DownloadMaxOverdrive, "worker.downloadMaxOverdrive", 5, "maximum number of active overdrive workers when downloading a slab")
flag.Uint64Var(&workerCfg.UploadMaxOverdrive, "worker.uploadMaxOverdrive", 5, "maximum number of active overdrive workers when uploading a slab")
flag.DurationVar(&autopilotCfg.AccountsRefillInterval, "autopilot.accountRefillInterval", defaultAccountRefillInterval, "interval at which the autopilot checks the workers' accounts balance and refills them if necessary") flag.DurationVar(&autopilotCfg.AccountsRefillInterval, "autopilot.accountRefillInterval", defaultAccountRefillInterval, "interval at which the autopilot checks the workers' accounts balance and refills them if necessary")
flag.BoolVar(&autopilotCfg.enabled, "autopilot.enabled", true, "enable/disable the autopilot - can be overwritten using the RENTERD_AUTOPILOT_ENABLED environment variable") flag.BoolVar(&autopilotCfg.enabled, "autopilot.enabled", true, "enable/disable the autopilot - can be overwritten using the RENTERD_AUTOPILOT_ENABLED environment variable")
flag.DurationVar(&autopilotCfg.Heartbeat, "autopilot.heartbeat", 10*time.Minute, "interval at which autopilot loop runs") flag.DurationVar(&autopilotCfg.Heartbeat, "autopilot.heartbeat", 10*time.Minute, "interval at which autopilot loop runs")
@ -246,7 +252,7 @@ func Main() {
}) })
*apiAddr = "http://" + l.Addr().String() *apiAddr = "http://" + l.Addr().String()
auth := jape.BasicAuth(getAPIPassword()) auth := jape.BasicAuth(GetAPIPassword())
mux := treeMux{ mux := treeMux{
h: createUIHandler(), h: createUIHandler(),
sub: make(map[string]treeMux), sub: make(map[string]treeMux),
@ -262,7 +268,7 @@ func Main() {
busAddr, busPassword := busCfg.remoteAddr, busCfg.apiPassword busAddr, busPassword := busCfg.remoteAddr, busCfg.apiPassword
if busAddr == "" { if busAddr == "" {
b, shutdownFn, err := node.NewBus(busCfg.BusConfig, *dir, getSeed(), logger) b, shutdownFn, err := node.NewBus(busCfg.BusConfig, *dir, GetSeed(), logger)
if err != nil { if err != nil {
log.Fatal("failed to create bus, err: ", err) log.Fatal("failed to create bus, err: ", err)
} }
@ -270,7 +276,7 @@ func Main() {
mux.sub["/api/bus"] = treeMux{h: auth(b)} mux.sub["/api/bus"] = treeMux{h: auth(b)}
busAddr = *apiAddr + "/api/bus" busAddr = *apiAddr + "/api/bus"
busPassword = getAPIPassword() busPassword = GetAPIPassword()
} else { } else {
fmt.Println("connecting to remote bus at", busAddr) fmt.Println("connecting to remote bus at", busAddr)
} }
@ -280,7 +286,7 @@ func Main() {
workerAddrs, workerPassword := workerCfg.remoteAddrs, workerCfg.apiPassword workerAddrs, workerPassword := workerCfg.remoteAddrs, workerCfg.apiPassword
if workerAddrs == "" { if workerAddrs == "" {
if workerCfg.enabled { if workerCfg.enabled {
w, shutdownFn, err := node.NewWorker(workerCfg.WorkerConfig, bc, getSeed(), logger) w, shutdownFn, err := node.NewWorker(workerCfg.WorkerConfig, bc, GetSeed(), logger)
if err != nil { if err != nil {
log.Fatal("failed to create worker", err) log.Fatal("failed to create worker", err)
} }
@ -288,7 +294,7 @@ func Main() {
mux.sub["/api/worker"] = treeMux{h: auth(w)} mux.sub["/api/worker"] = treeMux{h: auth(w)}
workerAddr := *apiAddr + "/api/worker" workerAddr := *apiAddr + "/api/worker"
workerPassword = getAPIPassword() workerPassword = GetAPIPassword()
workers = append(workers, worker.NewClient(workerAddr, workerPassword)) workers = append(workers, worker.NewClient(workerAddr, workerPassword))
} }
} else { } else {
@ -337,6 +343,9 @@ func Main() {
signalCh := make(chan os.Signal, 1) signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM) signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
ready <- true
select { select {
case <-signalCh: case <-signalCh:
log.Println("Shutting down...") log.Println("Shutting down...")
@ -359,3 +368,17 @@ func Main() {
} }
} }
} }
func GetApiAddr() string {
return *apiAddr
}
func Ready() bool {
if readyFired {
return true
}
readyFired = <-ready
return true
}