Skip to content

Commit

Permalink
Merge branch 'master' of github.com:atoz-technology/mantil-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Aug 11, 2021
2 parents 6667944 + 6b1f303 commit 173a864
Showing 1 changed file with 53 additions and 52 deletions.
105 changes: 53 additions & 52 deletions internal/commands/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,64 +56,37 @@ func (d *DeployCmd) Deploy() error {
return nil
}

// build function into binary with the function's name
func (d *DeployCmd) buildFunction(name, funcDir string) error {
return shell.Exec([]string{"env", "GOOS=linux", "GOARCH=amd64", "go", "build", "-o", name}, funcDir)
}

func (d *DeployCmd) deploySync() ([]mantil.FunctionUpdate, error) {
funcs, err := d.localFunctions()
localFuncs, err := d.localFunctions()
if err != nil {
return nil, err
}

addedFunctions := d.addedFunctions(funcs)
if len(addedFunctions) > 0 {
log.Printf("added functions - %s", strings.Join(addedFunctions, ","))
}
for _, af := range addedFunctions {
d.project.AddFunction(mantil.Function{Name: af})
}

removedFunctions := d.removedFunctions(funcs)
if len(removedFunctions) > 0 {
log.Printf("removed functions - %s", strings.Join(removedFunctions, ","))
}
for _, rf := range removedFunctions {
d.project.RemoveFunction(rf)
}

addedFuncs := d.processAddedFunctions(localFuncs)
removedFuncs := d.processRemovedFunctions(localFuncs)
funcsForDeploy := d.prepareFunctionsForDeploy()

var functionUpdates []mantil.FunctionUpdate
for _, f := range funcsForDeploy {
added := false
for _, af := range addedFunctions {
for _, af := range addedFuncs {
if af == f.Name {
added = true
break
}
}
removed := false
for _, rf := range removedFunctions {
if rf == f.Name {
removed = true
break
}
}
fu := mantil.FunctionUpdate{
Name: f.Name,
Hash: f.Hash,
S3Key: f.S3Key,
ImageKey: f.ImageKey,
Added: added,
Removed: removed,
Updated: !added && !removed,
Updated: !added,
}
functionUpdates = append(functionUpdates, fu)
}

for _, f := range removedFunctions {
for _, f := range removedFuncs {
functionUpdates = append(functionUpdates, mantil.FunctionUpdate{
Name: f,
Removed: true,
Expand All @@ -138,9 +111,17 @@ func (d *DeployCmd) localFunctions() ([]string, error) {
return functions, nil
}

func (d *DeployCmd) processAddedFunctions(localFuncs []string) []string {
addedFunctions := d.addedFunctions(localFuncs)
if len(addedFunctions) > 0 {
log.Printf("added - %s", strings.Join(addedFunctions, ","))
}
return addedFunctions
}

// compares local functions with the ones in project config and returns all that are newly added
func (d *DeployCmd) addedFunctions(functions []string) []string {
functionExists := func(name string) bool {
func (d *DeployCmd) addedFunctions(localFuncs []string) []string {
funcExistsInProject := func(name string) bool {
for _, f := range d.project.Functions {
if name == f.Name {
return true
Expand All @@ -149,18 +130,26 @@ func (d *DeployCmd) addedFunctions(functions []string) []string {
return false
}
added := []string{}
for _, fun := range functions {
if !functionExists(fun) {
for _, fun := range localFuncs {
if !funcExistsInProject(fun) {
added = append(added, fun)
}
}
return added
}

func (d *DeployCmd) processRemovedFunctions(localFuncs []string) []string {
removedFunctions := d.removedFunctions(localFuncs)
if len(removedFunctions) > 0 {
log.Printf("removed - %s", strings.Join(removedFunctions, ","))
}
return removedFunctions
}

// compares local functions with the ones in project config and returns all that are newly removed
func (d *DeployCmd) removedFunctions(functions []string) []string {
functionExists := func(name string) bool {
for _, f := range functions {
func (d *DeployCmd) removedFunctions(localFuncs []string) []string {
funcExistsLocally := func(name string) bool {
for _, f := range localFuncs {
if name == f {
return true
}
Expand All @@ -169,7 +158,7 @@ func (d *DeployCmd) removedFunctions(functions []string) []string {
}
removed := []string{}
for _, fun := range d.project.Functions {
if !functionExists(fun.Name) {
if !funcExistsLocally(fun.Name) {
removed = append(removed, fun.Name)
}
}
Expand All @@ -182,17 +171,12 @@ func (d *DeployCmd) prepareFunctionsForDeploy() []mantil.Function {
funcsForDeploy := []mantil.Function{}
for i, f := range d.project.Functions {
funcDir := path.Join(d.path, FunctionsDir, f.Name)
isImage := d.isFunctionImage(funcDir)

if err := d.buildFunction(BinaryName, funcDir); err != nil {
log.Printf("skipping function %s due to error while building - %v", f.Name, err)
continue
}

isImage := true
if _, err := os.Stat(path.Join(funcDir, "Dockerfile")); os.IsNotExist(err) {
isImage = false
}

binaryPath := path.Join(funcDir, BinaryName)
hash, err := util.FileHash(binaryPath)
if err != nil {
Expand All @@ -204,18 +188,19 @@ func (d *DeployCmd) prepareFunctionsForDeploy() []mantil.Function {
f.Hash = hash

if isImage {
log.Printf("Dockerfile found - creating function %s as image", f.Name)
log.Printf("Dockerfile found - creating function %s as image package type", f.Name)
image, err := docker.ProcessFunctionImage(d.aws, f, mantil.ProjectIdentifier(d.project.Name), funcDir)
if err != nil {
log.Printf("skipping function %s due to error while processing docker image - %v", f.Name, err)
continue
}
f.SetImageKey(image)
} else {
log.Printf("creating function %s from s3", f.Name)
log.Printf("creating function %s as zip package type", f.Name)
f.SetS3Key(fmt.Sprintf("functions/%s-%s.zip", f.Name, f.Hash))
if err := d.processFunctionS3(f, binaryPath); err != nil {
if err := d.uploadBinaryToS3(f.S3Key, binaryPath); err != nil {
log.Printf("skipping function %s due to error while processing s3 file", err)
continue
}
}

Expand All @@ -226,12 +211,28 @@ func (d *DeployCmd) prepareFunctionsForDeploy() []mantil.Function {
return funcsForDeploy
}

func (d *DeployCmd) processFunctionS3(f mantil.Function, binaryPath string) error {
func (d *DeployCmd) buildFunction(name, funcDir string) error {
return shell.Exec([]string{"env", "GOOS=linux", "GOARCH=amd64", "go", "build", "-o", name}, funcDir)
}

func (d *DeployCmd) isFunctionImage(funcDir string) bool {
_, err := os.Stat(path.Join(funcDir, "Dockerfile"))
if os.IsNotExist(err) {
return false
}
if err != nil {
log.Printf("could not detect if Dockerfile exists - processing function as zip package type")
return false
}
return true
}

func (d *DeployCmd) uploadBinaryToS3(key, binaryPath string) error {
buf, err := util.CreateZipForFile(binaryPath, BinaryName)
if err != nil {
return err
}
if err := d.aws.PutObjectToS3Bucket(d.project.Bucket, f.S3Key, bytes.NewReader(buf)); err != nil {
if err := d.aws.PutObjectToS3Bucket(d.project.Bucket, key, bytes.NewReader(buf)); err != nil {
return err
}
return nil
Expand Down

0 comments on commit 173a864

Please sign in to comment.