Giter Site home page Giter Site logo

Comments (3)

howech avatar howech commented on August 20, 2024

This is an excellent suggestion, and I will get on it when I get some time - maybe by the weekend.

If you want to take a crack at it, look at the way lib/cfndsl.rb works. Basically, it sets up the CloudFormation function to create a CloudFormationTemplate, evaluate the block given in its context, run a few tests and the it calls "generateOutput".

generateOutput is stupid - it just calls to_json and prints the result to stdout.

You could write your own version of the CloudFormation function found in lib/cfndsl.rb that does all of the same things, except that instead of calling x.generateOutput it returns x.to_json.

from cfndsl.

ianneub avatar ianneub commented on August 20, 2024

I gave it a shot and was able to get it working from irb, but not through Rails.

Here is my tiny patch: ianneub@f372496

From irb I do the following:

require './lib/cfndsl'

test =<<-END
Description "asdasdff"

Parameter "AppType" do
  Description "Name of the application type run. Either: abc or def"
  Type "String"
  Default "abc"
  AllowedValues ["abc", "def"]
end
END

CloudFormation do
  eval(test)
end

This is the output:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "asdasdff",
    "Parameters": {
        "AppType": {
            "Type": "String",
            "Description": "Name of the application type run. Either: abc or def",
            "Default": "abc",
            "AllowedValues": [
                "abc",
                "def"
            ]
        }
    }
}

Perfect!

However, from rails I get this:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "asdasdff",
    "Parameters": {
        "AppType": "abc"
    }
}

Here is my model:

class CloudFormation < ActiveRecord::Base
  def to_json
    begin
      the_code = self.code
      CloudFormation do
        eval(the_code)
      end
    rescue => e
      "#{e.class} - #{e.message} - #{e.backtrace.join("\n")}"
    end
  end
end

Not sure why it's not working through the blocks inside the eval from rails.

Any thoughts?

from cfndsl.

ianneub avatar ianneub commented on August 20, 2024

This isn't an ideal solution, but it's what I finally got working...

Instead of running the CloudFormation function in the Rails process I spawn a new process using Open3.

This has the benefit of being isolated from the Rails env so that cfndsl doesn't conflict with Rails and also so that I can eval user generated code in a safer env.

Here's how I did it:

Created a new file in the root of my rails project that accepts the cfndsl code to run over STDIN and outputs it back to STDOUT:

require 'cfndsl'

x = CfnDsl::CloudFormationTemplate.new
x.instance_eval STDIN.read
puts x.to_json

In my model I use this code to execute the process:

my_code_string = "Description 'asdf'"

stdin, stdout, stderr, wait_thr = Open3.popen3("ruby #{Rails.root}/cfndsl.rb")
stdin.write(my_code_string)
stdin.close
if wait_thr.value.exitstatus == 0
  stdout.read
else
  stderr.read
end

from cfndsl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.