Class: Paknife::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/paknife/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, backend, subcommand, node, knife_options, options = {}) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
17
18
# File 'lib/paknife/context.rb', line 9

def initialize(index, backend, subcommand, node, knife_options, options = {})
  @index = index
  @backend = backend
  @subcommand = subcommand
  @node = node
  @knife_options = knife_options
  @options = options

  setup_logger
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



7
8
9
# File 'lib/paknife/context.rb', line 7

def backend
  @backend
end

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/paknife/context.rb', line 7

def index
  @index
end

#knife_optionsObject (readonly)

Returns the value of attribute knife_options.



7
8
9
# File 'lib/paknife/context.rb', line 7

def knife_options
  @knife_options
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/paknife/context.rb', line 7

def logger
  @logger
end

#nodeObject (readonly)

Returns the value of attribute node.



7
8
9
# File 'lib/paknife/context.rb', line 7

def node
  @node
end

#subcommandObject (readonly)

Returns the value of attribute subcommand.



7
8
9
# File 'lib/paknife/context.rb', line 7

def subcommand
  @subcommand
end

Instance Method Details

#commandObject



45
46
47
48
49
50
51
52
53
# File 'lib/paknife/context.rb', line 45

def command
  [
    @options[:knife],
    backend,
    subcommand,
    node,
    knife_options,
  ].flatten.compact.join(" ")
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/paknife/context.rb', line 20

def run
  logger.info command
  Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
    stdin.close_write

    begin
      loop do
        IO.select([stdout, stderr]).flatten.compact.each do |io|
          io.each do |line|
            next if line.nil? || line.empty?

            if io == stdout
              logger.info line
            elsif io == stderr
              logger.warn line
            end
          end
        end
        break if stdout.eof? && stderr.eof?
      end
    rescue EOFError
    end
  end
end