Class: EC2::Platform::Base::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2/platform/base/pipeline.rb

Overview


Direct Known Subclasses

Linux::Pipeline, Solaris::Pipeline

Defined Under Namespace

Classes: ExecutionError, Stage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basename = 'pipeline', is_verbose = false) ⇒ Pipeline




71
72
73
74
75
76
77
# File 'lib/ec2/platform/base/pipeline.rb', line 71

def initialize(basename='pipeline', is_verbose=false)
  @stages = []
  @results = []
  @tempfiles = []
  @basename = basename
  @verbose = is_verbose
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



68
69
70
# File 'lib/ec2/platform/base/pipeline.rb', line 68

def basename
  @basename
end

#verboseObject

Returns the value of attribute verbose.



67
68
69
# File 'lib/ec2/platform/base/pipeline.rb', line 67

def verbose
  @verbose
end

Instance Method Details

#add(name, command, success = 0) ⇒ Object




80
81
82
83
# File 'lib/ec2/platform/base/pipeline.rb', line 80

def add(name, command, success=0)
  @stages << Stage.new(name, command, success)
  self
end

#cleanupObject




148
149
150
151
152
153
# File 'lib/ec2/platform/base/pipeline.rb', line 148

def cleanup
  @tempfiles.each do |file| 
    file.close(true) if file.is_a? Tempfile
    FileUtils.rm_f(file.path) if File.exist?(file.path)
  end
end

#commandObject




104
105
106
107
108
109
110
111
# File 'lib/ec2/platform/base/pipeline.rb', line 104

def command
  # Create the pipeline incantation
  pipeline = @stages.map { |s| s.command }.join(' | ') + '; '
  
  # Fudge pipeline incantation to make return codes for each
  # stage accessible from the associated pipeline stage
  pipestatus(pipeline)
end

#concat(arr) ⇒ Object




86
87
88
89
90
91
92
93
# File 'lib/ec2/platform/base/pipeline.rb', line 86

def concat(arr)
  if arr.is_a? Array
    arr.each do |e|
      self.add(e[0], e[1], e[2] || 0)
    end
  end
  self
end

#create_tempfilesObject




161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ec2/platform/base/pipeline.rb', line 161

def create_tempfiles
  @tempfiles = (0...@stages.length).map do |index|
    file = Tempfile.new("#{@basename}-pipestatus-#{index}")
    file.close(false)
    file
  end
  unless @tempfiles.length == @stages.length
    raise ExecutionError.new(
      @basename, nil,
      "Temp files count(#{@tempfiles.length}) != stages count(#{@stages.length})")
  end
end

#errorsObject




156
157
158
# File 'lib/ec2/platform/base/pipeline.rb', line 156

def errors
  @results.reject { |r| r.success }
end

#executeObject




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
# File 'lib/ec2/platform/base/pipeline.rb', line 114

def execute()
  @results = []
  create_tempfiles
  escaped_command = command.gsub("'","'\"'\"'")
  invocation = "/bin/bash -c '#{escaped_command}'"
  
  # Execute the pipeline invocation
  STDERR.puts("Pipeline.execute: command = [#{invocation}]") if verbose
  output = `#{invocation}`
  STDERR.puts("Pipeline.execute: output = [#{output.strip}]") if verbose
    
  unless $CHILD_STATUS.success?
    raise ExecutionError.new(@basename)
  end
    
  # Collect the pipeline's exit codes and see if they're good
  successful = true
  offender = nil
  @results = @tempfiles.zip(@stages).map do |file, stage|
    file.open()
    status = file.read().strip.to_i
    file.close(false)
    success = (stage.success == status)
    successful &&= success
    offender = stage.name unless successful
    Stage::Result.new(stage.name, status, success)
  end
  unless successful
    raise ExecutionError.new(@basename, offender)
  end
  output
end

#pipestatus(cmd) ⇒ Object


Given a pipeline of commands, modify it so that we can obtain the exit status of each pipeline stage by reading the tempfile associated with that stage. Must be implemented by subclasses



99
100
101
# File 'lib/ec2/platform/base/pipeline.rb', line 99

def pipestatus(cmd)
  raise 'unimplemented method'
end

#to_sObject




175
176
177
# File 'lib/ec2/platform/base/pipeline.rb', line 175

def to_s
  "Pipeline(stages=[#{@stages.join(', ')}], results=[#{@results.join(', ')}])"
end