Top Level Namespace

Defined Under Namespace

Modules: Gaptool

Instance Method Summary collapse

Instance Method Details

#infohelper(nodes, parseable, grepable) ⇒ Object



10
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
# File 'lib/gaptool-client.rb', line 10

def infohelper(nodes, parseable, grepable)
  if parseable
    puts nodes.to_json
  else
    nodes.each do |node|
      host = "#{node['role']}:#{node['environment']}:#{node['instance']}"
      unless grepable
        puts Rainbow(host).green
      end
      keys = node.keys.sort
      keys.each do |key|
        value = node[key]
        if grepable
          puts "#{host}|#{key}|#{value}"
        else
          value = Time.at(node[key].to_i) if key == 'launch_time'
          unless key == keys.last
            puts "#{Rainbow(key).cyan}: #{value}"
          else
            puts "#{Rainbow(key).cyan}: #{value}\n\n"
          end
        end
      end
    end
  end
end

#split_attrs(attribute_list) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gaptool-client.rb', line 78

def split_attrs(attribute_list)
  opts = {}
  attribute_list.each do |attr_|
    key, value = attr_.split('=', 2)
    split = key.split('.')
    cur = opts
    split.each_with_index do |part, idx|
      if idx == split.size - 1
        # leaf, add the value
        cur[part] = value
      else
        cur[part] ||= {}
      end
      cur = cur[part]
    end
  end
  opts
end

#sshcmd(node, commands) ⇒ Object



37
38
39
40
41
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
# File 'lib/gaptool-client.rb', line 37

def sshcmd(node, commands)
  Net::SSH.start(
    node['hostname'],
    'admin',
    :key_data => [$api.ssh(node['role'], node['environment'], node['instance'])['key']],
    :config => false,
    :keys_only => true,
    :paranoid => false
  ) do |ssh|
    exit_code = nil
    exit_signal = nil
    commands.each do |command|
      ssh.open_channel do |channel|
        channel.exec(command) do |chx, success|
          unless success
            abort "FAILED: couldn't execute command (ssh.channel.exec)"
          end
          channel.on_data do |chd,data|
            puts "#{Rainbow(node['role']).yellow}:#{Rainbow(node['environment']).yellow}:#{Rainbow(node['instance']).yellow}> #{data}"
          end

          channel.on_extended_data do |che,type,data|
            puts "#{Rainbow(node['role']).yellow}:#{Rainbow(node['environment']).yellow}:#{Rainbow(node['instance']).red}> #{data}"
          end

          channel.on_request("exit-status") do |chs,data|
            exit_code = data.read_long
            if exit_code != 0
              exit exit_code
            end
          end

          channel.on_request("exit-signal") do |chg, data|
            exit_signal = data.read_string
          end
        end
      end
    end
  end
end