Skip to content

Commit

Permalink
Added JSON to the evaluation context
Browse files Browse the repository at this point in the history
  • Loading branch information
f0rmiga committed May 31, 2019
1 parent 39f4ed6 commit 3f11c8d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
7 changes: 7 additions & 0 deletions assets/json_evaluation.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%=
config = {
Foo: "bar",
Bar: "baz"
}
JSON.generate(config)
%>
12 changes: 12 additions & 0 deletions assets/json_evaluation_job.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: json_evaluation

packages:
- foopackage

templates:
json_evaluation.erb: json_evaluation

consumes:
- name: barprovider
type: barprovider
4 changes: 3 additions & 1 deletion assets/no_properties_job.MF
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---
name: no_properties

packages:
- foopackage
- foopackage

templates:
no_properties.yml.erb: no_properties.yml

Expand Down
25 changes: 25 additions & 0 deletions bosh_template_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,28 @@ func TestWithNilContext(t *testing.T) {
assert.NoError(err)
assert.Equal("no_properties\n", string(output))
}

func TestWithJSONEvaluation(t *testing.T) {
// Arrange
assert := assert.New(t)
erbFile := filepath.Join(testDir(), "assets", "json_evaluation.erb")
jobSpecFile := filepath.Join(testDir(), "assets", "json_evaluation_job.MF")

erbRenderer := NewERBRenderer(
&EvaluationContext{},
&InstanceInfo{},
jobSpecFile)
outDir, err := ioutil.TempDir("", "bosh-erb-render")
assert.NoError(err)
outFile := filepath.Join(outDir, "output")

// Act
err = erbRenderer.Render(erbFile, outFile)
assert.NoError(err)

output, err := ioutil.ReadFile(outFile)

// Assert
assert.NoError(err)
assert.Equal(`{"Foo":"bar","Bar":"baz"}`+"\n", string(output))
}
5 changes: 3 additions & 2 deletions rb/template_evaluation_context.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "erb"
require "yaml"
require "bosh/template"
require "erb"
require "fileutils"
require "json"
require "yaml"

