Skip to content

Commit 3f3c34d

Browse files
authored
Add read function for the manifest (#153)
* Add read function for the manifest * Fix
1 parent a8bbb28 commit 3f3c34d

File tree

6 files changed

+88
-36
lines changed

6 files changed

+88
-36
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/sirupsen/logrus v1.9.3
1818
github.com/spf13/cobra v1.9.1
1919
github.com/spf13/pflag v1.0.6
20+
github.com/stretchr/testify v1.10.0
2021
github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4 v1.1.3
2122
gopkg.in/yaml.v2 v2.4.0
2223
)
@@ -119,7 +120,6 @@ require (
119120
github.com/rubenv/sql-migrate v1.7.1 // indirect
120121
github.com/russross/blackfriday/v2 v2.1.0 // indirect
121122
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
122-
github.com/stretchr/testify v1.10.0 // indirect
123123
github.com/supranational/blst v0.3.14 // indirect
124124
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
125125
github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,22 @@ github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1n
305305
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
306306
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
307307
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
308+
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
308309
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
309310
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
310311
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
311312
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
312313
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
313314
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
315+
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
314316
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
315317
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
316318
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
317319
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
318320
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
319321
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
322+
github.com/onsi/gomega v1.31.0 h1:54UJxxj6cPInHS3a35wm6BK/F9nHYueZ1NVujHDrnXE=
323+
github.com/onsi/gomega v1.31.0/go.mod h1:DW9aCi7U6Yi40wNVAvT6kzFnEVEI5n3DloYBiKiT6zk=
320324
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
321325
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
322326
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
@@ -563,6 +567,7 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
563567
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
564568
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
565569
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
570+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
566571
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
567572
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
568573
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func runIt(recipe playground.Recipe) error {
277277
if !interactive {
278278
// print services info
279279
fmt.Printf("\n========= Services started =========\n")
280-
for _, ss := range svcManager.Services() {
280+
for _, ss := range svcManager.Services {
281281
ports := ss.GetPorts()
282282
sort.Slice(ports, func(i, j int) bool {
283283
return ports[i].Name < ports[j].Name

playground/local_runner.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
126126

127127
// Create the concrete instances to run
128128
instances := []*instance{}
129-
for _, service := range manifest.Services() {
129+
for _, service := range manifest.Services {
130130
component := FindComponent(service.ComponentName)
131131
if component == nil {
132132
return nil, fmt.Errorf("component not found '%s'", service.ComponentName)
@@ -192,7 +192,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
192192
}
193193

194194
tasks := map[string]*task{}
195-
for _, svc := range manifest.services {
195+
for _, svc := range manifest.Services {
196196
tasks[svc.Name] = &task{
197197
status: taskStatusPending,
198198
logs: nil,
@@ -241,8 +241,8 @@ func (d *LocalRunner) printStatus() {
241241
lineOffset := 0
242242

243243
// Get ordered service names from manifest
244-
orderedServices := make([]string, 0, len(d.manifest.services))
245-
for _, svc := range d.manifest.services {
244+
orderedServices := make([]string, 0, len(d.manifest.Services))
245+
for _, svc := range d.manifest.Services {
246246
orderedServices = append(orderedServices, svc.Name)
247247
}
248248

@@ -453,7 +453,7 @@ func (d *LocalRunner) reservePort(startPort int, protocol string) int {
453453
}
454454

455455
func (d *LocalRunner) getService(name string) *Service {
456-
for _, svc := range d.manifest.services {
456+
for _, svc := range d.manifest.Services {
457457
if svc.Name == name {
458458
return svc
459459
}
@@ -748,13 +748,13 @@ func (d *LocalRunner) generateDockerCompose() ([]byte, error) {
748748
// for each service, reserve a port on the host machine. We use this ports
749749
// both to have access to the services from localhost but also to do communication
750750
// between services running inside docker and the ones running on the host machine.
751-
for _, svc := range d.manifest.services {
751+
for _, svc := range d.manifest.Services {
752752
for _, port := range svc.Ports {
753753
port.HostPort = d.reservePort(port.Port, port.Protocol)
754754
}
755755
}
756756

757-
for _, svc := range d.manifest.services {
757+
for _, svc := range d.manifest.Services {
758758
if d.isHostService(svc.Name) {
759759
// skip services that are going to be launched on host
760760
continue
@@ -924,7 +924,7 @@ func CreatePrometheusServices(manifest *Manifest, out *output) error {
924924
},
925925
})
926926

927-
for _, c := range manifest.services {
927+
for _, c := range manifest.Services {
928928
for _, port := range c.Ports {
929929
if port.Name == "metrics" {
930930
metricsPath := "/metrics"
@@ -967,7 +967,7 @@ func CreatePrometheusServices(manifest *Manifest, out *output) error {
967967
WithPort("metrics", 9090, "tcp").
968968
WithArtifact("/data/prometheus.yaml", "prometheus.yaml")
969969
srv.ComponentName = "null" // For now, later on we can create a Prometheus component
970-
manifest.services = append(manifest.services, srv)
970+
manifest.Services = append(manifest.Services, srv)
971971

972972
return nil
973973
}
@@ -1004,7 +1004,7 @@ func (d *LocalRunner) Run() error {
10041004
// Second, start the services that are running on the host machine
10051005
errCh := make(chan error)
10061006
go func() {
1007-
for _, svc := range d.manifest.services {
1007+
for _, svc := range d.manifest.Services {
10081008
if d.isHostService(svc.Name) {
10091009
if err := d.runOnHost(svc); err != nil {
10101010
errCh <- err

playground/manifest.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package playground
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -26,8 +27,8 @@ type Recipe interface {
2627
type Manifest struct {
2728
ctx *ExContext
2829

29-
// list of services
30-
services []*Service
30+
// list of Services
31+
Services []*Service `json:"services"`
3132

3233
// overrides is a map of service name to the path of the executable to run
3334
// on the host machine instead of a container.
@@ -88,10 +89,6 @@ type ServiceReady interface {
8889
Ready(instance *instance) error
8990
}
9091

91-
func (s *Manifest) Services() []*Service {
92-
return s.services
93-
}
94-
9592
// ReleaseService is a service that can also be runned as an artifact in the host machine
9693
type ReleaseService interface {
9794
ReleaseArtifact() *release
@@ -102,7 +99,7 @@ func (s *Manifest) AddService(name string, srv ServiceGen) {
10299
service.ComponentName = srv.Name()
103100
srv.Run(service, s.ctx)
104101

105-
s.services = append(s.services, service)
102+
s.Services = append(s.Services, service)
106103
}
107104

108105
func (s *Manifest) MustGetService(name string) *Service {
@@ -114,7 +111,7 @@ func (s *Manifest) MustGetService(name string) *Service {
114111
}
115112

116113
func (s *Manifest) GetService(name string) (*Service, bool) {
117-
for _, ss := range s.services {
114+
for _, ss := range s.Services {
118115
if ss.Name == name {
119116
return ss, true
120117
}
@@ -126,7 +123,7 @@ func (s *Manifest) GetService(name string) (*Service, bool) {
126123
// - checks if all the port dependencies are met from the service description
127124
// - downloads any local release artifacts for the services that require host execution
128125
func (s *Manifest) Validate() error {
129-
for _, ss := range s.services {
126+
for _, ss := range s.Services {
130127
// validate node port references
131128
for _, nodeRef := range ss.NodeRefs {
132129
targetService, ok := s.GetService(nodeRef.Service)
@@ -156,7 +153,7 @@ func (s *Manifest) Validate() error {
156153
}
157154

158155
// validate that the mounts are correct
159-
for _, ss := range s.services {
156+
for _, ss := range s.Services {
160157
for _, fileNameRef := range ss.FilesMapped {
161158
fileLoc := filepath.Join(s.out.dst, fileNameRef)
162159

@@ -170,7 +167,7 @@ func (s *Manifest) Validate() error {
170167
}
171168

172169
// validate that the mounts are correct
173-
for _, ss := range s.services {
170+
for _, ss := range s.Services {
174171
for _, fileNameRef := range ss.FilesMapped {
175172
fileLoc := filepath.Join(s.out.dst, fileNameRef)
176173

@@ -194,10 +191,10 @@ const (
194191
// Port describes a port that a service exposes
195192
type Port struct {
196193
// Name is the name of the port
197-
Name string
194+
Name string `json:"name"`
198195

199196
// Port is the port number
200-
Port int
197+
Port int `json:"port"`
201198

202199
// Protocol (tcp or udp)
203200
Protocol string
@@ -210,10 +207,10 @@ type Port struct {
210207

211208
// NodeRef describes a reference from one service to another
212209
type NodeRef struct {
213-
Service string
214-
PortLabel string
215-
Protocol string
216-
User string
210+
Service string `json:"service"`
211+
PortLabel string `json:"port_label"`
212+
Protocol string `json:"protocol"`
213+
User string `json:"user"`
217214
}
218215

219216
// serviceLogs is a service to access the logs of the running service
@@ -501,12 +498,12 @@ func (s *Manifest) GenerateDotGraph() string {
501498

502499
// Create a map of services for easy lookup
503500
servicesMap := make(map[string]*Service)
504-
for _, ss := range s.services {
501+
for _, ss := range s.Services {
505502
servicesMap[ss.Name] = ss
506503
}
507504

508505
// Add nodes (services) with their ports as labels
509-
for _, ss := range s.services {
506+
for _, ss := range s.Services {
510507
var ports []string
511508
for _, p := range ss.Ports {
512509
ports = append(ports, fmt.Sprintf("%s:%d", p.Name, p.Port))
@@ -523,7 +520,7 @@ func (s *Manifest) GenerateDotGraph() string {
523520
b.WriteString("\n")
524521

525522
// Add edges (connections between services)
526-
for _, ss := range s.services {
523+
for _, ss := range s.Services {
527524
sourceNode := strings.ReplaceAll(ss.Name, "-", "_")
528525
for _, ref := range ss.NodeRefs {
529526
targetNode := strings.ReplaceAll(ref.Service, "-", "_")
@@ -536,7 +533,7 @@ func (s *Manifest) GenerateDotGraph() string {
536533
}
537534

538535
// Add edges for dependws_on
539-
for _, ss := range s.services {
536+
for _, ss := range s.Services {
540537
for _, dep := range ss.DependsOn {
541538
sourceNode := strings.ReplaceAll(ss.Name, "-", "_")
542539
targetNode := strings.ReplaceAll(dep.Name, "-", "_")
@@ -557,8 +554,29 @@ func saveDotGraph(svcManager *Manifest, out *output) error {
557554
}
558555

559556
func (m *Manifest) SaveJson() error {
560-
format := map[string]interface{}{
561-
"services": m.services,
557+
return m.out.WriteFile("manifest.json", m)
558+
}
559+
560+
func ReadManifest(outputFolder string) (*Manifest, error) {
561+
// read outputFolder/manifest.json file
562+
manifestFile := filepath.Join(outputFolder, "manifest.json")
563+
if _, err := os.Stat(manifestFile); os.IsNotExist(err) {
564+
return nil, fmt.Errorf("manifest file %s does not exist", manifestFile)
565+
}
566+
manifest, err := os.ReadFile(manifestFile)
567+
if err != nil {
568+
return nil, fmt.Errorf("failed to read manifest file %s: %w", manifestFile, err)
569+
}
570+
571+
// parse the manifest file
572+
var manifestData Manifest
573+
if err := json.Unmarshal(manifest, &manifestData); err != nil {
574+
return nil, fmt.Errorf("failed to parse manifest file %s: %w", manifestFile, err)
575+
}
576+
577+
// set the output folder
578+
manifestData.out = &output{
579+
dst: outputFolder,
562580
}
563-
return m.out.WriteFile("manifest.json", format)
581+
return &manifestData, nil
564582
}

playground/manifest_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package playground
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func TestNodeRefString(t *testing.T) {
@@ -56,3 +58,30 @@ func TestNodeRefString(t *testing.T) {
5658
}
5759
}
5860
}
61+
62+
func TestManifestWriteRead(t *testing.T) {
63+
out := newTestOutput(t)
64+
65+
recipe := &L1Recipe{}
66+
67+
builder := recipe.Artifacts()
68+
builder.OutputDir(out.dst)
69+
70+
artifacts, err := builder.Build()
71+
assert.NoError(t, err)
72+
73+
manifest := recipe.Apply(&ExContext{}, artifacts)
74+
assert.NoError(t, manifest.SaveJson())
75+
76+
manifest2, err := ReadManifest(out.dst)
77+
assert.NoError(t, err)
78+
79+
for _, svc := range manifest.Services {
80+
svc2 := manifest2.MustGetService(svc.Name)
81+
assert.Equal(t, svc.Name, svc2.Name)
82+
assert.Equal(t, svc.Args, svc2.Args)
83+
assert.Equal(t, svc.Env, svc2.Env)
84+
assert.Equal(t, svc.Labels, svc2.Labels)
85+
assert.Equal(t, svc.VolumesMapped, svc2.VolumesMapped)
86+
}
87+
}

0 commit comments

Comments
 (0)