Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
janelletavares committed Sep 7, 2022
1 parent ac52a84 commit b8b1421
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 31 deletions.
59 changes: 54 additions & 5 deletions cmd/meroxa/root/apps/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"github.com/volatiletech/null/v8"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -240,7 +241,20 @@ func (d *Deploy) getPlatformImage(ctx context.Context) (string, error) {

func (d *Deploy) deployApp(ctx context.Context, imageName, gitSha, specVersion string) error {
d.logger.Infof(ctx, "Deploying application %q...", d.appName)
return d.turbineCLI.Deploy(ctx, imageName, d.appName, gitSha, specVersion)
deploymentSpec, err := d.turbineCLI.Deploy(ctx, imageName, d.appName, gitSha, specVersion)
if err != nil {
return nil
}
if specVersion != "" {
input := meroxa.CreateDeploymentInput{
Application: meroxa.EntityIdentifier{Name: null.StringFrom(d.appName)},
GitSha: gitSha,
SpecVersion: null.StringFrom(specVersion),
Spec: null.StringFrom(deploymentSpec),
}
err = d.client.CreateDeployment(ctx, input)
}
return err
}

func (d *Deploy) checkPreIRApplication(ctx context.Context) (*meroxa.Application, error) {
Expand Down Expand Up @@ -612,6 +626,38 @@ func (d *Deploy) prepareAppName(ctx context.Context) string {
return appName
}

func (d *Deploy) waitForDeployment() error {
startTime := time.Now().UTC()
finished := false
logs := []string{}
deploymentErr := fmt.Errorf("timed out; check `apps logs`")
for !finished && (time.Now().UTC().Sub(startTime) < 5*time.Minute) {
var deployment *meroxa.Deployment
deployment, err := d.client.GetLatestDeployment()
if err != nil {

}
newLogs := strings.Split(deployment.OutputLog.String, "\n")
if len(newLogs) > len(logs) {
for i := len(logs); i < len(newLogs); i++ {
if len(logs) != 0 && i != 0 {
d.logger.StopSpinner(newLogs[i-1])
}
d.logger.StartSpinner("\t", newLogs[i])
}
logs = append(logs, newLogs...)
}
if deployment.State.State == meroxa.DeploymentStateDeployed {
finished = true
deploymentErr = nil
} else if deployment.State.State == meroxa.DeploymentStateDeployingErrored {
d.logger.StopSpinnerWithStatus(logs[len(logs)-1], log.Failed)
return fmt.Errorf("failed to deploy Application %q", d.appName)
}
}
return deploymentErr
}

func (d *Deploy) Execute(ctx context.Context) error {
if err := d.validateAppJSON(ctx); err != nil {
return err
Expand Down Expand Up @@ -655,7 +701,7 @@ func (d *Deploy) Execute(ctx context.Context) error {
return err
}

app, err = d.client.CreateApplication(ctx, &meroxa.CreateApplicationInput{
app, err = d.client.CreateApplicationV2(ctx, &meroxa.CreateApplicationInput{
Name: d.appName,
Language: d.lang,
GitSha: gitSha,
Expand All @@ -678,14 +724,17 @@ func (d *Deploy) Execute(ctx context.Context) error {

if d.specVersion == "" {
app, err = d.checkPreIRApplication(ctx)
if err != nil {
return err
}
} else {
err = d.waitForDeployment()
}
if err != nil {
return err
}

dashboardURL := fmt.Sprintf("https://dashboard.meroxa.io/apps/%s/detail", d.appName)
output := fmt.Sprintf("\t%s Application %q successfully created!\n\n ✨ To visualize your application visit %s",
d.logger.SuccessfulCheck(), d.appName, dashboardURL)
d.logger.Info(ctx, output)
d.logger.JSON(ctx, app)
return nil
}
15 changes: 11 additions & 4 deletions cmd/meroxa/turbine/golang/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

// Deploy runs the binary previously built with the `--deploy` flag which should create all necessary resources.
func (t *turbineGoCLI) Deploy(ctx context.Context, imageName, appName, gitSha string, specVersion string) error {
func (t *turbineGoCLI) Deploy(ctx context.Context, imageName, appName, gitSha string, specVersion string) (string, error) {
deploymentSpec := ""
args := []string{
"--deploy",
"--appname",
Expand All @@ -35,16 +36,22 @@ func (t *turbineGoCLI) Deploy(ctx context.Context, imageName, appName, gitSha st

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

output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
return deploymentSpec, errors.New(string(output))
}
return nil

if specVersion != "" {
deploymentSpec, err = utils.GetTurbineResponseFromOutput(string(output))
err = fmt.Errorf(
"unable to receive the deployment spec for the Meroxa Application at %s has a Process", t.appPath)
}
return deploymentSpec, err
}

func (t *turbineGoCLI) GetResources(ctx context.Context, appName string) ([]utils.ApplicationResource, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/meroxa/turbine/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
type CLI interface {
Build(ctx context.Context, appName string, platform bool) (string, error)
CleanUpBinaries(ctx context.Context, appName string)
Deploy(ctx context.Context, imageName, appName, gitSha, specVersion string) error
Deploy(ctx context.Context, imageName, appName, gitSha, specVersion string) (string, error)
GetResources(ctx context.Context, appName string) ([]ApplicationResource, error)
GitInit(ctx context.Context, name string) error
GitChecks(ctx context.Context) error
Expand Down
25 changes: 16 additions & 9 deletions cmd/meroxa/turbine/javascript/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"os"
"regexp"
"strconv"

"github.com/meroxa/cli/cmd/meroxa/global"
Expand All @@ -24,19 +23,22 @@ func (t *turbineJsCLI) NeedsToBuild(ctx context.Context, path string) (bool, err
return false, err
}

r := regexp.MustCompile("\nturbine-response: (true|false)\n")
match := r.FindStringSubmatch(string(output))
if match == nil || len(match) < 2 {
isNeeded, err := utils.GetTurbineResponseFromOutput(string(output))
if err != nil {
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])
return strconv.ParseBool(isNeeded)
}

func (t *turbineJsCLI) Deploy(ctx context.Context, imageName, appName, gitSha, specVersion string) error {
func (t *turbineJsCLI) Deploy(ctx context.Context, imageName, appName, gitSha, specVersion string) (string, error) {
var (
output string
deploymentSpec string
)
params := []string{"clideploy", imageName, t.appPath, appName, gitSha}

if specVersion != "" {
Expand All @@ -47,13 +49,18 @@ func (t *turbineJsCLI) Deploy(ctx context.Context, imageName, appName, gitSha, s

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

_, err = utils.RunCmdWithErrorDetection(ctx, cmd, t.logger)
return err
output, err = utils.RunCmdWithErrorDetection(ctx, cmd, t.logger)
if specVersion != "" {
deploymentSpec, err = utils.GetTurbineResponseFromOutput(output)
err = fmt.Errorf(
"unable to receive the deployment spec for the Meroxa Application at %s has a Process", t.appPath)
}
return deploymentSpec, err
}

// GetResources asks turbine for a list of resources used by the given app.
Expand Down
7 changes: 4 additions & 3 deletions cmd/meroxa/turbine/mock/cli.go

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

26 changes: 17 additions & 9 deletions cmd/meroxa/turbine/python/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strconv"

"github.com/meroxa/cli/cmd/meroxa/global"
Expand All @@ -25,20 +24,24 @@ func (t *turbinePyCLI) NeedsToBuild(ctx context.Context, appName string) (bool,
string(output))
return false, err
}
r := regexp.MustCompile("^turbine-response: (true|false)\n")
match := r.FindStringSubmatch(string(output))
if match == nil || len(match) < 2 {

isNeeded, err := utils.GetTurbineResponseFromOutput(string(output))
if err != nil {
err := fmt.Errorf(
"unable to determine if the Meroxa Application at %s has a Process; %s",
t.appPath,
string(output))
return false, err
}
return strconv.ParseBool(match[1])
return strconv.ParseBool(isNeeded)
}

// Deploy creates Application entities.
func (t *turbinePyCLI) Deploy(ctx context.Context, imageName, appName, gitSha, specVersion string) error {
func (t *turbinePyCLI) Deploy(ctx context.Context, imageName, appName, gitSha, specVersion string) (string, error) {
var (
output string
deploymentSpec string
)
args := []string{"clideploy", t.appPath, imageName, appName, gitSha}

if specVersion != "" {
Expand All @@ -49,13 +52,18 @@ func (t *turbinePyCLI) Deploy(ctx context.Context, imageName, appName, gitSha, s

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

_, err = utils.RunCmdWithErrorDetection(ctx, cmd, t.logger)
return err
output, err = utils.RunCmdWithErrorDetection(ctx, cmd, t.logger)
if specVersion != "" {
deploymentSpec, err = utils.GetTurbineResponseFromOutput(output)
err = fmt.Errorf(
"unable to receive the deployment spec for the Meroxa Application at %s has a Process", t.appPath)
}
return deploymentSpec, err
}

// GetResources asks turbine for a list of resources used by the given app.
Expand Down
9 changes: 9 additions & 0 deletions cmd/meroxa/turbine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,12 @@ func cleanUpPythonTempBuildLocation(ctx context.Context, logger log.Logger, appP
fmt.Printf("unable to clean up Meroxa Application at %s; %s", appPath, string(output))
}
}

func GetTurbineResponseFromOutput(output string) (string, error) {
r := regexp.MustCompile("^turbine-response: ([^\n]*)")
match := r.FindStringSubmatch(string(output))
if match == nil || len(match) < 2 {
return "", fmt.Errorf("output is formatted unexpectedly")
}
return match[1], nil
}

0 comments on commit b8b1421

Please sign in to comment.