Skip to content

Commit

Permalink
Remove unused stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Aug 6, 2024
1 parent 7610ef8 commit 592efd6
Show file tree
Hide file tree
Showing 58 changed files with 1,036 additions and 6,911 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

protoc:
protoc --go_out=. --go-grpc_out=. ./internal/server/proto/*.proto
protoc --go_out=. --go-grpc_out=. ./internal/client/runner/structs/*.proto
12 changes: 4 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ require (
github.com/hashicorp/go-getter v1.7.1
github.com/hashicorp/go-hclog v1.3.0
github.com/hashicorp/go-memdb v1.3.3
github.com/mattn/go-sqlite3 v1.14.22
github.com/mitchellh/cli v1.1.4
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
github.com/mitchellh/mapstructure v1.5.0
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.37.0
github.com/ryanuber/columnize v2.1.2+incompatible
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
github.com/umbracle/babel v0.0.1
go.starlark.net v0.0.0-20230302034142-4b1e35fe2254
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/sys v0.1.0
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.28.1
)
Expand All @@ -37,10 +34,8 @@ require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
Expand All @@ -67,7 +62,6 @@ require (
github.com/kr/pretty v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
Expand All @@ -79,21 +73,23 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.1.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/oauth2 v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.100.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221025140454-527a21cfbd71 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.3.0 // indirect
)
87 changes: 2 additions & 85 deletions go.sum

Large diffs are not rendered by default.

186 changes: 186 additions & 0 deletions internal/backend/swarm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package backend

import (
"context"
"fmt"
"os"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/umbracle/vesta/internal/server/proto"
"github.com/umbracle/vesta/internal/uuid"
)

type Swarm struct {
client *client.Client
updater Updater
}

type Updater interface {
UpdateEvent(event *proto.Event2)
}

func NewSwarm(updater Updater) *Swarm {
client, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
client.NegotiateAPIVersion(context.Background())

s := &Swarm{
client: client,
updater: updater,
}
go s.trackEventUpdates()

return s
}

func (s *Swarm) trackEventUpdates() {
filters := filters.NewArgs()
filters.Add("label", "vesta=true")

msgCh, errCh := s.client.Events(context.Background(), types.EventsOptions{Filters: filters})

for {
select {
case msg := <-msgCh:
if msg.Actor.Attributes["role"] == "init-container" {
// we do not want to return attributes from this container
continue
}

deployment, ok := msg.Actor.Attributes["deployment"]
if !ok {
panic("??")
}
event := &proto.Event2{
Id: uuid.Generate(),
Deployment: deployment,
Task: msg.Actor.Attributes["name"],
Type: msg.Action,
}
s.updater.UpdateEvent(event)

case err := <-errCh:
fmt.Println("err", err)
}
}
}

func (s *Swarm) Deploy(name string, tasks map[string]*proto.Task) error {
// create the network reference
initRes := s.createNetworkContainer(name)

for name, t := range tasks {
opts, err := s.createContainerOptions(t, initRes)
if err != nil {
return err
}
body, err := s.client.ContainerCreate(context.Background(), opts.config, opts.host, opts.network, nil, name)
if err != nil {
return err
}
if err := s.client.ContainerStart(context.Background(), body.ID, types.ContainerStartOptions{}); err != nil {
return err
}
}

return nil
}

var (
networkInfraImage = "gcr.io/google_containers/pause-amd64:3.1"
)

type createContainerOptions struct {
name string
config *container.Config
host *container.HostConfig
network *network.NetworkingConfig
}

func (s *Swarm) createNetworkContainer(name string) string {
initContainerName := "init-" + name

opts := &createContainerOptions{
name: initContainerName,
config: &container.Config{
Image: networkInfraImage,
Hostname: "",
Labels: map[string]string{
"vesta": "true",
"role": "init-container",
},
},
host: &container.HostConfig{
NetworkMode: container.NetworkMode("vesta"),
},
network: &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{},
},
}

body, err := s.client.ContainerCreate(context.Background(), opts.config, opts.host, opts.network, nil, opts.name)
if err != nil {
panic(err)
}
if err := s.client.ContainerStart(context.Background(), body.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}

_, err = s.client.ContainerInspect(context.Background(), body.ID)
if err != nil {
panic(err)
}

return initContainerName
}

func (s *Swarm) createContainerOptions(task *proto.Task, network string) (*createContainerOptions, error) {
labels := map[string]string{}
for k, v := range task.Labels {
labels[k] = v
}
// append system wide labels
labels["vesta"] = "true"

config := &container.Config{
Image: task.Image + ":" + task.Tag,
Cmd: strslice.StrSlice(task.Args),
Labels: labels,
}
for k, v := range task.Env {
config.Env = append(config.Env, k+"="+v)
}

hostConfig := &container.HostConfig{
Binds: []string{},
NetworkMode: container.NetworkMode("container:" + network),
PidMode: container.PidMode("container:" + network),
}

for folder, data := range task.Data {
f, err := os.CreateTemp("", "vesta")
if err != nil {
return nil, err
}
if _, err := f.Write([]byte(data)); err != nil {
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}
hostConfig.Binds = append(hostConfig.Binds, f.Name()+":"+folder)
}

opts := &createContainerOptions{
config: config,
host: hostConfig,
}
return opts, nil
}
Loading

0 comments on commit 592efd6

Please sign in to comment.