Class: PoolParty::Remoting

Inherits:
Object show all
Includes:
PoolParty, Ec2Wrapper, Scheduler
Defined in:
lib/poolparty/remoting.rb

Direct Known Subclasses

Master

Instance Attribute Summary

Attributes included from Scheduler

#tasker

Instance Method Summary collapse

Methods included from Scheduler

#_tasker, #add_task, #daemonize, #interval, #run_thread_list, #run_thread_loop, #run_threads

Methods included from Ec2Wrapper

included

Methods included from PoolParty

#include_cloud_tasks, #load_app, #load_monitors, #load_plugins, #message, #options, #plugin_dir, #read_config_file, #register_monitor, #registered_monitor?, #registered_monitors, #reset!, #root_dir, #timer, #user_dir, #verbose?, #write_to_temp_file

Instance Method Details

#can_shutdown_an_instance?Boolean

Can we shutdown an instance?

Returns:

  • (Boolean)


120
121
122
# File 'lib/poolparty/remoting.rb', line 120

def can_shutdown_an_instance?
  minimum_number_of_instances_are_running?
end

#can_start_a_new_instance?Boolean

Can we start a new instance?

Returns:

  • (Boolean)


79
80
81
# File 'lib/poolparty/remoting.rb', line 79

def can_start_a_new_instance?
  maximum_number_of_instances_are_not_running?
end

#cloud_keypairsObject

list of keypairs for the current AWS access key and secret key



38
39
40
41
42
43
# File 'lib/poolparty/remoting.rb', line 38

def cloud_keypairs
  get_instances_description.each {|a| a[:keypair] = "no-keypair" unless a[:keypair] } # Get rid of the instances not launched with a keypair
  instances = get_instances_description.sort{|x,y| x[:keypair] <=> y[:keypair]}
  keypair = nil
  instances.map {|a| keypair != a[:keypair] ? (keypair = a[:keypair]; keypair) : nil }.compact
end

#list_of_all_instancesObject

List all instances, regardless of their key pairs or states



32
33
34
35
36
# File 'lib/poolparty/remoting.rb', line 32

def list_of_all_instances
  a_list = []
  cloud_keypairs.each {|keypair| list_of_instances(keypair).each { |inst| a_list << inst } }
  a_list
end

#list_of_instances(keypair = Application.keypair) ⇒ Object

List the instances for the current key pair, regardless of their states



28
29
30
# File 'lib/poolparty/remoting.rb', line 28

def list_of_instances(keypair = Application.keypair)
  get_instances_description.select {|a| a[:keypair] == keypair}
end

#list_of_nonterminated_instances(list = list_of_instances) ⇒ Object

list all the nonterminated instances



24
25
26
# File 'lib/poolparty/remoting.rb', line 24

def list_of_nonterminated_instances(list = list_of_instances)
  list.reject {|a| a[:status] =~ /terminated/}
end

#list_of_pending_instances(list = list_of_nonterminated_instances) ⇒ Object

Get a list of the pending instances



16
17
18
# File 'lib/poolparty/remoting.rb', line 16

def list_of_pending_instances(list = list_of_nonterminated_instances)
  list.select {|a| a[:status] =~ /pending/}
end

#list_of_running_instances(list = list_of_nonterminated_instances) ⇒ Object

GENERAL METHODS

LISTING

List all the running instances associated with this account



12
13
14
# File 'lib/poolparty/remoting.rb', line 12

def list_of_running_instances(list = list_of_nonterminated_instances)
  list.select {|a| a[:status] =~ /running/}
end

#list_of_terminating_instances(list = list_of_nonterminated_instances) ⇒ Object

list of shutting down instances



20
21
22
# File 'lib/poolparty/remoting.rb', line 20

def list_of_terminating_instances(list = list_of_nonterminated_instances)
  list.select {|a| a[:status] =~ /shutting/}
end

#maximum_number_of_instances_are_not_running?Boolean

Are the maximum number of instances running?

Returns:

  • (Boolean)


83
84
85
# File 'lib/poolparty/remoting.rb', line 83

def maximum_number_of_instances_are_not_running?
  list_of_running_instances.size < Application.maximum_instances
end

#minimum_number_of_instances_are_running?Boolean

