Class: Chimp::Chimp

Inherits:
Object
  • Object
show all
Defined in:
lib/right_chimp/Chimp.rb

Constant Summary collapse

@@verbose =

These class variables control verbosity

false
@@quiet =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChimp

Set up reasonable defaults



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/right_chimp/Chimp.rb', line 18

def initialize

  #
  # General configuration options
  #
  @progress     = false
  @prompt       = true
  @verify       = true
  @dry_run      = false
  @interactive  = true

  #
  # Job control options
  #
  @concurrency  = 1
  @delay        = 0
  @retry_count  = 0
  @hold         = false
  @timeout      = 900

  @limit_start  = 0
  @limit_end    = 0

  #
  # Action configuration
  #
  @action            = :action_none
  @group             = :default
  @group_type        = :parallel
  @group_concurrency = 1

  #
  # Options for selecting objects to work on
  #
  @current          = true
  @match_all        = true

  # This is an array of json data for each instance
  @servers          = []

  @arrays           = []
  @tags             = []
  @array_names      = []
  @deployment_names = []
  @template         = nil
  @script           = nil
  @ssh              = nil
  @ssh_user         = "rightscale"
  @report           = nil
  @inputs           = {}
  @set_tags         = []
  @ignore_errors    = false

  @break_array_into_instances = false
  @dont_check_templates_for_script = false

  #
  # chimpd configuration
  #
  @use_chimpd                 = false
  @chimpd_host                = 'localhost'
  @chimpd_port                = 9055
  @chimpd_wait_until_done     = false

  #
  # Will contain the operational scripts we have found
  # In the form: [name, href]
  @op_scripts = []

  #
  # This will contain the href and the name of the script to be run
  # in the form: [name, href]
  @script_to_run = nil
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def action
  @action
end

#all_scriptsObject

Returns the value of attribute all_scripts.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def all_scripts
  @all_scripts
end

#array_namesObject

Returns the value of attribute array_names.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def array_names
  @array_names
end

#chimpd_hostObject

Returns the value of attribute chimpd_host.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def chimpd_host
  @chimpd_host
end

#chimpd_portObject

Returns the value of attribute chimpd_port.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def chimpd_port
  @chimpd_port
end

#cli_argsObject

Returns the value of attribute cli_args.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def cli_args
  @cli_args
end

#concurrencyObject

Returns the value of attribute concurrency.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def concurrency
  @concurrency
end

#delayObject

Returns the value of attribute delay.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def delay
  @delay
end

#deployment_namesObject

Returns the value of attribute deployment_names.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def deployment_names
  @deployment_names
end

#dry_runObject

Returns the value of attribute dry_run.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def dry_run
  @dry_run
end

#groupObject

Returns the value of attribute group.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def group
  @group
end

#holdObject

Returns the value of attribute hold.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def hold
  @hold
end

#interactiveObject

Returns the value of attribute interactive.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def interactive
  @interactive
end

#job_idObject

Always returns 0. Used for chimpd compatibility.



1227
1228
1229
# File 'lib/right_chimp/Chimp.rb', line 1227

def job_id
  @job_id
end

#job_noteObject

Returns the value of attribute job_note.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def job_note
  @job_note
end

#job_uuidObject

Returns the value of attribute job_uuid.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def job_uuid
  @job_uuid
end

#limit_endObject

Returns the value of attribute limit_end.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def limit_end
  @limit_end
end

#limit_startObject

Returns the value of attribute limit_start.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def limit_start
  @limit_start
end

#progressObject

Returns the value of attribute progress.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def progress
  @progress
end

#promptObject

Returns the value of attribute prompt.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def prompt
  @prompt
end

#quietObject

Returns the value of attribute quiet.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def quiet
  @quiet
end

#reportObject

Returns the value of attribute report.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def report
  @report
end

#retry_countObject

Returns the value of attribute retry_count.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def retry_count
  @retry_count
end

#scriptObject

Returns the value of attribute script.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def script
  @script
end

#serversObject

Returns the value of attribute servers.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def servers
  @servers
end

#sshObject

Returns the value of attribute ssh.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def ssh
  @ssh
end

#tagsObject

Returns the value of attribute tags.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def tags
  @tags
end

#use_chimpdObject

Returns the value of attribute use_chimpd.



5
6
7
# File 'lib/right_chimp/Chimp.rb', line 5

def use_chimpd
  @use_chimpd
end

#verify(message, items, confirm = true) ⇒ Object

Allow the user to verify the list of servers that an operation will be run against.



1259
1260
1261
# File 'lib/right_chimp/Chimp.rb', line 1259

def verify
  @verify
end

Class Method Details

.failureObject



1245
1246
1247
# File 'lib/right_chimp/Chimp.rb', line 1245

def self.failure
  return Thread.current[:failure]
end

.get_job_uuidObject



1236
1237
1238
# File 'lib/right_chimp/Chimp.rb', line 1236

def self.get_job_uuid
  return Thread.current[:job_uuid]
end

.set_failure(status) ⇒ Object



1240
1241
1242
1243
# File 'lib/right_chimp/Chimp.rb', line 1240

