Class: WASP::Aws

Inherits:
Object show all
Defined in:
lib/wasp/ec2.rb

Constant Summary collapse

SLEEP_TIME =
1
LINE_LENGTH =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Aws

Returns a new instance of Aws.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/wasp/ec2.rb', line 18

def initialize (args)
  config_path = ENV["HOME"] + "/.waspaws.yml"
  begin
    AWS.config(YAML.load(File.read(config_path)))
    @ec2 = AWS::EC2.new
  rescue => ex
    puts "[WARN]".yellow + " #{ex.message}"
    puts "[WARN]".yellow + " Please set AWS credential file."
    exit false
  end
  
  # evaluate AWS access_key and secret_access_key
  begin
    print "EC2".green + " Checking access key validation.."
    @ec2.availability_zones.each do |av| 
      av.name 
    end
    puts " OK".green
  rescue => ex
    puts "[WARN]".yellow + " #{ex.message}"
    puts "[WARN]".yellow + " Please copy/paste correct AWS access_key and secret_access_key to config/aws.yml file"
    exit false
  end
  
  @num_wasps = if args[:server].nil? then WASP::Const::DEFAULT_WASPS
              else
                args[:server].to_i
              end
  @group = if args[:group].nil? then WASP::Const::DEFAULT_GROUP
           else
             args[:group]
           end
  @zone = if args[:zone].nil? then WASP::Const::DEFAULT_ZONE
          else
            args[:zone]
          end              
  @ami = if args[:ami].nil? then get_default_ami(@zone)
         else
           args[:ami]
         end
  @login = if args[:login].nil? then WASP::Const::DEFAULT_USER
           else
             args[:login]
           end     
  
  @key = args[:key]
  
  
  @regions = @ec2.regions
  @instance_list = []
  @instances = nil
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



5
6
7
# File 'lib/wasp/ec2.rb', line 5

def config_path
  @config_path
end

#ec2Object (readonly)

Returns the value of attribute ec2.



6
7
8
# File 'lib/wasp/ec2.rb', line 6

def ec2
  @ec2
end

#groupObject (readonly)

Returns the value of attribute group.



7
8
9
# File 'lib/wasp/ec2.rb', line 7

def group
  @group
end

#instance_listObject (readonly)

Returns the value of attribute instance_list.



11
12
13
# File 'lib/wasp/ec2.rb', line 11

def instance_list
  @instance_list
end

#instancesObject (readonly)

Returns the value of attribute instances.



9
10
11
# File 'lib/wasp/ec2.rb', line 9

def instances
  @instances
end

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/wasp/ec2.rb', line 8

def key
  @key
end

#loginObject (readonly)

Returns the value of attribute login.



13
14
15
# File 'lib/wasp/ec2.rb', line 13

def 
  @login
end

#regionsObject (readonly)

Returns the value of attribute regions.



10
11
12
# File 'lib/wasp/ec2.rb', line 10

def regions
  @regions
end

#threadsObject (readonly)

Returns the value of attribute threads.



12
13
14
# File 'lib/wasp/ec2.rb', line 12

def threads
  @threads
end

Instance Method Details

#_delete_fileObject



218
219
220
221
222
223
224
225
# File 'lib/wasp/ec2.rb', line 218

def _delete_file
  begin
    File.delete("#{ENV["HOME"]}/.nest")
  rescue => ex
    puts "[WARN]".yellow + "#{ex.message}"
    puts "There is no wasps in the air."
  end
end

#_get_instancesObject



173
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
# File 'lib/wasp/ec2.rb', line 173

def _get_instances
  status_of_wasps = {}
  if @instance_list.count == 0 then
    begin
      File.open("#{ENV["HOME"]}/.nest", "r") do |file|
        lines = file.readlines
        lines.each do |line|
          instance, key, zone,  = line.split(' ')
          
          if status_of_wasps[zone].nil? then
            status_of_wasps[zone] = {}
            status_of_wasps[zone][:instances] = []
            status_of_wasps[zone][:instances].push(instance)
            status_of_wasps[zone][:login] = 
          else
            status_of_wasps[zone][:instances].push(instance)
          end
        end
      end
    rescue => ex
      puts "There is no wasps in the air."
      
      return nil
    end
  end

  return status_of_wasps