Are the minimum number of instances running?

Returns:

  • (Boolean)


124
125
126
# File 'lib/poolparty/remoting.rb', line 124

def minimum_number_of_instances_are_running?
  list_of_running_instances.size > Application.minimum_instances
end

#number_of_all_pending_and_running_instancesObject

get the number of pending and running instances



64
65
66
67
# File 'lib/poolparty/remoting.rb', line 64

def number_of_all_pending_and_running_instances
  a_list = list_of_nonterminated_instances( list_of_all_instances )
  number_of_pending_and_running_instances_for_list( a_list )
end

#number_of_pending_and_running_instances(keypair = Application.keypair) ⇒ Object

get the number of pending and running instances for a keypair



59
60
61
62
# File 'lib/poolparty/remoting.rb', line 59

def number_of_pending_and_running_instances(keypair = Application.keypair)
  a_list = list_of_nonterminated_instances( list_of_instances(keypair) )
  number_of_pending_and_running_instances_for_list( a_list )
end

#number_of_pending_and_running_instances_for_list(a_list = list_of_instances) ⇒ Object

get the number of pending and running instances for a list



53
54
55
56
57
# File 'lib/poolparty/remoting.rb', line 53

def number_of_pending_and_running_instances_for_list(a_list = list_of_instances)
  running_list = list_of_running_instances( a_list )
  pending_list = list_of_pending_instances( a_list )
  number_of_running_instances(running_list) + number_of_pending_instances(pending_list)
end

#number_of_pending_instances(list = list_of_pending_instances) ⇒ Object

Get number of pending instances



45
46
47
# File 'lib/poolparty/remoting.rb', line 45

def number_of_pending_instances(list = list_of_pending_instances)
  list.size
end

#number_of_running_instances(list = list_of_running_instances) ⇒ Object

get the number of running instances



49
50
51
# File 'lib/poolparty/remoting.rb', line 49

def number_of_running_instances(list = list_of_running_instances)
  list.size
end

#request_launch_new_instanceObject

LAUNCHING

Request to launch a new instance



70
71
72
73
74
75
76
77
# File 'lib/poolparty/remoting.rb', line 70

def request_launch_new_instance
  if can_start_a_new_instance?
    request_launch_one_instance_at_a_time
    return true
  else
    return false
  end
end

#request_launch_new_instances(num = 1) ⇒ Object

Request to launch a number of instances



87
88
89
90
91
# File 'lib/poolparty/remoting.rb', line 87

def request_launch_new_instances(num=1)
  out = []
  num.times {out << request_launch_one_instance_at_a_time}
  out
end

#request_launch_one_instance_at_a_timeObject

Launch one instance at a time



93
94
95
96
97
98
99
100
# File 'lib/poolparty/remoting.rb', line 93

def request_launch_one_instance_at_a_time
  reset!
  while !number_of_pending_instances.zero?
    wait "5.seconds"
    reset!
  end
  return launch_new_instance!
end

#request_termination_of_all_instancesObject

Request termination of all instances regardless of their state (includes pending instances)



107
108
109
# File 'lib/poolparty/remoting.rb', line 107

def request_termination_of_all_instances
  list_of_instances.each {|a| terminate_instance!(a[:instance_id])}
end

#request_termination_of_instance(id) ⇒ Object

Terminate instance by id



111
112
113
114
115
116
117
118
# File 'lib/poolparty/remoting.rb', line 111

def request_termination_of_instance(id)
  if can_shutdown_an_instance?
    terminate_instance! id
    return true
  else
    return false
  end
end

#request_termination_of_running_instancesObject

SHUTDOWN

Terminate all running instances



103
104
105
# File 'lib/poolparty/remoting.rb', line 103

def request_termination_of_running_instances
  list_of_running_instances.each {|a| terminate_instance!(a[:instance_id])}
end

#running_instancesObject

Get the cached running_instances



128
129
130
# File 'lib/poolparty/remoting.rb', line 128

def running_instances
  @running_instances ||= update_instance_values
end

#update_instance_valuesObject

Update the instance values



132
133
134
# File 'lib/poolparty/remoting.rb', line 132

def update_instance_values
  @running_instances = list_of_running_instances.collect {|a| RemoteInstance.new(a) }.sort
end