Class: Rundeck::Execution

Inherits:
Object
  • Object
show all
Defined in:
lib/rundeck-ruby-client/execution.rb

Defined Under Namespace

Classes: QueryBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, hash, job) ⇒ Execution

Returns a new instance of Execution.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rundeck-ruby-client/execution.rb', line 8

def initialize(session, hash, job)
  @id = hash['id']
  @url=hash['href']
  @url = URI.join(session.server, URI.split(@url)[5]).to_s if @url # They always return a url of "localhost" for their executions. Replace it with the real URL
  @status=hash['status'].to_sym
  @date_started = hash['date_started']
  @date_ended = hash['date_ended']
  @user = hash['user']
  @args = (hash['argstring'] || "").split
                                  .each_slice(2)
                                  .reduce({}){|acc,cur| acc[cur[0]] = cur[1]; acc}
  @job = job
  @session = session
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def args
  @args
end

#date_endedObject (readonly)

Returns the value of attribute date_ended.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def date_ended
  @date_ended
end

#date_startedObject (readonly)

Returns the value of attribute date_started.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def date_started
  @date_started
end

#idObject (readonly)

Returns the value of attribute id.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def id
  @id
end

#jobObject (readonly)

Returns the value of attribute job.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def job
  @job
end

#sessionObject (readonly)

Returns the value of attribute session.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def session
  @session
end

#statusObject (readonly)

Returns the value of attribute status.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def status
  @status
end

#urlObject (readonly)

Returns the value of attribute url.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



22
23
24
# File 'lib/rundeck-ruby-client/execution.rb', line 22

def user
  @user
end

Class Method Details

.find(session, id) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rundeck-ruby-client/execution.rb', line 24

def self.find(session, id)
  result = session.get("api/1/execution/#{id}", *%w(result executions execution))
  return nil unless result
  job = Job.find(session, result['job']['id'])
  return nil unless job
  Execution.new(session, result, job)
end

.from_hash(session, hash) ⇒ Object



3
4
5
6
# File 'lib/rundeck-ruby-client/execution.rb', line 3

def self.from_hash(session, hash)
  job = Job.from_hash(session, hash['job'])
  new(session, hash, job)
end

.where(project) {|qb| ... } ⇒ Object

Yields:

  • (qb)


32
33
34
35
36
37
38
39
40
41
# File 'lib/rundeck-ruby-client/execution.rb', line 32

def self.where(project)
  qb = QueryBuilder.new
  yield qb if block_given?

  endpoint = "api/5/executions?project=#{project.name}#{qb.query}"
  pp endpoint
  results = project.session.get(endpoint, 'result', 'executions', 'execution') || []
  results = [results] if results.is_a?(Hash) #Work around an inconsistency in the API
  results.map {|hash| from_hash(project.session, hash)}
end

Instance Method Details

#outputObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/rundeck-ruby-client/execution.rb', line 43

def output
  ret = session.get("api/9/execution/#{id}/output")
  result = ret['result']
  raise "API call not successful" unless result && result['success']=='true'

  #sort the output by node
  ret = result['output'].slice(*%w(id completed hasFailedNodes))
  ret['log'] = result['output']['entries']['entry'].group_by{|e| e['node']}
  ret
end

#wait(title, interval, timeout) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rundeck-ruby-client/execution.rb', line 54

def wait title, interval, timeout
  puts "[#{Time.now}] Waiting for #{title}"
  start = Time.now.to_i
  stop = start + timeout
  while Time.now.to_i < stop
         begin
                 if yield
                        puts "[#{Time.now}] ok !, duration #{Time.now.to_i - start}"
                        return
                 end
         rescue
         end
         $stdout.write "[#{Time.now}] #{title} .\n"
         $stdout.flush
         sleep interval
  end
  raise "Timeout while waiting end of #{title}"
end

#wait_end(interval, timeout) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/rundeck-ruby-client/execution.rb', line 73

def wait_end interval, timeout
  follow_execs = nil
  wait "job #{@job.name}, execution #{@id} to be finished", interval, timeout do
    follow_execs = @job.executions.select{|exec| exec.id == @id}
    follow_execs.first.status != :running
  end
  follow_execs.first.output
end