Skip to content

Commit

Permalink
correct handling of array values for env
Browse files Browse the repository at this point in the history
fixes #18

Signed-off-by: Alex Suraci <[email protected]>
  • Loading branch information
vito committed Mar 22, 2019
1 parent 73be07f commit 7c7b5ac
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 65 deletions.
4 changes: 0 additions & 4 deletions jobs/web/templates/bpm.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ processes:

def env_file_flag(v, env)
case v
when Array
v.collect.with_index { |v, i| "/var/vcap/jobs/web/config/env/#{env}_#{i}" }.join(",")
when Hash
v.collect { |k, v| "#{k}:/var/vcap/jobs/web/config/env/#{env}_#{k}" }.join(",")
else
Expand All @@ -80,8 +78,6 @@ processes:

def env_windows_file_flag(v, env)
case v
when Array
v.collect.with_index { |v, i| "C:\\var\\vcap\\jobs\\web\\config\\env\\#{env}_#{i}" }.join(",")
when Hash
v.collect { |k, v| "#{k}:C:\\var\\vcap\\jobs\\web\\config\\env\\#{env}_#{k}" }.join(",")
else
Expand Down
9 changes: 2 additions & 7 deletions jobs/web/templates/pre_start.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mkdir -p /var/vcap/jobs/web/config/env
def env_file_content(v)
case v
when Array
v.join("\n").chomp + "\n"
v.collect(&:chomp).join("\n").chomp + "\n"
when String
v.chomp + "\n"
else
Expand All @@ -34,18 +34,13 @@ mkdir -p /var/vcap/jobs/web/config/env
path = "/var/vcap/jobs/web/config/env/#{env}"
case v
when Array
v.collect.with_index do |v, i|
fn = "#{path}_#{i}"
"cat > #{fn} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(fn)}"
end.join("\n\n")
when Hash
v.collect do |k, v|
fn = "#{path}_#{k}"
"cat > #{fn} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(fn)}"
end.join("\n\n")
else
"cat > #{path} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(path)}\n"
"cat > #{path} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(path)}"
end
end
-%>
Expand Down
4 changes: 0 additions & 4 deletions jobs/worker-windows/templates/env.ps1.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ $env:CONCOURSE_TSA_PUBLIC_KEY = "C:\var\vcap\jobs\worker-windows\config\worker_g

def env_file_flag(v, env)
case v
when Array
v.collect.with_index { |v, i| "/var/vcap/jobs/worker-windows/config/env/#{env}_#{i}" }.join(",")
when Hash
v.collect { |k, v| "#{k}:/var/vcap/jobs/worker-windows/config/env/#{env}_#{k}" }.join(",")
else
Expand All @@ -70,8 +68,6 @@ $env:CONCOURSE_TSA_PUBLIC_KEY = "C:\var\vcap\jobs\worker-windows\config\worker_g

def env_windows_file_flag(v, env)
case v
when Array
v.collect.with_index { |v, i| "C:\\var\\vcap\\jobs\\worker-windows\\config\\env\\#{env}_#{i}" }.join(",")
when Hash
v.collect { |k, v| "#{k}:C:\\var\\vcap\\jobs\\worker-windows\\config\\env\\#{env}_#{k}" }.join(",")
else
Expand Down
4 changes: 0 additions & 4 deletions jobs/worker/templates/env.sh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ export CONCOURSE_TSA_PUBLIC_KEY=/var/vcap/jobs/worker/config/worker_gateway_host

def env_file_flag(v, env)
case v
when Array
v.collect.with_index { |v, i| "/var/vcap/jobs/worker/config/env/#{env}_#{i}" }.join(",")
when Hash
v.collect { |k, v| "#{k}:/var/vcap/jobs/worker/config/env/#{env}_#{k}" }.join(",")
else
Expand All @@ -53,8 +51,6 @@ export CONCOURSE_TSA_PUBLIC_KEY=/var/vcap/jobs/worker/config/worker_gateway_host

def env_windows_file_flag(v, env)
case v
when Array
v.collect.with_index { |v, i| "C:\\var\\vcap\\jobs\\worker\\config\\env\\#{env}_#{i}" }.join(",")
when Hash
v.collect { |k, v| "#{k}:C:\\var\\vcap\\jobs\\worker\\config\\env\\#{env}_#{k}" }.join(",")
else
Expand Down
9 changes: 2 additions & 7 deletions jobs/worker/templates/pre_start.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mkdir -p /var/vcap/jobs/worker/config/env
def env_file_content(v)
case v
when Array
v.join("\n").chomp + "\n"
v.collect(&:chomp).join("\n").chomp + "\n"
when String
v.chomp + "\n"
else
Expand All @@ -35,18 +35,13 @@ mkdir -p /var/vcap/jobs/worker/config/env
path = "/var/vcap/jobs/worker/config/env/#{env}"
case v
when Array
v.collect.with_index do |v, i|
fn = "#{path}_#{i}"
"cat > #{fn} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(fn)}"
end.join("\n\n")
when Hash
v.collect do |k, v|
fn = "#{path}_#{k}"
"cat > #{fn} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(fn)}"
end.join("\n\n")
else
"cat > #{path} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(path)}\n"
"cat > #{path} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(path)}"
end
end
-%>
Expand Down
9 changes: 2 additions & 7 deletions tmpl/create_env_files.erb.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mkdir -p /var/vcap/jobs/{{.Name}}/config/env
def env_file_content(v)
case v
when Array
v.join("\n").chomp + "\n"
v.collect(&:chomp).join("\n").chomp + "\n"
when String
v.chomp + "\n"
else
Expand All @@ -27,18 +27,13 @@ mkdir -p /var/vcap/jobs/{{.Name}}/config/env
path = "/var/vcap/jobs/{{.Name}}/config/env/#{env}"

