Class: Gaptool::SshCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/gaptool-client.rb

Instance Method Summary collapse

Instance Method Details

#execute ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gaptool-client.rb', line 112

def execute
  if tmux?
    nodes = $api.getenvroles(role, environment)
    system "tmux start-server"
    nodes.each_index do |i|
      @ssh = $api.ssh(role, environment, nodes[i]['instance'])
      if i == 0
        system "tmux new-session -d -s #{role}-#{environment} -n #{nodes[i]['instance']}"
      else
        system "tmux new-window -t #{role}-#{environment}:#{i} -n #{nodes[i]['instance']}"
      end
      File.open("/tmp/gtkey-#{nodes[i]['instance']}", 'w') {|f| f.write(@ssh['key'])}
      File.chmod(0600, "/tmp/gtkey-#{nodes[i]['instance']}")
      system "tmux send-keys -t #{role}-#{environment}:#{i} 'SSH_AUTH_SOCK=\"\" ssh -i /tmp/gtkey-#{nodes[i]['instance']} admin@#{@ssh['hostname']}' C-m"
    end
    system "tmux attach -t #{role}-#{environment}"
  else
    if instance
      @ssh = $api.ssh(role, environment, instance)
    else
      nodes = $api.getenvroles(role, environment)
      if first? || nodes.size == 1
        puts "No instnace specified, but only one instance in cluster or first forced"
        @ssh = $api.ssh(role, environment, nodes.first['instance'])
      else
        puts "No instance specified, querying list."
        nodes.each_index do |i|
          puts "#{i}: #{nodes[i]['instance']}"
        end
        print "Select a node: ".color(:cyan)
        @ssh = $api.ssh(role, environment, nodes[$stdin.gets.chomp.to_i]['instance'])
      end
    end
    File.open('/tmp/gtkey', 'w') {|f| f.write(@ssh['key'])}
    File.chmod(0600, '/tmp/gtkey')
    system "SSH_AUTH_SOCK='' ssh -i /tmp/gtkey admin@#{@ssh['hostname']}"
  end
end