2024-09-17 18:54:17 +02:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-11-15 18:09:20 +01:00
|
|
|
"fmt"
|
2024-09-17 18:54:17 +02:00
|
|
|
"github.com/openziti/zrok/agent/agentGrpc"
|
|
|
|
"github.com/openziti/zrok/agent/proctree"
|
2024-11-15 18:09:20 +01:00
|
|
|
"github.com/openziti/zrok/cmd/zrok/subordinate"
|
2024-09-17 18:54:17 +02:00
|
|
|
"github.com/openziti/zrok/environment"
|
2024-11-15 18:09:20 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2024-09-17 18:54:17 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-09-18 17:54:37 +02:00
|
|
|
func (i *agentGrpcImpl) ShareReserved(_ context.Context, req *agentGrpc.ShareReservedRequest) (*agentGrpc.ShareReservedResponse, error) {
|
2024-09-17 18:54:17 +02:00
|
|
|
root, err := environment.LoadRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !root.IsEnabled() {
|
|
|
|
return nil, errors.New("unable to load environment; did you 'zrok enable'?")
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:16:56 +02:00
|
|
|
shrCmd := []string{os.Args[0], "share", "reserved", "--subordinate"}
|
2024-09-17 18:54:17 +02:00
|
|
|
shr := &share{
|
2024-11-15 18:09:20 +01:00
|
|
|
reserved: true,
|
|
|
|
sub: subordinate.NewMessageHandler(),
|
|
|
|
agent: i.agent,
|
|
|
|
}
|
|
|
|
shr.sub.MessageHandler = func(msg subordinate.Message) {
|
|
|
|
logrus.Info(msg)
|
|
|
|
}
|
|
|
|
var bootErr error
|
|
|
|
shr.sub.BootHandler = func(msgType string, msg subordinate.Message) {
|
|
|
|
bootErr = shr.bootHandler(msgType, msg)
|
|
|
|
}
|
|
|
|
shr.sub.MalformedHandler = func(msg subordinate.Message) {
|
|
|
|
logrus.Error(msg)
|
2024-09-17 18:54:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.OverrideEndpoint != "" {
|
|
|
|
shrCmd = append(shrCmd, "--override-endpoint", req.OverrideEndpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Insecure {
|
|
|
|
shrCmd = append(shrCmd, "--insecure")
|
|
|
|
}
|
|
|
|
shr.insecure = req.Insecure
|
|
|
|
|
|
|
|
shrCmd = append(shrCmd, req.Token)
|
|
|
|
shr.token = req.Token
|
|
|
|
|
2024-11-15 18:09:20 +01:00
|
|
|
shr.process, err = proctree.StartChild(shr.sub.Tail, shrCmd...)
|
2024-09-17 18:54:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-11-15 18:09:20 +01:00
|
|
|
<-shr.sub.BootComplete
|
2024-09-17 18:54:17 +02:00
|
|
|
|
2024-11-15 18:09:20 +01:00
|
|
|
if bootErr == nil {
|
|
|
|
go shr.monitor()
|
2024-09-25 17:06:06 +02:00
|
|
|
i.agent.addShare <- shr
|
2024-09-18 17:54:37 +02:00
|
|
|
return &agentGrpc.ShareReservedResponse{
|
2024-09-17 18:54:17 +02:00
|
|
|
Token: shr.token,
|
|
|
|
BackendMode: string(shr.backendMode),
|
|
|
|
ShareMode: string(shr.shareMode),
|
|
|
|
FrontendEndpoints: shr.frontendEndpoints,
|
|
|
|
Target: shr.target,
|
|
|
|
}, nil
|
|
|
|
|
2024-11-15 18:09:20 +01:00
|
|
|
} else {
|
|
|
|
if err := proctree.WaitChild(shr.process); err != nil {
|
|
|
|
logrus.Errorf("error joining: %v", err)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unable to start share: %v", bootErr)
|
|
|
|
}
|
2024-09-17 18:54:17 +02:00
|
|
|
}
|