case v
when Array
v.collect.with_index do |v, i|
fn = "#{path}_#{i}"
"cat > #{fn} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(fn)}"
end.join("\n\n")
when Hash
v.collect do |k, v|
fn = "#{path}_#{k}"
"cat > #{fn} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(fn)}"
end.join("\n\n")
else
"cat > #{path} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(path)}\n"
"cat > #{path} <<\"ENVGEN_EOF\"\n#{env_file_content(v)}ENVGEN_EOF\n#{env_file_perms(path)}"
end
end
-%>
Expand Down
37 changes: 9 additions & 28 deletions tmpl/create_env_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,27 @@
require "rspec"
require "json"

describe "env_file_write" do
describe "env_file_writer" do
context "Array" do
let(:value) {
[
"bar",
["quux", "quuux", "quuuux"],
{ "grunt" => "gorp" },
"foo",
"bar\n", # public_key variable attrs have a trailing linebreak
"baz",
]
}

let(:expected) {
<<~EOS
cat > /var/vcap/jobs/{{.Name}}/config/env/MyEnv_0 <<"ENVGEN_EOF"
cat > /var/vcap/jobs/{{.Name}}/config/env/MyEnv <<"ENVGEN_EOF"
foo
bar
baz
ENVGEN_EOF
if [[ "${ENV_FILE_OWNER:-}" != "" ]] ; then
chown ${ENV_FILE_OWNER}:${ENV_FILE_OWNER} /var/vcap/jobs/{{.Name}}/config/env/MyEnv_0
fi
chmod 0600 /var/vcap/jobs/{{.Name}}/config/env/MyEnv_0
cat > /var/vcap/jobs/{{.Name}}/config/env/MyEnv_1 <<"ENVGEN_EOF"
quux
quuux
quuuux
ENVGEN_EOF
if [[ "${ENV_FILE_OWNER:-}" != "" ]] ; then
chown ${ENV_FILE_OWNER}:${ENV_FILE_OWNER} /var/vcap/jobs/{{.Name}}/config/env/MyEnv_1
fi
chmod 0600 /var/vcap/jobs/{{.Name}}/config/env/MyEnv_1
cat > /var/vcap/jobs/{{.Name}}/config/env/MyEnv_2 <<"ENVGEN_EOF"
{"grunt":"gorp"}
ENVGEN_EOF
if [[ "${ENV_FILE_OWNER:-}" != "" ]] ; then
chown ${ENV_FILE_OWNER}:${ENV_FILE_OWNER} /var/vcap/jobs/{{.Name}}/config/env/MyEnv_2
chown ${ENV_FILE_OWNER}:${ENV_FILE_OWNER} /var/vcap/jobs/{{.Name}}/config/env/MyEnv
fi
chmod 0600 /var/vcap/jobs/{{.Name}}/config/env/MyEnv_2
chmod 0600 /var/vcap/jobs/{{.Name}}/config/env/MyEnv
EOS
}
it { expect(env_file_writer(value, "MyEnv")).to eq(expected) }
Expand Down Expand Up @@ -112,7 +94,6 @@
chown ${ENV_FILE_OWNER}:${ENV_FILE_OWNER} /var/vcap/jobs/{{.Name}}/config/env/MyEnv
fi
chmod 0600 /var/vcap/jobs/{{.Name}}/config/env/MyEnv
EOS
}
it { expect(env_file_writer(value, "MyEnv")).to eq(expected) }
Expand Down
4 changes: 0 additions & 4 deletions tmpl/env_helpers.erb.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

def env_file_flag(v, env)
case v
when Array
v.collect.with_index { |v, i| "/var/vcap/jobs/{{.Name}}/config/env/#{env}_#{i}" }.join(",")
when Hash
v.collect { |k, v| "#{k}:/var/vcap/jobs/{{.Name}}/config/env/#{env}_#{k}" }.join(",")
else
Expand All @@ -25,8 +23,6 @@

def env_windows_file_flag(v, env)
case v
when Array
v.collect.with_index { |v, i| "C:\\var\\vcap\\jobs\\{{.Name}}\\config\\env\\#{env}_#{i}" }.join(",")
when Hash
v.collect { |k, v| "#{k}:C:\\var\\vcap\\jobs\\{{.Name}}\\config\\env\\#{env}_#{k}" }.join(",")
else
Expand Down

0 comments on commit 7c7b5ac

Please sign in to comment.