Skip to content

Commit

Permalink
Python deploy (#312)
Browse files Browse the repository at this point in the history
* Misc final changes for Python deploy
  • Loading branch information
janelletavares authored Apr 15, 2022
1 parent 2a08bba commit 1603745
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 36 deletions.
23 changes: 14 additions & 9 deletions cmd/meroxa/root/apps/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
turbineGo "github.com/meroxa/cli/cmd/meroxa/turbine_cli/golang"
turbineJS "github.com/meroxa/cli/cmd/meroxa/turbine_cli/javascript"
turbinePY "github.com/meroxa/cli/cmd/meroxa/turbine_cli/python"
"github.com/meroxa/cli/config"
"github.com/meroxa/cli/log"
"github.com/meroxa/meroxa-go/pkg/meroxa"
Expand Down Expand Up @@ -283,7 +284,13 @@ func (d *Deploy) uploadSource(ctx context.Context, appPath, url string) error {
return err
}

// TODO: Remove d.tempPath for JS and Python apps
// TODO: Remove d.tempPath for JS apps
if d.lang == Python {
output, err := turbinePY.CleanUpApp(appPath)
if err != nil {
fmt.Printf("warning: failed to clean up app at %s: %v %s\n", appPath, err, output)
}
}
// remove .tar.gz file
return os.Remove(dFile)
}
Expand Down Expand Up @@ -355,13 +362,12 @@ func (d *Deploy) getPlatformImage(ctx context.Context, appPath string) (string,
sourceBlob := meroxa.SourceBlob{Url: s.GetUrl}
buildInput := &meroxa.CreateBuildInput{SourceBlob: sourceBlob}

d.logger.StartSpinner("\t", " Building Meroxa Process image...")

build, err := d.client.CreateBuild(ctx, buildInput)
if err != nil {
d.logger.StopSpinnerWithStatus("\t", log.Failed)
return "", err
}
d.logger.StartSpinner("\t", fmt.Sprintf(" Building Meroxa Process image (%s)...", build.Uuid))

for {
b, err := d.client.GetBuild(ctx, build.Uuid)
Expand All @@ -376,7 +382,7 @@ func (d *Deploy) getPlatformImage(ctx context.Context, appPath string) (string,
d.logger.StopSpinnerWithStatus(msg, log.Failed)
return "", fmt.Errorf("build with uuid %q errored", b.Uuid)
case "complete":
d.logger.StopSpinnerWithStatus("Process image built!", log.Successful)
d.logger.StopSpinnerWithStatus(fmt.Sprintf("Successfully built Process image! (%s)", build.Uuid), log.Successful)
return build.Image, nil
}
time.Sleep(pollDuration)
Expand All @@ -394,7 +400,7 @@ func (d *Deploy) deployApp(ctx context.Context, imageName string) (string, error
case JavaScript:
output, err = turbineJS.RunDeployApp(ctx, d.logger, d.path, imageName)
case Python:
// TODO: @eric
output, err = turbinePY.RunDeployApp(ctx, d.logger, d.path, imageName)
}

if err != nil {
Expand All @@ -421,9 +427,8 @@ func (d *Deploy) buildApp(ctx context.Context) error {
case JavaScript:
d.tempPath, err = turbineJS.BuildApp(d.path)
case Python:
// TODO: @eric
// path only for zipping = turbinePy.BuildApp(ctx, d.logger, d.path) => newPath string
// Dockerfile will already exist
d.tempPath, err = turbinePY.BuildApp(d.path)
}
if err != nil {
d.logger.StopSpinnerWithStatus("\t", log.Failed)
Expand All @@ -447,7 +452,7 @@ func (d *Deploy) getAppImage(ctx context.Context) (string, error) {
case JavaScript:
needsToBuild, err = turbineJS.NeedsToBuild(d.path)
case Python:
// TODO: @eric
needsToBuild, err = turbinePY.NeedsToBuild(d.path)
}
if err != nil {
d.logger.StopSpinnerWithStatus("\t", log.Failed)
Expand Down Expand Up @@ -548,7 +553,7 @@ func (d *Deploy) tearDownExistingResources(ctx context.Context) error {
return errors.New(msg)
}
resp, _ := d.client.DeleteApplicationEntities(ctx, d.appName)
if resp.Body != nil {
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
Expand Down
6 changes: 5 additions & 1 deletion cmd/meroxa/root/apps/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func (i *Init) GitInit(ctx context.Context, path string) error {
return errors.New("path is required")
}

cmd := exec.Command("git", "init", path)
cmd := exec.Command("git", "config", "--global", "init.defaultBranch", "main")
cmd.Path = path
_ = cmd.Run()

cmd = exec.Command("git", "init", path)
output, err := cmd.CombinedOutput()
if err != nil {
i.logger.Error(ctx, string(output))
Expand Down
57 changes: 53 additions & 4 deletions cmd/meroxa/turbine_cli/python/deploy.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package turbinepy

import (
"context"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"

"github.com/meroxa/cli/cmd/meroxa/global"
turbinecli "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
"github.com/meroxa/cli/log"
)

// TODO: Add a function that creates the needed structure for a python app
// BuildApp created the needed structure for a python app.
func BuildApp(path string) (string, error) {
cmd := exec.Command("turbine-py", "clibuild", path)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("unable to build Meroxa Application at %s; %s", path, string(output))
}
r := regexp.MustCompile("^turbine-response: ([^\n]*)")
match := r.FindStringSubmatch(string(output))
if match == nil || len(match) < 2 {
return "", fmt.Errorf("unable to build Meroxa Application at %s; %s", path, string(output))
}
return match[1], err
}

// NeedsToBuild determines if the app has functions or not.
func NeedsToBuild(path string) (bool, error) {
Expand All @@ -20,9 +40,38 @@ func NeedsToBuild(path string) (bool, error) {
string(output))
return false, err
}
return strconv.ParseBool(strings.TrimSpace(string(output)))
r := regexp.MustCompile("^turbine-response: (true|false)\n")
match := r.FindStringSubmatch(string(output))
if match == nil || len(match) < 2 {
err := fmt.Errorf(
"unable to determine if the Meroxa Application at %s has a Process; %s",
path,
string(output))
return false, err
}
return strconv.ParseBool(match[1])
}

// TODO: Add a function that actually creates the meroxa resources...
// RunDeployApp creates Application entities.
func RunDeployApp(ctx context.Context, l log.Logger, path, imageName string) (string, error) {
cmd := exec.Command("turbine-py", "clideploy", path, imageName)

accessToken, _, err := global.GetUserToken()
if err != nil {
return "", err
}
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("MEROXA_ACCESS_TOKEN=%s", accessToken))

return turbinecli.RunCmdWithErrorDetection(ctx, cmd, l)
}

// TODO: Have a script to cleanup the temp directory used (right after source is uploaded)
// CleanUpApp removes any temporary artifacts in the temp directory.
func CleanUpApp(path string) (string, error) {
cmd := exec.Command("turbine-py", "cliclean", path)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("unable to clean up Meroxa Application at %s; %s", path, string(output))
}
return strings.TrimSpace(string(output)), err
}
9 changes: 2 additions & 7 deletions cmd/meroxa/turbine_cli/python/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ package turbinepy
import (
"context"
"os/exec"
"path/filepath"

turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
"github.com/meroxa/cli/log"
)

func Run(ctx context.Context, l log.Logger, path string) error {
f, err := filepath.Abs(filepath.Join(path, "main.py"))
if err != nil {
return err
}
cmd := exec.Command("python3", f)
_, err = turbineCLI.RunCmdWithErrorDetection(ctx, cmd, l)
cmd := exec.Command("turbine-py", "run", path)
_, err := turbineCLI.RunCmdWithErrorDetection(ctx, cmd, l)
return err
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/manifoldco/promptui v0.8.0
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/meroxa/meroxa-go v0.0.0-20220415204845-92b08f71390e
github.com/meroxa/meroxa-go v0.0.0-20220415221139-f31436f83dca
github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20170819232839-0fbfe93532da
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182aff
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
github.com/meroxa/funtime v0.0.0-20220113012133-85e6e898fc73/go.mod h1:K2y2GvcA4Cg3dJtckcwYWnwnJzF63FDdtAQI0fToU0Q=
github.com/meroxa/meroxa-go v0.0.0-20220208195203-71ddc3133fab/go.mod h1:HDFszURCM1cOpKE699o5Hs0T2tEIXqY+vFcsur3RiwY=
github.com/meroxa/meroxa-go v0.0.0-20220415204845-92b08f71390e h1:Gkv7fU8Bc2sBbNoJu1K5ElHyJ3HsYb7k43ikJpaE4uM=
github.com/meroxa/meroxa-go v0.0.0-20220415204845-92b08f71390e/go.mod h1:BsqYa9jqfyGOAgfkggfK567b2Ahkb+RCH3lXDQGgrh8=
github.com/meroxa/meroxa-go v0.0.0-20220415221139-f31436f83dca h1:HTg9REXt9NqbX2syACtpUEwF/unzwWRjG9AkSaqcnLc=
github.com/meroxa/meroxa-go v0.0.0-20220415221139-f31436f83dca/go.mod h1:BsqYa9jqfyGOAgfkggfK567b2Ahkb+RCH3lXDQGgrh8=
github.com/meroxa/turbine-go v0.0.0-20220415203703-5ab9fcaf547d h1:JWJ0rrFmm9zV3WMMm3hIXS0Ebegm129SVs3zPcFi2Kk=
github.com/meroxa/turbine-go v0.0.0-20220415203703-5ab9fcaf547d/go.mod h1:F4ODyjX+tn5dYFRc1bQlHLUnTtlJIxs09AKHqfCD5Cg=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
Expand Down
22 changes: 11 additions & 11 deletions vendor/github.com/meroxa/meroxa-go/pkg/meroxa/application.go

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

15 changes: 15 additions & 0 deletions vendor/github.com/meroxa/meroxa-go/pkg/meroxa/connector.go

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ github.com/mattn/go-runewidth
# github.com/mattn/go-shellwords v1.0.12
## explicit; go 1.13
github.com/mattn/go-shellwords
# github.com/meroxa/meroxa-go v0.0.0-20220415204845-92b08f71390e
# github.com/meroxa/meroxa-go v0.0.0-20220415221139-f31436f83dca
## explicit; go 1.17
github.com/meroxa/meroxa-go/pkg/meroxa
github.com/meroxa/meroxa-go/pkg/mock
Expand Down

0 comments on commit 1603745

Please sign in to comment.