This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
Introduce ECS emulation mode #544
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f8bb03
Introduce ECS emulation mode
ndeloof c1ecb2b
Check we have compose 1.27 or later
ndeloof fed50d7
introduce ecs-local context
ndeloof 4693ce9
e2e test to check ecs-local context creation
ndeloof 4d11594
default description if none set by user
ndeloof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2020 Docker, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package local | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/docker/compose-cli/compose" | ||
"github.com/docker/compose-cli/containers" | ||
"github.com/docker/compose-cli/secrets" | ||
"github.com/docker/docker/client" | ||
|
||
"github.com/docker/compose-cli/backend" | ||
"github.com/docker/compose-cli/context/cloud" | ||
"github.com/docker/compose-cli/context/store" | ||
) | ||
|
||
const backendType = store.EcsLocalSimulationContextType | ||
|
||
func init() { | ||
backend.Register(backendType, backendType, service, getCloudService) | ||
} | ||
|
||
type ecsLocalSimulation struct { | ||
moby *client.Client | ||
} | ||
|
||
func service(ctx context.Context) (backend.Service, error) { | ||
apiClient, err := client.NewClientWithOpts(client.FromEnv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ecsLocalSimulation{ | ||
moby: apiClient, | ||
}, nil | ||
} | ||
|
||
func getCloudService() (cloud.Service, error) { | ||
return ecsLocalSimulation{}, nil | ||
} | ||
|
||
func (e ecsLocalSimulation) ContainerService() containers.Service { | ||
return nil | ||
} | ||
|
||
func (e ecsLocalSimulation) SecretsService() secrets.Service { | ||
return nil | ||
} | ||
|
||
func (e ecsLocalSimulation) ComposeService() compose.Service { | ||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
Copyright 2020 Docker, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package local | ||
|
||
import ( | ||
ndeloof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"bufio" | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/docker/compose-cli/compose" | ||
"github.com/docker/compose-cli/errdefs" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/compose-spec/compose-go/types" | ||
"github.com/pkg/errors" | ||
"github.com/sanathkr/go-yaml" | ||
"golang.org/x/mod/semver" | ||
) | ||
|
||
func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project) error { | ||
cmd := exec.Command("docker-compose", "version", "--short") | ||
b := bytes.Buffer{} | ||
b.WriteString("v") | ||
cmd.Stdout = bufio.NewWriter(&b) | ||
err := cmd.Run() | ||
if err != nil { | ||
return errors.Wrap(err, "ECS simulation mode require Docker-compose 1.27") | ||
} | ||
version := semver.MajorMinor(strings.TrimSpace(b.String())) | ||
if version == "" { | ||
return fmt.Errorf("can't parse docker-compose version: %s", b.String()) | ||
} | ||
if semver.Compare(version, "v1.27") < 0 { | ||
return fmt.Errorf("ECS simulation mode require Docker-compose 1.27, found %s", version) | ||
} | ||
|
||
converted, err := e.Convert(ctx, project) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd = exec.Command("docker-compose", "--context", "default", "--project-directory", project.WorkingDir, "--project-name", project.Name, "-f", "-", "up") | ||
cmd.Stdin = strings.NewReader(string(converted)) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project) ([]byte, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a unit test on the global Convert method ? |
||
project.Networks["credentials_network"] = types.NetworkConfig{ | ||
Driver: "bridge", | ||
Ipam: types.IPAMConfig{ | ||
Config: []*types.IPAMPool{ | ||
{ | ||
Subnet: "169.254.170.0/24", | ||
Gateway: "169.254.170.1", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// On Windows, this directory can be found at "%UserProfile%\.aws" | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i, service := range project.Services { | ||
service.Networks["credentials_network"] = &types.ServiceNetworkConfig{ | ||
Ipv4Address: fmt.Sprintf("169.254.170.%d", i+3), | ||
} | ||
service.DependsOn = append(service.DependsOn, "ecs-local-endpoints") | ||
service.Environment["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = aws.String("/creds") | ||
service.Environment["ECS_CONTAINER_METADATA_URI"] = aws.String("http://169.254.170.2/v3") | ||
project.Services[i] = service | ||
} | ||
|
||
project.Services = append(project.Services, types.ServiceConfig{ | ||
Name: "ecs-local-endpoints", | ||
Image: "amazon/amazon-ecs-local-container-endpoints", | ||
Volumes: []types.ServiceVolumeConfig{ | ||
{ | ||
Type: types.VolumeTypeBind, | ||
Source: "/var/run", | ||
Target: "/var/run", | ||
}, | ||
{ | ||
Type: types.VolumeTypeBind, | ||
Source: filepath.Join(home, ".aws"), | ||
Target: "/home/.aws", | ||
}, | ||
}, | ||
Environment: map[string]*string{ | ||
"HOME": aws.String("/home"), | ||
"AWS_PROFILE": aws.String("default"), | ||
}, | ||
Networks: map[string]*types.ServiceNetworkConfig{ | ||
"credentials_network": { | ||
Ipv4Address: "169.254.170.2", | ||
}, | ||
}, | ||
}) | ||
|
||
delete(project.Networks, "default") | ||
config := map[string]interface{}{ | ||
"services": project.Services, | ||
"networks": project.Networks, | ||
"volumes": project.Volumes, | ||
"secrets": project.Secrets, | ||
"configs": project.Configs, | ||
} | ||
return yaml.Marshal(config) | ||
} | ||
|
||
func (e ecsLocalSimulation) Down(ctx context.Context, projectName string) error { | ||
return errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose down") | ||
} | ||
|
||
func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, w io.Writer) error { | ||
return errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose logs") | ||
} | ||
|
||
func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) { | ||
return nil, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose ps") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2020 Docker, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package local | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/docker/compose-cli/context/cloud" | ||
"github.com/docker/compose-cli/ecs" | ||
"github.com/docker/compose-cli/errdefs" | ||
) | ||
|
||
var _ cloud.Service = ecsLocalSimulation{} | ||
|
||
func (e ecsLocalSimulation) Login(ctx context.Context, params interface{}) error { | ||
return errdefs.ErrNotImplemented | ||
} | ||
|
||
func (e ecsLocalSimulation) Logout(ctx context.Context) error { | ||
return errdefs.ErrNotImplemented | ||
} | ||
|
||
func (e ecsLocalSimulation) CreateContextData(ctx context.Context, params interface{}) (contextData interface{}, description string, err error) { | ||
opts := params.(ecs.ContextParams) | ||
if opts.Description == "" { | ||
opts.Description = "ECS local endpoints" | ||
} | ||
return struct{}{}, opts.Description, nil | ||
gtardif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol our imports are all borked