Skip to content

Commit

Permalink
testutil/compose: add monitoring static files (#581)
Browse files Browse the repository at this point in the history
Adds prometheus/grafana/jaeger monitoring to `compose run` command. The static assets are embedded into the `compose` binary and written to disk on `compose define`. 

Also rename `NodeOnly` to `Bootnode`.

category: feature
ticket: #568
  • Loading branch information
corverroos committed May 23, 2022
1 parent 7f3f30b commit 969dc9a
Show file tree
Hide file tree
Showing 13 changed files with 1,156 additions and 14 deletions.
51 changes: 49 additions & 2 deletions testutil/compose/define.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func Define(ctx context.Context, dir string, seed int, conf Config) error {
}}

data = tmplData{
NodeOnly: true,
ComposeDir: dir,
CharonImageTag: conf.ImageTag,
CharonEntrypoint: containerBinary,
Expand All @@ -118,7 +117,6 @@ func Define(ctx context.Context, dir string, seed int, conf Config) error {
// is used directly in their compose lock.

data = tmplData{
NodeOnly: true,
ComposeDir: dir,
CharonImageTag: conf.ImageTag,
CharonEntrypoint: "echo",
Expand All @@ -133,12 +131,61 @@ func Define(ctx context.Context, dir string, seed int, conf Config) error {
return err
}

if err := copyStaticFolders(dir); err != nil {
return err
}

log.Info(ctx, "Creating docker-compose.yml")
log.Info(ctx, "Create cluster definition: docker-compose up")

return writeDockerCompose(dir, data)
}

// copyStaticFolders copies the embedded static folders to the compose dir.
func copyStaticFolders(dir string) error {
const staticRoot = "static"
dirs, err := static.ReadDir(staticRoot)
if err != nil {
return errors.Wrap(err, "read dirs")
}
for _, d := range dirs {
if !d.IsDir() {
return errors.New("static files not supported")
}

if err := os.MkdirAll(path.Join(dir, d.Name()), 0o755); err != nil {
return errors.Wrap(err, "mkdir all")
}

files, err := static.ReadDir(path.Join(staticRoot, d.Name()))
if err != nil {
return errors.Wrap(err, "read files")
}

for _, f := range files {
if f.IsDir() {
return errors.New("child static dirs not supported")
}

info, err := f.Info()
if err != nil {
return errors.Wrap(err, "file info")
}

b, err := static.ReadFile(path.Join(staticRoot, d.Name(), f.Name()))
if err != nil {
return errors.Wrap(err, "read file")
}

if err := os.WriteFile(path.Join(dir, d.Name(), f.Name()), b, info.Mode()); err != nil {
return errors.Wrap(err, "write file")
}
}
}

return nil
}

func keyToENR(key *ecdsa.PrivateKey) (string, error) {
var r enr.Record
r.SetSeq(0)
Expand Down
10 changes: 5 additions & 5 deletions testutil/compose/docker-compose.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ x-node-base: &node-base
command: {{.CharonCommand}}
networks: [compose]
volumes: [{{.ComposeDir}}:/compose]
{{if not .NodeOnly }}depends_on: [bootnode] {{end}}
{{if .Bootnode }}depends_on: [bootnode] {{end}}

services:
{{ range $i, $node := .Nodes}}
Expand All @@ -25,8 +25,7 @@ services:
{{- end}}
{{- end}}
{{ end -}}

{{if not .NodeOnly }}
{{if .Bootnode }}
bootnode:
<<: *node-base
command: bootnode
Expand All @@ -36,7 +35,9 @@ services:
CHARON_DATA_DIR: /compose/bootnode
CHARON_P2P_BOOTNODES: ""
CHARON_P2P_EXTERNAL_HOSTNAME: bootnode

CHARON_P2P_TCP_ADDRESS: 0.0.0.0:16003
CHARON_P2P_UDP_ADDRESS: 0.0.0.0:16004
{{end}}
{{- range $i, $vc := .VCs}}
vc{{$i}}-{{$vc.Label}}:
{{if $vc.Build}} build: {{$vc.Build}} {{end}}
Expand All @@ -49,7 +50,6 @@ services:
volumes:
- .:/compose
{{end}}
{{end}}
{{if .Monitoring}}
prometheus:
image: prom/prometheus:latest
Expand Down
4 changes: 2 additions & 2 deletions testutil/compose/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func Lock(ctx context.Context, dir string) error {
}}

data = tmplData{
NodeOnly: true,
ComposeDir: dir,
CharonImageTag: conf.ImageTag,
CharonEntrypoint: containerBinary,
Expand All @@ -73,6 +72,7 @@ func Lock(ctx context.Context, dir string) error {
CharonImageTag: conf.ImageTag,
CharonEntrypoint: containerBinary,
CharonCommand: cmdDKG,
Bootnode: true,
Nodes: nodes,
}
default:
Expand Down Expand Up @@ -106,7 +106,7 @@ func newNodeEnvs(index int, validatorMock, beaconMock bool) []kv {
{"p2p_bootnodes", "http://bootnode:16000/enr"},
{"simnet_validator_mock", fmt.Sprintf(`"%v"`, validatorMock)},
{"simnet_beacon_mock", fmt.Sprintf(`"%v"`, beaconMock)},
{"log_level", "info"},
{"log_level", "debug"},
}
}

Expand Down
2 changes: 2 additions & 0 deletions testutil/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func Run(ctx context.Context, dir string) error {
CharonEntrypoint: containerBinary,
CharonCommand: cmdRun,
Nodes: nodes,
Bootnode: true,
Monitoring: true,
}

log.Info(ctx, "Created docker-compose.yml")
Expand Down
8 changes: 8 additions & 0 deletions testutil/compose/static/grafana/dashboards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: 1

providers:
- name: dashboards
type: file
updateIntervalSeconds: 30
options:
path: /etc/dashboards
44 changes: 44 additions & 0 deletions testutil/compose/static/grafana/datasource.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# config file version
apiVersion: 1

# list of datasources that should be deleted from the database
deleteDatasources:
- name: Prometheus
orgId: 1

# list of datasources to insert/update depending
# whats available in the database
datasources:
# <string, required> name of the datasource. Required
- name: Prometheus
# <string, required> datasource type. Required
type: prometheus
# <int> org id. will default to orgId 1 if not specified
orgId: 1
# <string> url
url: http://prometheus:9090
# <string> database password, if used
password:
# <string> database user, if used
user:
# <string> database name, if used
database:
# <bool> enable/disable basic auth
basicAuth: false
# <bool> enable/disable with credentials headers
withCredentials:
# <bool> mark as default datasource. Max one per org
isDefault: true
# <map> fields that will be converted to json and stored in json_data
jsonData:
graphiteVersion: "1.1"
tlsAuth: false
tlsAuthWithCACert: false
# <string> json object of data that will be encrypted.
secureJsonData:
tlsCACert: "..."
tlsClientCert: "..."
tlsClientKey: "..."
version: 1
# <bool> allow users to edit datasources from the UI.
editable: true
6 changes: 6 additions & 0 deletions testutil/compose/static/grafana/grafana.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[auth.anonymous]
enabled = true
org_role = Admin

[log]
level = warn
Loading

0 comments on commit 969dc9a

Please sign in to comment.