Skip to content

Commit

Permalink
Implement deploy interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ashconnor committed Jan 27, 2022
1 parent 3b87380 commit 2eb73c3
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 191 deletions.
17 changes: 9 additions & 8 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ func (b *Builder) build(ctx context.Context, src *component.Source, ui terminal.
RebaseName: "", // TODO: Follow symbolic links
}

dir, err := os.MkdirTemp("", "waypoint-plugin-s3")
destDir, err := os.MkdirTemp("", "waypoint-plugin-s3")
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "unable to create tmp directory: %s", err)
}
defer os.RemoveAll(dir)

archive.CopyTo(content, srcInfo, dir)
archive.CopyTo(content, srcInfo, destDir)

step.Done()

Expand All @@ -168,12 +167,14 @@ func (b *Builder) build(ctx context.Context, src *component.Source, ui terminal.

step.Done()

step = sg.Add("Zipping assets...")
defer step.Abort()
// step = sg.Add("Zipping assets...")
// defer step.Abort()

dockerClient.ContainerRemove(ctx, containerResp.ID, types.ContainerRemoveOptions{Force: true})
// // TODO zip files

step.Done()
// step.Done()

return &Zip{}, nil
return &Zip{
Path: destDir,
}, nil
}
2 changes: 1 addition & 1 deletion builder/output.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package platform;
option go_package = "github.com/hashicorp/waypoint-plugin-s3/builder";

