2022-10-26 21:21:31 +00:00
|
|
|
package daemon
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
import (
|
2024-12-14 14:57:07 +00:00
|
|
|
"context"
|
2024-09-07 13:11:04 +00:00
|
|
|
"errors"
|
2024-06-17 18:51:02 +00:00
|
|
|
"fmt"
|
2024-09-07 13:11:04 +00:00
|
|
|
"io/fs"
|
2024-09-10 20:51:33 +00:00
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/daemon/daecommon"
|
2024-12-14 14:57:07 +00:00
|
|
|
"isle/daemon/network"
|
2024-06-17 18:51:02 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-09-07 13:11:04 +00:00
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2024-06-17 18:51:02 +00:00
|
|
|
)
|
|
|
|
|
2024-09-07 13:11:04 +00:00
|
|
|
func getDefaultHTTPSocketDirPath() string {
|
|
|
|
path, err := firstExistingDir(
|
|
|
|
"/tmp",
|
|
|
|
"/run",
|
|
|
|
"/var/run",
|
|
|
|
"/dev/shm",
|
2022-10-26 19:47:39 +00:00
|
|
|
)
|
2024-06-17 18:51:02 +00:00
|
|
|
if err != nil {
|
2024-09-07 13:11:04 +00:00
|
|
|
panic(fmt.Sprintf("Failed to find directory for HTTP socket: %v", err))
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 13:11:04 +00:00
|
|
|
return path
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 13:11:04 +00:00
|
|
|
// HTTPSocketPath returns the path to the daemon's HTTP socket which is used for
|
|
|
|
// RPC and other functionality.
|
|
|
|
var HTTPSocketPath = sync.OnceValue(func() string {
|
|
|
|
return envOr(
|
|
|
|
"ISLE_DAEMON_HTTP_SOCKET_PATH",
|
|
|
|
func() string {
|
|
|
|
return filepath.Join(
|
|
|
|
getDefaultHTTPSocketDirPath(), "isle-daemon.sock",
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2024-06-17 18:51:02 +00:00
|
|
|
|
2024-09-10 20:51:33 +00:00
|
|
|
func pickNetworkConfig(
|
2024-12-14 14:57:07 +00:00
|
|
|
networkConfigs map[string]daecommon.NetworkConfig,
|
|
|
|
creationParams bootstrap.CreationParams,
|
2024-11-11 14:32:15 +00:00
|
|
|
) *daecommon.NetworkConfig {
|
2024-12-14 14:57:07 +00:00
|
|
|
if len(networkConfigs) == 1 { // DEPRECATED
|
|
|
|
if c, ok := networkConfigs[daecommon.DeprecatedNetworkID]; ok {
|
2024-11-11 14:32:15 +00:00
|
|
|
return &c
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
2024-09-10 20:51:33 +00:00
|
|
|
}
|
|
|
|
|
2024-12-14 14:57:07 +00:00
|
|
|
for searchStr, networkConfig := range networkConfigs {
|
2024-09-23 17:04:14 +00:00
|
|
|
if creationParams.Matches(searchStr) {
|
2024-11-11 14:32:15 +00:00
|
|
|
return &networkConfig
|
2024-09-10 20:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
return nil
|
2024-09-10 20:51:33 +00:00
|
|
|
}
|
|
|
|
|
2024-12-14 14:57:07 +00:00
|
|
|
func validateConfig(
|
|
|
|
ctx context.Context,
|
|
|
|
networkLoader network.Loader,
|
|
|
|
daemonConfig daecommon.Config,
|
|
|
|
loadableNetworks []bootstrap.CreationParams,
|
|
|
|
) error {
|
|
|
|
givenConfigs := daemonConfig.Networks
|
|
|
|
daemonConfig.Networks = map[string]daecommon.NetworkConfig{}
|
|
|
|
|
|
|
|
for _, creationParams := range loadableNetworks {
|
|
|
|
id := creationParams.ID
|
|
|
|
|
|
|
|
if c := pickNetworkConfig(givenConfigs, creationParams); c != nil {
|
|
|
|
daemonConfig.Networks[id] = *c
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := networkLoader.StoredConfig(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting stored config for %q: %w", id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
daemonConfig.Networks[id] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return daemonConfig.Validate()
|
|
|
|
}
|
|
|
|
|
2024-09-07 13:11:04 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Jigs
|
2024-06-17 18:51:02 +00:00
|
|
|
|
2024-09-07 13:11:04 +00:00
|
|
|
func envOr(name string, fallback func() string) string {
|
|
|
|
if v := os.Getenv(name); v != "" {
|
|
|
|
return v
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
2024-09-07 13:11:04 +00:00
|
|
|
return fallback()
|
|
|
|
}
|
2024-06-17 18:51:02 +00:00
|
|
|
|
2024-09-07 13:11:04 +00:00
|
|
|
func firstExistingDir(paths ...string) (string, error) {
|
|
|
|
var errs []error
|
|
|
|
for _, path := range paths {
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, fs.ErrExist):
|
|
|
|
continue
|
|
|
|
case err != nil:
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("checking if path %q exists: %w", path, err),
|
|
|
|
)
|
|
|
|
case !stat.IsDir():
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("path %q exists but is not a directory", path),
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return path, nil
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 13:11:04 +00:00
|
|
|
err := fmt.Errorf(
|
|
|
|
"no directory found at any of the following paths: %s",
|
|
|
|
strings.Join(paths, ", "),
|
|
|
|
)
|
|
|
|
if len(errs) > 0 {
|
|
|
|
err = errors.Join(slices.Insert(errs, 0, err)...)
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
2024-09-07 13:11:04 +00:00
|
|
|
return "", err
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|