class Hocho::Command

Public Instance Methods

apply(name) click to toggle source
# File lib/hocho/command.rb, line 57
def apply(name)
  hosts = inventory.filter({name: name}, exclude_filters: {name: options[:exclude]})
  if hosts.empty?
    raise "host name=#{name.inspect} not found"
  end

  if hosts.size > 1
    puts "Running sequencial on:"
    hosts.each do |host|
      puts " * #{host.name}"
    end
    puts
  end

  if config[:ask_sudo_password] || options[:sudo]
    print "sudo password: "
    sudo_password = $stdin.noecho { $stdin.gets.chomp }
    puts
  end

  hosts.each do |host|
    host.sudo_password = sudo_password if sudo_password
    Runner.new(
      host,
      driver: options[:driver],
      base_dir: config[:itamae_dir] || '.',
      initializers: config[:initializers] || [],
      driver_options: config[:driver_options] || {},
    ).run(
      dry_run: options[:dry_run],
    )
  end
end
list() click to toggle source
# File lib/hocho/command.rb, line 16
def list
  hosts = inventory.hosts

  if options[:verbose]
    case options[:format]
    when 'yaml'
      puts hosts.map(&:to_h).to_yaml
    when 'json'
      puts hosts.map(&:to_h).to_json
    end
  else
    case options[:format]
    when 'yaml'
      puts hosts.map(&:name).to_yaml
    when 'json'
      puts hosts.map(&:name).to_json
    end
  end
end
show(name) click to toggle source
# File lib/hocho/command.rb, line 38
def show(name)
  host = inventory.filter(name: name).first
  if host
    case options[:format]
    when 'yaml'
      puts host.to_h.to_yaml
    when 'json'
      puts host.to_h.to_json
    end
  else
    raise "host name=#{name.inspect} not found"
  end
end

Private Instance Methods

config() click to toggle source
# File lib/hocho/command.rb, line 97
def config
  @config ||= Hocho::Config.load(config_file).tap do |c|
    Dir.chdir c.base_dir # XXX:
  end
end
config_file() click to toggle source
# File lib/hocho/command.rb, line 103
def config_file
  options[:config] || ENV['HOCHO_CONFIG'] || './hocho.yml'
end
inventory() click to toggle source
# File lib/hocho/command.rb, line 93
def inventory
  @inventory ||= Hocho::Inventory.new(config.inventory_providers, config.property_providers)
end