Class: Joumae::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/joumae/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, resource_name:, client:) ⇒ Command

Returns a new instance of Command.



10
11
12
13
14
# File 'lib/joumae/command.rb', line 10

def initialize(cmd, resource_name:, client:)
  @cmd = cmd
  @resource_name = resource_name
  @client = client
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



8
9
10
# File 'lib/joumae/command.rb', line 8

def cmd
  @cmd
end

Instance Method Details

#loggerObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/joumae/command.rb', line 16

def logger
  @logger ||= Logger.new(STDOUT).tap do |logger|
    log_level_from_env = ENV['JOUMAE_LOG_LEVEL'] || 'INFO'
    logger.level = Logger.const_get(log_level_from_env)
    logger.formatter = proc do |severity, datetime, progname, msg|
      date_format = datetime.strftime("%Y-%m-%d %H:%M:%S")
      "#{cmd} (#{severity}): #{msg}\n"
    end
  end
end

#run!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/joumae/command.rb', line 27

def run!
  status = Joumae::Transaction.run!(resource_name: @resource_name, client: @client) do
    Open3.popen3("bash") do |i, o, e, w|
      i.write cmd
      i.close

      o.each do |line|
        STDOUT.puts line
        STDOUT.flush
      end

      e.each do |line|
        STDERR.puts line
        STDERR.flush
      end

      debug w.value

      w.value.exitstatus
    end
  end
  raise Joumae::CommandFailedError.new("Exit status(=#{status}) is non-zero.", status) if status != 0
end