Class: Trebuchet::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/trebuchet/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/trebuchet/server.rb', line 5

def initialize config=nil
  if File.exists?("trebuchet.yml")
    file = File.open('trebuchet.yml')
  elsif File.exists?("~/.trebuchet")
    file = File.open("~/.trebuchet")
  end
  @config = config || YAML::load(file)
  @concurrency = "1"
  @time = "1M"
  @url = "http://www.example.com"
  regions = @config.fetch("aws-config", {}).fetch("regions", ["us-east-1"])

  @connections = []
  regions.each do |region|
    @connections << Fog::Compute.new(:provider => "AWS", :aws_access_key_id => @config.fetch("aws-config", {}).fetch("access_key_id"), :aws_secret_access_key => @config.fetch("aws-config", {}).fetch("access_key_secret"), :region => region)
  end

  refresh_instances
end

Instance Attribute Details

#concurrencyObject

Returns the value of attribute concurrency.



3
4
5
# File 'lib/trebuchet/server.rb', line 3

def concurrency
  @concurrency
end

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/trebuchet/server.rb', line 3

def config
  @config
end

#connectionsObject

Returns the value of attribute connections.



3
4
5
# File 'lib/trebuchet/server.rb', line 3

def connections
  @connections
end

#instancesObject

Returns the value of attribute instances.



3
4
5
# File 'lib/trebuchet/server.rb', line 3

def instances
  @instances
end

#resultObject

Returns the value of attribute result.



3
4
5
# File 'lib/trebuchet/server.rb', line 3

def result
  @result
end

#timeObject

Returns the value of attribute time.



3
4
5
# File 'lib/trebuchet/server.rb', line 3

def time
  @time
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/trebuchet/server.rb', line 3

def url
  @url
end

Instance Method Details

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/trebuchet/server.rb', line 50

def run
  puts "Beginning Load Test"
  @result = Trebuchet::Result.new
  futures = []
  @instances.each do |instance|
    futures << Celluloid::Future.new { run_siege instance }
  end
  futures.each { |future| future.value }
  puts @result.to_s

  nil
end

#start(num_of_servers = 1) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/trebuchet/server.rb', line 25

def start num_of_servers=1
  puts "Starting #{num_of_servers} servers!"
  futures = []
  num_of_servers.times do
    futures << Celluloid::Future.new do
      @connections.sample.servers.bootstrap(:public_key_path => '~/.ssh/id_rsa.pub', :username => 'ubuntu', :tags => {:temporary => true}) rescue Net::SSH::Disconnect
    end
  end
  futures.each { |future| future.value }
  puts "#{num_of_servers} Servers Started!"

  refresh_instances

  puts "Setting up new instances!"
  @instances.each { |instance| setup_instance instance }

  puts "New instances configured and ready for use!"
end

#stopObject



44
45
46
47
48
# File 'lib/trebuchet/server.rb', line 44

def stop
  puts "Shutting down all instances"
  @instances.each { |instance| instance.destroy }
  refresh_instances
end