2020-08-25 10:30:51 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-08-25 10:30:51 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-08-31 14:26:00 +02:00
|
|
|
package local
|
2020-08-25 10:30:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-17 11:02:31 +01:00
|
|
|
"encoding/json"
|
2020-08-25 10:30:51 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
2020-12-03 09:24:15 +01:00
|
|
|
"github.com/pkg/errors"
|
2020-12-15 17:21:09 +01:00
|
|
|
"github.com/sanathkr/go-yaml"
|
2021-01-15 16:24:00 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
"github.com/docker/compose-cli/pkg/api"
|
2020-08-25 10:30:51 +02:00
|
|
|
)
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Build(ctx context.Context, project *types.Project, options api.BuildOptions) error {
|
2021-03-02 09:03:04 +01:00
|
|
|
return e.compose.Build(ctx, project, options)
|
2020-11-30 12:03:13 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Push(ctx context.Context, project *types.Project, options api.PushOptions) error {
|
2021-03-05 13:20:13 +01:00
|
|
|
return e.compose.Push(ctx, project, options)
|
2020-12-01 11:34:48 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Pull(ctx context.Context, project *types.Project, options api.PullOptions) error {
|
2021-03-22 22:50:46 +01:00
|
|
|
return e.compose.Pull(ctx, project, options)
|
2020-12-03 12:22:01 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Create(ctx context.Context, project *types.Project, opts api.CreateOptions) error {
|
2020-12-15 17:21:09 +01:00
|
|
|
enhanced, err := e.enhanceForLocalSimulation(project)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:23:37 +01:00
|
|
|
return e.compose.Create(ctx, enhanced, opts)
|
2020-12-02 18:23:01 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, options api.StartOptions) error {
|
2021-02-08 11:04:46 +01:00
|
|
|
return e.compose.Start(ctx, project, options)
|
2020-12-02 18:23:01 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Restart(ctx context.Context, project *types.Project, options api.RestartOptions) error {
|
2021-03-09 22:09:45 +01:00
|
|
|
return e.compose.Restart(ctx, project, options)
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project, options api.StopOptions) error {
|
2021-02-15 13:54:55 +01:00
|
|
|
return e.compose.Stop(ctx, project, options)
|
2021-01-20 13:35:06 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options api.UpOptions) error {
|
|
|
|
return api.ErrNotImplemented
|
2020-12-15 17:21:09 +01:00
|
|
|
}
|
2020-12-02 18:23:01 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Kill(ctx context.Context, project *types.Project, options api.KillOptions) error {
|
2021-01-31 18:42:13 +01:00
|
|
|
return e.compose.Kill(ctx, project, options)
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
|
2020-12-15 17:21:09 +01:00
|
|
|
enhanced, err := e.enhanceForLocalSimulation(project)
|
2020-08-31 14:26:00 +02:00
|
|
|
if err != nil {
|
2020-12-15 17:21:09 +01:00
|
|
|
return nil, err
|
2020-08-31 14:26:00 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 17:21:09 +01:00
|
|
|
delete(enhanced.Networks, "default")
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"services": enhanced.Services,
|
|
|
|
"networks": enhanced.Networks,
|
|
|
|
"volumes": enhanced.Volumes,
|
|
|
|
"secrets": enhanced.Secrets,
|
|
|
|
"configs": enhanced.Configs,
|
|
|
|
}
|
2020-12-16 16:16:39 +01:00
|
|
|
switch options.Format {
|
2020-12-15 17:21:09 +01:00
|
|
|
case "json":
|
|
|
|
return json.MarshalIndent(config, "", " ")
|
|
|
|
case "yaml":
|
|
|
|
return yaml.Marshal(config)
|
|
|
|
default:
|
2020-12-16 16:16:39 +01:00
|
|
|
return nil, fmt.Errorf("unsupported format %q", options)
|
2020-08-25 10:30:51 +02:00
|
|
|
}
|
2020-08-31 14:26:00 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:21:09 +01:00
|
|
|
func (e ecsLocalSimulation) enhanceForLocalSimulation(project *types.Project) (*types.Project, error) {
|
2020-08-25 10:30:51 +02:00
|
|
|
project.Networks["credentials_network"] = types.NetworkConfig{
|
2020-12-15 17:21:09 +01:00
|
|
|
Name: "credentials_network",
|
2020-08-25 10:30:51 +02:00
|
|
|
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 {
|
2020-08-31 14:26:00 +02:00
|
|
|
return nil, err
|
2020-08-25 10:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, service := range project.Services {
|
|
|
|
service.Networks["credentials_network"] = &types.ServiceNetworkConfig{
|
|
|
|
Ipv4Address: fmt.Sprintf("169.254.170.%d", i+3),
|
|
|
|
}
|
2020-09-08 10:50:50 +02:00
|
|
|
if service.DependsOn == nil {
|
|
|
|
service.DependsOn = types.DependsOnConfig{}
|
|
|
|
}
|
2020-09-07 10:52:34 +02:00
|
|
|
service.DependsOn["ecs-local-endpoints"] = types.ServiceDependency{
|
|
|
|
Condition: types.ServiceConditionStarted,
|
|
|
|
}
|
2020-08-25 10:30:51 +02:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2020-12-15 17:21:09 +01:00
|
|
|
return project, nil
|
2020-08-31 14:26:00 +02:00
|
|
|
}
|
2020-08-25 10:30:51 +02:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, options api.DownOptions) error {
|
2020-12-16 16:16:39 +01:00
|
|
|
options.RemoveOrphans = true
|
|
|
|
return e.compose.Down(ctx, projectName, options)
|
2020-08-31 14:26:00 +02:00
|
|
|
}
|
2020-08-26 09:12:30 +02:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error {
|
2020-12-15 17:21:09 +01:00
|
|
|
return e.compose.Logs(ctx, projectName, consumer, options)
|
2020-08-31 14:26:00 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string, options api.PsOptions) ([]api.ContainerSummary, error) {
|
2021-01-29 14:41:44 +01:00
|
|
|
return e.compose.Ps(ctx, projectName, options)
|
2020-08-25 10:30:51 +02:00
|
|
|
}
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) List(ctx context.Context, opts api.ListOptions) ([]api.Stack, error) {
|
2021-02-24 12:06:17 +01:00
|
|
|
return e.compose.List(ctx, opts)
|
2020-09-04 13:20:11 +02:00
|
|
|
}
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
|
|
|
|
return 0, errors.Wrap(api.ErrNotImplemented, "use docker-compose run")
|
2020-12-03 09:24:15 +01:00
|
|
|
}
|
2021-02-15 09:13:41 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Remove(ctx context.Context, project *types.Project, options api.RemoveOptions) error {
|
2021-02-15 09:13:41 +01:00
|
|
|
return e.compose.Remove(ctx, project, options)
|
|
|
|
}
|
2021-02-15 09:56:34 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Exec(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
|
|
|
|
return 0, api.ErrNotImplemented
|
2021-02-15 09:56:34 +01:00
|
|
|
}
|
2021-02-22 10:25:40 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Copy(ctx context.Context, project *types.Project, opts api.CopyOptions) error {
|
2021-05-02 03:20:52 +02:00
|
|
|
return e.compose.Copy(ctx, project, opts)
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Pause(ctx context.Context, project string, options api.PauseOptions) error {
|
2021-04-07 15:00:42 +02:00
|
|
|
return e.compose.Pause(ctx, project, options)
|
2021-02-22 10:25:40 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) UnPause(ctx context.Context, project string, options api.PauseOptions) error {
|
2021-04-07 15:00:42 +02:00
|
|
|
return e.compose.UnPause(ctx, project, options)
|
2021-02-22 10:25:40 +01:00
|
|
|
}
|
2021-03-05 12:40:56 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Top(ctx context.Context, projectName string, services []string) ([]api.ContainerProcSummary, error) {
|
2021-03-05 12:40:56 +01:00
|
|
|
return e.compose.Top(ctx, projectName, services)
|
|
|
|
}
|
2021-03-08 10:22:24 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Events(ctx context.Context, project string, options api.EventsOptions) error {
|
2021-03-05 10:09:27 +01:00
|
|
|
return e.compose.Events(ctx, project, options)
|
|
|
|
}
|
2021-03-19 09:49:14 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Port(ctx context.Context, project string, service string, port int, options api.PortOptions) (string, int, error) {
|
|
|
|
return "", 0, api.ErrNotImplemented
|
2021-03-19 09:49:14 +01:00
|
|
|
}
|
2021-04-07 13:16:22 +02:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (e ecsLocalSimulation) Images(ctx context.Context, projectName string, options api.ImagesOptions) ([]api.ImageSummary, error) {
|
|
|
|
return nil, api.ErrNotImplemented
|
2021-04-07 13:16:22 +02:00
|
|
|
}
|