Skip to content

Commit 74389b5

Browse files
Jordan/gcp debug pulumi (#1403)
* s/DebugPulumi/DebugPulumiNodeJS/g * setup DebugPulumiGolang * use DebugPulumiGolang for gcp cd * DEFANG_PULUMI_DIR is required to DebugPulumiNodeJS * wip * feat: add ErrLocalPulumiStopped to avoid debugger --------- Co-authored-by: Jordan Stephens <[email protected]>
1 parent d22ef5d commit 74389b5

File tree

6 files changed

+89
-7
lines changed

6 files changed

+89
-7
lines changed

src/cmd/cli/command/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func handleComposeUpErr(ctx context.Context, err error, project *compose.Project
224224
printDefangHint("To start a new project, do:", "new")
225225
}
226226

227-
if nonInteractive {
227+
if nonInteractive || errors.Is(err, byoc.ErrLocalPulumiStopped) {
228228
return err
229229
}
230230

src/pkg/cli/client/byoc/aws/byoc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (b *ByocAws) runCdCommand(ctx context.Context, cmd cdCmd) (ecs.TaskArn, err
430430
}
431431
env["DEFANG_MODE"] = strings.ToLower(cmd.mode.String())
432432

433-
if term.DoDebug() || os.Getenv("DEFANG_PULUMI_DIR") != "" {
433+
if os.Getenv("DEFANG_PULUMI_DIR") != "" {
434434
// Convert the environment to a human-readable array of KEY=VALUE strings for debugging
435435
debugEnv := []string{"AWS_REGION=" + b.driver.Region.String()}
436436
if awsProfile := os.Getenv("AWS_PROFILE"); awsProfile != "" {
@@ -439,7 +439,7 @@ func (b *ByocAws) runCdCommand(ctx context.Context, cmd cdCmd) (ecs.TaskArn, err
439439
for k, v := range env {
440440
debugEnv = append(debugEnv, k+"="+v)
441441
}
442-
if err := byoc.DebugPulumi(ctx, debugEnv, cmd.cmd...); err != nil {
442+
if err := byoc.DebugPulumiNodeJS(ctx, debugEnv, cmd.cmd...); err != nil {
443443
return nil, err
444444
}
445445
}

src/pkg/cli/client/byoc/common.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"os"
77
"os/exec"
8+
"path"
89
"strings"
910

1011
"github.com/DefangLabs/defang/src/pkg"
@@ -19,6 +20,7 @@ const (
1920
var (
2021
DefangPrefix = pkg.Getenv("DEFANG_PREFIX", "Defang") // prefix for all resources created by Defang
2122
DefangPulumiBackend = os.Getenv("DEFANG_PULUMI_BACKEND")
23+
ErrLocalPulumiStopped = errors.New("local pulumi command succeeded; stopping")
2224
PulumiConfigPassphrase = pkg.Getenv("PULUMI_CONFIG_PASSPHRASE", "asdf")
2325
)
2426

@@ -44,6 +46,7 @@ func GetPulumiBackend(stateUrl string) (string, string, error) {
4446
}
4547

4648
func runLocalCommand(ctx context.Context, dir string, env []string, cmd ...string) error {
49+
term.Debug("Running local command `", cmd, "` in dir ", dir)
4750
// TODO - use enums to define commands instead of passing strings down from the caller
4851
// #nosec G204
4952
command := exec.CommandContext(ctx, cmd[0], cmd[1:]...)
@@ -54,7 +57,7 @@ func runLocalCommand(ctx context.Context, dir string, env []string, cmd ...strin
5457
return command.Run()
5558
}
5659

57-
func DebugPulumi(ctx context.Context, env []string, cmd ...string) error {
60+
func DebugPulumiNodeJS(ctx context.Context, env []string, cmd ...string) error {
5861
// Locally we use the "dev" script from package.json to run Pulumi commands, which uses ts-node
5962
localCmd := append([]string{"npm", "run", "dev"}, cmd...)
6063
term.Debug(strings.Join(append(env, localCmd...), " "))
@@ -73,7 +76,35 @@ func DebugPulumi(ctx context.Context, env []string, cmd ...string) error {
7376
return err
7477
}
7578
// We always return an error to stop the CLI from "tailing" the cloud logs
76-
return errors.New("local pulumi command succeeded; stopping")
79+
return ErrLocalPulumiStopped
80+
}
81+
82+
func DebugPulumiGolang(ctx context.Context, env []string, cmd ...string) error {
83+
localCmd := append([]string{"go", "run", "./..."}, cmd...)
84+
term.Debug(strings.Join(append(env, localCmd...), " "))
85+
86+
dir := os.Getenv("DEFANG_PULUMI_DIR")
87+
if dir == "" {
88+
return nil // show the shell command, but use regular Pulumi command in cloud task
89+
}
90+
91+
if gopath, err := exec.Command("go", "env", "GOPATH").Output(); err != nil {
92+
return err
93+
} else {
94+
env = append(env, "GOPATH="+strings.TrimSpace(string(gopath)))
95+
}
96+
97+
// Run the Pulumi command locally
98+
env = append([]string{
99+
"PATH=" + os.Getenv("PATH"),
100+
"USER=" + pkg.GetCurrentUser(), // needed for Pulumi
101+
"HOME=" + os.Getenv("HOME"), // needed for go
102+
}, env...)
103+
if err := runLocalCommand(ctx, path.Join(dir, "cd", "gcp"), env, localCmd...); err != nil {
104+
return err
105+
}
106+
// We always return an error to stop the CLI from "tailing" the cloud logs
107+
return ErrLocalPulumiStopped
77108
}
78109

79110
func GetPrivateDomain(projectName string) string {

src/pkg/cli/client/byoc/common_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package byoc
22

3-
import "testing"
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
)
48

59
func TestGetPulumiBackend(t *testing.T) {
610
const stateUrl = "s3://my-bucket"
@@ -78,3 +82,37 @@ func TestGetPulumiBackend(t *testing.T) {
7882
})
7983
}
8084
}
85+
86+
func TestDebugPulumiGolang(t *testing.T) {
87+
pulumiDir := os.Getenv("DEFANG_PULUMI_DIR")
88+
if pulumiDir == "" {
89+
t.Skip("DEFANG_PULUMI_DIR not set; skipping local Pulumi Golang test")
90+
}
91+
92+
// REGION=us-central1 GCP_PROJECT_ID=jordan-project-463223 PULUMI_COPILOT=false PROJECT=nodejs-http PULUMI_BACKEND_URL=gs://defang-cd-tk0ak2pj5jwi PULUMI_CONFIG_PASSPHRASE=asdf PULUMI_SKIP_UPDATE_CHECK=true DEFANG_PREFIX=Defang DEFANG_STATE_URL=gs://defang-cd-tk0ak2pj5jwi GCP_PROJECT=jordan-project-463223 STACK=beta DEFANG_ORG=defang DEFANG_DEBUG=1 DEFANG_MODE=mode_unspecified DOMAIN=beta.nodejs-http.jordanstephens.defang.app DEFANG_JSON= REGION=us-central1 DEFANG_ETAG=iqzc5ldbyhsg
93+
envs := []string{
94+
"REGION=us-central1",
95+
"GCP_PROJECT_ID=jordan-project-463223",
96+
"PULUMI_COPILOT=false",
97+
"PROJECT=golang-http",
98+
"PULUMI_BACKEND_URL=gs://defang-cd-tk0ak2pj5jwi",
99+
"PULUMI_CONFIG_PASSPHRASE=asdf",
100+
"PULUMI_SKIP_UPDATE_CHECK=true",
101+
"DEFANG_PREFIX=Defang",
102+
"DEFANG_STATE_URL=gs://defang-cd-tk0ak2pj5jwi",
103+
"GCP_PROJECT=jordan-project-463223",
104+
"STACK=beta",
105+
"DEFANG_ORG=defang",
106+
"DEFANG_DEBUG=1",
107+
"DEFANG_MODE=mode_unspecified",
108+
"DOMAIN=beta.nodejs-http.jordanstephens.defang.app",
109+
"DEFANG_JSON=",
110+
"REGION=us-central1",
111+
"DEFANG_ETAG=iqzc5ldbyhsg",
112+
}
113+
114+
err := DebugPulumiGolang(context.Background(), envs, "up", "CqIBCgwKA2FwcDIFCLgXGAESNGFwcC0tMzAwMC5iZXRhLm5vZGVqcy1odHRwLmpvcmRhbnN0ZXBoZW5zLmRlZmFuZy5hcHAaC25vZGVqcy1odHRwIgxpcXpjNWxkYnloc2cqDEJVSUxEX1FVRVVFREouYXBwLmJldGEubm9kZWpzLWh0dHAuam9yZGFuc3RlcGhlbnMuZGVmYW5nLmFwcHgBkAEBIugEbmFtZTogbm9kZWpzLWh0dHAKc2VydmljZXM6CiAgYXBwOgogICAgYnVpbGQ6CiAgICAgIGNvbnRleHQ6IGdzOi8vZGVmYW5nLWNkLXRrMGFrMnBqNWp3aS91cGxvYWRzL3NoYTI1Ni03R2Z4eG51dmEwWkclMkJrYkxKVGI0SFVmZndFNGkwTDByT0JwT29Zdm03RVklM0QudGFyLmd6CiAgICAgIGRvY2tlcmZpbGU6IERvY2tlcmZpbGUKICAgIGRlcGxveToKICAgICAgcmVzb3VyY2VzOgogICAgICAgIHJlc2VydmF0aW9uczoKICAgICAgICAgIG1lbW9yeTogIjI2ODQzNTQ1NiIKICAgIGhlYWx0aGNoZWNrOgogICAgICB0ZXN0OgogICAgICAgIC0gQ01ECiAgICAgICAgLSBjdXJsCiAgICAgICAgLSAtZgogICAgICAgIC0gaHR0cDovL2xvY2FsaG9zdDozMDAwLwogICAgbmV0d29ya3M6CiAgICAgIGRlZmF1bHQ6IG51bGwKICAgIHBvcnRzOgogICAgICAtIG1vZGU6IGluZ3Jlc3MKICAgICAgICB0YXJnZXQ6IDMwMDAKICAgICAgICBwdWJsaXNoZWQ6ICIzMDAwIgogICAgICAgIHByb3RvY29sOiB0Y3AKICAgICAgICBhcHBfcHJvdG9jb2w6IGh0dHAKICAgIHJlc3RhcnQ6IHVubGVzcy1zdG9wcGVkCm5ldHdvcmtzOgogIGRlZmF1bHQ6CiAgICBuYW1lOiBub2RlanMtaHR0cF9kZWZhdWx0CiqDAWluZGV4LmRvY2tlci5pby9kZWZhbmdpby9jZDpwdWJsaWMtZ2NwLXYwLjYuMC04ODQtZzRlYzEwNDc3QHNoYTI1Njo1YWRhYzFmZWI1NzJmZDkzMzQwY2VkNWFlZGQ4NTE2ZDdkNTRiZDQwOWJiYWExNTRiYzY4YTQxMGJhZDg3OTM1")
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
}

src/pkg/cli/client/byoc/do/byoc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func (b *ByocDo) runCdCommand(ctx context.Context, projectName, delegateDomain s
608608
for i, v := range env {
609609
debugEnv[i] = v.Key + "=" + v.Value
610610
}
611-
if err := byoc.DebugPulumi(ctx, debugEnv, cmd...); err != nil {
611+
if err := byoc.DebugPulumiNodeJS(ctx, debugEnv, cmd...); err != nil {
612612
return nil, err
613613
}
614614
}

src/pkg/cli/client/byoc/gcp/byoc.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,19 @@ func (b *ByocGcp) runCdCommand(ctx context.Context, cmd cdCommand) (string, erro
372372
env[k] = v
373373
}
374374

375+
if os.Getenv("DEFANG_PULUMI_DIR") != "" {
376+
debugEnv := []string{"REGION=" + b.driver.Region}
377+
if gcpProject := os.Getenv("GCP_PROJECT_ID"); gcpProject != "" {
378+
debugEnv = append(debugEnv, "GCP_PROJECT_ID="+gcpProject)
379+
}
380+
for k, v := range env {
381+
debugEnv = append(debugEnv, k+"="+v)
382+
}
383+
if err := byoc.DebugPulumiGolang(ctx, debugEnv, cmd.Command...); err != nil {
384+
return "", err
385+
}
386+
}
387+
375388
execution, err := b.driver.Run(ctx, gcp.JobNameCD, env, cmd.Command...)
376389
if err != nil {
377390
return "", err

0 commit comments

Comments
 (0)