if $0 == __FILE__
context_path, spec_path, instance_path, src_path, dst_path = *ARGV
Expand Down
8 changes: 4 additions & 4 deletions rice-box.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ func init() {
// define files
file2 := &embedded.EmbeddedFile{
Filename: "template_evaluation_context.rb",
FileModTime: time.Unix(1559256632, 0),
FileModTime: time.Unix(1559331681, 0),

Content: string("require \"erb\"\nrequire \"yaml\"\nrequire \"bosh/template\"\nrequire \"fileutils\"\n\nif $0 == __FILE__\n context_path, spec_path, instance_path, src_path, dst_path = *ARGV\n\n puts \"Context file: #{context_path}\"\n puts \"Instance file: #{instance_path}\"\n puts \"Spec file: #{spec_path}\"\n puts \"Template file: #{src_path}\"\n puts \"Output file: #{dst_path}\"\n\n # Load the context hash\n context_hash = YAML.load_file(context_path)\n\n # Load the job spec\n job_spec = YAML.load_file(spec_path)\n job_spec['properties'] = {} if job_spec['properties'].nil?\n\n # Load the instace info\n instance_info = YAML.load_file(instance_path)\n\n # Read the erb template\n begin\n perms = File.stat(src_path).mode\n template = Bosh::Template::Test::Template.new(job_spec, src_path)\n rescue Errno::ENOENT\n raise \"failed to read template file #{src_path}\"\n end\n\n # Build links\n links = []\n if context_hash['properties'] && context_hash['properties']['bosh_containerization'] && context_hash['properties']['bosh_containerization']['consumes']\n context_hash['properties']['bosh_containerization']['consumes'].each_pair do |name, link|\n next if link['instances'].empty?\n\n instances = []\n link['instances'].each do |link_instance|\n instances << Bosh::Template::Test::InstanceSpec.new(\n address: link_instance['address'],\n az: link_instance['az'],\n id: link_instance['id'],\n index: link_instance['index'],\n name: link_instance['name'],\n bootstrap: link_instance['index'] == '0',\n )\n end\n links << Bosh::Template::Test::Link.new(name: name, instances: instances, properties: link['properties'])\n end\n end\n\n # Build instance\n instance = Bosh::Template::Test::InstanceSpec.new(\n address: instance_info['address'],\n az: instance_info['az'],\n bootstrap: instance_info['index'] == '0',\n deployment: instance_info['deployment'],\n id: instance_info['id'],\n index: instance_info['index'],\n ip: instance_info['ip'],\n name: instance_info['name'],\n networks: {'default' => {'ip' => instance_info['ip'],\n 'dns_record_name' => instance_info['address'],\n # TODO: Do we need more, like netmask and gateway?\n # https://github.com/cloudfoundry/bosh-agent/blob/master/agent/applier/applyspec/v1_apply_spec_test.go\n }},\n )\n\n # Process the Template\n output = template.render(context_hash['properties'], spec: instance, consumes: links)\n\n begin\n # Open the output file\n output_dir = File.dirname(dst_path)\n FileUtils.mkdir_p(output_dir)\n out_file = File.open(dst_path, 'w')\n\n # Write results to the output file\n out_file.write(output)\n\n # Set the appropriate permissions on the output file\n if File.basename(File.dirname(dst_path)) == 'bin'\n out_file.chmod(0755)\n else\n out_file.chmod(perms)\n end\n rescue Errno::ENOENT, Errno::EACCES => e\n out_file = nil\n raise \"failed to open output file #{dst_path}: #{e}\"\n ensure\n out_file.close unless out_file.nil?\n end\nend\n"),
Content: string("require \"bosh/template\"\nrequire \"erb\"\nrequire \"fileutils\"\nrequire \"json\"\nrequire \"yaml\"\n\nif $0 == __FILE__\n context_path, spec_path, instance_path, src_path, dst_path = *ARGV\n\n puts \"Context file: #{context_path}\"\n puts \"Instance file: #{instance_path}\"\n puts \"Spec file: #{spec_path}\"\n puts \"Template file: #{src_path}\"\n puts \"Output file: #{dst_path}\"\n\n # Load the context hash\n context_hash = YAML.load_file(context_path)\n\n # Load the job spec\n job_spec = YAML.load_file(spec_path)\n job_spec['properties'] = {} if job_spec['properties'].nil?\n\n # Load the instace info\n instance_info = YAML.load_file(instance_path)\n\n # Read the erb template\n begin\n perms = File.stat(src_path).mode\n template = Bosh::Template::Test::Template.new(job_spec, src_path)\n rescue Errno::ENOENT\n raise \"failed to read template file #{src_path}\"\n end\n\n # Build links\n links = []\n if context_hash['properties'] && context_hash['properties']['bosh_containerization'] && context_hash['properties']['bosh_containerization']['consumes']\n context_hash['properties']['bosh_containerization']['consumes'].each_pair do |name, link|\n next if link['instances'].empty?\n\n instances = []\n link['instances'].each do |link_instance|\n instances << Bosh::Template::Test::InstanceSpec.new(\n address: link_instance['address'],\n az: link_instance['az'],\n id: link_instance['id'],\n index: link_instance['index'],\n name: link_instance['name'],\n bootstrap: link_instance['index'] == '0',\n )\n end\n links << Bosh::Template::Test::Link.new(name: name, instances: instances, properties: link['properties'])\n end\n end\n\n # Build instance\n instance = Bosh::Template::Test::InstanceSpec.new(\n address: instance_info['address'],\n az: instance_info['az'],\n bootstrap: instance_info['index'] == '0',\n deployment: instance_info['deployment'],\n id: instance_info['id'],\n index: instance_info['index'],\n ip: instance_info['ip'],\n name: instance_info['name'],\n networks: {'default' => {'ip' => instance_info['ip'],\n 'dns_record_name' => instance_info['address'],\n # TODO: Do we need more, like netmask and gateway?\n # https://github.com/cloudfoundry/bosh-agent/blob/master/agent/applier/applyspec/v1_apply_spec_test.go\n }},\n )\n\n # Process the Template\n output = template.render(context_hash['properties'], spec: instance, consumes: links)\n\n begin\n # Open the output file\n output_dir = File.dirname(dst_path)\n FileUtils.mkdir_p(output_dir)\n out_file = File.open(dst_path, 'w')\n\n # Write results to the output file\n out_file.write(output)\n\n # Set the appropriate permissions on the output file\n if File.basename(File.dirname(dst_path)) == 'bin'\n out_file.chmod(0755)\n else\n out_file.chmod(perms)\n end\n rescue Errno::ENOENT, Errno::EACCES => e\n out_file = nil\n raise \"failed to open output file #{dst_path}: #{e}\"\n ensure\n out_file.close unless out_file.nil?\n end\nend\n"),
}

// define dirs
dir1 := &embedded.EmbeddedDir{
Filename: "",
DirModTime: time.Unix(1559254477, 0),
DirModTime: time.Unix(1559325973, 0),
ChildFiles: []*embedded.EmbeddedFile{
file2, // "template_evaluation_context.rb"

Expand All @@ -32,7 +32,7 @@ func init() {
// register embeddedBox
embedded.RegisterEmbeddedBox(`rb`, &embedded.EmbeddedBox{
Name: `rb`,
Time: time.Unix(1559254477, 0),
Time: time.Unix(1559325973, 0),
Dirs: map[string]*embedded.EmbeddedDir{
"": dir1,
},
Expand Down

0 comments on commit 3f11c8d

Please sign in to comment.