Class: Hocho::Command

Inherits:
Thor
  • Object
show all
Defined in:
lib/hocho/command.rb

Instance Method Summary collapse

Instance Method Details

#apply(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

#listObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# 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