def self.set_failure(status)
  #This is a current thread variable to avoid cross-talk between threads
  Thread.current[:failure] = status
end

.set_job_uuid(value) ⇒ Object



1231
1232
1233
1234
# File 'lib/right_chimp/Chimp.rb', line 1231

def self.set_job_uuid(value)
  #This is a current thread variable to avoid cross-talk between threads
  Thread.current[:job_uuid] = value
end

.set_verbose(v = true, q = false) ⇒ Object

Configure the Log object



1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'lib/right_chimp/Chimp.rb', line 1210

def self.set_verbose(v=true, q=false)
  @@verbose = v
  @@quiet = q

  STDOUT.sync = true
  STDERR.sync = true

  Log.threshold= Logger::DEBUG if @@verbose
end

.verbose?Boolean

Returns:

  • (Boolean)


1220
1221
1222
# File 'lib/right_chimp/Chimp.rb', line 1220

def self.verbose?
  return @@verbose
end

Instance Method Details

#add_to_queue(a) ⇒ Object



952
953
954
# File 'lib/right_chimp/Chimp.rb', line 952

def add_to_queue(a)
  a.each { |task| ChimpQueue.instance.push(@group, task) }
end

#ask_confirmation(prompt = 'Continue?', default = false) ⇒ Object

Asks for confirmation before continuing



1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
# File 'lib/right_chimp/Chimp.rb', line 1132

def ask_confirmation(prompt = 'Continue?', default = false)
  a = ''
  s = default ? '[Y/n]' : '[y/N]'
  d = default ? 'y' : 'n'
  until %w[y n].include? a
    a = ask("#{prompt} #{s} ") { |q| q.limit = 1; q.case = :downcase }
    a = d if a.length == 0
  end
  a == 'y'
end

#check_option_validityObject

Check for any invalid combinations of command line options



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/right_chimp/Chimp.rb', line 478

def check_option_validity
  if @hold && !@array_names.empty?
    puts "ERROR: Holding of array objects is not yet supported"
    exit 1
  end

  if @tags.empty? and @array_names.empty? and @deployment_names.empty? and not @chimpd_wait_until_done
    puts "ERROR: Please select the objects to operate upon."
    help
    exit 1
  end

  if not @array_names.empty? and ( not @tags.empty? or not @deployment_names.empty? )
    puts "ERROR: You cannot mix ServerArray queries with other types of queries."
    help
    exit 1
  end
end

#chimpd_wait_until_doneObject

Connect to chimpd and wait for the work queue to empty, and prompt the user if there are any errors.



1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
# File 'lib/right_chimp/Chimp.rb', line 1147

def chimpd_wait_until_done
  local_queue = ChimpQueue.instance
  $stdout.print "Waiting for chimpd jobs to complete for group #{@group}..."

  begin
    while !@dry_run
      local_queue = ChimpQueue.instance

      #
      # load up remote chimpd jobs into the local queue
      # this makes all the standard queue control methods available to us
      #
      sleeping_counter = 0
      while true
        local_queue.reset!

        begin
          all = ChimpDaemonClient.retrieve_group_info(@chimpd_host, @chimpd_port, @group, :all)
        rescue RestClient::ResourceNotFound
          sleep 5
          $stdout.print "\nINFO: Waiting on group #{@group}"
          retry
        end

        ChimpQueue.instance.create_group(@group)
        ChimpQueue[@group].set_jobs(all)

        break if ChimpQueue[@group].done?

        if sleeping_counter % 240 == 0
          $stdout.print "\nWaiting for group #{@group}" unless sleeping_counter == 0
        end
        $stdout.print "."
        $stdout.flush
        sleeping_counter += 5
        sleep 5
      end

      #
      # If verify_results returns true, then ask chimpd to requeue all failed jobs.
      #
      if verify_results(@group)
        break
      else
        ChimpDaemonClient.retry_group(@chimpd_host, @chimpd_port, @group)
      end
    end
  ensure
    #$stdout.print " done\n"
  end
end

#detect_right_script(st, script) ⇒ Object

This function returns @script_to_run which is extracted from matching the desired script against all server templates or the script URL



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/right_chimp/Chimp.rb', line 651

def detect_right_script(st, script)
  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
  if st.nil?
    return executable
  end

  # Take the sts and extract all operational scripts
  @op_scripts = extract_operational_scripts(st)

  # if script is empty, we will list all common scripts
  # if not empty, we will list the first matching one
  if @script == "" and @script != nil
    # list all operational scripts
    reduce_to_common_scripts(st.size)

    script_id = list_and_select_op_script

    # Provide the name + href
    s = Executable.new
    s.params['right_script']['href'] = @op_scripts[script_id][1].right_script.href
    s.params['right_script']['name'] = @op_scripts[script_id][0]
    @script_to_run = s
  else
    # Try to find the rightscript in our list of common operational scripts
    @op_scripts.each  do |rb|
      script_name = rb[0]
      if script_name.downcase.include?(script.downcase)
        # We only need the name and the href
        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 "[#{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
    #
    if @script_to_run == nil
      # Search outside common op scripts
      search_for_script_in_sts(script, st)
      if @script_to_run.nil?
        if @interactive
          puts "ERROR: Sorry, didnt find that ( "+script+" ), provide an URI instead"
          puts "I searched in:"
          st.each { |s|
            puts "   *  "+s[1]['name']+"  [Rev"+s[1]['version'].to_s+"]"
          }
          if not @ignore_errors
            exit 1
          end
        else
          Log.error "["+self.job_uuid+"] Sorry, didnt find the script: ( "+script+" )!"
          return nil
        end
      else
        if self.job_uuid.nil?
          self.job_uuid = ""
        end
        Log.warn "["+self.job_uuid+"]\"#{@script_to_run.params['right_script']['name']}\" is not a common operational script!"
        return @script_to_run
      end
    end
  end
