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-12-13 12:52:39 +00:00
|
|
|
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2024-09-09 14:34:00 +00:00
|
|
|
)
|
|
|
|
|
2024-12-13 12:52:39 +00:00
|
|
|
func networkLogger(
|
|
|
|
logger *mlog.Logger, creationParams bootstrap.CreationParams,
|
|
|
|
) *mlog.Logger {
|
|
|
|
return logger.WithNamespace("networks").WithNamespace(creationParams.Domain)
|
|
|
|
}
|
|
|
|
|
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-11 14:32:15 +00:00
|
|
|
networks map[string]joinedNetwork,
|
2024-09-23 17:04:14 +00:00
|
|
|
) (
|
2024-11-11 14:32:15 +00:00
|
|
|
joinedNetwork, error,
|
2024-09-23 17:04:14 +00:00
|
|
|
) {
|
|
|
|
if len(networks) == 0 {
|
2024-11-11 14:32:15 +00:00
|
|
|
return joinedNetwork{}, ErrNoNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
networkSearchStr := getNetworkSearchStr(ctx)
|
|
|
|
if networkSearchStr == "" {
|
|
|
|
if len(networks) > 1 {
|
|
|
|
return joinedNetwork{}, ErrNoMatchingNetworks
|
|
|
|
}
|
|
|
|
for _, network := range networks {
|
|
|
|
return network, nil
|
|
|
|
}
|
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-11 14:32:15 +00:00
|
|
|
return joinedNetwork{}, fmt.Errorf("getting loadable networks: %w", err)
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
matchingNetworkIDs := make([]string, 0, len(networks))
|
2024-09-23 17:04:14 +00:00
|
|
|
for _, creationParam := range creationParams {
|
|
|
|
if networkSearchStr == "" || creationParam.Matches(networkSearchStr) {
|
|
|
|
matchingNetworkIDs = append(matchingNetworkIDs, creationParam.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matchingNetworkIDs) == 0 {
|
2024-11-11 14:32:15 +00:00
|
|
|
return joinedNetwork{}, ErrNoMatchingNetworks
|
2024-09-23 17:04:14 +00:00
|
|
|
} else if len(matchingNetworkIDs) > 1 {
|
2024-11-11 14:32:15 +00:00
|
|
|
return joinedNetwork{}, ErrMultipleMatchingNetworks
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return networks[matchingNetworkIDs[0]], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func alreadyJoined(
|
2024-12-17 10:33:19 +00:00
|
|
|
networks map[string]joinedNetwork, creationParams bootstrap.CreationParams,
|
|
|
|
) bool {
|
|
|
|
for _, network := range networks {
|
|
|
|
if network.creationParams.Conflicts(creationParams) {
|
|
|
|
return true
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 10:33:19 +00:00
|
|
|
return false
|
2024-09-23 17:04:14 +00:00
|
|
|
}
|