Skip to content

Commit 7cedaa6

Browse files
authored
Merge pull request #500 from kool-dev/patch-deploy-tgz
include files needed for deployment - env files
2 parents 8c16a4b + dac626c commit 7cedaa6

File tree

3 files changed

+103
-54
lines changed

3 files changed

+103
-54
lines changed

commands/cloud_deploy.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,17 @@ func (d *KoolDeploy) createReleaseFile() (filename string, err error) {
237237
allFiles = append(allFiles, filepath.Join(d.env.Get("PWD"), "docker-compose.yml"))
238238
}
239239

240+
// we need to include the .env files as well
241+
allFiles = append(allFiles, d.cloudConfig.GetEnvFiles()...)
242+
240243
d.shell.Println("Compressing files:")
241244
for _, file := range allFiles {
242245
d.shell.Println(" -", file)
243246
}
244247

245248
tarball.SourceDir(d.env.Get("PWD"))
246249

247-
filename, err = tarball.CompressFiles(d.handleDeployEnv(allFiles))
250+
filename, err = tarball.CompressFiles(allFiles)
248251

249252
if err == nil {
250253
d.shell.Println("Files compression done.")
@@ -253,32 +256,6 @@ func (d *KoolDeploy) createReleaseFile() (filename string, err error) {
253256
return
254257
}
255258

256-
// handleDeployEnv tackles a special case on kool.deploy.env file.
257-
// This file can or cannot be versioned (good practice not to, since
258-
// it may include sensitive data). In the case of it being ignored
259-
// from GIT, we still are required to send it - it is required for
260-
// the Deploy API.
261-
func (d *KoolDeploy) handleDeployEnv(files []string) []string {
262-
path := filepath.Join(d.env.Get("PWD"), koolDeployEnv)
263-
if _, envErr := os.Stat(path); os.IsNotExist(envErr) {
264-
return files
265-
}
266-
267-
var isAlreadyIncluded bool = false
268-
for _, file := range files {
269-
if file == koolDeployEnv {
270-
isAlreadyIncluded = true
271-
break
272-
}
273-
}
274-
275-
if !isAlreadyIncluded {
276-
files = append(files, koolDeployEnv)
277-
}
278-
279-
return files
280-
}
281-
282259
func (d *KoolDeploy) loadAndValidateConfig() (err error) {
283260
if d.cloudConfig, err = cloud.ParseCloudConfig(d.env.Get("PWD"), setup.KoolDeployFile); err != nil {
284261
return

commands/cloud_deploy_test.go

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,77 +26,120 @@ func TestNewKoolDeploy(t *testing.T) {
2626
}
2727
}
2828

29-
func fakeKoolDeploy() *KoolDeploy {
29+
func fakeKoolDeploy(pwd string) *KoolDeploy {
3030
c := NewCloud()
3131
c.Fake()
3232
return &KoolDeploy{
3333
*(newDefaultKoolService().Fake()),
3434
c,
35-
setup.NewDefaultCloudSetupParser(""),
35+
setup.NewDefaultCloudSetupParser(pwd),
3636
&KoolCloudDeployFlags{},
3737
environment.NewFakeEnvStorage(),
3838
nil,
3939
}
4040
}
4141

42-
func TestHandleDeployEnv(t *testing.T) {
43-
fake := fakeKoolDeploy()
42+
func TestCreateReleaseFileNoConfig(t *testing.T) {
43+
fake := fakeKoolDeploy("")
4444

45-
files := []string{}
45+
if _, err := fake.createReleaseFile(); err == nil || !strings.Contains(err.Error(), "no kool.cloud.yml config files found") {
46+
t.Errorf("expected error on createReleaseFile when no kool.deploy.yml exists in current working directory; got: %v", err)
47+
}
48+
}
4649

50+
func TestCreateReleaseFileCreatesTgz(t *testing.T) {
4751
tmpDir := t.TempDir()
52+
mockConfig(tmpDir, t, nil)
53+
54+
fake := fakeKoolDeploy(tmpDir)
4855
fake.env.Set("PWD", tmpDir)
4956

50-
files = fake.handleDeployEnv(files)
57+
if err := fake.loadAndValidateConfig(); err != nil {
58+
t.Errorf("unexpected error on loadAndValidateConfig; got: %v", err)
59+
}
5160

52-
if len(files) != 0 {
53-
t.Errorf("expected files to continue empty - no kool.deploy.env exists")
61+
if tg, err := fake.createReleaseFile(); err != nil {
62+
t.Errorf("unexpected error on createReleaseFile; got: %v", err)
63+
} else if _, err := os.Stat(tg); err != nil {
64+
t.Errorf("expected tgz file to be created; got: %v", err)
5465
}
66+
}
67+
68+
func TestCreateReleaseFileCreatesTgzWithEnvFile(t *testing.T) {
69+
tmpDir := t.TempDir()
70+
mockConfig(tmpDir, t, []byte("services:\n foo:\n image: bar\n env:\n source: 'foo.env'\n"))
5571

56-
if err := os.WriteFile(filepath.Join(tmpDir, "kool.deploy.env"), []byte("FOO=BAR"), os.ModePerm); err != nil {
72+
if err := os.WriteFile(filepath.Join(tmpDir, "foo.env"), []byte("FOO=BAR"), os.ModePerm); err != nil {
5773
t.Fatal(err)
5874
}
5975

60-
files = fake.handleDeployEnv(files)
76+
fake := fakeKoolDeploy(tmpDir)
77+
fake.env.Set("PWD", tmpDir)
6178

62-
if len(files) != 1 {
63-
t.Errorf("expected files to have added kool.deploy.env")
79+
if err := fake.loadAndValidateConfig(); err != nil {
80+
t.Errorf("unexpected error on loadAndValidateConfig; got: %v", err)
6481
}
6582

66-
files = fake.handleDeployEnv(files)
83+
if tg, err := fake.createReleaseFile(); err != nil {
84+
t.Errorf("unexpected error on createReleaseFile; got: %v", err)
85+
} else if _, err := os.Stat(tg); err != nil {
86+
t.Errorf("expected tgz file to be created; got: %v", err)
87+
}
6788

68-
if len(files) != 1 {
69-
t.Errorf("expected files to continue since was already there kool.deploy.env")
89+
if !fake.shell.(*shell.FakeShell).CalledPrintln {
90+
t.Error("expected Println to have been called on shell")
91+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[0], "Compressing files:") {
92+
t.Error("expected to print 'Compressing files:'")
93+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[1], "- "+filepath.Join(tmpDir, "kool.cloud.yml")) {
94+
t.Error("expected to print '- " + filepath.Join(tmpDir, "kool.cloud.yml") + "'")
95+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[2], "- "+filepath.Join(tmpDir, "docker-compose.yml")) {
96+
t.Error("expected to print '- " + filepath.Join(tmpDir, "docker-compose.yml") + "'")
97+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[3], "- "+filepath.Join(tmpDir, "foo.env")) {
98+
t.Error("expected to print '- " + filepath.Join(tmpDir, "foo.env") + "'")
7099
}
71100
}
72101

73-
func TestCreateReleaseFile(t *testing.T) {
74-
fake := fakeKoolDeploy()
75-
102+
func TestCreateReleaseFileCreatesTgzWithEnvironmentFile(t *testing.T) {
76103
tmpDir := t.TempDir()
77-
fake.env.Set("PWD", tmpDir)
104+
mockConfig(tmpDir, t, []byte("services:\n foo:\n image: bar\n environment: 'bar.env'\n"))
78105

79-
if _, err := fake.createReleaseFile(); err == nil || !strings.Contains(err.Error(), "no kool.cloud.yml config files found") {
80-
t.Errorf("expected error on createReleaseFile when no kool.deploy.yml exists in current working directory; got: %v", err)
106+
if err := os.WriteFile(filepath.Join(tmpDir, "bar.env"), []byte("BAR=FOO"), os.ModePerm); err != nil {
107+
t.Fatal(err)
81108
}
82109

83-
mockConfig(tmpDir, t, nil)
110+
fake := fakeKoolDeploy(tmpDir)
111+
fake.env.Set("PWD", tmpDir)
112+
113+
if err := fake.loadAndValidateConfig(); err != nil {
114+
t.Errorf("unexpected error on loadAndValidateConfig; got: %v", err)
115+
}
84116

85117
if tg, err := fake.createReleaseFile(); err != nil {
86118
t.Errorf("unexpected error on createReleaseFile; got: %v", err)
87119
} else if _, err := os.Stat(tg); err != nil {
88120
t.Errorf("expected tgz file to be created; got: %v", err)
89121
}
122+
123+
if !fake.shell.(*shell.FakeShell).CalledPrintln {
124+
t.Error("expected Println to have been called on shell")
125+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[0], "Compressing files:") {
126+
t.Error("expected to print 'Compressing files:'")
127+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[1], "- "+filepath.Join(tmpDir, "kool.cloud.yml")) {
128+
t.Error("expected to print '- " + filepath.Join(tmpDir, "kool.cloud.yml") + "'")
129+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[2], "- "+filepath.Join(tmpDir, "docker-compose.yml")) {
130+
t.Error("expected to print '- " + filepath.Join(tmpDir, "docker-compose.yml") + "'")
131+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[3], "- "+filepath.Join(tmpDir, "bar.env")) {
132+
t.Error("expected to print '- " + filepath.Join(tmpDir, "bar.env") + "'")
133+
}
90134
}
91135

92136
func TestCleanupReleaseFile(t *testing.T) {
93-
fake := fakeKoolDeploy()
94-
95137
tmpDir := t.TempDir()
96-
fake.env.Set("PWD", tmpDir)
97-
98138
mockConfig(tmpDir, t, nil)
99139

140+
fake := fakeKoolDeploy("")
141+
fake.env.Set("PWD", tmpDir)
142+
100143
f := filepath.Join(tmpDir, "kool.cloud.yml")
101144
fake.cleanupReleaseFile(f)
102145
if _, err := os.Stat(f); !os.IsNotExist(err) {
@@ -113,7 +156,7 @@ func TestCleanupReleaseFile(t *testing.T) {
113156
}
114157

115158
func TestLoadAndValidateConfig(t *testing.T) {
116-
fake := fakeKoolDeploy()
159+
fake := fakeKoolDeploy("")
117160

118161
tmpDir := t.TempDir()
119162
fake.env.Set("PWD", tmpDir)

services/cloud/deploy_validator.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@ type DeployConfig struct {
1919
Cloud *CloudConfig
2020
}
2121

22+
func (c *DeployConfig) GetEnvFiles() []string {
23+
files := make(map[string]bool)
24+
25+
for _, srv := range c.Cloud.Services {
26+
if srv.Env != nil {
27+
if env, ok := srv.Env.(map[interface{}]interface{}); ok {
28+
if envFile, okSrc := env["source"].(string); okSrc {
29+
files[envFile] = true
30+
}
31+
}
32+
}
33+
34+
if srv.Environment != nil {
35+
if envFile, ok := srv.Environment.(string); ok {
36+
files[envFile] = true
37+
}
38+
}
39+
}
40+
41+
envs := make([]string, 0, len(files))
42+
43+
for envFile := range files {
44+
envs = append(envs, filepath.Join(c.Meta.WorkingDir, envFile))
45+
}
46+
47+
return envs
48+
}
49+
2250
// CloudConfig is the configuration for a deploy parsed from kool.cloud.yml
2351
type CloudConfig struct {
2452
// version of the Kool.dev Cloud config file
@@ -36,6 +64,7 @@ type DeployConfigService struct {
3664

3765
Public interface{} `yaml:"public,omitempty"`
3866
Environment interface{} `yaml:"environment"`
67+
Env interface{} `yaml:"env"`
3968
}
4069

4170
// DeployConfigBuild is the configuration for a service to be built

0 commit comments

Comments
 (0)