class Cronicle::CLI

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/cronicle/cli.rb, line 21
def initialize(*args)
  super

  if options['debug']
    Cronicle::Logger.instance.set_debug(true)
  end

  if not $stdin.tty? or not options['color']
    String.disable_colorization = true
  end

  options['require'].each {|lib| require(lib) }
end

Public Instance Methods

apply() click to toggle source
# File lib/cronicle/cli.rb, line 44
def apply
  with_logging do
    set_ssh_options
    client.apply(jobfile)
  end
end
cleanup() click to toggle source
# File lib/cronicle/cli.rb, line 52
def cleanup
  with_logging do
    set_ssh_options
    client.cleanup
  end
end
exec(job_name) click to toggle source
# File lib/cronicle/cli.rb, line 36
def exec(job_name)
  with_logging do
    set_ssh_options
    client.exec(jobfile, job_name)
  end
end

Private Instance Methods

client() click to toggle source
# File lib/cronicle/cli.rb, line 73
def client
  Cronicle::Client.new(host_list, client_options)
end
client_options() click to toggle source
# File lib/cronicle/cli.rb, line 94
def client_options
  client_opts = {
    :sudo_password => options['sudo-password'],
    :ssh_user => options['ssh-user'],
    :concurrency => options['concurrency'],
    :var_dir => options['var-dir'],
    :dry_run => options['dry-run'],
    :verbose => options['verbose']
  }

  if options['ask-pass']
    hl = HighLine.new
    client_opts[:sudo_password] = hl.ask('Password: ') {|q| q.echo = '*' }
  end

  client_opts
end
host_list() click to toggle source
# File lib/cronicle/cli.rb, line 87
def host_list
  Cronicle::HostList.new(
    options.fetch('hosts', ''),
    host_list_options
  )
end
host_list_options() click to toggle source
# File lib/cronicle/cli.rb, line 112
def host_list_options
  {
    :roles => options['target-roles']
  }
end
jobfile() click to toggle source
# File lib/cronicle/cli.rb, line 77
def jobfile
  file = options['file']

  unless File.exist?(file)
    raise Thor::Error, "No Jobfile found (looking for: #{file})"
  end

  file
end
set_ssh_options() click to toggle source
# File lib/cronicle/cli.rb, line 118
def set_ssh_options
  conn_timeout = options['connection-timeout']
  ssh_options = {}

  if options['ssh-options']
    JSON.parse(options['ssh-options']).each do |key, value|
      ssh_options[key.to_sym] = value
    end
  end

  ssh_config = options['ssh-config']

  if ssh_config
    ssh_config = File.expand_path(ssh_config)
    ssh_options[:config] = ssh_config if File.exist?(ssh_config)
  end

  SSHKit::Backend::Netssh.configure do |ssh|
    ssh.connection_timeout = conn_timeout if conn_timeout
    ssh.ssh_options = ssh_options unless ssh_options.empty?
  end
end
with_logging() { || ... } click to toggle source
# File lib/cronicle/cli.rb, line 61
def with_logging
  begin
    yield
  rescue => e
    if options['debug']
      raise e
    else
      log(:error, e.message, :color => :red)
    end
  end
end