Class: KnifeNodeContextExec::NodeRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/knife-node-context-exec/runner.rb

Overview

Runs a command in the context of each node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, working_directory, template_filename, script_filename, command) ⇒ NodeRunner

Returns a new instance of NodeRunner.



10
11
12
13
14
15
16
# File 'lib/knife-node-context-exec/runner.rb', line 10

def initialize(node, working_directory, template_filename, script_filename, command)
  @node = node
  @working_directory = working_directory
  @template_filename = template_filename
  @script_filename = script_filename
  @command = command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



8
9
10
# File 'lib/knife-node-context-exec/runner.rb', line 8

def command
  @command
end

#nodeObject (readonly)

Returns the value of attribute node.



8
9
10
# File 'lib/knife-node-context-exec/runner.rb', line 8

def node
  @node
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/knife-node-context-exec/runner.rb', line 8

def output
  @output
end

#script_filenameObject (readonly)

Returns the value of attribute script_filename.



8
9
10
# File 'lib/knife-node-context-exec/runner.rb', line 8

def script_filename
  @script_filename
end

#template_filenameObject (readonly)

Returns the value of attribute template_filename.



8
9
10
# File 'lib/knife-node-context-exec/runner.rb', line 8

def template_filename
  @template_filename
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



8
9
10
# File 'lib/knife-node-context-exec/runner.rb', line 8

def working_directory
  @working_directory
end

Instance Method Details

#runObject



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
# File 'lib/knife-node-context-exec/runner.rb', line 18

def run
  @thread = Thread.new do
    script_directory = "#{working_directory}/#{node.name}"
    FileUtils.makedirs(script_directory)
    template = File.read(template_filename)
    script = ERB.new(template).result(binding)
    full_script_filename = File.join(script_directory, script_filename)
    File.open(full_script_filename, 'w') { |file| file.puts(script) }
    cooked_command = command.gsub('%script%') do
      full_script_filename.gsub(File::SEPARATOR) { File::ALT_SEPARATOR || File::SEPARATOR }
    end
    @output = []
    yield(">>>> Command: #{cooked_command} <<<<")
    Open3.pipeline_r(cooked_command) do |output, _|
      yield(">>>> Executing process for #{node.name} <<<<")
      output.each_line do |line|
        line.rstrip!
        @output << line
        yield(line)
      end
      yield(">>>> Completed process for #{node.name} <<<<")
    end
  end
  self
end

#waitObject



44
45
46
47
# File 'lib/knife-node-context-exec/runner.rb', line 44

def wait
  @thread.join
  self
end