end

#detect_server_template(servers) ⇒ Object

Given a list of servers



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/right_chimp/Chimp.rb', line 626

def detect_server_template(servers)

  Log.debug "[#{Chimp.get_job_uuid}] Looking for server template"
  st = []
  if servers[0].nil?
    return (st)
  end

  st += servers.collect { |s|
    [s['href'],s['server_template']]
  }.uniq {|a| a[0]}

  #
  # We return an array of server_template resources
  # of the type [ st_href, st object ]
  #
  Log.debug "[#{Chimp.get_job_uuid}] Found server templates"

  return(st)
end

#disable_loggingObject

Disable rest_connection logging



1202
1203
1204
1205
# File 'lib/right_chimp/Chimp.rb', line 1202

def disable_logging
  ENV['REST_CONNECTION_LOG'] = "/dev/null"
  ENV['RESTCLIENT_LOG'] = "/dev/null"
end

#do_workObject

Do work: either by submitting to chimpd or running it ourselves.



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'lib/right_chimp/Chimp.rb', line 1061

def do_work
  done = false

  while not done
    queue_runner(@concurrency, @delay, @retry_count, @progress)

    if @interactive and @verify
      done = verify_results(@group)
    else
      done = true
    end
  end

  if not @verify
    failed_workers, results_display = get_results(group)
    exit 1 if failed_workers.size > 0
  end

  puts "chimp run complete"
end

#extract_operational_scripts(st) ⇒ Object

Returns all matching operational scripts in the st list passed



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/right_chimp/Chimp.rb', line 799

def extract_operational_scripts(st)
  op_scripts = []
  size = st.size
  st.each do |s|
    # Example of s structure
    # ["/api/server_templates/351930003",
    #   {"id"=>351930003,
    #    "name"=>"RightScale Right_Site - 2015q1",
    #    "kind"=>"cm#server_template",
    #    "version"=>5,
    #    "href"=>"/api/server_templates/351930003"} ]
    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
    raise "ERROR: No operational scripts found on the server(s). "
    st.each {|s|
      puts "         (Search performed on server template '#{s[1]['name']}')"
    }
  end
  return op_scripts
end

#generate_jobs(queue_servers, queue_template, queue_executable) ⇒ Object

Load up the queue with work



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'lib/right_chimp/Chimp.rb', line 840

