Fix daemon EnvVar dirs not being created on startup

This commit is contained in:
Brian Picciano 2024-07-12 16:34:56 +02:00
parent c5e919dc86
commit 778db848c6
2 changed files with 53 additions and 6 deletions

View File

@ -147,6 +147,10 @@ func NewDaemon(
bootstrapFilePath = bootstrap.StateDirPath(d.opts.EnvVars.StateDirPath) bootstrapFilePath = bootstrap.StateDirPath(d.opts.EnvVars.StateDirPath)
) )
if err := d.opts.EnvVars.init(); err != nil {
return nil, fmt.Errorf("initializing daemon directories: %w", err)
}
currBootstrap, err := bootstrap.FromFile(bootstrapFilePath) currBootstrap, err := bootstrap.FromFile(bootstrapFilePath)
if errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
// daemon has never had a network created or joined // daemon has never had a network created or joined

View File

@ -13,17 +13,62 @@ import (
"github.com/adrg/xdg" "github.com/adrg/xdg"
) )
// DEPRECATED
//
// EnvVars are variables which are derived based on the environment which the // EnvVars are variables which are derived based on the environment which the
// process is running in. // process is running in.
//
// TODO EnvVars should be private to this package.
type EnvVars struct { type EnvVars struct {
RuntimeDirPath string RuntimeDirPath string
StateDirPath string StateDirPath string
} }
func (e EnvVars) init() error {
var errs []error
mkDir := func(path string) error {
{
parentPath := filepath.Dir(path)
parentInfo, err := os.Stat(parentPath)
if err != nil {
return fmt.Errorf("checking parent path %q: %w", parentPath, err)
} else if !parentInfo.IsDir() {
return fmt.Errorf("%q is not a directory", parentPath)
}
}
info, err := os.Stat(path)
if errors.Is(err, fs.ErrNotExist) {
// fine
} else if err != nil {
return fmt.Errorf("checking path: %w", err)
} else if !info.IsDir() {
return fmt.Errorf("path is not a directory")
} else {
return nil
}
if err := os.Mkdir(path, 0700); err != nil {
return fmt.Errorf("creating directory: %w", err)
}
return nil
}
if err := mkDir(e.RuntimeDirPath); err != nil {
errs = append(errs, fmt.Errorf(
"creating runtime directory %q: %w",
e.RuntimeDirPath,
err,
))
}
if err := mkDir(e.StateDirPath); err != nil {
errs = append(errs, fmt.Errorf(
"creating state directory %q: %w",
e.StateDirPath,
err,
))
}
return errors.Join(errs...)
}
func getDefaultHTTPSocketDirPath() string { func getDefaultHTTPSocketDirPath() string {
path, err := firstExistingDir( path, err := firstExistingDir(
"/run", "/run",
@ -51,8 +96,6 @@ var HTTPSocketPath = sync.OnceValue(func() string {
) )
}) })
// DEPRECATED
//
// GetEnvVars will return the EnvVars of the current processes, as determined by // GetEnvVars will return the EnvVars of the current processes, as determined by
// the process's environment. // the process's environment.
var GetEnvVars = sync.OnceValue(func() (v EnvVars) { var GetEnvVars = sync.OnceValue(func() (v EnvVars) {