Class: Hotdog::Commands::Ssh

Inherits:
Search show all
Defined in:
lib/hotdog/commands/ssh.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::MASK_DATABASE, BaseCommand::MASK_QUERY, BaseCommand::PERSISTENT_DB

Instance Attribute Summary

Attributes inherited from Search

#remote_command

Attributes inherited from BaseCommand

#application, #logger, #options

Instance Method Summary collapse

Methods inherited from Search

#evaluate, #get_hosts_with_search_tags, #parse, #parse_options

Methods inherited from BaseCommand

#execute, #fixed_string?, #initialize, #parse_options, #reload

Constructor Details

This class inherits a constructor from Hotdog::Commands::BaseCommand

Instance Method Details

#define_options(optparse, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hotdog/commands/ssh.rb', line 11

def define_options(optparse, options={})
  options[:index] = nil
  options[:options] = []
  options[:user] = nil
  options[:port] = nil
  options[:identity_file] = nil
  options[:forward_agent] = false

  optparse.on("-n", "--index INDEX", "Use this index of host if multiple servers are found", Integer) do |index|
    options[:index] = index
  end
  optparse.on("-o SSH_OPTION", "Passes this string to ssh command through shell. This option may be given multiple times") do |option|
    options[:options] += [option]
  end
  optparse.on("-i SSH_IDENTITY_FILE", "SSH identity file path") do |path|
    options[:identity_file] = path
  end
  optparse.on("-A", "Enable agent forwarding", TrueClass) do |b|
    options[:forward_agent] = b
  end
  optparse.on("-p PORT", "Port of the remote host", Integer) do |port|
    options[:port] = port
  end
  optparse.on("-u SSH_USER", "SSH login user name") do |user|
    options[:user] = user
  end
  optparse.on("-v", "--verbose", "Enable verbose ode") do |v|
    options[:verbose] = v
  end
end

#run(args = [], options = {}) ⇒ Object



42
43
44
45
46
47
48
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
# File 'lib/hotdog/commands/ssh.rb', line 42

def run(args=[], options={})
  expression = args.join(" ").strip
  if expression.empty?
    # return everything if given expression is empty
    expression = "*"
  end

  begin
    node = parse(expression)
  rescue Parslet::ParseFailed => error
    STDERR.puts("syntax error: " + error.cause.ascii_tree)
    exit(1)
  end

  result0 = evaluate(node, self)
  if 0 < result0.length
    if result0.length == 1
      exec_command([result0.first], options)
    else
      if options[:index] and options[:index] < result0.length
        exec_command([result0[options[:index]]], options)
      else
        result, fields = get_hosts_with_search_tags(result0, node)

        # add "index" field
        result = result.each_with_index.map { |host, i| [i] + host }
        fields = ["index"] + fields

        STDERR.print(format(result, fields: fields))
        logger.info("found %d host(s)." % result.length)
        exit(1)
      end
    end
  else
    STDERR.puts("no match found: #{expression}")
    exit(1)
  end
  exit(127)
end