lib/right_chimp/Chimp.rb in right_chimp-2.0.1 vs lib/right_chimp/Chimp.rb in right_chimp-2.0.2
- old
+ new
@@ -1,8 +1,7 @@
+ #The Chimp class encapsulates the command-line program logic
#
-# The Chimp class encapsulates the command-line program logic
-#
module Chimp
class Chimp
attr_accessor :concurrency, :delay, :retry_count, :hold, :progress, :prompt,
:quiet, :use_chimpd, :chimpd_host, :chimpd_port, :tags, :array_names,
@@ -16,10 +15,11 @@
#
# Set up reasonable defaults
#
def initialize
+
#
# General configuration options
#
@progress = false
@prompt = true
@@ -164,13 +164,13 @@
end
if Chimp.failure
#This is the failure point when executing standalone
Log.error "##################################################"
- Log.error " API CALL FAILED FOR:"
- Log.error " chimp #{@cli_args} "
- Log.error " Run manually!"
+ Log.error "[#{Chimp.get_job_uuid}] API CALL FAILED FOR:"
+ Log.error "[#{Chimp.get_job_uuid}] chimp #{@cli_args} "
+ Log.error "[#{Chimp.get_job_uuid}] Run manually!"
Log.error "##################################################"
exit 1
end
#
# Optionally display the list of objects to operate on
@@ -213,17 +213,20 @@
# The first thing to do here is make an api1.5 call to get the array hrefs.
arrays_hrefs=get_hrefs_for_arrays(@array_names)
# Then we filter on all the instances by this href
all_instances = Connection.all_instances() unless arrays_hrefs.empty?
if all_instances.nil?
- Log.debug "No results from API query"
+ Log.debug "[#{Chimp.get_job_uuid}] No results from API query"
else
arrays_hrefs.each { |href|
@servers += all_instances.select {|s|
s['links']['incarnator']['href'] == href
}
}
+
+ Log.debug "[#{Chimp.get_job_uuid}] Found #{@servers.count} servers for that array query"
+
end
# The result will be stored (not returned) into @servers
end
#
@@ -267,12 +270,16 @@
script_number = File.basename(@script)
s=Executable.new
s.params['right_script']['href']="right_script_href=/api/right_scripts/"+script_number
#Make an 1.5 call to extract name, by loading resource.
- Log.debug "Making API 1.5 call : client.resource(#{s.params['right_script']['href'].scan(/=(.*)/).last.last})"
- the_name = Connection.client.resource(s.params['right_script']['href'].scan(/=(.*)/).last.last).name
+ Log.debug "[#{Chimp.get_job_uuid}] Making API 1.5 call : client.resource(#{s.params['right_script']['href'].scan(/=(.*)/).last.last})"
+ begin
+ the_name = Connection.client.resource(s.params['right_script']['href'].scan(/=(.*)/).last.last).name
+ rescue
+ Log.error "[#{Chimp.get_job_uuid}] Failed to make 1.5 call for rightscript href"
+ end
s.params['right_script']['name'] = the_name
@executable=s
else
#If its not an url, go ahead try to locate it in the ST"
@executable = detect_right_script(@server_template, @script)
@@ -491,19 +498,19 @@
servers = Connection.instances(filter)
end
if servers.nil?
if @ignore_errors
- Log.warn "[#{Chimp.get_job_uuid}]Tag query returned no results: #{tags.join(" ")}"
+ Log.warn "[#{Chimp.get_job_uuid}] Tag query returned no results: #{tags.join(" ")}"
else
- raise "[#{Chimp.get_job_uuid}]Tag query returned no results: #{tags.join(" ")}\n"
+ raise "[#{Chimp.get_job_uuid}] Tag query returned no results: #{tags.join(" ")}\n"
end
elsif servers.empty?
if @ignore_errors
- Log.warn "[#{Chimp.get_job_uuid}]Tag query returned no results: #{tags.join(" ")}"
+ Log.warn "[#{Chimp.get_job_uuid}] Tag query returned no results: #{tags.join(" ")}"
else
- raise "[#{Chimp.get_job_uuid}]Tag query returned no results: #{tags.join(" ")}\n"
+ raise "[#{Chimp.get_job_uuid}] Tag query returned no results: #{tags.join(" ")}\n"
end
end
servers = verify_tagged_instances(servers,tags)
@@ -530,10 +537,12 @@
if matching_servers.size != servers.size
if @ignore_errors
Log.error "[#{Chimp.get_job_uuid}] #{servers.size - matching_servers.size} instances didnt match tag selection."
Log.error "[#{Chimp.get_job_uuid}] #{tags.join(" ")}"
Chimp.set_failure(true)
+ Log.error "[#{Chimp.get_job_uuid}] Set failure to true because of discrepancy"
+
servers = []
else
raise "[#{Chimp.get_job_uuid}] #{servers.size - matching_servers.size} instances didnt match tag selection"
end
end
@@ -562,11 +571,11 @@
arrays_hrefs = []
if names.size > 0
names.each do |array_name|
# Find if arrays exist, if not raise warning.
# One API call per array
- Log.debug "Making API 1.5 call: client.server_arrays.index(:filter => [#{array_name}])"
+ Log.debug "[#{Chimp.get_job_uuid}] Making API 1.5 call: client.server_arrays.index(:filter => [#{array_name}])"
result = Connection.client.server_arrays.index(:filter => ["name==#{array_name}"])
# Result is an array with all the server arrays
if result.size != 0
arrays_hrefs += result.collect(&:href)
else
@@ -589,11 +598,11 @@
#
# Given a list of servers
#
def detect_server_template(servers)
- Log.debug "Looking for server template"
+ Log.debug "[#{Chimp.get_job_uuid}] Looking for server template"
st = []
if servers[0].nil?
return (st)
end
@@ -603,21 +612,21 @@
#
# We return an array of server_template resources
# of the type [ st_href, st object ]
#
- Log.debug "Found server templates"
+ Log.debug "[#{Chimp.get_job_uuid}] Found server templates"
return(st)
end
#
# This function returns @script_to_run which is extracted from matching
# the desired script against all server templates or the script URL
#
def detect_right_script(st, script)
- Log.debug "Looking for rightscript"
+ Log.debug "[#{Chimp.get_job_uuid}] Looking for rightscript"
executable = nil
# In the event that chimpd find @op_scripts as nil, set it as an array.
if @op_scripts.nil?
@op_scripts = []
end
@@ -650,11 +659,11 @@
s = Executable.new
s.params['right_script']['href'] = rb[1].right_script.href
s.params['right_script']['name'] = script_name
@script_to_run = s
- Log.debug "Found rightscript"
+ Log.debug "[#{Chimp.get_job_uuid}] Found rightscript"
return @script_to_run
end
end
#
# If we reach here it means we didnt find the script in the operationals
@@ -688,16 +697,20 @@
end
def search_for_script_in_sts(script, st)
# Loop and look inside every st
st.each do |s|
- Log.debug "Making API 1.5 call: client.resource(#{s[1]['href']})"
- temp=Connection.client.resource(s[1]['href'])
+ Log.debug "[#{Chimp.get_job_uuid}] Making API 1.5 call: client.resource(#{s[1]['href']})"
+ begin
+ temp=Connection.client.resource(s[1]['href'])
+ rescue
+ Log.error "[#{Chimp.get_job_uuid}] Failed to load href for ST"
+ end
temp.runnable_bindings.index.each do |x|
# Look for first match
if x.raw['right_script']['name'].downcase.include?(script.downcase)
- Log.debug "Found requested righscript: #{script}"
+ Log.debug "[#{Chimp.get_job_uuid}] Found requested righscript: #{script}"
# Provide the name + href
s = Executable.new
s.params['right_script']['href'] = x.raw['links'].find{|i| i['rel'] == 'right_script'}['href']
s.params['right_script']['name'] = x.raw['right_script']['name']
@script_to_run = s
@@ -767,18 +780,24 @@
# {"id"=>351930003,
# "name"=>"RightScale Right_Site - 2015q1",
# "kind"=>"cm#server_template",
# "version"=>5,
# "href"=>"/api/server_templates/351930003"} ]
- Log.debug "Making API 1.5 call: client.resource"
- temp=Connection.client.resource(s[1]['href'])
- temp.runnable_bindings.index.each do |x|
- # only add the operational ones
- if x.sequence == "operational"
- name = x.raw['right_script']['name']
- op_scripts.push([name, x])
+ Log.debug "[#{Chimp.get_job_uuid}] Making API 1.5 call: client.resource (ST)"
+ begin
+ temp=Connection.client.resource(s[1]['href'])
+ Log.debug "[#{Chimp.get_job_uuid}] API 1.5 call client.resource (ST) complete"
+ temp.runnable_bindings.index.each do |x|
+ # only add the operational ones
+ if x.sequence == "operational"
+ name = x.raw['right_script']['name']
+ op_scripts.push([name, x])
+ end
end
+ rescue Exception => e
+ Log.error "[#{Chimp.get_job_uuid}] API 1.5 call client.resource (ST) failed"
+ Log.error "[#{Chimp.get_job_uuid}] #{e.message}"
end
end
#We now only have operational runnable_bindings under the script_objects array
if op_scripts.length < 1
@@ -794,22 +813,22 @@
# Load up the queue with work
#
def generate_jobs(queue_servers, queue_template, queue_executable)
counter = 0
tasks = []
- Log.debug "Loading queue..."
+ Log.debug "[#{Chimp.get_job_uuid}] Loading queue..."
#
# Configure group
#
if not ChimpQueue[@group]
ChimpQueue.instance.create_group(@group, @group_type, @group_concurrency)
end
#
# Process Server selection
#
- Log.debug("Processing server selection")
+ Log.debug("[#{Chimp.get_job_uuid}] Processing server selection for task creation")
queue_servers.sort! { |a,b| a['name'] <=> b['name'] }
queue_servers.each do |server|
#
# Handle limiting options
@@ -851,12 +870,17 @@
s.params['locked'] = server['locked']
s.params['state'] = server['state']
s.params['datacenter'] = server['links']['datacenter']['name']
# This will be useful for later on when we need to run scripts
- Log.debug "Making API 1.5 call: client.resource"
- s.object = Connection.client.resource(server['href'])
+ Log.debug "[#{Chimp.get_job_uuid}] Making API 1.5 call: client.resource (SERVER) for task creation"
+ begin
+ s.object = Connection.client.resource(server['href'])
+ Log.debug "[#{Chimp.get_job_uuid}] Making API 1.5 call: client.resource (SERVER) for task creation COMPLETE"
+ rescue
+ Log.error "[#{Chimp.get_job_uuid}] Failed to load server href via API1.5 for task creation"
+ end
e = nil
# If @script has been passed
if queue_executable
@@ -890,10 +914,11 @@
if e != nil
e.dry_run = @dry_run
e.quiet = @@quiet
e.status = Executor::STATUS_HOLDING if @hold
+ Log.debug "[#{Chimp.get_job_uuid}] Pushing task (end of control)"
tasks.push(e)
end
end
return(tasks)
end
@@ -1027,47 +1052,45 @@
puts "chimp run complete"
end
#
- # Allow the set/retrieval of job_uuid from outside
- #
- def self.get_job_uuid
- @job_uuid
- end
-
- def self.set_job_uuid(value)
- @job_uuid = value
- end
-
- #
# Completely process a non-interactive chimp object command
# This is used by chimpd, when processing a task.
#
def process
Chimp.set_failure(false)
Chimp.set_job_uuid(self.job_uuid)
Log.debug "[#{Chimp.get_job_uuid}] Processing task"
+ Log.debug "[#{Chimp.get_job_uuid}] Trying to get array_info" unless Chimp.failure
get_array_info unless Chimp.failure
+
+ Log.debug "[#{Chimp.get_job_uuid}] Trying to get server_info" unless Chimp.failure
get_server_info unless Chimp.failure
+
+ Log.debug "[#{Chimp.get_job_uuid}] Trying to get template_info" unless Chimp.failure
get_template_info unless Chimp.failure
+
+ Log.debug "[#{Chimp.get_job_uuid}] Trying to get executable_info" unless Chimp.failure
get_executable_info unless Chimp.failure
if Chimp.failure
+
Log.error "##################################################"
Log.error "["+self.job_uuid+"] API CALL FAILED FOR:"
Log.error "["+self.job_uuid+"] chimp #{@cli_args} "
Log.error "["+self.job_uuid+"] Run manually!"
Log.error "##################################################"
return []
else
if @servers.first.nil? or @executable.nil?
- Log.warn "["+self.job_uuid+"] Nothing to do for \"chimp #{@cli_args}\"."
+ Log.warn "[#{Chimp.get_job_uuid}] Nothing to do for \"chimp #{@cli_args}\"."
return []
else
+ Log.debug "[#{Chimp.get_job_uuid}] Generating job..."
return generate_jobs(@servers, @server_template, @executable)
end
end
end
@@ -1165,20 +1188,28 @@
#
def job_id
return 0
end
- def self.get_job_uuid
- @job_uuid
+ def self.set_job_uuid(value)
+ #This is a current thread variable to avoid cross-talk between threads
+ Thread.current[:job_uuid] = value
end
- def self.failure
- return @failure
+ def self.get_job_uuid
+ return Thread.current[:job_uuid]
end
def self.set_failure(status)
- @failure = status
+ #This is a current thread variable to avoid cross-talk between threads
+ Thread.current[:failure] = status
end
+
+ def self.failure
+ return Thread.current[:failure]
+ end
+
+
####################################################
#private
####################################################