From d4c5ce3eb9fc7439c45eabd436dc14254811b3b9 Mon Sep 17 00:00:00 2001 From: stormcat24 Date: Mon, 28 Dec 2015 10:40:54 +0900 Subject: [PATCH 1/5] updated aws-sdk-go to v1.0.7 --- glide.lock | 8 ++++---- glide.yaml | 2 +- task/task_test.go | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/glide.lock b/glide.lock index 6d25c0c..f67efd7 100644 --- a/glide.lock +++ b/glide.lock @@ -1,8 +1,8 @@ hash: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -updated: 2015-12-21T02:53:18.665578098+09:00 +updated: 2015-12-28T10:23:07.087460087+09:00 imports: - name: github.com/aws/aws-sdk-go - version: 18177cad2597e3cac4173b4f267b784abdb3c854 + version: bf2f8fe7f45e68017086d069498638893feddf64 subpackages: - aws - private/endpoints @@ -52,7 +52,7 @@ imports: version: f18420efc3b4f8e9f3d51f6bd2476e92c46260e9 repo: https://golang.org/x/crypto - name: golang.org/x/net - version: 68a055e15f0ce90989da5564fd947fb72ce67513 + version: 5d0a0f8cd486626821d2ba44d471ab1c9271d38f - name: golang.org/x/sys version: 833a04a10549a95dc34458c195cbad61bbb6cb4d subpackages: @@ -61,7 +61,7 @@ imports: version: cf4986612c83df6c55578ba198316d1684a9a287 repo: https://golang.org/x/text - name: golang.org/x/tools - version: fa833fdef560f0fe9b40dbb37fd03030ac3d514b + version: d6e83e534da905609e21e4086a0fbaed33f88d07 - name: google.golang.org/appengine version: 58c0e2a2044a8d1abd8dd1d97939cd74497d0806 repo: https://google.golang.org/appengine diff --git a/glide.yaml b/glide.yaml index 41cdad7..41fa0ee 100644 --- a/glide.yaml +++ b/glide.yaml @@ -10,7 +10,7 @@ import: - package: gopkg.in/yaml.v2 version: 7ad95dd0798a40da1ccdff6dff35fd177b5edf40 - package: github.com/aws/aws-sdk-go - ref: v1.0.6 + ref: v1.0.7 subpackages: - aws - private/endpoints diff --git a/task/task_test.go b/task/task_test.go index 5f53b44..c872b03 100644 --- a/task/task_test.go +++ b/task/task_test.go @@ -85,6 +85,7 @@ func TestCreateContainerDefinition(t *testing.T) { ExtraHosts: []string{ "host1:192.168.1.100", }, + Hostname: "example.com", LogDriver: "syslog", LogOpt: map[string]string{ "syslog-address": "tcp://192.168.0.42:123", From 99b5f03a9b5e30e1a330a6fdba20176a843d973c Mon Sep 17 00:00:00 2001 From: stormcat24 Date: Mon, 28 Dec 2015 14:57:55 +0900 Subject: [PATCH 2/5] support env_file #70 --- operation/version.go | 2 +- task/schema.go | 42 ++++++++++++++- task/task.go | 8 ++- task/task_test.go | 119 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 4 deletions(-) diff --git a/operation/version.go b/operation/version.go index e08ecac..cd41540 100644 --- a/operation/version.go +++ b/operation/version.go @@ -1,3 +1,3 @@ package operation -const Version string = "0.2.0-RC2" +const Version string = "0.2.0-RC3" diff --git a/task/schema.go b/task/schema.go index dba6380..c50279d 100644 --- a/task/schema.go +++ b/task/schema.go @@ -3,7 +3,9 @@ package task import ( "errors" "fmt" + "github.com/joho/godotenv" "gopkg.in/yaml.v2" + "path/filepath" ) type TaskDefinition struct { @@ -16,6 +18,7 @@ type ContainerDefinition struct { Image string `yaml:"image"` Ports []string `yaml:"ports"` Environment map[string]string `yaml:"environment"` + EnvFiles []string `yaml:"env_file"` Links []string `yaml:"links"` Volumes []string `yaml:"volumes"` VolumesFrom []string `yaml:"volumes_from"` @@ -45,7 +48,7 @@ type Ulimit struct { Hard int64 `yaml:"hard"` } -func CreateTaskDefinition(taskDefName string, data string) (*TaskDefinition, error) { +func CreateTaskDefinition(taskDefName string, data string, basedir string) (*TaskDefinition, error) { containerMap := map[string]ContainerDefinition{} if err := yaml.Unmarshal([]byte(data), &containerMap); err != nil { @@ -56,6 +59,33 @@ func CreateTaskDefinition(taskDefName string, data string) (*TaskDefinition, err for name, container := range containerMap { con := container con.Name = name + + environment := map[string]string{} + if len(container.EnvFiles) > 0 { + for _, envfile := range container.EnvFiles { + var path string + if filepath.IsAbs(envfile) { + path = envfile + } else { + path = fmt.Sprintf("%s/%s", basedir, envfile) + } + + envmap, err := readEnvFile(path) + if err != nil { + return nil, err + } + + for key, value := range envmap { + environment[key] = value + } + } + } + + for key, value := range container.Environment { + environment[key] = value + } + + con.Environment = environment containers[name] = &con } @@ -66,3 +96,13 @@ func CreateTaskDefinition(taskDefName string, data string) (*TaskDefinition, err return &taskDef, nil } + +func readEnvFile(envpath string) (map[string]string, error) { + + envmap, err := godotenv.Read(envpath) + if err != nil { + return map[string]string{}, err + } + + return envmap, nil +} diff --git a/task/task.go b/task/task.go index 951da08..aa01989 100644 --- a/task/task.go +++ b/task/task.go @@ -55,7 +55,7 @@ func (self *TaskDefinitionController) searchTaskDefinitions(projectDir string) ( taskDefMap := map[string]*TaskDefinition{} filePattern := regexp.MustCompile(`^.+\/(.+)\.yml$`) - filepath.Walk(taskDir, func(path string, info os.FileInfo, err error) error { + err := filepath.Walk(taskDir, func(path string, info os.FileInfo, err error) error { if info.IsDir() || !strings.HasSuffix(path, ".yml") { return nil } @@ -69,7 +69,7 @@ func (self *TaskDefinitionController) searchTaskDefinitions(projectDir string) ( tokens := filePattern.FindStringSubmatch(path) taskDefName := tokens[1] - taskDefinition, err := CreateTaskDefinition(taskDefName, merged) + taskDefinition, err := CreateTaskDefinition(taskDefName, merged, filepath.Dir(path)) if err != nil { return err } @@ -79,6 +79,10 @@ func (self *TaskDefinitionController) searchTaskDefinitions(projectDir string) ( return nil }) + if err != nil { + return taskDefMap, err + } + return taskDefMap, nil } diff --git a/task/task_test.go b/task/task_test.go index c872b03..a0f7741 100644 --- a/task/task_test.go +++ b/task/task_test.go @@ -1,6 +1,9 @@ package task import ( + "fmt" + "io/ioutil" + "path/filepath" "testing" ) @@ -301,3 +304,119 @@ func TestCreateContainerDefinition(t *testing.T) { } } + +func TestReadEnvFileEnvFormat(t *testing.T) { + + f, _ := ioutil.TempFile("", "TestReadEnvFileEnvFormat.env") + f.WriteString(` +PARAM1=VALUE1_env +PARAM2=VALUE2_env + `) + defer f.Close() + + actual, err := readEnvFile(f.Name()) + if err != nil { + t.Fatalf("cannot open file.") + } + + if 2 != len(actual) { + t.Fatalf("len: expect = %v, but actual = %v", 2, len(actual)) + } + + if val, ok := actual["PARAM1"]; ok { + if "VALUE1_env" != val { + t.Errorf("actual[PARAM1]: expect = %v, but actual = %v", "VALUE1_env", val) + } + } else { + t.Errorf("actual[PARAM1]: not found") + } + +} + +func TestReadEnvFileYamlFormat(t *testing.T) { + + f, _ := ioutil.TempFile("", "TestReadEnvFileYamlFormat.env") + f.WriteString(` +PARAM1: VALUE1_env +PARAM2: VALUE2_env + `) + defer f.Close() + + actual, err := readEnvFile(f.Name()) + if err != nil { + t.Fatalf("cannot open file.") + } + + if 2 != len(actual) { + t.Fatalf("len: expect = %v, but actual = %v", 2, len(actual)) + } + + if val, ok := actual["PARAM1"]; ok { + if "VALUE1_env" != val { + t.Errorf("actual[PARAM1]: expect = %v, but actual = %v", "VALUE1_env", val) + } + } else { + t.Errorf("actual[PARAM1]: not found") + } + +} + +func TestCreateTaskDefinition(t *testing.T) { + + f, _ := ioutil.TempFile("", "TestCreateTaskDefinition.env") + f.WriteString(` +PARAM2: VALUE2_env +PARAM3: VALUE3_env + `) + defer f.Close() + + yaml := fmt.Sprintf(` +nginx: + image: nginx:latest + ports: + - 80:80 + env_file: + - %s + environment: + PARAM1: un_override_value + PARAM2: override_value + memory: 1024 + cpu_units: 1024 + essential: true +`, f.Name()) + + taskdef, err := CreateTaskDefinition("test-web", yaml, filepath.Dir(f.Name())) + if err != nil { + t.Fatal(err) + } + + if "test-web" != taskdef.Name { + t.Errorf("Name: expect = %v, but actual = %v", "test-web", taskdef.Name) + } + + if con, ok := taskdef.ContainerDefinitions["nginx"]; ok { + if "nginx" != con.Name { + t.Errorf("Name: expect = %v, but actual = %v", "nginx", con.Name) + } + + value1, _ := con.Environment["PARAM1"] + value2, _ := con.Environment["PARAM2"] + value3, _ := con.Environment["PARAM3"] + + if "un_override_value" != value1 { + t.Errorf("con.Environment[%v]: expect = %v, but actual = %v", "PARAM1", "un_override_value", value1) + } + + if "override_value" != value2 { + t.Errorf("con.Environment[%v]: expect = %v, but actual = %v", "PARAM2", "override_value", value2) + } + + if "VALUE3_env" != value3 { + t.Errorf("con.Environment[%v]: expect = %v, but actual = %v", "PARAM3", "override_value", value3) + } + + } else { + t.Errorf("ContainerDefinitions[nginx]: not found") + } + +} From 002364d497dd54ca35f000a5c3a92df1a41be5b9 Mon Sep 17 00:00:00 2001 From: stormcat24 Date: Mon, 28 Dec 2015 16:13:38 +0900 Subject: [PATCH 3/5] add env_file document --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 3a86ffd..066aee0 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,20 @@ You can set value for these parameters by using `-p` option. ecs-formation task -p NGINX_VERSION=1.0 -p NGINX_PORT=80 plan your-web-task ``` +#### env_file + +You can use `env_file` like docker-compose. https://docs.docker.com/compose/compose-file/#env-file + +```Ruby +nginx: + image: stormcat24/nginx:${NGINX_VERSION} + ports: + - 80:${NGINX_PORT} + env_file: + - ./test1.env + - ../test2.env +``` + License === See [LICENSE](LICENSE). From b6edf772cc70a072dbd69e6986f4fb77bb32727c Mon Sep 17 00:00:00 2001 From: stormcat24 Date: Mon, 28 Dec 2015 16:38:43 +0900 Subject: [PATCH 4/5] updated deps --- Makefile | 8 ++++++-- glide.lock | 11 ++++------- glide.yaml | 2 ++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index b9e22ca..99d9bfd 100644 --- a/Makefile +++ b/Makefile @@ -11,14 +11,18 @@ TEST_TARGETS=$(addprefix test-,$(PACKAGES)) deps: go get github.com/Masterminds/glide - GO15VENDOREXPERIMENT=1 glide update --cache + GO15VENDOREXPERIMENT=1 glide install --cache deps-test: go get github.com/Masterminds/glide - GO15VENDOREXPERIMENT=1 glide update --cache + GO15VENDOREXPERIMENT=1 glide install --cache go get github.com/golang/lint/golint go get github.com/jstemmer/go-junit-report +update: + rm -rf ./vendor + GO15VENDOREXPERIMENT=1 glide update --cache + build: GO15VENDOREXPERIMENT=1 go build -o bin/ecs-formation main.go diff --git a/glide.lock b/glide.lock index f67efd7..cf2411c 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -updated: 2015-12-28T10:23:07.087460087+09:00 +hash: d3a36753aff53f2cef70fe8433d23b3c58f925e4ec59eb1b59959b8ff3f7a15d +updated: 2015-12-28T16:38:18.864668709+09:00 imports: - name: github.com/aws/aws-sdk-go version: bf2f8fe7f45e68017086d069498638893feddf64 @@ -27,6 +27,8 @@ imports: version: 68415e7123da32b07eab49c96d2c4d6158360e9b - name: github.com/jmespath/go-jmespath version: 53d419364047eb08a64c29471db23c60ea585b85 +- name: github.com/joho/godotenv + version: 4ed13390c0acd2ff4e371e64d8b97c8954138243 - name: github.com/lsegal/gucumber version: e8116c9c66e641e9f81fc0a79fac923dfc646378 - name: github.com/mattn/go-shellwords @@ -45,12 +47,10 @@ imports: - color - name: github.com/stretchr/objx version: 1a9d0bb9f541897e62256577b352fdbc1fb4fd94 - repo: https://github.com/stretchr/objx - name: github.com/stretchr/testify version: e3a8ff8ce36581f87a15341206f205b1da467059 - name: golang.org/x/crypto version: f18420efc3b4f8e9f3d51f6bd2476e92c46260e9 - repo: https://golang.org/x/crypto - name: golang.org/x/net version: 5d0a0f8cd486626821d2ba44d471ab1c9271d38f - name: golang.org/x/sys @@ -59,17 +59,14 @@ imports: - unix - name: golang.org/x/text version: cf4986612c83df6c55578ba198316d1684a9a287 - repo: https://golang.org/x/text - name: golang.org/x/tools version: d6e83e534da905609e21e4086a0fbaed33f88d07 - name: google.golang.org/appengine version: 58c0e2a2044a8d1abd8dd1d97939cd74497d0806 - repo: https://google.golang.org/appengine - name: gopkg.in/airbrake/gobrake.v2 version: c9d51adc624b5cc4c1bf8de730a09af4878ffe2d - name: gopkg.in/gemnasium/logrus-airbrake-hook.v2 version: 31e6fd4bd5a98d8ee7673d24bc54ec73c31810dd - repo: https://gopkg.in/gemnasium/logrus-airbrake-hook.v2 - name: gopkg.in/yaml.v2 version: 7ad95dd0798a40da1ccdff6dff35fd177b5edf40 devImports: [] diff --git a/glide.yaml b/glide.yaml index 41fa0ee..1dd5d1c 100644 --- a/glide.yaml +++ b/glide.yaml @@ -39,3 +39,5 @@ import: version: f4e566c536cf69158e808ec28ef4182a37fdc981 - package: github.com/naoina/go-stringutil version: 360db0db4b01d34e12a2ec042c09e7d37fece761 +- package: github.com/joho/godotenv + version: 4ed13390c0acd2ff4e371e64d8b97c8954138243 From fc7c8e307fd7462bd6c583490ebc534717226b5d Mon Sep 17 00:00:00 2001 From: stormcat24 Date: Mon, 28 Dec 2015 16:41:18 +0900 Subject: [PATCH 5/5] fixed deps --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 99d9bfd..7fcdc7a 100644 --- a/Makefile +++ b/Makefile @@ -11,11 +11,11 @@ TEST_TARGETS=$(addprefix test-,$(PACKAGES)) deps: go get github.com/Masterminds/glide - GO15VENDOREXPERIMENT=1 glide install --cache + GO15VENDOREXPERIMENT=1 glide update --cache deps-test: go get github.com/Masterminds/glide - GO15VENDOREXPERIMENT=1 glide install --cache + GO15VENDOREXPERIMENT=1 glide update --cache go get github.com/golang/lint/golint go get github.com/jstemmer/go-junit-report