Module: PoolParty::Remote::Remoter

Included in:
PoolParty::Remote
Defined in:
lib/poolparty/net/remoter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



204
205
206
# File 'lib/poolparty/net/remoter.rb', line 204

def self.included(receiver)
  receiver.extend self
end

Instance Method Details

#can_shutdown_an_instance? ⇒ Boolean

Can we shutdown an instance?

Returns:

  • (Boolean)


96
97
98
# File 'lib/poolparty/net/remoter.rb', line 96

def can_shutdown_an_instance?
  list_of_running_instances.size > minimum_instances
end

#can_start_a_new_instance? ⇒ Boolean

Can we start a new instance?

Returns:

  • (Boolean)


111
112
113
# File 'lib/poolparty/net/remoter.rb', line 111

def can_start_a_new_instance?
  maximum_number_of_instances_are_not_running?
end

#contract_cloud_if_necessary(force = false) ⇒ Object

Contract the cloud If we can shutdown an instnace and the load allows us to contract the cloud, then we should request_termination_of_non_master_instance



176
177
178
179
180
# File 'lib/poolparty/net/remoter.rb', line 176

def contract_cloud_if_necessary(force=false)
  if can_shutdown_an_instance?          
    request_termination_of_non_master_instance if should_contract_cloud?(force)
  end
end

#expand_cloud_if_necessary(force = false) ⇒ Object

Expand the cloud If we can start a new instance and the load requires us to expand the cloud, then we should request_launch_new_instances Wait for the instance to boot up and when it does come back online, then provision it as a slave, this way, it is ready for action from the get go



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/poolparty/net/remoter.rb', line 162

def expand_cloud_if_necessary(force=false)
  if can_start_a_new_instance? && should_expand_cloud?(force)
    @out = request_launch_new_instances(1)
    
    reset!
    when_no_pending_instances do
      @ri = list_of_running_instances.last
      PoolParty::Provisioner.provision_slave(@ri, self, !force)
    end
  end
end

#get_remote_nodes ⇒ Object



67
68
69
70
71
72
73
# File 'lib/poolparty/net/remoter.rb', line 67

def get_remote_nodes
  returning Array.new do |instances|
    list_of_instances(respond_to?(:keypair) ? keypair : nil).each do |h|
      instances << PoolParty::Remote::RemoteInstance.new(h)
    end
  end
end

#get_working_listing_file ⇒ Object

Get the instance first instance file that exists on the system from the expected places denoted in the local_instances_list_file_locations



76
77
78
# File 'lib/poolparty/net/remoter.rb', line 76

def get_working_listing_file
  local_instances_list_file_locations.reject {|f| f unless File.file?(f) }.first
end

#launch_minimum_number_of_instances ⇒ Object

This will launch the minimum_instances if the minimum number of instances are not running If the minimum number of instances are not running and if we can start a new instance



140
141
142
143
144
145
146
147
# File 'lib/poolparty/net/remoter.rb', line 140

def launch_minimum_number_of_instances
  if can_start_a_new_instance?
    while !minimum_number_of_instances_are_running?
      request_launch_one_instance_at_a_time
      wait "5.seconds"
    end
  end
end

#list_from_local ⇒ Object

Open the cached local copy of the instances list and create a new RemoteInstance from each line



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/poolparty/net/remoter.rb', line 37

def list_from_local
  list_file = get_working_listing_file
  if list_file
    out = returning Array.new do |instances|
      open(list_file).read.split("\n").each do |line|
        instances << RemoteInstance.new(line)
      end
    end
  else
    out = list_from_remote(:cache => true)
  end
  return out
end

#list_from_remote(options = {}) ⇒ Object

List the instances that are known from the remoter_base Create a RemoteInstance for each of the instances from the hash returned by the list of instances, write them to the cached file and then return the array of instances



54
55
56
57
58
# File 'lib/poolparty/net/remoter.rb', line 54

def list_from_remote(options={})
  out_array = get_remote_nodes
  write_to_file( local_instances_list_file_locations.first, out_array.map{|a| a.to_s }.join("\n")) if options[:cache]
  out_array
end

#list_of_node_ips(options = {}) ⇒ Object

An array of node ips. Mainly used for puppet templating



64
65
66
# File 'lib/poolparty/net/remoter.rb', line 64

def list_of_node_ips(options={})
  list_of_running_instances.collect {|ri| ri.ip }
end

#list_of_node_names(options = {}) ⇒ Object

Get the names of the nodes. Mainly used for puppet templating



60
61
62
# File 'lib/poolparty/net/remoter.rb', line 60

def list_of_node_names(options={})
  list_of_running_instances.collect {|ri| ri.name }
end

#local_instances_list_file_locations ⇒ Object

Expected places for the instances.list to be located at on the machine



80
81
82
83
84
85
86
87
88
# File 'lib/poolparty/net/remoter.rb', line 80

def local_instances_list_file_locations
  [
    "#{Base.storage_directory}/#{name}-instances.list",
    "#{Base.base_config_directory}/#{name}-instances.list",
    "~/.#{name}-instances.list",
    "~/#{name}-instances.list",          
    "#{name}-instances.list"
  ]
end

#maximum_number_of_instances_are_not_running? ⇒ Boolean

Are the maximum number of instances running?

Returns:

  • (Boolean)


