Class: Pero::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pero/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



13
14
15
16
# File 'lib/pero/cli.rb', line 13

def initialize(*)
  super
  Pero.log.level = ::Logger.const_get(options[:log_level].upcase) if options[:log_level]
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/pero/cli.rb', line 8

def exit_on_failure?
  true
end

.shared_optionsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pero/cli.rb', line 18

def self.shared_options
  option :log_level, type: :string, aliases: ['-l'], default: 'info'
  option :user, type: :string, aliases: ['-x'], desc: 'ssh user'
  option :key, type: :string, aliases: ['-i'], desc: 'ssh private key'
  option :port, type: :numeric, aliases: ['-p'], desc: 'ssh port'
  option 'timeout', default: 10, type: :numeric, desc: 'ssh connect timeout'
  option :ssh_config, type: :string, desc: 'ssh config path'
  option :environment, type: :string, desc: 'puppet environment'
  option :ask_password, type: :boolean, default: false, desc: 'ask ssh or sudo password'
  option :vagrant, type: :boolean, default: false, desc: 'use vagrarant'
  option :sudo, type: :boolean, default: true, desc: 'use sudo'
  option 'concurrent', aliases: '-C', default: 3, type: :numeric, desc: 'running concurrent'
end

Instance Method Details

#apply(name_regexp) ⇒ Object



49
50
51
52
53
54
55
56
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
# File 'lib/pero/cli.rb', line 49

def apply(name_regexp)
  if !options['image-name'] && !options['server-version']
    Pero.log.error 'image-name or server-version are required'
    return
  end

  prepare
  nodes = Pero::History.search(name_regexp)
  if nodes.empty?
    Pero.log.info 'No matching node found.'
    return
  end
  m = Mutex.new

  begin
    Parallel.each(nodes, in_threads: options['concurrent']) do |n|
      opt = merge_options(n, options)
      puppet = Pero::Puppet.new(opt['host'], opt, m)
      puppet.apply
    end
  rescue StandardError => e
    Pero.log.error e.backtrace.join("\n")
    Pero.log.error e.inspect
  ensure
    if options['one-shot']
      Pero.log.info 'stop puppet master container'
      opt = merge_options(nodes.first, options)
      Pero::Puppet.new(opt['host'], opt, m).stop_master
    else
      Pero.log.info 'puppet master container keep running'
    end
  end
end

#bootstrap(*hosts) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pero/cli.rb', line 87

def bootstrap(*hosts)
  options['environment'] = 'production' if options['environment'].nil? || options['environment'].empty?
  m = Mutex.new
  Parallel.each(hosts, in_threads: options['concurrent']) do |host|
    raise "unknown option #{host}" if host =~ /^-/

    puppet = Pero::Puppet.new(host, options, m)

    Pero.log.info "bootstrap pero #{host}"
    puppet.install
  end
rescue StandardError => e
  Pero.log.error e.backtrace.join("\n")
  Pero.log.error e.inspect
end

#versionsObject



33
34
35
36
37
# File 'lib/pero/cli.rb', line 33

def versions
  Pero::Puppet::Redhat.show_versions
rescue StandardError => e
  Pero.log.error e.inspect
end