Sha256: b6ed3bdd45f26cc561f0c8c4dac85b5f790b6eb043c6d063cf8cec3942df1e14

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

require 'thor'
require 'json'

include Specinfra::Helper::Set

module Koma
  class CLI < Thor
    desc 'ssh', 'stdout server inventory'
    option :key,
           type: :string,
           banner: '<key1,key2,..>',
           desc: 'inventory keys',
           aliases: :k
    option :identity_file,
           type: :string,
           banner: '<identity_file>',
           desc: 'identity file',
           aliases: :i
    option :port,
           type: :numeric,
           banner: '<port>',
           desc: 'port',
           aliases: :p
    def ssh(host)
      user, host = host.split('@') if host.include?('@')
      set :backend, :ssh
      set :host, host
      set :request_pty, true
      ssh_options = Net::SSH::Config.for(host)
      ssh_options[:user] = user if user
      ssh_options[:keys] = [options[:identity_file]] if options[:identity_file]
      ssh_options[:port] = options[:port] if options[:port]
      set :ssh_options, ssh_options
      puts JSON.pretty_generate out(options[:key])
    end

    desc 'exec', 'stdout local inventory'
    option :key,
           type: :string,
           banner: '<key1,key2,..>',
           desc: 'inventory keys',
           aliases: :k
    def exec
      set :backend, :exec
      puts JSON.pretty_generate out(options[:key])
    end

    desc 'keys', 'server inventory keys'
    def keys
      inventory_keys.each do |key|
        puts key
      end
    end

    private

    def inventory_keys
      Specinfra::HostInventory.inventory_keys
    end

    def out(key = nil)
      out = {}
      keys = if key.nil?
               inventory_keys
             else
               key.split(',')
             end
      keys.each do |k|
        begin
          out[k] = Specinfra.backend.host_inventory[k]
          out[k] = Specinfra.backend.host_inventory[k].inspect if k == 'ec2'
        rescue NotImplementedError
          out[k] = nil
        end
      end
      out
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
koma-0.2.0 lib/koma/cli.rb