2024-09-17 18:54:17 +02:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"github.com/openziti/zrok/agent/agentGrpc"
|
|
|
|
"github.com/openziti/zrok/agent/proctree"
|
|
|
|
"github.com/openziti/zrok/environment"
|
|
|
|
"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{
|
|
|
|
reserved: true,
|
|
|
|
bootComplete: make(chan struct{}),
|
2024-09-25 17:06:06 +02:00
|
|
|
agent: i.agent,
|
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
|
|
|
|
|
|
|
|
shr.process, err = proctree.StartChild(shr.tail, shrCmd...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go shr.monitor()
|
|
|
|
<-shr.bootComplete
|
|
|
|
|
|
|
|
if shr.bootErr == nil {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, shr.bootErr
|
|
|
|
}
|