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.



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

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.



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

def args
  @args
end

#date_endedObject (readonly)

Returns the value of attribute date_ended.



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

def date_ended
  @date_ended
end

#date_startedObject (readonly)

Returns the value of attribute date_started.



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

def date_started
  @date_started
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#jobObject (readonly)

Returns the value of attribute job.



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

def job
  @job
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.find(session, id) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rundeck-ruby-client/execution.rb', line 27

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



6
7
8
9
# File 'lib/rundeck-ruby-client/execution.rb', line 6

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

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

Yields:

  • (qb)


35
36
37
38
39
40
41
42
43
44
# File 'lib/rundeck-ruby-client/execution.rb', line 35

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rundeck-ruby-client/execution.rb', line 46

def output
  ret = session.get("api/9/execution/#{id}/output")
  result = ret['result']
  raise "API call not successful" unless result && result['success']=='true'
  ret = result['output'].slice(*%w(id completed hasFailedNodes))
  logs = result['output']['entries']['entry']
  if logs.class == Array
    logs = logs.group_by{|e| e['node']}
  else
    logs = {"localhost" => [logs]}
  end
  ret['log'] = logs
  ret = [ret] if ret.class != Array
  ret
end

#wait_end(interval, timeout) ⇒ Object

http request are done at each loop so, be nice with interval :)



63
64
65
66
67
68
69
70
71
72
# File 'lib/rundeck-ruby-client/execution.rb', line 63

def wait_end interval, timeout
  Timeout.timeout(timeout) do
      exec = Execution::find(@session, @id)
    until exec.status != :running do
      exec = Execution::find(@session, @id)
      sleep interval
    end
  end
  self.output
end