end

#_write_to_fileObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/wasp/ec2.rb', line 202

def _write_to_file
  begin
    File.open("#{ENV["HOME"]}/.nest", "a+") do |file|
      if @instances.class == Array then
        @instances.each do |i|
          file.puts("#{i.id} #{i.key_name} #{i.availability_zone} #{@login}\n")
        end
      else
        file.puts("#{@instances.id} #{@instances.key_name} #{@instances.availability_zone} #{@login}\n")
      end
    end
  rescue => ex
    puts "[WARN]".yellow + " #{ex.message}"
  end
end

#createObject



143
144
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
# File 'lib/wasp/ec2.rb', line 143

def create 
  puts "EC2".green + " #{@num_wasps} wasps will be launched.."
  
  begin
    @instances = @ec2.regions[@zone].instances.create(:image_id=>@ami,
                                          :security_groups=>[@group],
                                          :key_name=>@key + '-' + @zone,
                                          :instance_type=>'t1.micro',
                                          :count=>@num_wasps)                                      
    num_wasps = 0
  rescue => ex
    puts "[WARN]".yellow + " #{ex.message}"
    return false
  end
  if @instances.class == Array then
    @instances.each do |i|
      waiting_wasp(i)
    end
    num_wasps = @instances.count
  else
    waiting_wasp(@instances)
    num_wasps = 1
  end
  
  _write_to_file

  puts "The swarm has assembled " + "#{num_wasps}".yellow + " wasps.\n"
  true
end

#get_default_ami(zone) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/wasp/ec2.rb', line 325

def get_default_ami(zone)
  case
  when zone.match(/eu-west-1/)
    "ami-895069fd"
  when zone.match(/sa-east-1/)
    "ami-b673acab"
  when zone.match(/us-west-1/)
    "ami-6da8f128"
  when zone.match(/us-west-2/)
    "ami-ae05889e"
  when zone.match(/ap-northeast-1/)
    "ami-10299f11"
  when zone.match(/us-east-1/)
    "ami-baba68d3"
  when zone.match(/ap-southeast-1/)
    "ami-4296d210"
  else
    "ami-baba68d3"
  end
end

#get_ec2_zone(zone) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/wasp/ec2.rb', line 304

def get_ec2_zone(zone)
  case
  when zone.match(/eu-west-1/)
    "eu-west-1"
  when zone.match(/sa-east-1/)
    "sa-east-1"
  when zone.match(/us-west-1/)
    "us-west-1"
  when zone.match(/us-west-2/)
    "us-west-2"
  when zone.match(/ap-northeast-1/)
    "ap-northeast-1"
  when zone.match(/us-east-1/)
    "us-east-1"
  when zone.match(/ap-southeast-1/)
    "ap-southeast-1"
  else
    "us-east-1"
  end
end

#get_keypairObject



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
# File 'lib/wasp/ec2.rb', line 71

def get_keypair 
  make_key = true
  key = @regions[@zone].key_pairs.filter('key-name', @key + '-' + @zone).first
  
  if key.nil? == false then
    if not File.exists?("#{ENV['HOME']}/.ssh/#{@key}-#{@zone}.pem") then
      key.delete 
      make_key = true
    else
      make_key = false
    end
  end
              
  if make_key then
    key = @regions[@zone].key_pairs.create(@key + '-' + @zone)
    keyfile = "#{ENV['HOME']}/.ssh/#{@key}-#{@zone}.pem"
    begin
      File.delete(keyfile) if File.exists?(keyfile)
      File.open(keyfile, "w") do |f|
        f.write(key.private_key)
      end
      File.chmod(0400, keyfile)
    rescue => ex
      print "[WARN]".yellow + " #{ex.message}"
      exit false
    end
  end

  puts "EC2".green + " Private key is created in " + "~/.ssh/#{@key}-#{@zone}.pem".bold
end

#get_regionsObject



