Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bump deps and fix tooling #113

Merged
merged 15 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
tidy:
@go mod tidy
@cd ./core && go mod tidy
@cd ./cosmos && go mod tidy

test:
@go test ./... -race
@cd ./core && go test ./... -race
@cd ./cosmos && go test ./... -race

govulncheck:
@echo "--> Running govulncheck"
@go run golang.org/x/vuln/cmd/govulncheck -test ./...

lint:
@echo "--> Running linter"
@go run github.com/golangci/golangci-lint/cmd/golangci-lint run --out-format=tab
@echo "--> Running linter for core pkg..."
@cd ./core && go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest run --out-format=tab
@echo "--> Running linter for cosmos pkg..."
@cd ./cosmos && go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest run --out-format=tab

lint-fix:
@echo "--> Running linter"
@go run github.com/golangci/golangci-lint/cmd/golangci-lint run --fix --out-format=tab --issues-exit-code=0
@echo "--> Running linter for core pkg..."
@cd ./core && go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest run --fix --out-format=tab --issues-exit-code=0
@echo "--> Running linter for cosmos pkg..."
@cd ./cosmos && go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest run --fix --out-format=tab --issues-exit-code=0

format:
@find . -name '*.go' -type f -not -path "*.git*" | xargs go run mvdan.cc/gofumpt -w .
@find . -name '*.go' -type f -not -path "*.git*" | xargs go run github.com/client9/misspell/cmd/misspell -w
@find . -name '*.go' -type f -not -path "*.git*" | xargs go run golang.org/x/tools/cmd/goimports -w -local github.com/skip-mev/petri

.PHONY: format lint-fix lint govulncheck test tidy
.PHONY: format lint-fix lint govulncheck test tidy
277 changes: 209 additions & 68 deletions core/go.mod

Large diffs are not rendered by default.

770 changes: 678 additions & 92 deletions core/go.sum

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions core/monitoring/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bytes"
"context"
_ "embed"
"github.com/skip-mev/petri/core/v2/provider"
"go.uber.org/zap"
"text/template"

"go.uber.org/zap"

"github.com/skip-mev/petri/core/v2/provider"
)

const DEFAULT_PROMETHEUS_URL = "http://prometheus:9090"
Expand Down Expand Up @@ -57,13 +59,11 @@ func SetupGrafanaTask(ctx context.Context, logger *zap.Logger, p provider.Provid
},
ProviderSpecificConfig: opts.ProviderSpecificConfig,
})

if err != nil {
return nil, err
}

err = task.WriteFile(ctx, "grafana.ini", []byte(grafanaConfig))

if err != nil {
return nil, err
}
Expand All @@ -74,19 +74,16 @@ func SetupGrafanaTask(ctx context.Context, logger *zap.Logger, p provider.Provid
}

err = task.WriteFile(ctx, "conf/provisioning/datasources/prometheus.yml", []byte(parsedGrafanaDatasourceConfig))

if err != nil {
return nil, err
}

err = task.WriteFile(ctx, "conf/provisioning/dashboards/dashboards.yml", []byte(grafanaDashboardProvisioningConfig))

if err != nil {
return nil, err
}

err = task.WriteFile(ctx, "conf/provisioning/dashboards/dashboard.json", []byte(opts.DashboardJSON))

