Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/pkg/integration/agent.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package integration
6
7
import (
8
"flag"
9
"fmt"
10
"log"
11
"net"
12
"net/http"
13
"net/rpc"
14
"os"
15
"os/signal"
16
"strconv"
17
"syscall"
18
)
19
20
// ServeAgent is the main entrypoint for agents. It establishes flags and starts an RPC server
21
// on a port passed as flag.
22
func ServeAgent(done chan struct{}, rcvr interface{}) {
23
defaultPort, _ := strconv.Atoi(os.Getenv("AGENT_RPC_PORT"))
24
port := flag.Int("rpc-port", defaultPort, "the port on wich to run the RPC server on")
25
flag.Parse()
26
27
ta := &testAgent{
28
Done: done,
29
}
30
31
err := rpc.RegisterName("TestAgent", ta)
32
if err != nil {
33
log.Fatalf("cannot register test agent service: %q", err)
34
}
35
err = rpc.Register(rcvr)
36
if err != nil {
37
log.Fatalf("cannot register agent service: %q", err)
38
}
39
rpc.HandleHTTP()
40
addr := fmt.Sprintf(":%d", *port)
41
l, err := net.Listen("tcp", addr)
42
if err != nil {
43
log.Fatalf("cannot start RPC server on :%d", *port)
44
}
45
46
go func() {
47
err := http.Serve(l, nil)
48
if err != nil {
49
log.Fatalf("cannot start RPC server on :%d", *port)
50
}
51
}()
52
53
fmt.Printf("agent running on %s\n", addr)
54
sigChan := make(chan os.Signal, 1)
55
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
56
57
select {
58
case <-sigChan:
59
case <-done:
60
}
61
fmt.Println("shutting down")
62
}
63
64
type testAgent struct {
65
Done chan struct{}
66
}
67
68
const (
69
// MethodTestAgentShutdown refers to the shutdown method of the TestAgent service
70
MethodTestAgentShutdown = "TestAgent.Shutdown"
71
)
72
73
// TestAgentShutdownRequest are the arguments for MethodTestAgentShutdown
74
type TestAgentShutdownRequest struct{}
75
76
// TestAgentShutdownResponse is the response of MethodTestAgentShutdown
77
type TestAgentShutdownResponse struct{}
78
79
func (t *testAgent) Shutdown(args *TestAgentShutdownRequest, reply *TestAgentShutdownResponse) error {
80
close(t.Done)
81
*reply = TestAgentShutdownResponse{}
82
return nil
83
}
84
85