2020-08-18 11:38:23 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-08-18 11:38:23 +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-17 17:48:52 +02:00
|
|
|
package ecs
|
2020-08-04 18:04:06 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/awslabs/goformation/v4/cloudformation"
|
2020-11-05 09:27:14 +01:00
|
|
|
"github.com/sanathkr/go-yaml"
|
2020-08-04 18:04:06 +02:00
|
|
|
)
|
|
|
|
|
2020-11-05 09:27:14 +01:00
|
|
|
func marshall(template *cloudformation.Template, format string) ([]byte, error) {
|
|
|
|
var (
|
|
|
|
source func() ([]byte, error)
|
|
|
|
marshal func(in interface{}) ([]byte, error)
|
|
|
|
unmarshal func(in []byte, out interface{}) error
|
|
|
|
)
|
|
|
|
switch format {
|
|
|
|
case "yaml":
|
|
|
|
source = template.YAML
|
|
|
|
marshal = yaml.Marshal
|
|
|
|
unmarshal = yaml.Unmarshal
|
|
|
|
case "json":
|
|
|
|
source = template.JSON
|
|
|
|
marshal = func(in interface{}) ([]byte, error) {
|
|
|
|
return json.MarshalIndent(in, "", " ")
|
|
|
|
}
|
|
|
|
unmarshal = json.Unmarshal
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported format %q", format)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := source()
|
2020-08-04 18:04:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var unmarshalled interface{}
|
2020-11-05 09:27:14 +01:00
|
|
|
if err := unmarshal(raw, &unmarshalled); err != nil {
|
2020-08-04 18:04:06 +02:00
|
|
|
return nil, fmt.Errorf("invalid JSON: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-11-05 09:27:14 +01:00
|
|
|
if input, ok := unmarshalled.(map[interface{}]interface{}); ok {
|
2020-08-04 18:04:06 +02:00
|
|
|
if resources, ok := input["Resources"]; ok {
|
2020-11-05 09:27:14 +01:00
|
|
|
for _, uresource := range resources.(map[interface{}]interface{}) {
|
|
|
|
if resource, ok := uresource.(map[interface{}]interface{}); ok {
|
2020-08-04 18:04:06 +02:00
|
|
|
if resource["Type"] == "AWS::ECS::TaskDefinition" {
|
2020-11-05 09:27:14 +01:00
|
|
|
properties := resource["Properties"].(map[interface{}]interface{})
|
2020-08-04 18:04:06 +02:00
|
|
|
for _, def := range properties["ContainerDefinitions"].([]interface{}) {
|
2020-11-05 09:27:14 +01:00
|
|
|
containerDefinition := def.(map[interface{}]interface{})
|
2020-08-04 18:04:06 +02:00
|
|
|
if strings.HasSuffix(containerDefinition["Name"].(string), "_InitContainer") {
|
2020-11-05 09:27:14 +01:00
|
|
|
containerDefinition["Essential"] = false
|
2020-08-04 18:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 09:27:14 +01:00
|
|
|
return marshal(unmarshalled)
|
2020-08-04 18:04:06 +02:00
|
|
|
}
|