if err != nil {
return nil, err
}
Expand All @@ -96,7 +93,6 @@ func SetupGrafanaTask(ctx context.Context, logger *zap.Logger, p provider.Provid

func parseGrafanaDatasourceTemplate(opts GrafanaOptions) (string, error) {
parsedGrafanaDatasourceTemplate, err := template.New("datasources.yml").Parse(grafanaDatasourceTemplate)

if err != nil {
return "", err
}
Expand All @@ -114,7 +110,6 @@ func parseGrafanaDatasourceTemplate(opts GrafanaOptions) (string, error) {
}

err = parsedGrafanaDatasourceTemplate.Execute(&buf, variables)

if err != nil {
return "", err
}
Expand Down
24 changes: 2 additions & 22 deletions core/monitoring/grafana_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,145 +2,125 @@ package monitoring

import (
"context"
"time"

"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"time"
)

// SnapshotGrafanaDashboard takes a snapshot of a grafana dashboard and returns the snapshot URL. This function uses
// the rod library to control a headless browser.
func SnapshotGrafanaDashboard(ctx context.Context, uid, grafanaURL string) (string, error) {
url, err := launcher.New().Headless(false).Launch()

if err != nil {
return "", err
}

browser := rod.New().ControlURL(url)

err = browser.Connect()

if err != nil {
return "", err
}

defer browser.MustClose()

loginPage, err := browser.Page(proto.TargetCreateTarget{URL: grafanaURL + "/login"})

if err != nil {
return "", err
}

err = loginPage.WaitLoad()

if err != nil {
return "", err
}

usernameInput, err := loginPage.Element("input[name='user']")

if err != nil {
return "", err
}

err = usernameInput.Input("admin")

if err != nil {
return "", err
}

passwordInput, err := loginPage.Element("input[name='password']")

if err != nil {
return "", err
}

err = passwordInput.Input("admin")

if err != nil {
return "", err
}

submitButton, err := loginPage.Element("button[type='submit']")

if err != nil {
return "", err
}

err = submitButton.Click(proto.InputMouseButtonLeft, 1)

if err != nil {
return "", err
}

time.Sleep(time.Second)

page, err := browser.Page(proto.TargetCreateTarget{URL: grafanaURL + "/d/" + uid})

if err != nil {
return "", err
}

err = page.WaitLoad()

if err != nil {
return "", err
}

shareDashboardButton, err := page.Element("[data-testid='data-testid share-button']")

if err != nil {
return "", err
}

err = shareDashboardButton.Click(proto.InputMouseButtonLeft, 1)

if err != nil {
return "", err
}

snapshotButton, err := page.Element("[aria-label='Tab Snapshot']")

if err != nil {
return "", err
}

err = snapshotButton.Click(proto.InputMouseButtonLeft, 1)

if err != nil {
return "", err
}

publishButton, err := page.ElementR("span", "Publish to snapshots.raintank.io")

if err != nil {
return "", err
}

err = publishButton.Click(proto.InputMouseButtonLeft, 1)

if err != nil {
return "", err
}

// wait for the snapshot to be created

err = page.WaitElementsMoreThan("input[id='snapshot-url-input']", 0)

if err != nil {
return "", err
}

snapshotURLInput, err := page.Element("input[id='snapshot-url-input']")

if err != nil {
return "", err
}

snapshotURL, err := snapshotURLInput.Attribute("value")

if err != nil {
return "", err
}
Expand Down
9 changes: 4 additions & 5 deletions core/monitoring/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"context"
_ "embed"
"fmt"
"github.com/skip-mev/petri/core/v2/provider"
"go.uber.org/zap"
"strings"
"text/template"

"go.uber.org/zap"

"github.com/skip-mev/petri/core/v2/provider"
)

//go:embed files/prometheus/config/prometheus.yml
Expand Down Expand Up @@ -53,7 +55,6 @@ func SetupPrometheusTask(ctx context.Context, logger *zap.Logger, p provider.Pro
}

err = task.WriteFile(ctx, "prometheus.yml", []byte(parsedPrometheusConfig))

if err != nil {
return nil, err
}
Expand All @@ -63,7 +64,6 @@ func SetupPrometheusTask(ctx context.Context, logger *zap.Logger, p provider.Pro

func parsePrometheusConfig(opts PrometheusOptions) (string, error) {
parsedPrometheusConfig, err := template.New("prometheus.yml").Parse(prometheusConfigTemplate)

if err != nil {
return "", err
}
Expand All @@ -77,7 +77,6 @@ func parsePrometheusConfig(opts PrometheusOptions) (string, error) {
}

err = parsedPrometheusConfig.Execute(&buf, variables)

if err != nil {
return "", err
}
Expand Down
1 change: 0 additions & 1 deletion core/provider/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (t *TaskDefinition) ValidateBasic() error {
}

portInt, err := strconv.ParseUint(port, 10, 64)

if err != nil {
return fmt.Errorf("port must be a valid unsigned integer")
}
Expand Down
6 changes: 4 additions & 2 deletions core/provider/definitions_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package provider_test

import (
"github.com/skip-mev/petri/core/v2/provider"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

"github.com/skip-mev/petri/core/v2/provider"
)

var validImageDefinition = provider.ImageDefinition{
Expand Down
17 changes: 4 additions & 13 deletions core/provider/digitalocean/digitalocean_provider.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package digitalocean

import (
"context"
"fmt"

"github.com/digitalocean/godo"
"github.com/puzpuzpuz/xsync/v3"
"github.com/skip-mev/petri/core/v2/provider"
"github.com/skip-mev/petri/core/v2/util"
"go.uber.org/zap"
"golang.org/x/crypto/ssh"
)

import (
"context"
"github.com/skip-mev/petri/core/v2/provider"
"github.com/skip-mev/petri/core/v2/util"
)

var _ provider.Provider = (*Provider)(nil)
Expand Down Expand Up @@ -48,7 +47,6 @@ func NewDigitalOceanProvider(ctx context.Context, logger *zap.Logger, providerNa
}

userIPs, err := getUserIPs(ctx)

if err != nil {
return nil, err
}
Expand All @@ -73,20 +71,17 @@ func NewDigitalOceanProvider(ctx context.Context, logger *zap.Logger, providerNa
logger.Debug("petri tag", zap.String("tag", digitalOceanProvider.petriTag))

_, err = digitalOceanProvider.createTag(ctx, digitalOceanProvider.petriTag)

if err != nil {
return nil, err
}

firewall, err := digitalOceanProvider.createFirewall(ctx, userIPs)

if err != nil {
return nil, fmt.Errorf("failed to create firewall: %w", err)
}

digitalOceanProvider.firewallID = firewall.ID
_, err = digitalOceanProvider.createSSHKey(ctx, sshPubKey)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,7 +110,6 @@ func (p *Provider) Teardown(ctx context.Context) error {

func (p *Provider) teardownTasks(ctx context.Context) error {
res, err := p.doClient.Droplets.DeleteByTag(ctx, p.petriTag)

if err != nil {
return err
}
Expand All @@ -129,7 +123,6 @@ func (p *Provider) teardownTasks(ctx context.Context) error {

func (p *Provider) teardownFirewall(ctx context.Context) error {
res, err := p.doClient.Firewalls.Delete(ctx, p.firewallID)

if err != nil {
return err
}
Expand All @@ -143,7 +136,6 @@ func (p *Provider) teardownFirewall(ctx context.Context) error {

func (p *Provider) teardownSSHKey(ctx context.Context) error {
res, err := p.doClient.Keys.DeleteByFingerprint(ctx, p.sshFingerprint)

if err != nil {
return err
}
Expand All @@ -157,7 +149,6 @@ func (p *Provider) teardownSSHKey(ctx context.Context) error {

func (p *Provider) teardownTag(ctx context.Context) error {
res, err := p.doClient.Tags.Delete(ctx, p.petriTag)

if err != nil {
return err
}
Expand Down
Loading
Loading