Class: MiasmaTerraform::Stack::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/miasma-terraform/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, opts = {}) ⇒ self

Create a new action to run

Parameters:

  • command (String)
  • opts (Hash) (defaults to: {})


69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/miasma-terraform/stack.rb', line 69

def initialize(command, opts={})
  @command = command.dup.freeze
  @options = opts.to_smash
  @io_callbacks = []
  @complete_callbacks = []
  @start_callbacks = []
  @cached_output = Smash.new(
    :stdout => StringIO.new(''),
    :stderr => StringIO.new('')
  )
  if(@options.delete(:auto_start))
    start!
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



62
63
64
# File 'lib/miasma-terraform/stack.rb', line 62

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



62
63
64
# File 'lib/miasma-terraform/stack.rb', line 62

def options
  @options
end

#stdinObject (readonly)

Returns the value of attribute stdin.



62
63
64
# File 'lib/miasma-terraform/stack.rb', line 62

def stdin
  @stdin
end

#waiterObject (readonly)

Returns the value of attribute waiter.



62
63
64
# File 'lib/miasma-terraform/stack.rb', line 62

def waiter
  @waiter
end

Instance Method Details

#complete!Process::Status

Wait for the process to complete

Returns:

  • (Process::Status)


101
102
103
104
105
106
107
108
109
# File 'lib/miasma-terraform/stack.rb', line 101

def complete!
  start! unless waiter
  if(@process_manager)
    @process_manager.join
  end
  result = waiter.value
  MiasmaTerraform::Stack.deregister_action(self)
  result
end

#on_complete {|result, self| ... } ⇒ Object

Register a block to be run when a process completes

Yield Parameters:

  • result (Process::Status)
  • self (Action)


143
144
145
146
# File 'lib/miasma-terraform/stack.rb', line 143

def on_complete(&block)
  @complete_callbacks << block
  self
end

#on_io {|line, type| ... } ⇒ Object

Register a block to be run when process output is received

Yield Parameters:

  • line (String)

    output line

  • type (Symbol)

    output type (:stdout or :stderr)



134
135
136
137
# File 'lib/miasma-terraform/stack.rb', line 134

def on_io(&block)
  @io_callbacks << block
  self
end

#on_start {|self| ... } ⇒ Object

Register a block to be run when a process starts

Yield Parameters:



151
152
153
154
# File 'lib/miasma-terraform/stack.rb', line 151

def on_start(&block)
  @start_callbacks << block
  self
end

#start!Object

Start the process



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/miasma-terraform/stack.rb', line 85

def start!
  opts = Hash[@options.map{|k,v| [k.to_sym,v]}]
  MiasmaTerraform::Stack.register_action(self)
  @stdin, @stdout, @stderr, @waiter = Open3.popen3(@command, **opts)
  @start_callbacks.each do |callback|
    callback.call(self)
  end
  unless(@io_callbacks.empty? && @complete_callbacks.empty?)
    manage_process!
  end
  true
end

#stderrIO

Returns stderr stream.

Returns:

  • (IO)

    stderr stream



112
113
114
115
116
117
118
# File 'lib/miasma-terraform/stack.rb', line 112

def stderr
  if(@stderr == :managed_io)
    @cached_output[:stderr]
  else
    @stderr
  end
end

#stdoutIO

Returns stdout stream.

Returns:

  • (IO)

    stdout stream



121
122
123
124
125
126
127
# File 'lib/miasma-terraform/stack.rb', line 121

def stdout
  if(@stdout == :managed_io)
    @cached_output[:stdout]
  else
    @stdout
  end
end