@ -14,6 +14,8 @@ import (
"cryptic-net/daemon"
"code.betamike.com/cryptic-io/pmux/pmuxlib"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
)
// The daemon sub-command deals with starting an actual cryptic-net daemon
@ -40,6 +42,7 @@ import (
// is overwritten and true is returned.
func reloadBootstrap (
ctx context . Context ,
logger * mlog . Logger ,
hostBootstrap bootstrap . Bootstrap ,
) (
bootstrap . Bootstrap , bool , error ,
@ -47,7 +50,7 @@ func reloadBootstrap(
thisHost := hostBootstrap . ThisHost ( )
newHosts , err := hostBootstrap . GetGarageBootstrapHosts ( ctx )
newHosts , err := hostBootstrap . GetGarageBootstrapHosts ( ctx , logger )
if err != nil {
return bootstrap . Bootstrap { } , false , fmt . Errorf ( "getting hosts from garage: %w" , err )
}
@ -85,6 +88,7 @@ func reloadBootstrap(
// updated boostrap info.
func runDaemonPmuxOnce (
ctx context . Context ,
logger * mlog . Logger ,
hostBootstrap bootstrap . Bootstrap ,
daemonConfig daemon . Config ,
) (
@ -128,13 +132,13 @@ func runDaemonPmuxOnce(
pmuxlib . Run ( ctx , os . Stdout , os . Stderr , pmuxConfig )
} ( )
if err := waitForGarageAndNebula ( ctx , hostBootstrap , daemonConfig ) ; err != nil {
if err := waitForGarageAndNebula ( ctx , logger , hostBootstrap , daemonConfig ) ; err != nil {
return bootstrap . Bootstrap { } , fmt . Errorf ( "waiting for nebula/garage to start up: %w" , err )
}
err = doOnce ( ctx , func ( ctx context . Context ) error {
if err := hostBootstrap . PutGarageBoostrapHost ( ctx ) ; err != nil {
fmt . Fprintf ( os . Stderr , "updating host info in garage: %v\n " , err )
logger . Error ( ctx , "updating host info in garage" , err )
return err
}
@ -148,8 +152,8 @@ func runDaemonPmuxOnce(
if len ( daemonConfig . Storage . Allocations ) > 0 {
err := doOnce ( ctx , func ( ctx context . Context ) error {
if err := garageApplyLayout ( ctx , hostBootstrap , daemonConfig ) ; err != nil {
fmt . Fprintf ( os . Stderr , "applying garage layout: %v\n " , err )
if err := garageApplyLayout ( ctx , logger , hostBootstrap , daemonConfig ) ; err != nil {
logger . Error ( ctx , "applying garage layout" , err )
return err
}
@ -179,7 +183,7 @@ func runDaemonPmuxOnce(
err error
)
if hostBootstrap , changed , err = reloadBootstrap ( ctx , hostBootstrap ) ; err != nil {
if hostBootstrap , changed , err = reloadBootstrap ( ctx , logger , hostBootstrap ) ; err != nil {
return bootstrap . Bootstrap { } , fmt . Errorf ( "reloading bootstrap: %w" , err )
} else if changed {
@ -212,15 +216,29 @@ var subCmdDaemon = subCmd{
` Path to a bootstrap.yml file. This only needs to be provided the first time the daemon is started, after that it is ignored. If the cryptic-net binary has a bootstrap built into it then this argument is always optional. ` ,
)
logLevelStr := flags . StringP (
"log-level" , "l" , "info" ,
` Maximum log level which should be output. Values can be "debug", "info", "warn", "error", "fatal". Does not apply to sub-processes ` ,
)
if err := flags . Parse ( subCmdCtx . args ) ; err != nil {
return fmt . Errorf ( "parsing flags: %w" , err )
}
ctx := subCmdCtx . ctx
if * dumpConfig {
return daemon . CopyDefaultConfig ( os . Stdout , envAppDirPath )
}
runtimeDirCleanup , err := setupAndLockRuntimeDir ( )
logLevel := mlog . LevelFromString ( * logLevelStr )
if logLevel == nil {
return fmt . Errorf ( "couldn't parse log level %q" , * logLevelStr )
}
logger := subCmdCtx . logger . WithMaxLevel ( logLevel . Int ( ) )
runtimeDirCleanup , err := setupAndLockRuntimeDir ( ctx , logger )
if err != nil {
return fmt . Errorf ( "setting up runtime directory: %w" , err )
}
@ -249,7 +267,11 @@ var subCmdDaemon = subCmd{
return false
}
fmt . Fprintf ( os . Stderr , "bootstrap file found at %q\n" , path )
logger . Info (
mctx . Annotate ( ctx , "bootstrap-file-path" , path ) ,
"bootstrap file found" ,
)
hostBootstrapPath = path
return true
}
@ -289,7 +311,7 @@ var subCmdDaemon = subCmd{
for {
hostBootstrap , err = runDaemonPmuxOnce ( subCmdCtx . ctx , hostBootstrap , daemonConfig )
hostBootstrap , err = runDaemonPmuxOnce ( ctx , logger , hostBootstrap , daemonConfig )
if errors . Is ( err , context . Canceled ) {
return nil