lib/cloudstack-cli/commands/stack.rb in cloudstack-cli-0.11.2 vs lib/cloudstack-cli/commands/stack.rb in cloudstack-cli-0.12.0

- old
+ new

@@ -1,10 +1,10 @@ class Stack < CloudstackCli::Base - desc "create STACKFILE", "create a stack of servers" + desc "create STACKFILE", "create a stack of servers" def create(stackfile) - stack = parse_stackfile(stackfile) + stack = parse_stackfile(stackfile) say "Create stack #{stack["name"]}...", :green projectid = find_project(stack["project"])['id'] if stack["project"] jobs = [] client.verbose = false stack["servers"].each do |instance| @@ -40,11 +40,11 @@ } end end end watch_jobs(jobs) - + say "Check for port forwarding rules...", :green jobs = [] stack["servers"].each do |instance| instance["name"].gsub(', ', ',').split(',').each do |name| if port_rules = string_to_array(instance["port_rules"]) @@ -103,16 +103,27 @@ end end no_commands do def parse_stackfile(stackfile) + handler = case File.extname(stackfile) + when ".json" + Object.const_get "JSON" + when ".yaml", ".yml" + Object.const_get "YAML" + else + say "File extension #{File.extname(stackfile)} not supported. Supported extensions are .json, .yaml or .yml", :red + exit + end begin - return JSON.parse File.read(stackfile) + return handler.load File.read(stackfile) rescue SystemCallError - $stderr.puts "Can't find the stack file #{stackfile}." - rescue JSON::ParserError => e - $stderr.puts "Error parsing json file.\n#{e.message}." - exit + say "Can't find the stack file #{stackfile}.", :red + exit 1 + rescue => e + say "Error parsing #{File.extname(stackfile)} file:", :red + say e.message + exit 1 end end def load_string_or_array(item) item.is_a?(Array) ? item : [item]