From 3f11c8d3a52a2b80bd1698724264dac49b9733aa Mon Sep 17 00:00:00 2001 From: Thulio Ferraz Assis Date: Fri, 31 May 2019 12:44:25 -0700 Subject: [PATCH] Added JSON to the evaluation context --- assets/json_evaluation.erb | 7 +++++++ assets/json_evaluation_job.MF | 12 ++++++++++++ assets/no_properties_job.MF | 4 +++- bosh_template_go_test.go | 25 +++++++++++++++++++++++++ rb/template_evaluation_context.rb | 5 +++-- rice-box.go | 8 ++++---- 6 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 assets/json_evaluation.erb create mode 100644 assets/json_evaluation_job.MF diff --git a/assets/json_evaluation.erb b/assets/json_evaluation.erb new file mode 100644 index 0000000..0011c15 --- /dev/null +++ b/assets/json_evaluation.erb @@ -0,0 +1,7 @@ +<%= + config = { + Foo: "bar", + Bar: "baz" + } + JSON.generate(config) +%> diff --git a/assets/json_evaluation_job.MF b/assets/json_evaluation_job.MF new file mode 100644 index 0000000..209b354 --- /dev/null +++ b/assets/json_evaluation_job.MF @@ -0,0 +1,12 @@ +--- +name: json_evaluation + +packages: +- foopackage + +templates: + json_evaluation.erb: json_evaluation + +consumes: +- name: barprovider + type: barprovider diff --git a/assets/no_properties_job.MF b/assets/no_properties_job.MF index adee5e8..6fff2bb 100644 --- a/assets/no_properties_job.MF +++ b/assets/no_properties_job.MF @@ -1,7 +1,9 @@ --- name: no_properties + packages: - - foopackage +- foopackage + templates: no_properties.yml.erb: no_properties.yml diff --git a/bosh_template_go_test.go b/bosh_template_go_test.go index 87b79b5..7ecf061 100644 --- a/bosh_template_go_test.go +++ b/bosh_template_go_test.go @@ -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)) +} diff --git a/rb/template_evaluation_context.rb b/rb/template_evaluation_context.rb index f661375..6b18908 100644 --- a/rb/template_evaluation_context.rb +++ b/rb/template_evaluation_context.rb @@ -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 diff --git a/rice-box.go b/rice-box.go index c2c8cd8..764ff28 100644 --- a/rice-box.go +++ b/rice-box.go @@ -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" @@ -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, },