Skip to content

Commit

Permalink
add destroy function
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Jul 27, 2021
1 parent 321fbfd commit 1783637
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 17 deletions.
3 changes: 1 addition & 2 deletions api/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func (h *Deploy) Deploy(ctx context.Context, req *DeployRequest) (*DeployRespons
if req.ProjectName == "" || req.Token == "" {
return nil, fmt.Errorf("bad request")
}
projectBucket := mantil.ProjectBucket(req.ProjectName)
p, err := mantil.LoadProject(projectBucket)
p, err := mantil.LoadProject(req.ProjectName)
if err != nil {
return nil, err
}
Expand Down
47 changes: 47 additions & 0 deletions api/destroy/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package destroy

import (
"context"
"fmt"
"log"

"github.com/atoz-technology/mantil-backend/internal/destroy"
"github.com/atoz-technology/mantil-backend/internal/mantil"
)

type Destroy struct{}

type DestroyRequest struct {
ProjectName string
Token string
}

type DestroyResponse struct {
}

func (d *Destroy) Invoke(ctx context.Context, req *DestroyRequest) (*DestroyResponse, error) {
return d.Destroy(ctx, req)
}

func (f *Destroy) Destroy(ctx context.Context, req *DestroyRequest) (*DestroyResponse, error) {
if req.ProjectName == "" || req.Token == "" {
return nil, fmt.Errorf("bad request")
}
p, err := mantil.LoadProject(req.ProjectName)
if err != nil {
return nil, err
}
if p.Token != req.Token {
return nil, fmt.Errorf("access denied")
}
err = destroy.Destroy(p, "/tmp")
if err != nil {
log.Printf("%v", err)
return nil, err
}
return &DestroyResponse{}, nil
}

func New() *Destroy {
return &Destroy{}
}
1 change: 1 addition & 0 deletions functions/destroy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
destroy
21 changes: 21 additions & 0 deletions functions/destroy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM public.ecr.aws/lambda/go:latest

# terrraform install
RUN yum -y install unzip wget git
ARG version=1.0.0
ARG arch=_linux_amd64 #_linux_arm64
RUN wget https://releases.hashicorp.com/terraform/$version/terraform_${version}${arch}.zip \
&& unzip terraform_${version}${arch}.zip \
&& rm terraform_${version}${arch}.zip \
&& mv terraform /usr/bin/ \
&& terraform --version
ENV TF_IN_AUTOMATION=1

# aws cli install
# ref: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-linux.html#cliv2-linux-install
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
&& unzip awscliv2.zip \
&& ./aws/install

COPY destroy /var/task/
CMD [ "destroy" ]
11 changes: 11 additions & 0 deletions functions/destroy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"github.com/atoz-technology/mantil-backend/api/destroy"
"github.com/atoz-technology/mantil.go"
)

func main() {
var api = destroy.New()
mantil.LambdaHandler(api)
}
11 changes: 11 additions & 0 deletions internal/assets/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package assets

import "net/http"

func StartServer() {
go func() {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(AssetFile()))
http.ListenAndServe(":8080", mux)
}()
}
4 changes: 2 additions & 2 deletions internal/assets/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,16 @@ func (a *AWS) GetProjectToken(name, policy string) (*stsTypes.Credentials, error
}
return rsp.Credentials, nil
}

func (a *AWS) UpdateLambdaFunctionCodeImage(function, image string) error {
ufci := &lambda.UpdateFunctionCodeInput{
FunctionName: aws.String(function),
ImageUri: aws.String(image),
}

_, err := a.lambdaClient.UpdateFunctionCode(context.TODO(), ufci)
if err != nil {
return fmt.Errorf("could not update lambda function %s with image %s", function, image)
}
return nil
}
22 changes: 11 additions & 11 deletions internal/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package deploy
import (
"fmt"
"log"
"net/http"

"github.com/atoz-technology/mantil-backend/internal/assets"
"github.com/atoz-technology/mantil-backend/internal/aws"
Expand All @@ -28,11 +27,7 @@ func New(project *mantil.Project, updates []mantil.FunctionUpdate, path string)
if err != nil {
return nil, err
}
go func() {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(assets.AssetFile()))
http.ListenAndServe(":8080", mux)
}()
assets.StartServer()
return &Deploy{
aws: awsClient,
project: project,
Expand Down Expand Up @@ -116,10 +111,15 @@ func (d *Deploy) applyInfrastructure() error {
return nil
}

func (d *Deploy) updateLambdaFunction(fu mantil.FunctionUpdate) error {
lambdaName := fmt.Sprintf("%s-mantil-team-%s-%s", d.project.Organization.Name, d.project.Name, fu.Name)
if err := d.aws.UpdateLambdaFunctionCodeFromS3(lambdaName, d.project.Bucket, fu.S3Key); err != nil {
return fmt.Errorf("could not update lambda %s due to error %v", lambdaName, err)
func (d *Deploy) updateLambdaFunction(f mantil.FunctionUpdate) error {
lambdaName := fmt.Sprintf("%s-mantil-team-%s-%s", d.project.Organization.Name, d.project.Name, f.Name)
var err error
if f.S3Key != "" {
err = d.aws.UpdateLambdaFunctionCodeFromS3(lambdaName, d.project.Bucket, f.S3Key)
} else if f.ImageKey != "" {
err = d.aws.UpdateLambdaFunctionCodeImage(lambdaName, f.ImageKey)
} else {
err = fmt.Errorf("could not update lambda function %s due to missing key", lambdaName)
}
return nil
return err
}
31 changes: 31 additions & 0 deletions internal/destroy/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package destroy

import (
"fmt"

"github.com/atoz-technology/mantil-backend/internal/assets"
"github.com/atoz-technology/mantil-backend/internal/aws"
"github.com/atoz-technology/mantil-backend/internal/mantil"
"github.com/atoz-technology/mantil-backend/internal/terraform"
)

func Destroy(project *mantil.Project, path string) error {
assets.StartServer()
tf := terraform.New(path)
if err := tf.ApplyForProject(project, true); err != nil {
return fmt.Errorf("could not terraform destroy - %v", err)
}
aws, err := aws.New()
if err != nil {
return fmt.Errorf("could not initialize aws - %v", err)
}
bucketName := project.Bucket
bucketExists, _ := aws.S3BucketExists(bucketName)
if bucketExists {
err = aws.DeleteS3Bucket(bucketName)
if err != nil {
return fmt.Errorf("could not delete bucket %s - %v", bucketName, err)
}
}
return nil
}
5 changes: 3 additions & 2 deletions internal/mantil/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func NewProject(name, token string) (*Project, error) {
return p, nil
}

func LoadProject(bucket string) (*Project, error) {
func LoadProject(projectName string) (*Project, error) {
bucket := ProjectBucket(projectName)
awsClient, err := aws.New()
if err != nil {
return nil, err
Expand Down Expand Up @@ -132,7 +133,7 @@ func (p *Project) AddFunctionDefaults() {
if f.Path == "" {
f.Path = f.Name
}
if f.S3Key == "" {
if f.S3Key == "" && f.ImageKey == "" {
if f.Hash != "" {
f.S3Key = fmt.Sprintf("functions/%s-%s.zip", f.Name, f.Hash)
} else {
Expand Down

0 comments on commit 1783637

Please sign in to comment.