2022-10-26 22:23:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
2023-09-05 21:14:40 +00:00
|
|
|
"isle/bootstrap"
|
2022-10-26 22:23:39 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2022-10-26 22:37:03 +00:00
|
|
|
func loadHostBootstrap() (bootstrap.Bootstrap, error) {
|
2022-10-26 22:23:39 +00:00
|
|
|
|
2024-06-24 12:45:57 +00:00
|
|
|
stateDirPath := bootstrap.StateDirPath(daemonEnvVars.StateDirPath)
|
2022-10-26 22:23:39 +00:00
|
|
|
|
2024-06-17 12:13:53 +00:00
|
|
|
hostBootstrap, err := bootstrap.FromFile(stateDirPath)
|
2022-10-26 22:23:39 +00:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2023-09-05 21:14:40 +00:00
|
|
|
return bootstrap.Bootstrap{}, fmt.Errorf(
|
|
|
|
"%q not found, has the daemon ever been run?",
|
2024-06-17 12:13:53 +00:00
|
|
|
stateDirPath,
|
2023-09-05 21:14:40 +00:00
|
|
|
)
|
2022-10-26 22:23:39 +00:00
|
|
|
|
|
|
|
} else if err != nil {
|
2024-06-17 12:13:53 +00:00
|
|
|
return bootstrap.Bootstrap{}, fmt.Errorf("loading %q: %w", stateDirPath, err)
|
2022-10-26 22:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hostBootstrap, nil
|
|
|
|
}
|
|
|
|
|
2024-06-17 20:15:28 +00:00
|
|
|
func writeBootstrapToStateDir(hostBootstrap bootstrap.Bootstrap) error {
|
2022-10-26 22:23:39 +00:00
|
|
|
|
2024-06-24 12:45:57 +00:00
|
|
|
path := bootstrap.StateDirPath(daemonEnvVars.StateDirPath)
|
2022-10-26 22:23:39 +00:00
|
|
|
dirPath := filepath.Dir(path)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dirPath, 0700); err != nil {
|
|
|
|
return fmt.Errorf("creating directory %q: %w", dirPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating file %q: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return hostBootstrap.WriteTo(f)
|
|
|
|
}
|