Skip to content

Commit f126130

Browse files
edwardrfedw-defang
andauthored
Add behavior to aws byoc client (#627)
* Add behavior to aws byoc client * Use unspecified as 0 for behavior * Rename to UNSPECIFIED_BEHAVIOR and create behavior flag type * Hide unspecifed_behavior from help message --------- Co-authored-by: Edward J <[email protected]>
1 parent 6a0d57d commit f126130

File tree

8 files changed

+240
-216
lines changed

8 files changed

+240
-216
lines changed

src/cmd/cli/command/behavior.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
8+
)
9+
10+
type Behavior defangv1.Behavior
11+
12+
func (b Behavior) String() string {
13+
return strings.ToLower(defangv1.Behavior_name[int32(b)])
14+
}
15+
func (b *Behavior) Set(s string) error {
16+
behavior, ok := defangv1.Behavior_value[strings.ToUpper(s)]
17+
if !ok {
18+
return fmt.Errorf("invalid behavior: %s, valid values are: %v", s, strings.Join(allBehaviors(), ", "))
19+
}
20+
*b = Behavior(behavior)
21+
return nil
22+
}
23+
func (b Behavior) Type() string {
24+
return "behavior"
25+
}
26+
27+
func (b Behavior) Value() defangv1.Behavior {
28+
return defangv1.Behavior(b)
29+
}
30+
31+
func allBehaviors() []string {
32+
behaviors := make([]string, 0, len(defangv1.Behavior_name)-1)
33+
for i, behavior := range defangv1.Behavior_name {
34+
if i == 0 {
35+
continue
36+
}
37+
behaviors = append(behaviors, strings.ToLower(behavior))
38+
}
39+
return behaviors
40+
}

src/cmd/cli/command/compose.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
)
2020

