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

Create local deployment using docker #19

Merged
merged 2 commits into from
Aug 19, 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
8 changes: 8 additions & 0 deletions cmd/builder/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ var commands = map[string]build.Command{
return nil
},
},
"localnet/start": {
Description: "Starts localnet",
Fn: localnetStart,
},
"localnet/remove": {
Description: "Removes localnet",
Fn: localnetRemove,
},
}
25 changes: 25 additions & 0 deletions cmd/builder/localnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"context"

"github.com/outofforest/build"

"github.com/sei-protocol/build/localnet/testgo"
"github.com/sei-protocol/build/localnet/testrust"
"github.com/sei-protocol/build/pkg/localnet"
)

func localnetStart(ctx context.Context, deps build.DepsFunc) error {
deps(buildGo, buildRust)

_, err := localnet.Start(ctx,
testgo.New(testgo.Config{Name: "testgo"}),
testrust.New(testrust.Config{Name: "testrust"}),
)
return err
}

func localnetRemove(ctx context.Context, _ build.DepsFunc) error {
return localnet.Remove(ctx)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/outofforest/build v1.16.4
github.com/outofforest/libexec v0.3.9
github.com/outofforest/logger v0.5.4
github.com/outofforest/parallel v0.2.3
github.com/outofforest/run v0.6.0
github.com/pkg/errors v0.9.1
github.com/samber/lo v1.46.0
Expand All @@ -16,7 +17,6 @@ require (

require (
github.com/outofforest/ioc/v2 v2.5.2 // indirect
github.com/outofforest/parallel v0.2.3 // indirect
github.com/ridge/must v0.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
31 changes: 31 additions & 0 deletions localnet/testgo/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package testgo

import (
"fmt"
"path/filepath"

"github.com/sei-protocol/build/pkg/localnet/infra"
"github.com/sei-protocol/build/pkg/tools"
"github.com/sei-protocol/build/pkg/tools/docker"
)

// Config stores go app config.
type Config struct {
Name string
}

// New creates new go app.
func New(config Config) *infra.App {
return &infra.App{
RunAsUser: true,
Name: config.Name,
Image: fmt.Sprintf("alpine:%s", docker.AlpineVersion),
Volumes: []infra.Volume{
{
Source: filepath.Join("bin", tools.PlatformDocker.String()),
Destination: "/usr/local/localnet/bin",
},
},
Entrypoint: "/usr/local/localnet/bin/test-go",
}
}
31 changes: 31 additions & 0 deletions localnet/testrust/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package testrust

import (
"fmt"
"path/filepath"

"github.com/sei-protocol/build/pkg/localnet/infra"
"github.com/sei-protocol/build/pkg/tools"
"github.com/sei-protocol/build/pkg/tools/docker"
)

// Config stores rust app config.
type Config struct {
Name string
}

// New creates new rust app.
func New(config Config) *infra.App {
return &infra.App{
RunAsUser: true,
Name: config.Name,
Image: fmt.Sprintf("alpine:%s", docker.AlpineVersion),
Volumes: []infra.Volume{
{
Source: filepath.Join("bin", tools.PlatformDocker.String()),
Destination: "/usr/local/localnet/bin",
},
},
Entrypoint: "/usr/local/localnet/bin/test-rust",
}
}
13 changes: 13 additions & 0 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"

"github.com/pkg/errors"
Expand Down Expand Up @@ -46,3 +47,15 @@ func OnModule(fileName string, fn func(path string) error) error {
return fn(filepath.Dir(path))
})
}

// ToolCmd returns command executing a tool available in PATH.
func ToolCmd(tool string, args []string) *exec.Cmd {
verifyTool(tool)
return exec.Command(tool, args...)
}

func verifyTool(tool string) {
if _, err := exec.LookPath(tool); err != nil {
panic(errors.Errorf("%s is not available, please install it", tool))
}
}
30 changes: 30 additions & 0 deletions pkg/localnet/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package localnet

import (
"context"

"github.com/sei-protocol/build/pkg/localnet/infra"
)

// Start starts environment.
func Start(ctx context.Context, apps ...*infra.App) (infra.AppSet, error) {
appSet := make(infra.AppSet, 0, len(apps))
for _, app := range apps {
appSet = append(appSet, app)
}

if err := infra.NewDocker().Deploy(ctx, appSet); err != nil {
return nil, err
}
return appSet, nil
}

// Stop stops environment.
func Stop(ctx context.Context, appSet infra.AppSet) error {
return infra.NewDocker().Stop(ctx, appSet)
}

// Remove removes environment.
func Remove(ctx context.Context) error {
return infra.NewDocker().Remove(ctx)
}
Loading