|
| 1 | +/* |
| 2 | + Copyright 2020 Docker, Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ecs |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/aws/aws-sdk-go/aws" |
| 28 | + "github.com/compose-spec/compose-go/types" |
| 29 | + "github.com/sanathkr/go-yaml" |
| 30 | + |
| 31 | + "github.com/compose-spec/compose-go/cli" |
| 32 | +) |
| 33 | + |
| 34 | +func (c *ecsAPIService) Emulate(ctx context.Context, options *cli.ProjectOptions) error { |
| 35 | + project, err := cli.ProjectFromOptions(options) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + project.Networks["credentials_network"] = types.NetworkConfig{ |
| 40 | + Driver: "bridge", |
| 41 | + Ipam: types.IPAMConfig{ |
| 42 | + Config: []*types.IPAMPool{ |
| 43 | + { |
| 44 | + Subnet: "169.254.170.0/24", |
| 45 | + Gateway: "169.254.170.1", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + // On Windows, this directory can be found at "%UserProfile%\.aws" |
| 52 | + home, err := os.UserHomeDir() |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + for i, service := range project.Services { |
| 58 | + service.Networks["credentials_network"] = &types.ServiceNetworkConfig{ |
| 59 | + Ipv4Address: fmt.Sprintf("169.254.170.%d", i+3), |
| 60 | + } |
| 61 | + service.DependsOn = append(service.DependsOn, "ecs-local-endpoints") |
| 62 | + service.Environment["AWS_DEFAULT_REGION"] = aws.String(c.ctx.Region) |
| 63 | + service.Environment["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = aws.String("/creds") |
| 64 | + service.Environment["ECS_CONTAINER_METADATA_URI"] = aws.String("http://169.254.170.2/v3") |
| 65 | + project.Services[i] = service |
| 66 | + } |
| 67 | + |
| 68 | + project.Services = append(project.Services, types.ServiceConfig{ |
| 69 | + Name: "ecs-local-endpoints", |
| 70 | + Image: "amazon/amazon-ecs-local-container-endpoints", |
| 71 | + Volumes: []types.ServiceVolumeConfig{ |
| 72 | + { |
| 73 | + Type: types.VolumeTypeBind, |
| 74 | + Source: "/var/run", |
| 75 | + Target: "/var/run", |
| 76 | + }, |
| 77 | + { |
| 78 | + Type: types.VolumeTypeBind, |
| 79 | + Source: filepath.Join(home, ".aws"), |
| 80 | + Target: "/home/.aws", |
| 81 | + }, |
| 82 | + }, |
| 83 | + Environment: map[string]*string{ |
| 84 | + "HOME": aws.String("/home"), |
| 85 | + "AWS_PROFILE": aws.String("default"), |
| 86 | + }, |
| 87 | + Networks: map[string]*types.ServiceNetworkConfig{ |
| 88 | + "credentials_network": { |
| 89 | + Ipv4Address: "169.254.170.2", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
| 94 | + delete(project.Networks, "default") |
| 95 | + config := map[string]interface{}{ |
| 96 | + "services": project.Services, |
| 97 | + "networks": project.Networks, |
| 98 | + "volumes": project.Volumes, |
| 99 | + "secrets": project.Secrets, |
| 100 | + "configs": project.Configs, |
| 101 | + } |
| 102 | + marshal, err := yaml.Marshal(config) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + cmd := exec.Command("docker-compose", "--context", "default", "--project-directory", project.WorkingDir, "--project-name", project.Name, "-f", "-", "up") |
| 108 | + cmd.Stdin = strings.NewReader(string(marshal)) |
| 109 | + cmd.Stdout = os.Stdout |
| 110 | + cmd.Stderr = os.Stderr |
| 111 | + return cmd.Run() |
| 112 | +} |
0 commit comments