2121
func makeComposeUpCmd() *cobra.Command {
22+
behavior := Behavior(defangv1.Behavior_DEVELOPMENT)
2223
composeUpCmd := &cobra.Command{
2324
Use: "up",
2425
Annotations: authNeededAnnotation,
@@ -29,7 +30,7 @@ func makeComposeUpCmd() *cobra.Command {
2930
var detach, _ = cmd.Flags().GetBool("detach")
3031

3132
since := time.Now()
32-
deploy, project, err := cli.ComposeUp(cmd.Context(), client, force)
33+
deploy, project, err := cli.ComposeUp(cmd.Context(), client, force, behavior.Value())
3334
if err != nil {
3435
if !errors.Is(err, types.ErrComposeFileNotFound) {
3536
return err
@@ -151,6 +152,7 @@ func makeComposeUpCmd() *cobra.Command {
151152
composeUpCmd.Flags().BoolP("detach", "d", false, "run in detached mode")
152153
composeUpCmd.Flags().Bool("force", false, "force a build of the image even if nothing has changed")
153154
composeUpCmd.Flags().Bool("tail", false, "tail the service logs after updating") // obsolete, but keep for backwards compatibility
155+
composeUpCmd.Flags().VarP(&behavior, "behavior", "b", "behavior for the deployment, possible values: "+strings.Join(allBehaviors(), ", "))
154156
_ = composeUpCmd.Flags().MarkHidden("tail")
155157
return composeUpCmd
156158
}
@@ -165,7 +167,7 @@ func makeComposeStartCmd() *cobra.Command {
165167
RunE: func(cmd *cobra.Command, args []string) error {
166168
var force, _ = cmd.Flags().GetBool("force")
167169

168-
deploy, _, err := cli.ComposeUp(cmd.Context(), client, force)
170+
deploy, _, err := cli.ComposeUp(cmd.Context(), client, force, defangv1.Behavior_UNSPECIFIED_BEHAVIOR)
169171
if err != nil {
170172
return err
171173
}
@@ -283,7 +285,7 @@ func makeComposeConfigCmd() *cobra.Command {
283285
RunE: func(cmd *cobra.Command, args []string) error {
284286
cli.DoDryRun = true // config is like start in a dry run
285287
// force=false to calculate the digest
286-
if _, _, err := cli.ComposeUp(cmd.Context(), client, false); !errors.Is(err, cli.ErrDryRun) {
288+
if _, _, err := cli.ComposeUp(cmd.Context(), client, false, defangv1.Behavior_UNSPECIFIED_BEHAVIOR); !errors.Is(err, cli.ErrDryRun) {
287289
return err
288290
}
289291
return nil

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (b *ByocAws) Deploy(ctx context.Context, req *defangv1.DeployRequest) (*def
178178
return nil, err
179179
}
180180
}
181-
taskArn, err := b.runCdCommand(ctx, "up", payloadString)
181+
taskArn, err := b.runCdCommand(ctx, req.Behavior, "up", payloadString)
182182
if err != nil {
183183
return nil, err
184184
}
@@ -326,8 +326,9 @@ func (b *ByocAws) environment() map[string]string {
326326
}
327327
}
328328

329-
func (b *ByocAws) runCdCommand(ctx context.Context, cmd ...string) (ecs.TaskArn, error) {
329+
func (b *ByocAws) runCdCommand(ctx context.Context, behavior defangv1.Behavior, cmd ...string) (ecs.TaskArn, error) {
330330
env := b.environment()
331+
env["DEFANG_BEHAVIOR"] = strings.ToLower(behavior.String())
331332
if term.DoDebug() {
332333
debugEnv := fmt.Sprintf("AWS_REGION=%q", b.driver.Region)
333334
if awsProfile := os.Getenv("AWS_PROFILE"); awsProfile != "" {
@@ -346,7 +347,7 @@ func (b *ByocAws) Delete(ctx context.Context, req *defangv1.DeleteRequest) (*def
346347
return nil, err
347348
}
348349
// FIXME: this should only delete the services that are specified in the request, not all
349-
taskArn, err := b.runCdCommand(ctx, "up", "")
350+
taskArn, err := b.runCdCommand(ctx, defangv1.Behavior_UNSPECIFIED_BEHAVIOR, "up", "")
350351
if err != nil {
351352
return nil, annotateAwsError(err)
352353
}
@@ -655,7 +656,7 @@ func (b *ByocAws) BootstrapCommand(ctx context.Context, command string) (string,
655656
if err := b.setUp(ctx); err != nil {
656657
return "", err
657658
}
658-
cdTaskArn, err := b.runCdCommand(ctx, command)
659+
cdTaskArn, err := b.runCdCommand(ctx, defangv1.Behavior_UNSPECIFIED_BEHAVIOR, command)
659660
if err != nil || cdTaskArn == nil {
660661
return "", annotateAwsError(err)
661662
}

src/pkg/cli/composeUp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func buildContext(force bool) compose.BuildContext {
3030
}
3131

3232
// ComposeUp validates a compose project and uploads the services using the client
33-
func ComposeUp(ctx context.Context, c client.Client, force bool) (*defangv1.DeployResponse, *types.Project, error) {
33+
func ComposeUp(ctx context.Context, c client.Client, force bool, behavior defangv1.Behavior) (*defangv1.DeployResponse, *types.Project, error) {
3434
project, err := c.LoadProject(ctx)
3535
if err != nil {
3636
return nil, project, err
@@ -61,6 +61,7 @@ func ComposeUp(ctx context.Context, c client.Client, force bool) (*defangv1.Depl
6161
}
6262

6363
resp, err := c.Deploy(ctx, &defangv1.DeployRequest{
64+
Behavior: behavior,
6465
Project: project.Name,
6566
Services: services,
6667
})

src/pkg/cli/composeUp_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/DefangLabs/defang/src/pkg/cli/client"
1111
"github.com/DefangLabs/defang/src/pkg/cli/compose"
12+
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
1213
)
1314

1415
func TestComposeUp(t *testing.T) {
@@ -26,7 +27,7 @@ func TestComposeUp(t *testing.T) {
2627
}))
2728
defer server.Close()
2829

29-
_, project, err := ComposeUp(context.Background(), client.MockClient{UploadUrl: server.URL + "/", Project: proj}, false)
30+
_, project, err := ComposeUp(context.Background(), client.MockClient{UploadUrl: server.URL + "/", Project: proj}, false, defangv1.Behavior_DEVELOPMENT)
3031
if !errors.Is(err, ErrDryRun) {
3132
t.Fatalf("ComposeUp() failed: %v", err)
3233
}

src/pkg/cmd/color.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)