Skip to content

Commit

Permalink
chore: refactor apps deploy so could be re-utilized
Browse files Browse the repository at this point in the history
  • Loading branch information
raulb committed Apr 5, 2022
1 parent dcfa52c commit 73e67f4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 50 deletions.
122 changes: 80 additions & 42 deletions cmd/meroxa/root/apps/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,42 +352,83 @@ func (d *Deploy) getPlatformImage(ctx context.Context, appPath string) (string,
// 2. Build image // common
// 3. Push image // common
// 4. Run Turbine deploy // different
func (d *Deploy) deploy(ctx context.Context, appPath string, l log.Logger) (string, error) {
func (d *Deploy) deployApp(ctx context.Context, imageName string) (string, error) {
var output string
var err error

switch d.lang {
case GoLang:
output, err = turbineGo.RunDeployApp(ctx, d.logger, d.path, d.appName, imageName)
if err != nil {
return "", err
}
case "js", JavaScript, NodeJs:
// TODO: @james
// TODO: Do less here!!!
return "", turbineJS.Deploy(ctx, d.path, d.logger)
case "py":
// TODO: @eric
default:
return "", fmt.Errorf("language %q not supported. %s", d.lang, LanguageNotSupportedError)
}
return output, nil

}

// prepareApp will call any specifics to the turbine library to prepare a directory that's ready
// to compress, and build, to then later on call the specific command to deploy depending on the language
func (d *Deploy) prepareApp(ctx context.Context) error {
switch d.lang {
case GoLang:
return turbineGo.BuildBinary(ctx, d.logger, d.path, d.appName, true)
case "js", JavaScript, NodeJs:
// TODO: @james
case "py":
// TODO: @eric
default:
return fmt.Errorf("language %q not supported. %s", d.lang, LanguageNotSupportedError)
}
return nil
}

// getAppImage will check what type of build needs to perform and ultimately will return the image name to use
// when deploying
func (d *Deploy) getAppImage(ctx context.Context) (string, error) {
var fqImageName string
d.appName = path.Base(appPath)
var needsToBuild bool
var err error

err := turbineGo.BuildBinary(ctx, l, appPath, d.appName, true)
if err != nil {
return "", err
switch d.lang {
case GoLang:
needsToBuild, err = turbineGo.NeedsToBuild(d.path, d.appName)
case "js", JavaScript, NodeJs:
// TODO: @james
case "py":
// TODO: @eric
default:
return "", fmt.Errorf("language %q not supported. %s", d.lang, LanguageNotSupportedError)
}

// If no need to build, return empty imageName which won't be utilized by the deploy process anyways
// TODO: Check if we should do this cleaner
if !needsToBuild {
return "", nil
}

var ok bool
// check for image instances
if ok, err = turbineGo.NeedsToBuild(appPath, d.appName); ok {
// TODO: Deployment to DockerHub could be common to any language?
if d.goDeploy.LocalDeployment {
fqImageName, err = d.goDeploy.GetDockerImageName(ctx, d.logger, d.path, d.appName)
if err != nil {
l.Errorf(ctx, err.Error())
return "", err
}

if d.goDeploy.LocalDeployment {
fqImageName, err = d.goDeploy.GetDockerImageName(ctx, l, appPath, d.appName)
if err != nil {
return "", err
}
} else {
fqImageName, err = d.getPlatformImage(ctx, appPath)
if err != nil {
return "", err
}
} else {
fqImageName, err = d.getPlatformImage(ctx, d.path)
if err != nil {
return "", err
}
}

// creates all resources
output, err := turbineGo.RunDeployApp(ctx, l, appPath, d.appName, fqImageName)
if err != nil {
return output, err
}
return output, nil
return fqImageName, nil
}

func (d *Deploy) Execute(ctx context.Context) error {
Expand All @@ -396,12 +437,13 @@ func (d *Deploy) Execute(ctx context.Context) error {
if err != nil {
return err
}
var deployOutput string

d.path, err = turbineCLI.GetPath(d.flags.Path)
if err != nil {
return err
}
d.appName = path.Base(d.path)

d.lang, err = turbineCLI.GetLangFromAppJSON(d.path)
if err != nil {
return err
Expand All @@ -417,22 +459,18 @@ func (d *Deploy) Execute(ctx context.Context) error {
return err
}

// 1. set up the app structure (CLI does this for any language)
// 2. *depending on the language* call something to create the dockerfile <=
// 3. CLI would handle:
// 3.1 creating the tar.zip,
// 3.2 post /sources
// 3.3 uploading the tar.zip
// 3.4 post /builds
// 4. CLI would call (depending on language) the deploy script <=
switch d.lang {
case GoLang:
deployOutput, err = d.deploy(ctx, d.path, d.logger)
case "js", JavaScript, NodeJs:
err = turbineJS.Deploy(ctx, d.path, d.logger)
default:
return fmt.Errorf("language %q not supported. %s", d.lang, LanguageNotSupportedError)
// After this point, CLI will package it up and will build it
err = d.prepareApp(ctx)
if err != nil {
return err
}

fqImage, err := d.getAppImage(ctx)
if err != nil {
return err
}

deployOutput, err := d.deployApp(ctx, fqImage)
if err != nil {
return err
}
Expand Down
6 changes: 0 additions & 6 deletions cmd/meroxa/turbine_cli/golang/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import (
"github.com/meroxa/cli/log"
)

type Deploy struct {
DockerHubUserNameEnv string
DockerHubAccessTokenEnv string
LocalDeployment bool
}

// RunDeployApp runs the binary previously built with the `--deploy` flag which should create all necessary resources.
func RunDeployApp(ctx context.Context, l log.Logger, appPath, appName, imageName string) (string, error) {
l.Infof(ctx, "Deploying application %q...", appName)
Expand Down
7 changes: 6 additions & 1 deletion cmd/meroxa/turbine_cli/golang/local_deployment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package turbineGo TODO: Reorganize this in a different pkg
package turbinego

import (
Expand All @@ -18,6 +17,12 @@ import (
"github.com/meroxa/cli/log"
)

type Deploy struct {
DockerHubUserNameEnv string
DockerHubAccessTokenEnv string
LocalDeployment bool
}

func (gd *Deploy) getAuthConfig() string {
dhUsername := gd.DockerHubUserNameEnv
dhAccessToken := gd.DockerHubAccessTokenEnv
Expand Down
2 changes: 1 addition & 1 deletion cmd/meroxa/turbine_cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func CreateTarAndZipFile(src string, buf io.Writer) error {
appDir := filepath.Base(src)

// Change to parent's app directory
pwd, err := switchToAppDirectory(filepath.Dir(appDir))
pwd, err := switchToAppDirectory(src)
if err != nil {
return err
}
Expand Down

0 comments on commit 73e67f4

Please sign in to comment.