Class: Gaptool::SshCommand

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

Instance Method Summary collapse

Instance Method Details

#execute ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/gaptool-client.rb', line 145

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