Skip to content

Commit

Permalink
Merge pull request #987 from aziontech/dev
Browse files Browse the repository at this point in the history
Deploy to production - 2024/10/30
  • Loading branch information
maxwelbm authored Oct 31, 2024
2 parents f98d819 + 6cffa72 commit df38d5b
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 253 deletions.
3 changes: 3 additions & 0 deletions messages/deploy/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ var (
ErrorZipNotExist = "Zip file %s does not exist"
ErrorUploadZip = "Failed to upload zip file %s: %w"
ErrorDelFileZip = "Deleting zip file %s: %v"
ErrorRelPath = "Error determining relative path for %s: %v"
ErrorResetPointFile = "Error resetting file pointer %s: %v"
ErrorCopyContentFile = "Error copying contents of file %s to ZIP: %v"
)
77 changes: 77 additions & 0 deletions pkg/api/s3/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package s3

import (
"context"
"errors"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
msg "github.com/aziontech/azion-cli/messages/deploy"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/logger"

"go.uber.org/zap"
)

const (
region = "us-east"
endpoint = "https://s3.us-east-005.azionstorage.net"
)

type CustomEndpointResolver struct {
URL string
SigningRegion string
}

// ResolveEndpoint is the method that defines the custom endpoint
func (e *CustomEndpointResolver) ResolveEndpoint(service, region string) (aws.Endpoint, error) { // nolint
return aws.Endpoint{ //nolint
URL: e.URL,
SigningRegion: e.SigningRegion,
}, nil
}

func New(s3AccessKey, s3SecretKey string) (aws.Config, error) {
endpointResolver := &CustomEndpointResolver{
URL: endpoint,
SigningRegion: region,
}

cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(s3AccessKey, s3SecretKey, "")),
config.WithEndpointResolver(endpointResolver), // nolint
)
if err != nil {
return aws.Config{}, errors.New(msg.ErrorUnableSDKConfig + err.Error())

}
return cfg, nil
}

func UploadFile(ctx context.Context, cfg aws.Config, fileOps *contracts.FileOps, bucketName, prefix string) error {
file := fileOps.Path
if prefix != "" {
file = fmt.Sprintf("%s%s", prefix, fileOps.Path)
}

s3Client := s3.NewFromConfig(cfg)

logger.Debug("Object_key: " + file)
uploadInput := &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(file), // Name of the file in the bucket
Body: fileOps.FileContent,
}

_, err := s3Client.PutObject(ctx, uploadInput)
if err != nil {
logger.Debug("Error while uploading file <"+fileOps.Path+"> to storage api", zap.Error(err))
return fmt.Errorf(msg.ErrorUploadFileBucket, bucketName, err)
}

return nil
}
4 changes: 2 additions & 2 deletions pkg/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type DeployCmd struct {
CaptureLogs func(execId string, token string, cmd *DeployCmd) error
CheckToken func(f *cmdutil.Factory) error
ReadSettings func() (token.Settings, error)
UploadFiles func(f *cmdutil.Factory, msgs *[]string, prefix string, pathStatic string, settings token.Settings) error
UploadFiles func(f *cmdutil.Factory, conf *contracts.AzionApplicationOptions, msgs *[]string, pathStatic, bucket string, cmd *DeployCmd, settings token.Settings) error
OpenBrowserFunc func(input string) error
}

Expand Down Expand Up @@ -216,7 +216,7 @@ func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {

conf.Prefix = cmd.VersionID()

err = cmd.UploadFiles(f, &msgs, conf.Prefix, localDir, settings)
err = cmd.UploadFiles(f, conf, &msgs, localDir, settings.S3Bucket, cmd, settings)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestDeploy_Run(t *testing.T) {
cmd.ReadSettings = func() (token.Settings, error) {
return token.Settings{}, nil
}
cmd.UploadFiles = func(f *cmdutil.Factory, msgs *[]string, prefix, pathStatic string, settings token.Settings) error {
cmd.UploadFiles = func(f *cmdutil.Factory, conf *contracts.AzionApplicationOptions, msgs *[]string, pathStatic, bucket string, cmd *DeployCmd, settings token.Settings) error {
return nil
}
},
Expand All @@ -142,7 +142,7 @@ func TestDeploy_Run(t *testing.T) {
cmd.CaptureLogs = MockCaptureLogs
cmd.CheckToken = MockCheckToken
cmd.ReadSettings = MockReadSettings
cmd.UploadFiles = func(f *cmdutil.Factory, msgs *[]string, prefix, pathStatic string, settings token.Settings) error {
cmd.UploadFiles = func(f *cmdutil.Factory, conf *contracts.AzionApplicationOptions, msgs *[]string, pathStatic, bucket string, cmd *DeployCmd, settings token.Settings) error {
return nil
}
},
Expand All @@ -158,7 +158,7 @@ func TestDeploy_Run(t *testing.T) {
cmd.WriteAzionJsonContent = MockWriteAzionJsonContent
cmd.CheckToken = MockCheckToken
cmd.ReadSettings = MockReadSettings
cmd.UploadFiles = func(f *cmdutil.Factory, msgs *[]string, prefix, pathStatic string, settings token.Settings) error {
cmd.UploadFiles = func(f *cmdutil.Factory, conf *contracts.AzionApplicationOptions, msgs *[]string, pathStatic, bucket string, cmd *DeployCmd, settings token.Settings) error {
return nil
}
cmd.CallScript = func(token string, id string, secret string, prefix string, name string, cmd *DeployCmd) (string, error) {
Expand Down
Loading

0 comments on commit df38d5b

Please sign in to comment.