Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kt5356 committed Jun 23, 2024
1 parent be1cb7c commit 35aadf9
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 54 deletions.
134 changes: 92 additions & 42 deletions src/pkg/cli/client/byoc/do/byoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/digitalocean/godo"
"os"
"strings"

Expand All @@ -21,8 +22,10 @@ import (
)

const (
DockerHub = "DOCKER_HUB"
Docr = "DOCR"
DockerHub = "DOCKER_HUB"
Docr = "DOCR"
Secret = "SECRET"
CommandPrefix = "node lib/index.js"
)

type ByocDo struct {
Expand Down Expand Up @@ -111,19 +114,16 @@ func (b *ByocDo) Deploy(ctx context.Context, req *defangv1.DeployRequest) (*defa

term.Debug(fmt.Sprintf("PAYLOAD STRING: %s", payloadString))

//appID, err := b.runCdCommand(ctx, "up", payloadString)
//if err != nil {
// return nil, err
//}
//b.AppIds[etag] = appID

//return &defangv1.DeployResponse{
// Services: serviceInfos,
// Etag: etag,
//}, nil
appID, err := b.runCdCommand(ctx, fmt.Sprintf("%s %s %s", CommandPrefix, "up", payloadString))
if err != nil {
return nil, err
}
b.appIds[etag] = appID

res := &defangv1.DeployResponse{}
return res, nil
return &defangv1.DeployResponse{
Services: serviceInfos,
Etag: etag,
}, nil
}

func (b *ByocDo) BootstrapCommand(ctx context.Context, command string) (string, error) {
Expand Down Expand Up @@ -210,36 +210,86 @@ func (b *ByocDo) Get(ctx context.Context, s *defangv1.ServiceID) (*defangv1.Serv
return nil, nil
}

func (b *ByocDo) runCdCommand(ctx context.Context, cmd ...string) (string, error) {
func (b *ByocDo) runCdCommand(ctx context.Context, cmd string) (string, error) {
env := b.environment()
if term.DoDebug {
debugEnv := " -"
for k, v := range env {
debugEnv += " " + k + "=" + v
}
term.Debug(debugEnv, "npm run dev", strings.Join(cmd, " "))
}
return b.driver.Run(ctx, env, cmd...)
//if term.DoDebug {
// debugEnv := " -"
// for k, v := range env {
// debugEnv += " " + k + "=" + v
// }
// term.Debug(debugEnv, "npm run dev", strings.Join(cmd, " "))
//}
return b.driver.Run(ctx, env, cmd)
}

func (b *ByocDo) environment() map[string]string {
func (b *ByocDo) environment() []*godo.AppVariableDefinition {
region := b.driver.Region // TODO: this should be the destination region, not the CD region; make customizable
return map[string]string{
// "AWS_REGION": region.String(), should be set by ECS (because of CD task role)
"DEFANG_PREFIX": byoc.DefangPrefix,
"DEFANG_DEBUG": os.Getenv("DEFANG_DEBUG"), // TODO: use the global DoDebug flag
"DEFANG_ORG": b.TenantID,
"DOMAIN": b.ProjectDomain,
"PRIVATE_DOMAIN": b.PrivateDomain,
"PROJECT": b.PulumiProject,
"PULUMI_BACKEND_URL": fmt.Sprintf(`s3://%s.digitaloceanspaces.com/%s`, region, b.driver.BucketName), // TODO: add a way to override bucket
"PULUMI_CONFIG_PASSPHRASE": pkg.Getenv("PULUMI_CONFIG_PASSPHRASE", "asdf"), // TODO: make customizable
"STACK": b.PulumiStack,
"NPM_CONFIG_UPDATE_NOTIFIER": "false",
"PULUMI_SKIP_UPDATE_CHECK": "true",
"DO_PAT": os.Getenv("DO_PAT"),
"DO_SPACES_ID": os.Getenv("DO_SPACES_ID"),
"DO_SPACES_KEY": os.Getenv("DO_SPACES_KEY"),
return []*godo.AppVariableDefinition{
{
Key: "DEFANG_PREFIX",
Value: byoc.DefangPrefix,
},
{
Key: "DEFANG_DEBUG",
Value: pkg.Getenv("DEFANG_DEBUG", "false"),
},
{
Key: "DEFANG_ORG",
Value: b.TenantID,
},
{
Key: "DEFANG_CLOUD",
Value: "do",
},
{
Key: "DOMAIN",
Value: b.ProjectDomain,
},
{
Key: "PRIVATE_DOMAIN",
Value: b.PrivateDomain,
},
{
Key: "PROJECT",
Value: b.PulumiProject,
},
{
Key: "PULUMI_BACKEND_URL",
Value: fmt.Sprintf(`s3://%s.digitaloceanspaces.com/%s`, region, b.driver.BucketName),
},
{
Key: "PULUMI_CONFIG_PASSPHRASE",
Value: pkg.Getenv("PULUMI_CONFIG_PASSPHRASE", "asdf"),
},
{
Key: "STACK",
Value: b.PulumiStack,
},
{
Key: "NPM_CONFIG_UPDATE_NOTIFIER",
Value: "false",
},
{
Key: "PULUMI_SKIP_UPDATE_CHECK",
Value: "true",
},
{
Key: "DIGITALOCEAN_TOKEN",
Value: os.Getenv("DO_PAT"),
},
{
Key: "SPACES_ACCESS_KEY_ID",
Value: pkg.Getenv("SPACES_ACCESS_KEY_ID", ""),
Type: Secret,
},
{
Key: "SPACES_SECRET_ACCESS_KEY",
Value: pkg.Getenv("SPACES_SECRET_ACCESS_KEY", ""),
},
{
Key: "REGION",
Value: "sfo3",
},
}
}

Expand Down Expand Up @@ -287,8 +337,8 @@ func (b *ByocDo) setUp(ctx context.Context) error {
// Kind: godo.AppJobSpecKind_PreDeploy,
// },
//}

//if err := b.Driver.SetUp(ctx, serviceContainers, jobContainers); err != nil {
//
//if err := b.driver.SetUp(ctx, serviceContainers, jobContainers); err != nil {
// return err
//}

Expand Down
36 changes: 24 additions & 12 deletions src/pkg/clouds/do/appPlatform/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"regexp"
)

const (
DockerHub = "DOCKER_HUB"
Docr = "DOCR"
)

type DoAppPlatform struct {
DoApp
appName string
Expand Down Expand Up @@ -69,20 +74,27 @@ func (d DoApp) SetUp(ctx context.Context, services []*godo.AppServiceSpec, jobs
return nil
}

func (d DoApp) Run(ctx context.Context, env map[string]string, cmd ...string) (string, error) {
//client := d.newClient(ctx)
func (d DoApp) Run(ctx context.Context, env []*godo.AppVariableDefinition, cmd string) (string, error) {
client := d.newClient(ctx)

//app, _, err := client.Apps.Get(ctx, d.AppID)
//if err != nil {
// return "", err
//}
//
//appInfo, _, err := client.Apps.Update(ctx, d.AppID, &godo.AppUpdateRequest{
// Spec: app.Spec,
//})
app, _, err := client.Apps.Create(ctx, &godo.AppCreateRequest{
Spec: &godo.AppSpec{
Name: "defang-cd",
Services: []*godo.AppServiceSpec{{
Name: fmt.Sprintf("defang-cd-%s", d.ProjectName),
Envs: env,
Image: &godo.ImageSourceSpec{
RegistryType: Docr,
Repository: "defangmvp/do-cd",
},
InstanceCount: 1,
InstanceSizeSlug: "basic-xs",
RunCommand: cmd,
}},
},
})

//return appInfo.ID, err
return "foo", nil
return app.ID, err
}

func (d DoApp) newClient(ctx context.Context) *godo.Client {
Expand Down

0 comments on commit 35aadf9

Please sign in to comment.