message Zip {
string location = 1;
string path = 1;
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/hashicorp/waypoint-plugin-s3
go 1.17

require (
github.com/aws/aws-sdk-go v1.15.11
github.com/docker/docker v20.10.12+incompatible
github.com/hashicorp/go-hclog v0.16.1
github.com/hashicorp/waypoint-plugin-sdk v0.0.0-20211012192505-5c78341a47e4
Expand Down Expand Up @@ -30,6 +31,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/go-ini/ini v1.25.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -43,6 +45,7 @@ require (
github.com/hashicorp/hcl/v2 v2.10.1-0.20210621220818-327f3ce2570e // indirect
github.com/hashicorp/yamux v0.0.0-20210316155119-a95892c5f864 // indirect
github.com/iancoleman/strcase v0.1.2 // indirect
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lab47/vterm v0.0.0-20201001232628-a9dd795f94c2 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/aws/aws-sdk-go v1.15.11 h1:m45+Ru/wA+73cOZXiEGLDH2d9uLN3iHqMc0/z4noDXE=
github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -309,6 +310,7 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-ini/ini v1.25.4 h1:Mujh4R/dH6YL8bxuISne3xX2+qcQ9p0IxKAP6ExWoUo=
github.com/go-ini/ini v1.25.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -410,6 +412,7 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
github.com/gookit/color v1.3.1 h1:PPD/C7sf8u2L8XQPdPgsWRoAiLQGZEZOzU3cf5IYYUk=
github.com/gookit/color v1.3.1/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/handlers v0.0.0-20150720190736-60c7bfde3e33/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
Expand Down Expand Up @@ -470,6 +473,7 @@ github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6t
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7 h1:SMvOWPJCES2GdFracYbBQh93GXac8fq7HeN6JnpduB8=
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/joefitzgerald/rainbow-reporter v0.1.0/go.mod h1:481CNgqmVHQZzdIbN52CupLJyoVwB10FQ/IQlF1pdL8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand All @@ -478,6 +482,7 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
Expand Down Expand Up @@ -697,7 +702,9 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down
150 changes: 59 additions & 91 deletions platform/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package platform
import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/waypoint-plugin-s3/registry"
"github.com/hashicorp/waypoint-plugin-sdk/component"
"github.com/hashicorp/waypoint-plugin-sdk/framework/resource"
sdk "github.com/hashicorp/waypoint-plugin-sdk/proto/gen"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type DeployConfig struct {
Region string "hcl:directory,optional"
Region string `hcl:"region,optional"`
BucketName string `hcl:"bucket_name,optional"`
}

type Platform struct {
Expand All @@ -32,12 +34,16 @@ func (p *Platform) ConfigSet(config interface{}) error {
c, ok := config.(*DeployConfig)
if !ok {
// The Waypoint SDK should ensure this never gets hit
return fmt.Errorf("Expected *DeployConfig as parameter")
return fmt.Errorf("expected *DeployConfig as parameter")
}

// validate the config
if c.Region == "" {
return fmt.Errorf("Region must be set to a valid directory")
return fmt.Errorf("region must be set to a valid AWS region")
}

if c.BucketName == "" {
return fmt.Errorf("bucket_name must be set to a valid S3 bucket")
}

return nil
Expand All @@ -51,43 +57,12 @@ func (p *Platform) getConnectContext() (interface{}, error) {
return nil, nil
}

// Resource manager will tell the Waypoint Plugin SDK how to create and delete
// certain resources for your deployments.
//
// For example, your deployment might need to create a "container" or "load balancer".
// Your plugin could implement two resources through ResourceManager and the Waypoint
// Plugin SDK will automatically create or delete these resources as well as
// obtain the defined status for them.
//
// ResourceManager can also be implemented for Release as well.
func (p *Platform) resourceManager(log hclog.Logger, dcr *component.DeclaredResourcesResp) *resource.Manager {
return resource.NewManager(
resource.WithLogger(log.Named("resource_manager")),
resource.WithValueProvider(p.getConnectContext),
resource.WithDeclaredResourcesResp(dcr),
resource.WithResource(resource.NewResource(
resource.WithName("template_example"),
resource.WithState(&Resource_Deployment{}),
resource.WithCreate(p.resourceDeploymentCreate),
resource.WithDestroy(p.resourceDeploymentDestroy),
resource.WithStatus(p.resourceDeploymentStatus),
resource.WithPlatform("template_platform"), // Update this to match your plugins platform, like Kubernetes
resource.WithCategoryDisplayHint(sdk.ResourceCategoryDisplayHint_INSTANCE_MANAGER), // This is meant for the UI to determine what kind of icon to show
)),
// NOTE: Add more resource funcs here if your plugin has more than 1 resource
)
}

// Implement Builder
// Implement Platform
func (p *Platform) DeployFunc() interface{} {
// return a function which will be called by Waypoint
return p.deploy
}

func (p *Platform) StatusFunc() interface{} {
return p.status
}

// A BuildFunc does not have a strict signature, you can define the parameters
// you need based on the Available parameters that the Waypoint SDK provides.
// Waypoint will automatically inject parameters as specified
Expand All @@ -102,7 +77,7 @@ func (p *Platform) StatusFunc() interface{} {
// - terminal.UI
// - *component.LabelSet

// In addition to default input parameters the registry.Artifact from the Build step
// In addition to default input parameters the registry.Zip from the Build step
// can also be injected.
//
// The output parameters for BuildFunc must be a Struct which can
Expand All @@ -116,81 +91,74 @@ func (b *Platform) deploy(
ui terminal.UI,
log hclog.Logger,
dcr *component.DeclaredResourcesResp,
artifact *registry.Artifact,
zip *registry.Zip,
) (*Deployment, error) {
u := ui.Status()
defer u.Close()
u.Update("Deploy application")
// the session the S3 Uploader will use
sess := session.Must(session.NewSession(&aws.Config{Region: &b.config.Region}))

var result Deployment
// create an uploader with the session and default options
uploader := s3manager.NewUploader(sess)

// Create our resource manager and create deployment resources
rm := b.resourceManager(log, dcr)
// walk temp dir
objects := []s3manager.BatchUploadObject{}

// These params must match exactly to your resource manager functions. Otherwise
// they will not be invoked during CreateAll()
if err := rm.CreateAll(
ctx, log, u, ui,
artifact, &result,
); err != nil {
return nil, err
}
err := filepath.Walk(zip.Path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Store our resource state
result.ResourceState = rm.State()
stat, err := os.Stat(path)
if err != nil {
return err
}

u.Update("Application deployed")
if stat.IsDir() {
return nil
}

return &Deployment{}, nil
}
relativePath, err := filepath.Rel(zip.Path, path)
if err != nil {
return err
}

// This function is the top level status command that gets invoked when Waypoint
// attempts to determine the health of a dpeloyment. It will also invoke the
// status for each resource involed for the given deployment if any.
func (d *Platform) status(
ctx context.Context,
ji *component.JobInfo,
ui terminal.UI,
log hclog.Logger,
deployment *Deployment,
) (*sdk.StatusReport, error) {
sg := ui.StepGroup()
s := sg.Add("Checking the status of the deployment...")

rm := d.resourceManager(log, nil)

// If we don't have resource state, this state is from an older version
// and we need to manually recreate it.
if deployment.ResourceState == nil {
rm.Resource("deployment").SetState(&Resource_Deployment{
Name: deployment.Id,
})
} else {
// Load our set state
if err := rm.LoadState(deployment.ResourceState); err != nil {
return nil, err
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open file %q, %v", path, err)
}

objects = append(objects, s3manager.BatchUploadObject{Object: &s3manager.UploadInput{
Key: aws.String(relativePath),
Bucket: aws.String(b.config.BucketName),
Body: f,
}})

return nil
})

if err != nil {
return nil, err
}

// This will call the StatusReport func on every defined resource in ResourceManager
report, err := rm.StatusReport(ctx, log, sg, ui)
iter := &s3manager.UploadObjectsIterator{Objects: objects}
err = uploader.UploadWithIterator(ctx, iter)
if err != nil {
return nil, status.Errorf(codes.Internal, "resource manager failed to generate resource statuses: %s", err)
return nil, err
}

report.Health = sdk.StatusReport_UNKNOWN
s.Update("Deployment is currently not implemented!")
s.Done()
u.Update("Application deployed")

return report, nil
return &Deployment{}, nil
}

func (b *Platform) resourceDeploymentCreate(
ctx context.Context,
log hclog.Logger,
st terminal.Status,
ui terminal.UI,
artifact *registry.Artifact,
zip *registry.Zip,
result *Deployment,
) error {
// Create your deployment resource here!
Expand All @@ -202,7 +170,7 @@ func (b *Platform) resourceDeploymentStatus(
ctx context.Context,
ui terminal.UI,
sg terminal.StepGroup,
artifact *registry.Artifact,
zip *registry.Zip,
) error {
// Determine health status of "this" resource.
return nil
Expand Down
Loading

0 comments on commit 2eb73c3

Please sign in to comment.