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"
|
|
|
|
)
|
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
func pickNetwork(
|
|
|
|
ctx context.Context,
|
2024-11-10 21:13:06 +00:00
|
|
|
networkLoader network.Loader,
|
2024-11-10 21:49:36 +00:00
|
|
|
networks map[string]network.Network,
|
2024-09-23 17:04:14 +00:00
|
|
|
) (
|
2024-11-10 21:49:36 +00:00
|
|
|
network.Network, error,
|
2024-09-23 17:04:14 +00:00
|
|
|
) {
|
|
|
|
if len(networks) == 0 {
|
2024-11-10 21:49:36 +00:00
|
|
|
return nil, ErrNoNetwork
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
2024-11-10 21:13:06 +00:00
|
|
|
creationParams, err := networkLoader.Loadable(ctx)
|
2024-09-23 17:04:14 +00:00
|
|
|
if err != nil {
|
2024-11-10 21:49:36 +00:00
|
|
|
return nil, 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-10 21:49:36 +00:00
|
|
|
return nil, ErrNoMatchingNetworks
|
2024-09-23 17:04:14 +00:00
|
|
|
} else if len(matchingNetworkIDs) > 1 {
|
2024-11-10 21:49:36 +00:00
|
|
|
return nil, ErrMultipleMatchingNetworks
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return networks[matchingNetworkIDs[0]], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func alreadyJoined(
|
|
|
|
ctx context.Context,
|
2024-11-10 21:49:36 +00:00
|
|
|
networks map[string]network.Network,
|
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
|
|
|
|
}
|