-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnode.go
More file actions
178 lines (155 loc) · 5.15 KB
/
node.go
File metadata and controls
178 lines (155 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package spamoor
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/celestiaorg/tastora/framework/docker/container"
"github.com/celestiaorg/tastora/framework/docker/internal"
"github.com/celestiaorg/tastora/framework/types"
"github.com/docker/go-connections/nat"
"go.uber.org/zap"
)
type nodeType int
func (nodeType) String() string { return "spamoor" }
type Ports struct {
Web string // web UI + /metrics
}
func defaultInternalPorts() Ports { return Ports{Web: "8080"} }
type Config struct {
DockerClient types.TastoraDockerClient
DockerNetworkID string
Logger *zap.Logger
Image container.Image
RPCHosts []string
PrivateKey string
AdditionalStartArgs []string
HostNetwork bool
}
type Node struct {
*container.Node
cfg Config
logger *zap.Logger
started bool
mu sync.Mutex
external types.Ports // HTTP field stores web/metrics host port
name string
}
func newNode(ctx context.Context, cfg Config, testName string, index int, name string) (*Node, error) {
log := cfg.Logger.With(zap.String("component", "spamoor-daemon"), zap.Int("i", index))
n := &Node{cfg: cfg, logger: log, name: name}
n.Node = container.NewNode(cfg.DockerNetworkID, cfg.DockerClient, testName, cfg.Image, "/home/spamoor", index, nodeType(0), log)
lc := container.NewLifecycle(cfg.Logger, cfg.DockerClient, n.Name())
if cfg.HostNetwork {
lc.SetHostNetwork(true)
}
n.SetContainerLifecycle(lc)
if err := n.CreateAndSetupVolume(ctx, n.Name()); err != nil {
return nil, err
}
return n, nil
}
func (n *Node) Name() string {
if n.name != "" {
return fmt.Sprintf("spamoor-%s-%d-%s", n.name, n.Index, internal.SanitizeDockerResourceName(n.TestName))
}
return fmt.Sprintf("spamoor-%d-%s", n.Index, internal.SanitizeDockerResourceName(n.TestName))
}
func (n *Node) HostName() string { return internal.CondenseHostName(n.Name()) }
func (n *Node) GetNetworkInfo(ctx context.Context) (types.NetworkInfo, error) {
internalIP, err := internal.GetContainerInternalIP(ctx, n.DockerClient, n.ContainerLifecycle.ContainerID())
if err != nil {
return types.NetworkInfo{}, err
}
return types.NetworkInfo{
Internal: types.Network{Hostname: n.HostName(), IP: internalIP, Ports: types.Ports{HTTP: defaultInternalPorts().Web}},
External: types.Network{Hostname: "0.0.0.0", Ports: n.external},
}, nil
}
func (n *Node) Start(ctx context.Context) error {
n.mu.Lock()
defer n.mu.Unlock()
if n.started {
return n.StartContainer(ctx)
}
if err := n.createNodeContainer(ctx); err != nil {
return err
}
if err := n.ContainerLifecycle.StartContainer(ctx); err != nil {
return err
}
var mapped string
if n.cfg.HostNetwork {
mapped = defaultInternalPorts().Web
} else {
hostPorts, err := n.ContainerLifecycle.GetHostPorts(ctx, defaultInternalPorts().Web+"/tcp")
if err != nil {
return err
}
mapped = internal.MustExtractPort(hostPorts[0])
}
n.external = types.Ports{HTTP: mapped}
n.started = true
// readiness wait for /metrics endpoint (best-effort)
waitHTTP(fmt.Sprintf("http://127.0.0.1:%s/metrics", n.external.HTTP), 20*time.Second)
return nil
}
// API returns a client bound to this node's exposed HTTP port.
func (n *Node) API() *API {
base := fmt.Sprintf("http://127.0.0.1:%s", n.external.HTTP)
return NewAPI(base)
}
func (n *Node) createNodeContainer(ctx context.Context) error {
p := defaultInternalPorts()
// Daemon flags only; entrypoint will be spamoor-daemon
dbPath := fmt.Sprintf("%s/%s", n.HomeDir(), "spamoor.db")
binds := n.Bind()
cmd := []string{
"--privkey", n.cfg.PrivateKey,
"--port", p.Web,
"--db", dbPath,
}
for _, h := range n.cfg.RPCHosts {
if s := strings.TrimSpace(h); s != "" {
cmd = append(cmd, "--rpchost", s)
}
}
cmd = append(cmd, n.cfg.AdditionalStartArgs...)
port := nat.Port(p.Web + "/tcp")
ports := nat.PortMap{
port: []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: ""}},
}
// IMPORTANT: override entrypoint to the daemon (absolute path inside image)
return n.CreateContainer(
ctx,
n.TestName,
n.NetworkID,
n.cfg.Image,
ports,
"",
binds,
nil,
n.HostName(),
cmd,
nil,
[]string{"/app/spamoor-daemon"}, // entrypoint override
)
}
// waitHTTP polls a URL until it succeeds or the timeout elapses.
func waitHTTP(url string, timeout time.Duration) {
deadline := time.Now().Add(timeout)
client := &http.Client{Timeout: 500 * time.Millisecond}
for time.Now().Before(deadline) {
resp, err := client.Get(url)
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 500 {
_ = resp.Body.Close()
return
}
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
time.Sleep(100 * time.Millisecond)
}
}