Class: Gaptool::SshCommand

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

Instance Method Summary collapse

Instance Method Details

#executeObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/gaptool-client.rb', line 174

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 instance 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 Rainbow("Select a node: ").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