Class: LanesCli::Lanes

Inherits:
Thor
  • Object
show all
Includes:
CommandLineReporter
Defined in:
lib/lanes.rb

Instance Method Summary collapse

Instance Method Details

#list(lane = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/lanes.rb', line 27

def list(lane=nil)
  servers = AWS.instance.fetchServers(lane)
  servers.sort_by!{ |s| [s[:lane], s[:name]] }
  display_server_table(servers)
  
end

#sh(lane) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
# File 'lib/lanes.rb', line 58

def sh(lane)
  servers = AWS.instance.fetchServers(lane)
  servers.sort_by{ |s| s[:ip] }

  display_server_table(servers, title: "Available Servers:")

  mods = Props.instance.sshMod(lane)
  identity = if mods['identity'] then mods['identity'] else '' end
  puts "Identity file #{mods['identity']} will be used" if identity

  command = options[:cmd].join(' ')
  if options[:confirm] then
    puts "Confirmed via command line. Moving forward with execution of \"#{command}\" on these machines:"
    confirm = 'CONFIRM'
  else
    confirm = ask "Type CONFIRM to execute \"#{command} \" on these machines:"
  end

  if confirm == 'CONFIRM' then
    servers.each{ |server|
      user = if mods['user'] then mods['user'] else 'ec2-user' end
      Net::SSH.start( server[:ip], user,
        :keys => [identity],
        # :verbose => :debug,
        :encryption => "blowfish-cbc",
        :compression => "zlib",
        :host_key => "ssh-rsa") do |ssh|
          puts "Executing on %{name} ( %{ip} ):\t #{command} \n" % server
          stdout = ''
          ssh.exec!(command) do |channel, stream, data|
            stdout << data
          end
          puts "Completed. %{name}\n\tOutput: #{stdout}\n\n " % server
        end
    }



    confirmPath = options[:urlConfirm]
    if confirmPath != nil then
      confirmDelay = (options[:urlConfirmDelay] or 5)
      confirmTimeout = (options[:urlConfirmTimeout] or 30);
      startTime = Time.new.to_i

      # we better sleep for a few, otherwise the shutdown won't have executed
      puts "Sleeping for #{confirmDelay} seconds, then trying the confirmation endpoint for #{confirmTimeout} seconds..."
      sleep confirmDelay
      while Time.new.to_i - startTime < confirmTimeout && servers.length > 0 do
        servers.each_with_index{ |server, index|
          begin
            res = RestClient.get (confirmPath % server)
            if res.code >= 200 && res.code < 300 then
              puts "\t => #{server[:ip]} responded with #{res.code}"
              servers.delete_at(index)
            else
              puts "\t XX #{server[:ip]} responded with #{res.code}" if options[:v]
            end
          rescue => e
            puts "\t XX #{server[:ip]} connection failed: #{e}" if options[:v]
          end
        }

        sleep 5 if servers.length > 0
        puts "\t => #{servers.length} server(s) remaining..." if servers.length > 0
      end

      if servers.length == 0 then
        puts "Successfully confirmed endpoints responded with a 2XX"
      else
        puts "The following server(s) did not respond with a 2XX:"
        servers.each{ |server|
          puts "\t%{name} (%{lane}) \t %{ip} \t %{id} " % server
        }
      end
    end
  else
    puts 'Aborted!'
    exit 1
  end
end

#ssh(lane = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lanes.rb', line 35

def ssh(lane=nil)
  chosen = chooseServer(lane)
  if chosen != nil
    mods = Props.instance.sshMod(chosen[:lane])
    identity = "-i #{mods['identity']}" if mods['identity']
    tunnels = "-L#{mods['tunnel']}" if mods['tunnel']
    tunnels = mods['tunnels'].map{|tunnel| "-L#{tunnel}"}.join(' ') if mods['tunnels']
    user = mods['user'] ? mods['user'] : 'ec2-user'
    cmd = "ssh #{user}@%{ip} #{identity} #{tunnels}" % chosen
    exec cmd
  else
    puts 'Canceled'
  end
end

#switch(profile) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/lanes.rb', line 17

def switch(profile)
  path = ENV['HOME'] + '/.lanes/lanes.yml'
  data = YAML.load_file path
  data["profile"] = profile
  File.open(path, 'w') { |f| YAML.dump(data, f) }

  puts "Switched to #{profile}"
end