2024-09-09 14:34:00 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2024-09-23 17:04:14 +00:00
|
|
|
"context"
|
2024-09-09 14:34:00 +00:00
|
|
|
"fmt"
|
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/daemon/network"
|
|
|
|
"isle/toolkit"
|
|
|
|
)
|
|
|
|
|
|
|
|
func networkStateDir(
|
|
|
|
networksStateDir toolkit.Dir, networkID string, mayExist bool,
|
|
|
|
) (
|
|
|
|
toolkit.Dir, error,
|
|
|
|
) {
|
|
|
|
return networksStateDir.MkChildDir(networkID, mayExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkRuntimeDir(
|
|
|
|
networksRuntimeDir toolkit.Dir, networkID string, mayExist bool,
|
|
|
|
) (
|
|
|
|
toolkit.Dir, error,
|
|
|
|
) {
|
|
|
|
return networksRuntimeDir.MkChildDir(networkID, mayExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkDirs(
|
|
|
|
networksStateDir, networksRuntimeDir toolkit.Dir,
|
|
|
|
networkID string,
|
|
|
|
mayExist bool,
|
|
|
|
) (
|
|
|
|
stateDir, runtimeDir toolkit.Dir, err error,
|
|
|
|
) {
|
|
|
|
h := new(toolkit.MkDirHelper)
|
|
|
|
stateDir, _ = h.Maybe(
|
|
|
|
networkStateDir(networksStateDir, networkID, mayExist),
|
|
|
|
)
|
|
|
|
runtimeDir, _ = h.Maybe(
|
|
|
|
networkRuntimeDir(networksRuntimeDir, networkID, mayExist),
|
|
|
|
)
|
|
|
|
err = h.Err()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
// loadableNetworks returns the CreationParams for each Network which is able to
|
2024-09-09 14:34:00 +00:00
|
|
|
// be loaded.
|
2024-09-23 17:04:14 +00:00
|
|
|
func loadableNetworks(
|
2024-09-09 14:34:00 +00:00
|
|
|
networksStateDir toolkit.Dir,
|
|
|
|
) (
|
|
|
|
[]bootstrap.CreationParams, error,
|
|
|
|
) {
|
|
|
|
networkStateDirs, err := networksStateDir.ChildDirs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"listing children of %q: %w", networksStateDir.Path, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
creationParams := make([]bootstrap.CreationParams, 0, len(networkStateDirs))
|
|
|
|
|
|
|
|
for _, networkStateDir := range networkStateDirs {
|
|
|
|
thisCreationParams, err := network.LoadCreationParams(networkStateDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"loading creation params from %q: %w",
|
|
|
|
networkStateDir.Path,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
creationParams = append(creationParams, thisCreationParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
return creationParams, nil
|
|
|
|
}
|
2024-09-23 17:04:14 +00:00
|
|
|
|
|
|
|
func pickNetwork(
|
|
|
|
ctx context.Context,
|
2024-11-09 20:13:33 +00:00
|
|
|
networks map[string]joinedNetwork,
|
2024-09-23 17:04:14 +00:00
|
|
|
networksStateDir toolkit.Dir,
|
|
|
|
) (
|
2024-11-09 20:13:33 +00:00
|
|
|
joinedNetwork, error,
|
2024-09-23 17:04:14 +00:00
|
|
|
) {
|
|
|
|
if len(networks) == 0 {
|
2024-11-09 20:13:33 +00:00
|
|
|
return joinedNetwork{}, ErrNoNetwork
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
creationParams, err := loadableNetworks(networksStateDir)
|
|
|
|
if err != nil {
|
2024-11-09 20:13:33 +00:00
|
|
|
return joinedNetwork{}, fmt.Errorf("getting loadable networks: %w", err)
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
networkSearchStr = getNetworkSearchStr(ctx)
|
|
|
|
matchingNetworkIDs = make([]string, 0, len(networks))
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, creationParam := range creationParams {
|
|
|
|
if networkSearchStr == "" || creationParam.Matches(networkSearchStr) {
|
|
|
|
matchingNetworkIDs = append(matchingNetworkIDs, creationParam.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matchingNetworkIDs) == 0 {
|
2024-11-09 20:13:33 +00:00
|
|
|
return joinedNetwork{}, ErrNoMatchingNetworks
|
2024-09-23 17:04:14 +00:00
|
|
|
} else if len(matchingNetworkIDs) > 1 {
|
2024-11-09 20:13:33 +00:00
|
|
|
return joinedNetwork{}, ErrMultipleMatchingNetworks
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return networks[matchingNetworkIDs[0]], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func alreadyJoined(
|
|
|
|
ctx context.Context,
|
2024-11-09 20:13:33 +00:00
|
|
|
networks map[string]joinedNetwork,
|
2024-09-23 17:04:14 +00:00
|
|
|
creationParams bootstrap.CreationParams,
|
|
|
|
) (
|
|
|
|
bool, error,
|
|
|
|
) {
|
|
|
|
for networkID, network := range networks {
|
|
|
|
existingCreationParams, err := network.GetNetworkCreationParams(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"getting creation params of network %q: %w", networkID, err,
|
|
|
|
)
|
|
|
|
} else if existingCreationParams.Conflicts(creationParams) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|