Skip to content

Commit 8f434fc

Browse files
author
Akinori Yamada
committed
Merge pull request #71 from stormcat24/feature/env_file
support env_file
2 parents bf2b075 + fc7c8e3 commit 8f434fc

File tree

8 files changed

+196
-15
lines changed

8 files changed

+196
-15
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ deps-test:
1919
go get github.com/golang/lint/golint
2020
go get github.com/jstemmer/go-junit-report
2121

22+
update:
23+
rm -rf ./vendor
24+
GO15VENDOREXPERIMENT=1 glide update --cache
25+
2226
build:
2327
GO15VENDOREXPERIMENT=1 go build -o bin/ecs-formation main.go
2428

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ You can set value for these parameters by using `-p` option.
234234
ecs-formation task -p NGINX_VERSION=1.0 -p NGINX_PORT=80 plan your-web-task
235235
```
236236
237+
#### env_file
238+
239+
You can use `env_file` like docker-compose. https://docs.docker.com/compose/compose-file/#env-file
240+
241+
```Ruby
242+
nginx:
243+
image: stormcat24/nginx:${NGINX_VERSION}
244+
ports:
245+
- 80:${NGINX_PORT}
246+
env_file:
247+
- ./test1.env
248+
- ../test2.env
249+
```
250+
237251
License
238252
===
239253
See [LICENSE](LICENSE).

glide.lock

Lines changed: 7 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import:
1010
- package: gopkg.in/yaml.v2
1111
version: 7ad95dd0798a40da1ccdff6dff35fd177b5edf40
1212
- package: github.com/aws/aws-sdk-go
13-
ref: v1.0.6
13+
ref: v1.0.7
1414
subpackages:
1515
- aws
1616
- private/endpoints
@@ -39,3 +39,5 @@ import:
3939
version: f4e566c536cf69158e808ec28ef4182a37fdc981
4040
- package: github.com/naoina/go-stringutil
4141
version: 360db0db4b01d34e12a2ec042c09e7d37fece761
42+
- package: github.com/joho/godotenv
43+
version: 4ed13390c0acd2ff4e371e64d8b97c8954138243

operation/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package operation
22

3-
const Version string = "0.2.0-RC2"
3+
const Version string = "0.2.0-RC3"

task/schema.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package task
33
import (
44
"errors"
55
"fmt"
6+
"github.com/joho/godotenv"
67
"gopkg.in/yaml.v2"
8+
"path/filepath"
79
)
810

911
type TaskDefinition struct {
@@ -16,6 +18,7 @@ type ContainerDefinition struct {
1618
Image string `yaml:"image"`
1719
Ports []string `yaml:"ports"`
1820
Environment map[string]string `yaml:"environment"`
21+
EnvFiles []string `yaml:"env_file"`
1922
Links []string `yaml:"links"`
2023
Volumes []string `yaml:"volumes"`
2124
VolumesFrom []string `yaml:"volumes_from"`
@@ -45,7 +48,7 @@ type Ulimit struct {
4548
Hard int64 `yaml:"hard"`
4649
}
4750

48-
func CreateTaskDefinition(taskDefName string, data string) (*TaskDefinition, error) {
51+
func CreateTaskDefinition(taskDefName string, data string, basedir string) (*TaskDefinition, error) {
4952

5053
containerMap := map[string]ContainerDefinition{}
5154
if err := yaml.Unmarshal([]byte(data), &containerMap); err != nil {
@@ -56,6 +59,33 @@ func CreateTaskDefinition(taskDefName string, data string) (*TaskDefinition, err
5659
for name, container := range containerMap {
5760
con := container
5861
con.Name = name
62+
63+
environment := map[string]string{}
64+
if len(container.EnvFiles) > 0 {
65+
for _, envfile := range container.EnvFiles {
66+
var path string
67+
if filepath.IsAbs(envfile) {
68+
path = envfile
69+
} else {
70+
path = fmt.Sprintf("%s/%s", basedir, envfile)
71+
}
72+
73+
envmap, err := readEnvFile(path)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
for key, value := range envmap {
79+
environment[key] = value
80+
}
81+
}
82+
}
83+
84+
for key, value := range container.Environment {
85+
environment[key] = value
86+
}
87+
88+
con.Environment = environment
5989
containers[name] = &con
6090
}
6191

@@ -66,3 +96,13 @@ func CreateTaskDefinition(taskDefName string, data string) (*TaskDefinition, err
6696

6797
return &taskDef, nil
6898
}
99+
100+
func readEnvFile(envpath string) (map[string]string, error) {
101+
102+
envmap, err := godotenv.Read(envpath)
103+
if err != nil {
104+
return map[string]string{}, err
105+
}
106+
107+
return envmap, nil
108+
}

task/task.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (self *TaskDefinitionController) searchTaskDefinitions(projectDir string) (
5555
taskDefMap := map[string]*TaskDefinition{}
5656
filePattern := regexp.MustCompile(`^.+\/(.+)\.yml$`)
5757

58-
filepath.Walk(taskDir, func(path string, info os.FileInfo, err error) error {
58+
err := filepath.Walk(taskDir, func(path string, info os.FileInfo, err error) error {
5959
if info.IsDir() || !strings.HasSuffix(path, ".yml") {
6060
return nil
6161
}
@@ -69,7 +69,7 @@ func (self *TaskDefinitionController) searchTaskDefinitions(projectDir string) (
6969
tokens := filePattern.FindStringSubmatch(path)
7070
taskDefName := tokens[1]
7171

72-
taskDefinition, err := CreateTaskDefinition(taskDefName, merged)
72+
taskDefinition, err := CreateTaskDefinition(taskDefName, merged, filepath.Dir(path))
7373
if err != nil {
7474
return err
7575
}
@@ -79,6 +79,10 @@ func (self *TaskDefinitionController) searchTaskDefinitions(projectDir string) (
7979
return nil
8080
})
8181

82+
if err != nil {
83+
return taskDefMap, err
84+
}
85+
8286
return taskDefMap, nil
8387
}
8488

task/task_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package task
22

33
import (
4+
"fmt"
5+
"io/ioutil"
6+
"path/filepath"
47
"testing"
58
)
69

@@ -85,6 +88,7 @@ func TestCreateContainerDefinition(t *testing.T) {
8588
ExtraHosts: []string{
8689
"host1:192.168.1.100",
8790
},
91+
Hostname: "example.com",
8892
LogDriver: "syslog",
8993
LogOpt: map[string]string{
9094
"syslog-address": "tcp://192.168.0.42:123",
@@ -300,3 +304,119 @@ func TestCreateContainerDefinition(t *testing.T) {
300304
}
301305

302306
}
307+
308+
func TestReadEnvFileEnvFormat(t *testing.T) {
309+
310+
f, _ := ioutil.TempFile("", "TestReadEnvFileEnvFormat.env")
311+
f.WriteString(`
312+
PARAM1=VALUE1_env
313+
PARAM2=VALUE2_env
314+
`)
315+
defer f.Close()
316+
317+
actual, err := readEnvFile(f.Name())
318+
if err != nil {
319+
t.Fatalf("cannot open file.")
320+
}
321+
322+
if 2 != len(actual) {
323+
t.Fatalf("len: expect = %v, but actual = %v", 2, len(actual))
324+
}
325+
326+
if val, ok := actual["PARAM1"]; ok {
327+
if "VALUE1_env" != val {
328+
t.Errorf("actual[PARAM1]: expect = %v, but actual = %v", "VALUE1_env", val)
329+
}
330+
} else {
331+
t.Errorf("actual[PARAM1]: not found")
332+
}
333+
334+
}
335+
336+
func TestReadEnvFileYamlFormat(t *testing.T) {
337+
338+
f, _ := ioutil.TempFile("", "TestReadEnvFileYamlFormat.env")
339+
f.WriteString(`
340+
PARAM1: VALUE1_env
341+
PARAM2: VALUE2_env
342+
`)
343+
defer f.Close()
344+
345+
actual, err := readEnvFile(f.Name())
346+
if err != nil {
347+
t.Fatalf("cannot open file.")
348+
}
349+
350+
if 2 != len(actual) {
351+
t.Fatalf("len: expect = %v, but actual = %v", 2, len(actual))
352+
}
353+
354+
if val, ok := actual["PARAM1"]; ok {
355+
if "VALUE1_env" != val {
356+
t.Errorf("actual[PARAM1]: expect = %v, but actual = %v", "VALUE1_env", val)
357+
}
358+
} else {
359+
t.Errorf("actual[PARAM1]: not found")
360+
}
361+
362+
}
363+
364+
func TestCreateTaskDefinition(t *testing.T) {
365+
366+
f, _ := ioutil.TempFile("", "TestCreateTaskDefinition.env")
367+
f.WriteString(`
368+
PARAM2: VALUE2_env
369+
PARAM3: VALUE3_env
370+
`)
371+
defer f.Close()
372+
373+
yaml := fmt.Sprintf(`
374+
nginx:
375+
image: nginx:latest
376+
ports:
377+
- 80:80
378+
env_file:
379+
- %s
380+
environment:
381+
PARAM1: un_override_value
382+
PARAM2: override_value
383+
memory: 1024
384+
cpu_units: 1024
385+
essential: true
386+
`, f.Name())
387+
388+
taskdef, err := CreateTaskDefinition("test-web", yaml, filepath.Dir(f.Name()))
389+
if err != nil {
390+
t.Fatal(err)
391+
}
392+
393+
if "test-web" != taskdef.Name {
394+
t.Errorf("Name: expect = %v, but actual = %v", "test-web", taskdef.Name)
395+
}
396+
397+
if con, ok := taskdef.ContainerDefinitions["nginx"]; ok {
398+
if "nginx" != con.Name {
399+
t.Errorf("Name: expect = %v, but actual = %v", "nginx", con.Name)
400+
}
401+
402+
value1, _ := con.Environment["PARAM1"]
403+
value2, _ := con.Environment["PARAM2"]
404+
value3, _ := con.Environment["PARAM3"]
405+
406+
if "un_override_value" != value1 {
407+
t.Errorf("con.Environment[%v]: expect = %v, but actual = %v", "PARAM1", "un_override_value", value1)
408+
}
409+
410+
if "override_value" != value2 {
411+
t.Errorf("con.Environment[%v]: expect = %v, but actual = %v", "PARAM2", "override_value", value2)
412+
}
413+
414+
if "VALUE3_env" != value3 {
415+
t.Errorf("con.Environment[%v]: expect = %v, but actual = %v", "PARAM3", "override_value", value3)
416+
}
417+
418+
} else {
419+
t.Errorf("ContainerDefinitions[nginx]: not found")
420+
}
421+
422+
}

0 commit comments

Comments
 (0)