#!/usr/bin/env ruby require 'thor' require_relative '../lib/convection/control/cloud' module Convection ## # Convection CLI ## class CLI < Thor class_option :cloudfile, :type => :string, :default => 'Cloudfile' def initialize(*args) super @cloud = Control::Cloud.new @cwd = Dir.getwd end desc 'converge STACK', 'Converge your cloud' option :stack_group, :type => :string, :desc => 'The name of a stack group defined in your cloudfile to converge' option :stacks, :type => :array, :desc => 'A ordered space separated list of stacks to converge' def converge(stack = nil) @cloud.configure(File.absolute_path(options['cloudfile'], @cwd)) @cloud.converge(stack, stack_group: options[:stack_group], stacks: options[:stacks]) do |event, errors| say_status(*event.to_thor) errors.each do |error| say "* #{ error.message }" error.backtrace.each { |b| say " #{ b }" } end unless errors.nil? end end desc 'diff STACK', 'Show changes that will be applied by converge' option :verbose, :type => :boolean, :aliases => '--v', :desc => 'Show stack progress' option :'very-verbose', :type => :boolean, :aliases => '--vv', :desc => 'Show unchanged stacks' option :stack_group, :type => :string, :desc => 'The name of a stack group defined in your cloudfile to diff' option :stacks, :type => :array, :desc => 'A ordered space separated list of stacks to diff' def diff(stack = nil) @cloud.configure(File.absolute_path(options['cloudfile'], @cwd)) last_event = nil @cloud.diff(stack, stack_group: options[:stack_group], stacks: options[:stacks]) do |d| if d.is_a? Model::Event if options[:'very-verbose'] || d.name == :error say_status(*d.to_thor) elsif options[:verbose] say_status(*d.to_thor) if d.name == :compare end last_event = d elsif d.is_a? Model::Diff if !options[:'very-verbose'] && !options[:verbose] say_status(*last_event.to_thor) unless last_event.nil? last_event = nil end say_status(*d.to_thor) else say_status(*d.to_thor) end end end desc 'print STACK', 'Print the rendered template for STACK' def print(stack) @cloud.configure(File.absolute_path(options['cloudfile'], @cwd)) puts @cloud.stacks[stack].to_json(true) end desc 'validate STACK', 'Validate the rendered template for STACK' def validate(stack) @cloud.configure(File.absolute_path(options['cloudfile'], @cwd)) @cloud.stacks[stack].validate end end end Convection::CLI.start(ARGV)