class Pero::CLI

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/pero/cli.rb, line 8
def exit_on_failure?
  true
end
new(*) click to toggle source
Calls superclass method
# 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
shared_options() click to toggle source
# 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

Public Instance Methods

apply(name_regexp) click to toggle source
# File lib/pero/cli.rb, line 51
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)
  return unless nodes
  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 => 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"
      Parallel.each(nodes, in_threads: options["concurrent"]) do |n|
        opt = merge_options(n, options)
        Pero::Puppet.new(opt["host"], opt, m).stop_master
      end
    else
      Pero.log.info "puppet master container keep running"
    end
  end
end
bootstrap(*hosts) click to toggle source
# File lib/pero/cli.rb, line 90
def bootstrap(*hosts)
  begin
    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 => e
    Pero.log.error e.backtrace.join("\n")
    Pero.log.error e.inspect
  end
end
merge_options(node, options) click to toggle source
# File lib/pero/cli.rb, line 108
def merge_options(node, options)
  opt = node["last_options"].merge(options)
  opt["environment"] = "production" if opt["environment"].nil? || opt["environment"].empty?
  if options["image-name"]
    opt.delete("server-version")
  else
    opt.delete("image-name")
  end
  opt
end
prepare() click to toggle source
# File lib/pero/cli.rb, line 119
def prepare
  `bundle install` if File.exists?("Gemfile")
  `bundle exec librarian-puppet install` if File.exists?("Puppetfile")
end
versions() click to toggle source
# File lib/pero/cli.rb, line 33
def versions
  begin
    Pero::Puppet::Redhat.show_versions
  rescue => e
    Pero.log.error e.inspect
  end
end