def generate_jobs(queue_servers, queue_template, queue_executable)
  counter = 0
  tasks = []
  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("[#{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
    #
    counter += 1
    next if @limit_start.to_i > 0 and counter < @limit_start.to_i
    break if @limit_end.to_i > 0 and counter > @limit_end.to_i

    #
    # Construct the Server object
    #
    s = Server.new

    s.params['href']                  = server['href']

    s.params['current_instance_href'] = s.params['href']
    s.params['current-instance-href'] = s.params['href']

    s.params['name']                  = server['name']
    s.params['nickname']              = s.params['name']

    s.params['ip_address']            = server['public_ip_addresses'].first
    s.params['ip-address']            = s.params['ip_address']

    s.params['private-ip-address']    = server['private_ip_addresses'].first
    s.params['private_ip_address']    = s.params['private-ip-address']

    s.params['resource_uid']          = server['resource_uid']
    s.params['resource-uid']          = s.params['resource_uid']

    s.params['instance-type']         = server['links']['instance_type']['name']
    s.params['instance_type']         = s.params['instance-type']
    s.params['ec2_instance_type']     = s.params['instance-type']
    s.params['ec2-instance-type']     = s.params['instance-type']

    s.params['dns-name']              = server['public_dns_names'].first
    s.params['dns_name']              = s.params['dns-name']

    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 "[#{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
      e = ExecRightScript.new(
        :server => s,
        :exec => queue_executable,
        :job_uuid => @job_uuid,
        :job_notes => @job_notes,
        :inputs => @inputs,
        :timeout => @timeout,
        :verbose => @@verbose,
        :quiet => @@quiet
      )
    elsif @ssh
      e = ExecSSH.new(
        :server => s,
        :ssh_user => @ssh_user,
        :exec => @ssh,
        :verbose => @@verbose,
        :quiet => @@quiet
      )
    elsif @report
      if s.href
        e = ExecReport.new(:server => s, :verbose => @@verbose, :quiet => @@quiet)
        e.fields = @report
      end
    elsif @set_tags.size > 0
      e = ExecSetTags.new(:server => s, :verbose => @@verbose, :quiet => @@quiet)
      e.tags = set_tags
    end

    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

#get_array_infoObject

Load up @array with server arrays to operate on



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/right_chimp/Chimp.rb', line 209

def get_array_info
  return if @array_names.empty?

  # 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 "[#{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

#get_executable_infoObject

Get the Executable (RightScript) info from the API



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/right_chimp/Chimp.rb', line 255

def get_executable_info
  if not (@servers.empty? )
    if (@script != nil)
      # If script is an uri/url no need to "detect it"
      # https://my.rightscale.com/acct/9202/right_scripts/205347
      if @script =~ /\A#{URI::regexp}\z/
        if not @use_chimpd || !@prompt
          puts "=================================================="
          puts "WARNING! You will be running this script on all "
          puts "server matches! (Press enter to continue)"
          puts "=================================================="
          gets
        end

        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 "[#{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)
      end
    else
      # @script could be nil because we want to run ssh
      if @action == :action_ssh
        puts "Using SSH command: \"#{@ssh}\"" if @action == :action_ssh
      end
    end
  end
end

#get_failuresObject



1053
1054
1055
# File 'lib/right_chimp/Chimp.rb', line 1053

def get_failures
  return get_results(@group)
end

#get_hrefs_for_arrays(names) ⇒ Object

Given some array names, return the arrays hrefs Api1.5



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/right_chimp/Chimp.rb', line 585

def get_hrefs_for_arrays(names)
  result = []
  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 "[#{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
        if @exact
          #remove results that do not exactly match
          result.each{ |r|
            if array_names.include?(r.raw['name'])
              arrays_hrefs += [ r.href ]
            end
          }
        else
          arrays_hrefs += result.collect(&:href)
        end
      else
        if @ignore_errors
          Log.debug "[#{Chimp.get_job_uuid}] Could not find array \"#{array_name}\""
        else
          Log.error "[#{Chimp.get_job_uuid}] Could not find array \"#{array_name}\""
        end
      end
    end
    if ( arrays_hrefs.empty? )
      Log.debug "[#{Chimp.get_job_uuid}] Did not find any arrays that matched!" unless names.size == 1
    end

    return(arrays_hrefs)

  end
end

#get_results(group_name) ⇒ Object

Get the results from the QueueRunner and format them in a way that’s easy to display to the user



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'lib/right_chimp/Chimp.rb', line 1025

def get_results(group_name)
  queue = ChimpQueue.instance
  Log.debug("getting results for group #{group_name}")
  results = queue.group[@group].results()
  failed_workers = []
  results_display = []

  results.each do |result|
    next if result == nil

    if result[:status] == :error
      name = result[:host] || "unknown"
      message = result[:error].to_s || "unknown"
      message.sub!("\n", "")
      failed_workers << result[:worker]
      results_display << "#{name[0..40]}  >> #{message}"
    end
  end

  return [failed_workers, results_display]
end

#get_server_infoObject

Go through each of the various ways to specify servers via the command line (tags, deployments, etc.) and get all the info needed from the RightScale API.



236
237
238
239
240
# File 'lib/right_chimp/Chimp.rb', line 236

def get_server_info
  @servers += get_servers_by_tag(@tags) unless tags.empty?
  # Perhaps allow searchign by deployment
  @servers += get_servers_by_deployment(@deployment_names) unless @deployment_names.empty?
end

#get_servers_by_deployment(names) ⇒ Object

Parse deployment names and get Server objects



567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/right_chimp/Chimp.rb', line 567

def get_servers_by_deployment(names)
  servers = []
  all_instances = Connection.all_instances

  if @exact
    result = all_instances.select {|i| names.any? {|n| i['links']['deployment']['name'].eql? n }}
  else
    result = all_instances.select {|i| names.any? {|n| i['links']['deployment']['name'] =~ /#{n}/ }}
  end
  servers = result

  return(servers)
end

#get_servers_by_tag(tags) ⇒ Object

Api1.6 equivalent



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/right_chimp/Chimp.rb', line 500

def get_servers_by_tag(tags)
  # Take tags and collapse it,
  # Default case, tag is AND
  if @match_all
    t = tags.join("&tag=")
    filter = "tag=#{t}"
    servers = Connection.instances(filter)
  else
    t = tags.join(",")
    filter = "tag=#{t}"
    servers = Connection.instances(filter)
  end

  if servers.nil?
    if @ignore_errors
      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"
    end
  elsif servers.empty?
    if @ignore_errors
      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"
    end
  end

  servers = verify_tagged_instances(servers,tags)

  return(servers)
end

#get_template_infoObject

Get the ServerTemplate info from the API



245
246
247
248
249
250
# File 'lib/right_chimp/Chimp.rb', line 245

def get_template_info
  # If we have a server or an array
  if not (@servers.first.nil? and @array_names.empty?)
    @server_template = detect_server_template(@servers)
  end
end

#helpObject

Print out help information



1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
# File 'lib/right_chimp/Chimp.rb', line 1387

def help
  puts
  puts "chimp -- a RightScale Platform command-line tool"
  puts
  puts "To select servers using tags:"
  puts "  --tag=<tag>                       example: --tag=service:dataservice=true"
  puts "  --tag-use-and                     'and' all tags when selecting servers (default)"
  puts "  --tag-use-or                      'or' all tags when selecting servers"
  puts
  puts "To select arrays or deployments:"
  puts "  --array=<name>                    array to execute upon"
  puts "  --deployment=<name>               deployment to execute upon"
  puts
  puts "To perform an action, specify one of the following:"
  puts "  --script=[<name>|<uri>|<id>]      name/uri/id of RightScript to run, empty for opscripts list"
  puts "  --report=<field-1>,<field-2>...   produce a report (see below)"
  puts "  --ssh=<command>                   command to execute via SSH"
  puts "  --ssh-user=<username>             username to use for SSH login (default: root)"
  puts
  puts "Action options:"
  puts "  --input=\"<name>=<value>\"          set input <name> for RightScript execution"
  puts
  puts "Execution options:"
  puts "  --group=<name>                    specify an execution group"
  puts "  --group-type=<serial|parallel>    specify group execution type"
  puts "  --group-concurrency=<n>           specify group concurrency, e.g. for parallel groups"
  puts
  puts "  --concurrency=<n>                 number of concurrent actions to perform. Default: 1"
  puts "  --delay=<seconds>                 delay a number of seconds between operations"
  puts
  puts "General options:"
  puts "  --dry-run                         only show what would be done"
  puts "  --ignore-errors                   ignore errors when server selection fails"
  puts "  --retry=<n>                       number of times to retry. Default: 0"
  puts "  --timeout=<seconds>               set the timeout to wait for a RightScript to complete"
  puts "  --progress                        toggle progress indicator"
  puts "  --noprompt                        don't prompt with list of objects to run against"
  puts "  --noverify                        disable interactive verification of errors"
  puts "  --verbose                         be more verbose"
  puts "  --dont-check-templates            don't check for script even if servers have diff. templates"
  puts "  --quiet                           suppress non-essential output"
  puts "  --version                         display version and exit"
  puts
  puts "chimpd options:"
  puts "  --chimpd=<port>                   send jobs to chimpd listening on <port> on localhost"
  puts "  --chimpd-wait-until-done          wait until all chimpd jobs are done"
  puts "  --hold                            create a job in chimpd without executing until requested"
  puts
  puts "Misc Notes:"
  puts "  * If you leave the name of a --script or --ssh command blank, chimp will prompt you"
  puts "  * URIs must be API URIs in the format https://us-3.rightscale.com/acct/<acct>/right_scripts/<script_id>"
  puts "  * The following reporting keywords can be used: ip-address,name,href,private-ip-address,resource_uid,"
  puts "  * ec2-instance-type,datacenter,dns-name,locked,tag=foo"
end

#list_and_select_op_scriptObject

Presents the user with a list of scripts contained in @op_scripts and Returns an integer indicating the selection



754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/right_chimp/Chimp.rb', line 754

def list_and_select_op_script
  puts "List of available operational scripts:"
  puts "------------------------------------------------------------"
  for i in 0..@op_scripts.length - 1
    puts "  %3d. #{@op_scripts[i][0]}" % i
  end
  puts "------------------------------------------------------------"
  while true
    printf "Type the number of the script to run and press Enter (Ctrl-C to quit): "
    script_id = Integer(gets.chomp) rescue -1
    if script_id >= 0 && script_id < @op_scripts.length
      puts "Script choice: #{script_id}. #{@op_scripts[ script_id ][0]}"
      break
    else
      puts "#{script_id < 0 ? 'Invalid input' : 'Input out of range'}."
    end
  end

  return script_id
end

#make_human_readable_list_of_objectsObject

Generate a human readable list of objects



1374
1375
1376
1377
1378
1379
1380
1381
1382
# File 'lib/right_chimp/Chimp.rb', line 1374

def make_human_readable_list_of_objects
  list_of_objects = []

  if @servers and not @servers.first.nil?
    list_of_objects += @servers.map { |s| s['name'] }
  end

  return(list_of_objects)
end

#parse_command_lineObject

Parse command line options



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/right_chimp/Chimp.rb', line 298

def parse_command_line
  begin
    opts = GetoptLong.new(
      [ '--tag', '-t', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--tag-use-and', '-a', GetoptLong::NO_ARGUMENT ],
      [ '--tag-use-or', '-o', GetoptLong::NO_ARGUMENT ],
      [ '--array', '-r', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--deployment', '-e', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--script', '-s', GetoptLong::OPTIONAL_ARGUMENT ],
      [ '--ssh', '-x', GetoptLong::OPTIONAL_ARGUMENT ],
      [ '--input', '-i', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--set-template', '-m', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--set-tag', '-w', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--report', '-b', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--progress', '-p', GetoptLong::NO_ARGUMENT ],
      [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
      [ '--quiet', '-q', GetoptLong::NO_ARGUMENT ],
      [ '--noprompt', '-z', GetoptLong::NO_ARGUMENT ],
      [ '--concurrency', '-c', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--delay', '-d', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--retry', '-y', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--hold', '-7', GetoptLong::NO_ARGUMENT ],
      [ '--dry-run', '-n', GetoptLong::NO_ARGUMENT ],
      [ '--limit', '-l', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--version', '-1', GetoptLong::NO_ARGUMENT ],
      [ '--chimpd', '-f', GetoptLong::OPTIONAL_ARGUMENT ],
      [ '--chimpd-wait-until-done', '-j', GetoptLong::NO_ARGUMENT ],
      [ '--dont-check-templates', '-0', GetoptLong::NO_ARGUMENT ],
      [ '--ignore-errors', '-9', GetoptLong::NO_ARGUMENT ],
      [ '--ssh-user', '-u', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
      [ '--group', '-g', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--group-type', '-2', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--group-concurrency', '-3', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--timing-log', '-4', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--timeout', '-5',  GetoptLong::REQUIRED_ARGUMENT ],
      [ '--noverify', '-6', GetoptLong::NO_ARGUMENT ],
      [ '--exact-matching', '-8', GetoptLong::NO_ARGUMENT ],
      [ '--job-notes', '-k', GetoptLong::OPTIONAL_ARGUMENT ]
    )

    opts.each do |opt, arg|
      case opt
        when '--help', '-h'
          help
          exit 0
        when '--tag', '-t'
          @tags << arg
        when '--tag-use-and', '-a'
          @match_all = true
        when '--tag-use-or', '-o'
          @match_all = false
        when '--array', '-a'
          @array_names << arg
        when '--deployment', '-e'
          @deployment_names << arg
        when '--template', '-m'
          @template = arg
        when '--script', '-s'
          set_action(:action_rightscript)
          if arg == ""
            # Empty but not nil means show list of operational scripts to choose from
            @script = ""
          else
            @script = arg
          end
        when '--ssh', '-x'
          set_action(:action_ssh)
          @break_array_into_instances = true
          if arg == ""
            print "Enter SSH command line to execute: "
            @ssh = gets.chomp
          else
            @ssh = arg
          end
        when '--ssh-user', '-u'
          @ssh_user = arg
        when '--input', '-i'
          arg =~ /(.+)=(.+)/
          @inputs[$1]=$2
        when '--set-template', '-m'
          set_action(:action_set)
          @template = arg
        when '--set-tag', '-w'
          set_action(:action_set)
          @set_tags << arg
        when '--report', '-b'
          set_action(:action_report)
          @report = arg
          @@verbose = false
          @@quiet = true
          @break_array_into_instances = true
          @concurrency = 5 if @concurrency == 1
        when '--progress', '-p'
          @progress = @progress ? false : true
        when '--noprompt', '-z'
          @prompt = false
        when '--concurrency', '-c'
          @concurrency = arg.to_i
        when '--delay', '-d'
          @delay = arg.to_i
        when '--hold', '-7'
          @hold = true
        when '--retry', '-y'
          @retry_count = arg.to_i
        when '--limit', '-l'
          @limit_start, @limit_end = arg.split(',')
        when '--verbose', '-v'
          @@verbose = true
          Log.threshold = Logger::DEBUG
        when '--quiet', '-q'
          @@quiet = true
        when '--dont-check-templates', '-0'
          @dont_check_templates_for_script = true
        when '--version'
          puts VERSION
          exit 0
        when '--chimpd'
          @use_chimpd = true
          unless arg.empty?
            if arg =~ /[\d]+\.[\d]+\.[\d]+\.[\d]+:[\d]+/ 
              @chimpd_host, @chimpd_port = arg.split(':')
              @chimpd_port = @chimpd_port.to_i
            else
              @chimpd_port = arg.to_i
            end
          end
        when '--chimpd-wait-until-done'
          @use_chimpd = true
          @chimpd_wait_until_done = true
        when '--dry-run', '-n'
          @dry_run = true
        when '--ignore-errors', '-9'
          @ignore_errors = true
        when '--group', '-g'
          @group = arg.to_sym
        when '--group-type'
          @group_type = arg.to_sym
        when '--group-concurrency'
          @group_concurrency = arg.to_i
        when '--job-notes'
          @job_notes = arg
        when '--timing-log'
          @timing_log = arg
        when '--timeout'
          @timeout = arg
        when '--noverify'
          @verify = false
        when '--exact-matching'
          @exact = true
      end
    end

    if @use_chimpd && ( @script.nil? || @script.empty? )
      if @chimpd_wait_until_done == false
        puts "#######################################################"
        puts "ERROR: --script cannot be empty when sending to chimpd"
        puts "#######################################################"
        exit 1
      end
    end


  rescue GetoptLong::InvalidOption => ex
    help
    exit 1
  end

  #
  # Before we're totally done parsing command line options,
  # let's make sure that a few things make sense
  #
  if @group_concurrency > @concurrency
    @concurrency = @group_concurrency
  end
end


1047
1048
1049
1050
1051
# File 'lib/right_chimp/Chimp.rb', line 1047

def print_timings
  ChimpQueue.instance.group[@group].results.each do |task|
    puts "Host: #{task[:host]} Type: #{task[:name]} Time: #{task[:total]} seconds"
  end
end

#processObject

Completely process a non-interactive chimp object command This is used by chimpd, when processing a task.



1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
# File 'lib/right_chimp/Chimp.rb', line 1086

def process
  Chimp.set_failure(false)
  Chimp.set_job_uuid(self.job_uuid)

  Log.debug "[#{Chimp.get_job_uuid}] Processing task"
  # Add to our "processing" counter

  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

  # All elements of task have been processed
  ChimpDaemon.instance.semaphore.synchronize do
    ChimpDaemon.instance.proc_counter -= 1
  end

  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 "[#{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

#queue_runner(concurrency, delay, retry_count, progress) ⇒ Object

Execute the user’s command and provide for retrys etc.



959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File 'lib/right_chimp/Chimp.rb', line 959

def queue_runner(concurrency, delay, retry_count, progress)
  queue = ChimpQueue.instance
  queue.max_threads = concurrency
  queue.delay = delay
  queue.retry_count = retry_count
  total_queue_size = queue.size

  puts "Executing..." unless progress or not quiet
  pbar = ProgressBar.new("Executing", 100) if progress
  queue.start

  queue.wait_until_done(@group) do
    pbar.set(((total_queue_size.to_f - queue.size.to_f)/total_queue_size.to_f*100).to_i) if progress
  end

  pbar.finish if progress
end

#reduce_to_common_scripts(number_of_st) ⇒ Object

Takes the number of st’s to search in, and reduces @op_scripts to only those who are repeated enough times.



780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/right_chimp/Chimp.rb', line 780

def reduce_to_common_scripts(number_of_st)
    counts = Hash.new 0
    @op_scripts.each { |s| counts[s[0]] +=1 }

    b = @op_scripts.inject({}) do |res, row|
      res[row[0]] ||= []
      res[row[0]] << row[1]
      res
    end

    b.inject([]) do |res, (key, values)|
      res << [key, values.first] if values.size >= number_of_st
      @op_scripts = res
    end
end

#runObject

Entry point for the chimp command line application



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
172
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
201
202
203
204
# File 'lib/right_chimp/Chimp.rb', line 96

def run
  queue = ChimpQueue.instance

  arguments = []

  ARGV.each { |arg| arguments << arg.clone }

  self.cli_args=arguments.collect {|param|
    param.gsub(/(?<==).*/) do |match|
      match='"'+match+'"'
    end
  }.join(" ")


  parse_command_line if @interactive

  check_option_validity if @interactive
  disable_logging unless @@verbose


  puts "chimp #{VERSION} executing..." if (@interactive and not @use_chimpd) and not @@quiet

  #
  # Wait for chimpd to complete tasks
  #
  if @chimpd_wait_until_done
    chimpd_wait_until_done
    exit
  end

  #
  # Send the command to chimpd for execution
  #
  if @use_chimpd
    timestamp=Time.now.to_i
    length=6
    self.job_uuid = (36**(length-1) + rand(36**length - 36**(length-1))).to_s(36)
    ChimpDaemonClient.submit(@chimpd_host, @chimpd_port, self,self.job_uuid)
    exit
  else
    #Connect to the Api
    Connection.instance
    if @interactive
      Connection.connect
    else
      Connection.connect_and_cache
    end
  end

  # If we're processing the command ourselves, then go
  # ahead and start making API calls to select the objects
  # to operate upon
  #

  # Get elements if --array has been passed
  get_array_info

  # Get elements if we are searching by tags
  get_server_info

  # At this stage @servers should be populated with our findings
  # Get ST info for all elements
  if not @servers.empty?
    get_template_info unless @servers.empty?

    puts "Looking for the rightscripts (This might take some time)" if (@interactive and not @use_chimpd) and not @@quiet
    get_executable_info
  end

  if Chimp.failure
    #This is the failure point when executing standalone
    Log.error "##################################################"
    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
  # and prompt the user
  #
  if @prompt and @interactive
    list_of_objects = make_human_readable_list_of_objects
    confirm = (list_of_objects.size > 0 and @action != :action_none) or @action == :action_none

    if @script_to_run.nil?
      verify("Your command will be executed on the following:", list_of_objects, confirm)
    else
      verify("Your command \""+@script_to_run.params['right_script']['name']+"\" will be executed on the following:", list_of_objects, confirm)
    end
  end
  #
  # Load the queue with work
  #
  if not @servers.first.nil? and ( not @executable.nil? or @action == :action_ssh or @action == :action_report)
    jobs = generate_jobs(@servers, @server_template, @executable)
    add_to_queue(jobs)
  end

  #
  # Exit early if there is nothing to do
  #
  if @action == :action_none or ( queue.group[@group].nil? || queue.group[@group].size == 0)
    puts "No actions to perform." unless self.quiet
  else
    do_work
  end
end

#search_for_script_in_sts(script, st) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/right_chimp/Chimp.rb', line 724

def search_for_script_in_sts(script, st)
  # Loop and look inside every st
  st.each do |s|
    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 "[#{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
      end
    end
  end
  # If we hit here, we never found the desired script
  return
end

#set_action(a) ⇒ Object

Set the action



980
981
982
983
# File 'lib/right_chimp/Chimp.rb', line 980

def set_action(a)
  raise ArgumentError.new "Cannot reset action" unless @action == :action_none
  @action = a
end

#verify_results(group = :default) ⇒ Object

Allow user to verify results and retry if necessary



988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/right_chimp/Chimp.rb', line 988

def verify_results(group = :default)
  failed_workers, results_display = get_results(group)

  #
  # If no workers failed, then we're done.
  #
  return true if failed_workers.empty?

  #
  # Some workers failed; offer the user a chance to retry them
  #
  verify("The following objects failed:", results_display, false)

  while true
    puts "(R)etry failed jobs"
    puts "(A)bort chimp run"
    puts "(I)gnore errors and continue"
    command = gets()

    if command =~ /^a/i
      puts "Aborting!"
      exit 1
    elsif command =~ /^i/i
      puts "Ignoring errors and continuing"
      exit 0
    elsif command =~ /^r/i
      puts "Retrying..."
      ChimpQueue.instance.group[group].requeue_failed_jobs!
      return false
    end
  end
end

#verify_tagged_instances(servers, tags) ⇒ Object

Verify that all returned instances from the API match our tag request



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/right_chimp/Chimp.rb', line 535

def verify_tagged_instances(servers,tags)
  array_list = servers
  # servers is an array of hashes
  # verify that each object contains the tags.
  if @match_all
    # has to contain BOTH
    matching_servers = array_list.select { |instance| (tags - instance['tags']).empty? }

  else
    # has to contain ANY
    matching_servers = array_list.select { |instance| tags.any? {|tag| instance['tags'].include?(tag)  }}
  end

  # Shall there be a discrepancy, we need to raise an error and end the run.
  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

  return servers
end

#warn_if_rightscript_not_in_all_servers(servers, server_template, rightscript_executable) ⇒ Object

Verify that the given rightscript_executable (the object corresponding to the script) that is associated with the server_template exists in all servers (No need to check server arrays, they must all have the same template.)

Returns: none. Prints a warning if any server does not have the script in its template.



1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
# File 'lib/right_chimp/Chimp.rb', line 1284

def warn_if_rightscript_not_in_all_servers(servers, server_template, rightscript_executable)

  return if servers.length < 2 or not server_template or not rightscript_executable

  main_server_template      = server_template
  main_server_template_name = main_server_template.params['nickname']
  main_server_template_href = main_server_template.params['href']

  # Find which server has the specified template (the "main" template)
  server_that_has_main_template = nil
  for i in (0..servers.length - 1)
    if servers[i] and servers[i]['server_template_href'] == main_server_template_href
      server_that_has_main_template = servers[i]
      break
    end
  end
  if not server_that_has_main_template
    puts "internal error validating rightscript presence in all servers"
    return
  end

  some_servers_have_different_template = false
  num_servers_missing_rightscript      = 0

  for i in (0..servers.length - 1)
    next if servers[i].empty?

    this_server_template_href = servers[i]['server_template_href']

    # If the server's template has the same href, this server is good
    next if this_server_template_href == main_server_template_href

    if not some_servers_have_different_template
      some_servers_have_different_template = true
      if not @@quiet
        puts "Note: servers below have different server templates:"
        puts "      - server '#{server_that_has_main_template['nickname']}: "
        if @@verbose
          puts "                template name: '#{main_server_template_name}'"
          puts "                         href: '#{main_server_template_href}'"
        end
      end
    end

    this_server_template = ::ServerTemplate.find(this_server_template_href)
    next if this_server_template == nil
    if not @@quiet
      puts "      - server '#{servers[i]['nickname']}: "
      if @@verbose
        puts "                template name: '#{this_server_template.params['nickname']}'"
        puts "                         href: '#{this_server_template.params['href']}'"
      end
    end

    # Now check if the offending template has the rightscript in question
    has_script = false
    this_server_template.executables.each do |cur_script|
      if rightscript_executable['right_script']['href'] == cur_script['right_script']['href']
        has_script = true
        break
      end
    end
    if not has_script
      if not @@quiet
        puts "    >>  WARNING: The above server's template does not include the execution rightscript!"
      end
      num_servers_missing_rightscript += 1
      if num_servers_missing_rightscript == 1
        if @@verbose
          puts "                 script name: \'#{rightscript_executable['right_script']['name']}\', href: \'#{rightscript_executable['right_script']['href']}\'"
        end
      end
    end
  end
  if some_servers_have_different_template
    if num_servers_missing_rightscript == 0
      puts "Script OK. The servers have different templates, but they all contain the script, \'#{rightscript_executable['right_script']['name']}\'"
    else
      puts "WARNING: total of #{num_servers_missing_rightscript} servers listed do not have the rightscript in their template."
    end
  else
    if not @@quiet
      puts "Script OK. All the servers share the same template and the script is included in it."
    end
  end
end