346
347
348
349
350
351
352
353
# File 'lib/wasp/ec2.rb', line 346

def get_regions
  regions = []
  @ec2.regions.each do |r|
    regions.push(r.name)
  end
  
  regions
end

#get_security_groupObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/wasp/ec2.rb', line 102

def get_security_group
  found = false
  puts "EC2".green + " Look up existed security group [" + "#{@group}".bold + "].."
  security_groups = @regions[@zone].security_groups
  
  found = true if security_groups.filter('group-name', @group).first != nil
  
  if not found then
    puts "EC2".green + " Creating security group " + "#{@group}".bold + ".."
    wasps = security_groups.create(@group)
    puts "EC2".green + " Open inbound tcp port :#{WASP::Const::SSH_PORT}"
    wasps.authorize_ingress(:tcp, WASP::Const::SSH_PORT)
    puts "EC2".green + " Open inbound tcp port :#{WASP::Const::NATS_PORT}"
    wasps.authorize_ingress(:tcp, WASP::Const::NATS_PORT)
    puts "EC2".green + " Allow ping"
    wasps.allow_ping
  end
  
  puts "EC2".green + " Security group [#{@group}] have set.."
end

#get_wasp_domain(zone = WASP::Const::DEFAULT_ZONE, id) ⇒ Object



294
295
296
297
298
299
300
301
302
# File 'lib/wasp/ec2.rb', line 294

def get_wasp_domain(zone=WASP::Const::DEFAULT_ZONE , id)
  begin
    z = get_ec2_zone(zone)
    @ec2.regions[z].instances[id].dns_name
  rescue => ex
    puts "[WARN]".yellow + " #{ex.message}"
    return nil
  end
end

#get_wasp_status(zone = WASP::Const::DEFAULT_ZONE, id) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/wasp/ec2.rb', line 284

def get_wasp_status(zone=WASP::Const::DEFAULT_ZONE , id)
  begin
    z = get_ec2_zone(zone)
    @ec2.regions[z].instances[id].status
  rescue => ex
    puts "[WARN]".yellow + " #{ex.message}"
    return nil
  end        
end

#get_waspsObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/wasp/ec2.rb', line 254

def get_wasps
  wasps = []
  ins = _get_instances
  
  return nil if ins.nil?
  
  count = ins.count
  ins.each do |region, info|
    r = get_ec2_zone(region)
     = info[:login]
    info[:instances].each do |it|
      i = @ec2.regions[r].instances[it]
    
      wasps.push({:instance_id => i.id, 
                 :instance_name => i.dns_name, 
                 :region => region,
                 :login => ,
                 :key_name => i.key_pair.name,
                 :report => nil,
                 :wavereport => nil})
    end
  end
  
  wasps
end

#statusObject



280
281
282
# File 'lib/wasp/ec2.rb', line 280

def status
  return ins = _get_instances
end

#terminateObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/wasp/ec2.rb', line 227

def terminate
  count = 0
  
  #if @instances.nil? then
    ins = _get_instances
    
    return nil if ins.nil?
    
    
    ins.each do |region, info|
      r = get_ec2_zone(region)
      info[:instances].each do |i| 
        count += 1
        puts "Wasp" + " #{i}".yellow + " from #{region} is going home"
        begin
          @ec2.regions[r].instances[i].terminate
        rescue => e
          puts "#{e.message}"
        end
      end
    end
  
  _delete_file
  
  puts "EC2".green + " Stood down #{count} wasps.\n"
end

#waiting_wasp(i) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/wasp/ec2.rb', line 123

def waiting_wasp (i)
  banner = "EC2".green + " Launching wasps: "
  
  display banner, false      
  while i.status == :pending do
    print '.'
    sleep SLEEP_TIME
  end

  if i.status == :running then
    clear(LINE_LENGTH)
    display "#{banner}#{'OK'.green}"
    @instance_list.push(i.id)
    puts "Wasp " + "#{i.id}".yellow + " is ready to attack"
    i.tags.Name = 'a wasp!'
  else
    puts "EC2".green + " #{i.id} is going to wrong place"
  end
end