examples/mockcloud/generator.rb in nuri-0.5.2 vs examples/mockcloud/generator.rb in nuri-0.5.3
- old
+ new
@@ -1,66 +1,22 @@
#!/usr/bin/env ruby
-def generate(number_apps)
- header = 'include "../modules/node/node.sfp"
-include "../modules/mockcloud/mockcloud.sfp"
-include "../modules/vm/vm.sfp"
-include "../modules/apache/apache.sfp"
-include "../modules/mysql/mysql.sfp"
-include "../modules/wordpresscluster/wordpresscluster.sfp"
+require 'erb'
+require 'ostruct'
-proxy isa Node {
- sfpAddress is "localhost"
- cloud isa MockCloud
-}
-'
- vmlb = 'vmlb isa VM {
- apache isa Apache {
- running is true
- is_load_balancer is true
- lb_members is (%members%)
- }
-}'
- vmapp = '%vmapp% isa VM {
- apache isa Apache {
- running is true
- }
- wp_web isa WordpressWeb {
- installed is true
- http is %vmapp%.apache
- database is vmdb.wp_db
- }
-}'
- vmdb = 'vmdb isa VM {
- mysql isa Mysql {
- running is true
- }
- wp_db isa WordpressDB {
- installed is true
- mysql is vmdb.mysql
- }
-}'
- output = ''
- members = ''
- global = "global {\n"
- 1.upto(number_apps) do |i|
- name = "vmapp#{i}"
- output += vmapp.gsub(/%vmapp%/, name) + "\n"
- members += "#{name},"
- global += "\tif vmlb.apache.running is true then #{name}.apache.running is true\n"
- global += "\tif #{name}.apache.running is true then vmdb.mysql.running is true\n"
+class ErbBinding < OpenStruct
+ def render(template)
+ ERB.new(template).result(binding)
end
- global += "}\n"
- output = header +
- vmlb.sub(/%members%/, members.chop) + "\n" +
- output + vmdb + "\n" + global
- output
end
-if $0 == __FILE__
- if ARGV[0] == 'help'
- puts "Usage: generator.rb [total-app-layer]"
- else
- number_apps = (ARGV.length > 0 ? ARGV[0].to_i : 3)
- puts generate(number_apps)
+if ARGV.length <= 0
+ puts "Usage: generator.rb <template-file> [key=value]*"
+else
+ template = File.read(ARGV.shift)
+ data = {}
+ ARGV.each do |arg|
+ key, value = arg.split('=')
+ data[key] = value
end
+ puts ErbBinding.new(data).render(template)
end