Skip to content

Commit 03f9e17

Browse files
committed
wip
1 parent 5f74a78 commit 03f9e17

File tree

19 files changed

+285
-80
lines changed

19 files changed

+285
-80
lines changed

src/cmd/cli/command/commands.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/DefangLabs/defang/src/pkg/cli"
1919
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
2020
"github.com/DefangLabs/defang/src/pkg/cli/client/byoc"
21+
byocAws "github.com/DefangLabs/defang/src/pkg/cli/client/byoc/aws"
2122
"github.com/DefangLabs/defang/src/pkg/cli/client/byoc/gcp"
2223
"github.com/DefangLabs/defang/src/pkg/cli/compose"
2324
"github.com/DefangLabs/defang/src/pkg/clouds/aws"
@@ -141,6 +142,17 @@ func Execute(ctx context.Context) error {
141142
return nil
142143
}
143144

145+
var cloudformationCmd = &cobra.Command{
146+
Use: "cloudformation",
147+
Short: "CloudFormation template related commands",
148+
Hidden: true,
149+
RunE: func(cmd *cobra.Command, args []string) error {
150+
template, err := byocAws.PrintCloudFormationTemplate()
151+
term.Println(string(template))
152+
return err
153+
},
154+
}
155+
144156
func SetupCommands(ctx context.Context, version string) {
145157
cobra.EnableTraverseRunHooks = true // we always need to run the RootCmd's pre-run hook
146158

@@ -222,8 +234,10 @@ func SetupCommands(ctx context.Context, version string) {
222234
lsCommand.Aliases = []string{"getServices", "ps", "ls", "list"}
223235
RootCmd.AddCommand(lsCommand)
224236

225-
// Get Status Command
226-
RootCmd.AddCommand(getVersionCmd)
237+
// Version Command
238+
RootCmd.AddCommand(versionCmd)
239+
240+
RootCmd.AddCommand(cloudformationCmd)
227241

228242
// Config Command (was: secrets)
229243
configSetCmd.Flags().BoolP("name", "n", false, "name of the config (backwards compat)")
@@ -614,7 +628,7 @@ func collectUnsetEnvVars(project *composeTypes.Project) []string {
614628
return nil
615629
}
616630

617-
var getVersionCmd = &cobra.Command{
631+
var versionCmd = &cobra.Command{
618632
Use: "version",
619633
Args: cobra.NoArgs,
620634
Aliases: []string{"ver", "stat", "status"}, // for backwards compatibility

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

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
3636
"github.com/aws/aws-sdk-go-v2/service/sts"
3737
"github.com/aws/smithy-go"
38-
"github.com/aws/smithy-go/ptr"
3938
"github.com/bufbuild/connect-go"
4039
composeTypes "github.com/compose-spec/compose-go/v2/types"
4140
"google.golang.org/protobuf/proto"
@@ -128,48 +127,18 @@ func initStsClient(cfg awssdk.Config) {
128127
}
129128
}
130129

130+
func (b *ByocAws) makeContainers() []types.Container {
131+
return makeContainers(b.PulumiVersion, b.CDImage)
132+
}
133+
131134
func (b *ByocAws) SetUpCD(ctx context.Context) error {
132135
if b.SetupDone {
133136
return nil
134137
}
135138

136139
term.Debugf("Using CD image: %q", b.CDImage)
137140

138-
cdTaskName := byoc.CdTaskPrefix
139-
containers := []types.Container{
140-
{
141-
// FIXME: get the Pulumi image or version from Fabric: https://github.com/DefangLabs/defang/issues/1027
142-
Image: "public.ecr.aws/pulumi/pulumi-nodejs:" + b.PulumiVersion,
143-
Name: ecs.CdContainerName,
144-
Cpus: 2.0,
145-
Memory: 2048_000_000, // 2G
146-
Essential: ptr.Bool(true),
147-
VolumesFrom: []string{
148-
cdTaskName,
149-
},
150-
WorkDir: "/app",
151-
DependsOn: map[string]types.ContainerCondition{cdTaskName: "START"},
152-
EntryPoint: []string{"node", "lib/index.js"},
153-
},
154-
{
155-
Image: b.CDImage,
156-
Name: cdTaskName,
157-
Essential: ptr.Bool(false),
158-
Volumes: []types.TaskVolume{
159-
{
160-
Source: "pulumi-plugins",
161-
Target: "/root/.pulumi/plugins",
162-
ReadOnly: true,
163-
},
164-
{
165-
Source: "cd",
166-
Target: "/app",
167-
ReadOnly: true,
168-
},
169-
},
170-
},
171-
}
172-
if err := b.driver.SetUp(ctx, containers); err != nil {
141+
if err := b.driver.SetUp(ctx, b.makeContainers()); err != nil {
173142
return AnnotateAwsError(err)
174143
}
175144

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package aws
2+
3+
import (
4+
"github.com/DefangLabs/defang/src/pkg/cli/client/byoc"
5+
"github.com/DefangLabs/defang/src/pkg/clouds/aws/ecs"
6+
"github.com/DefangLabs/defang/src/pkg/clouds/aws/ecs/cfn"
7+
"github.com/DefangLabs/defang/src/pkg/types"
8+
"github.com/aws/smithy-go/ptr"
9+
)
10+
11+
func makeContainers(pulumiVersion, cdImage string) []types.Container {
12+
cdSidecarName := byoc.CdTaskPrefix
13+
return []types.Container{
14+
{
15+
Image: "public.ecr.aws/pulumi/pulumi-nodejs:" + pulumiVersion,
16+
Name: ecs.CdContainerName,
17+
Cpus: 2.0,
18+
Memory: 2048_000_000, // 2G
19+
Essential: ptr.Bool(true),
20+
VolumesFrom: []string{
21+
cdSidecarName,
22+
},
23+
WorkDir: "/app",
24+
DependsOn: map[string]types.ContainerCondition{cdSidecarName: "START"},
25+
EntryPoint: []string{"node", "lib/index.js"},
26+
},
27+
{
28+
Image: cdImage,
29+
Name: cdSidecarName,
30+
Essential: ptr.Bool(false),
31+
Volumes: []types.TaskVolume{
32+
{
33+
Source: "pulumi-plugins",
34+
Target: "/root/.pulumi/plugins",
35+
ReadOnly: true,
36+
},
37+
{
38+
Source: "cd",
39+
Target: "/app",
40+
ReadOnly: true,
41+
},
42+
},
43+
},
44+
}
45+
}
46+
47+
func PrintCloudFormationTemplate() ([]byte, error) {
48+
containers := makeContainers("latest", "defanglabs/defang-cd:latest")
49+
template := cfn.CreateTemplate("test-stack", containers)
50+
return template.YAML()
51+
}

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,15 @@ func NewByocBaseClient(tenantName types.TenantName, backend ProjectBackend, stac
6868
return b
6969
}
7070

71-
func (b *ByocBaseClient) GetProjectLastCDImage(ctx context.Context, projectName string) (string, error) {
72-
projUpdate, err := b.projectBackend.GetProjectUpdate(ctx, projectName)
73-
if err != nil {
74-
return "", err
75-
}
76-
77-
if projUpdate == nil {
78-
return "", nil
79-
}
80-
81-
return projUpdate.CdVersion, nil
82-
}
83-
8471
func (b *ByocBaseClient) Debug(context.Context, *defangv1.DebugRequest) (*defangv1.DebugResponse, error) {
8572
return nil, client.ErrNotImplemented("AI debugging is not yet supported for BYOC")
8673
}
8774

8875
func (b *ByocBaseClient) SetCanIUseConfig(quotas *defangv1.CanIUseResponse) {
89-
// Allow local override of the CD image
90-
b.CDImage = pkg.Getenv("DEFANG_CD_IMAGE", quotas.CdImage)
91-
b.AllowScaling = quotas.AllowScaling
92-
b.AllowGPU = quotas.Gpu
93-
b.PulumiVersion = pkg.Getenv("DEFANG_PULUMI_VERSION", quotas.PulumiVersion)
76+
b.CanIUseConfig.AllowGPU = quotas.Gpu
77+
b.CanIUseConfig.AllowScaling = quotas.AllowScaling
78+
b.CanIUseConfig.CDImage = quotas.CdImage
79+
b.CanIUseConfig.PulumiVersion = quotas.PulumiVersion
9480
}
9581

9682
func (b *ByocBaseClient) ServicePrivateDNS(name string) string {

src/pkg/cli/client/caniuse.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package client
33
import (
44
"context"
55

6+
"github.com/DefangLabs/defang/src/pkg"
7+
"github.com/DefangLabs/defang/src/pkg/term"
68
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
79
)
810

@@ -25,6 +27,17 @@ func CanIUseProvider(ctx context.Context, client FabricClient, provider Provider
2527
if err != nil {
2628
return err
2729
}
30+
31+
// Allow local override of the CD image and Pulumi version
32+
resp.CdImage = pkg.Getenv("DEFANG_CD_IMAGE", resp.CdImage)
33+
resp.PulumiVersion = pkg.Getenv("DEFANG_PULUMI_VERSION", resp.PulumiVersion)
34+
if resp.CdImage == "previous" {
35+
if projUpdate, err := provider.GetProjectUpdate(ctx, projectName); err != nil {
36+
term.Debugf("unable to get project update for project %s: %v", projectName, err)
37+
} else if projUpdate != nil {
38+
resp.CdImage = projUpdate.CdVersion
39+
}
40+
}
2841
provider.SetCanIUseConfig(resp)
2942
return nil
3043
}

src/pkg/clouds/aws/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (a *Aws) LoadConfig(ctx context.Context) (aws.Config, error) {
2727
return cfg, err
2828
}
2929
if cfg.Region == "" {
30-
return cfg, errors.New("missing AWS region: set AWS_REGION or edit your AWS profile")
30+
return cfg, errors.New("missing AWS region: set AWS_REGION or edit your AWS profile at ~/.aws/config")
3131
}
3232
a.Region = Region(cfg.Region)
3333
return cfg, err

src/pkg/clouds/aws/ecs/cfn/outputs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cfn
22

33
const (
44
OutputsBucketName = "bucketName"
5+
OutputsCIRoleARN = "ciRoleArn"
56
OutputsClusterName = "clusterName"
67
OutputsDefaultSecurityGroupID = "defaultSecurityGroupId"
78
OutputsLogGroupARN = "logGroupArn"

src/pkg/clouds/aws/ecs/cfn/params.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ const (
99
ParamsOidcProviderSubjects = "OidcProviderSubjects"
1010
ParamsOidcProviderThumbprints = "OidcProviderThumbprints"
1111
ParamsRetainBucket = "RetainBucket"
12-
ParamsUseSpotInstances = "UseSpotInstances"
1312
)

0 commit comments

Comments
 (0)