115
116
117
# File 'lib/poolparty/net/remoter.rb', line 115

def maximum_number_of_instances_are_not_running?
  list_of_running_instances.size < maximum_instances
end

#minimum_number_of_instances_are_running? ⇒ Boolean

More functional methods Are the minimum number of instances running?

Returns:

  • (Boolean)


92
93
94
# File 'lib/poolparty/net/remoter.rb', line 92

def minimum_number_of_instances_are_running?
  list_of_running_instances.size >= minimum_instances
end

#request_launch_new_instances(num = 1) ⇒ Object

Request to launch a number of instances



100
101
102
103
104
# File 'lib/poolparty/net/remoter.rb', line 100

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

#request_launch_one_instance_at_a_time ⇒ Object

Launch new instance while waiting for the number of pending instances to be zero before actually launching. This ensures that we only launch one instance at a time



121
122
123
124
125
# File 'lib/poolparty/net/remoter.rb', line 121

def request_launch_one_instance_at_a_time        
  when_no_pending_instances do
    launch_new_instance!
  end
end

#request_termination_of_non_master_instance ⇒ Object

Let's terminate an instance that is not the master instance



106
107
108
109
# File 'lib/poolparty/net/remoter.rb', line 106

def request_termination_of_non_master_instance
  inst = nonmaster_nonterminated_instances.last
  terminate_instance!(inst.instance_id) if inst
end

#rsync_command ⇒ Object



32
33
34
# File 'lib/poolparty/net/remoter.rb', line 32

def rsync_command
  "rsync --delete -azP --exclude cache -e '#{ssh_string}'"
end

#rsync_storage_files_to(instance = nil) ⇒ Object

Rsync command to the instance



183
184
185
186
187
# File 'lib/poolparty/net/remoter.rb', line 183

def rsync_storage_files_to(instance=nil)        
  hide_output do
    Kernel.system "#{rsync_storage_files_to_command(instance)}" if instance
  end
end

#rsync_storage_files_to_command(remote_instance) ⇒ Object



10
11
12
13
14
# File 'lib/poolparty/net/remoter.rb', line 10

def rsync_storage_files_to_command(remote_instance)
  if remote_instance
    "#{rsync_command} #{Base.storage_directory} #{remote_instance.ip}:#{Base.remote_storage_path}"
  end
end

#run_command_on(cmd, instance = nil) ⇒ Object

Take the rsync command and execute it on the system if there is an instance given



190
191
192
# File 'lib/poolparty/net/remoter.rb', line 190

def run_command_on(cmd, instance=nil)        
  Kernel.system "#{run_command_on_command(cmd, instance)}" if instance
end

#run_command_on_command(cmd = "ls -l", remote_instance = nil) ⇒ Object



15
16
17
# File 'lib/poolparty/net/remoter.rb', line 15

def run_command_on_command(cmd="ls -l", remote_instance=nil)
  "#{ssh_command(remote_instance)} '#{cmd}'"
end

#should_contract_cloud?(force = false) ⇒ Boolean

Stub method for the time being to handle the contraction of the cloud

Returns:

  • (Boolean)


153
154
155
# File 'lib/poolparty/net/remoter.rb', line 153

def should_contract_cloud?(force=false)
  force || false
end

#should_expand_cloud?(force = false) ⇒ Boolean

Stub method for the time being to handle expansion of the cloud

Returns:

  • (Boolean)


149
150
151
# File 'lib/poolparty/net/remoter.rb', line 149

def should_expand_cloud?(force=false)
  force || false
end

#ssh_array ⇒ Object

Array of ssh options Includes StrictHostKeyChecking to no Ssh with the user in Base And including the keypair_path



29
30
31
# File 'lib/poolparty/net/remoter.rb', line 29

def ssh_array
  ["-o StrictHostKeyChecking=no", "-l '#{Base.user}'", '-i "'+full_keypair_path+'"']
end

#ssh_command(remote_instance) ⇒ Object



18
19
20
# File 'lib/poolparty/net/remoter.rb', line 18

def ssh_command(remote_instance)
  "#{ssh_string} #{remote_instance.ip}"
end

#ssh_into(instance = nil) ⇒ Object

Ssh into the instance given



195
196
197
# File 'lib/poolparty/net/remoter.rb', line 195

def ssh_into(instance=nil)
  Kernel.system "#{ssh_command(instance)}" if instance
end

#ssh_into_instance_number(num = 0) ⇒ Object

Find the instance by the number given and then ssh into the instance



200
201
202
# File 'lib/poolparty/net/remoter.rb', line 200

def ssh_into_instance_number(num=0)
  ssh_into( get_instance_by_number( num || 0 ) )
end

#ssh_string ⇒ Object

Generic commandable strings



22
23
24
# File 'lib/poolparty/net/remoter.rb', line 22

def ssh_string
  (["ssh"] << ssh_array).join(" ")
end

#when_no_pending_instances(&block) ⇒ Object

A convenience method for waiting until there are no more pending instances and then running the block



128
129
130
131
132
133
134
135
136
# File 'lib/poolparty/net/remoter.rb', line 128

def when_no_pending_instances(&block)
  reset!
  if list_of_pending_instances.size > 0
    wait "5.seconds"
    when_no_pending_instances(&block)
  else
    block.call if block
  end
end