Module: Common

Defined in:
lib/common/attribute_resolver.rb,
lib/common/log.rb,
lib/common/helpers.rb,
lib/common/attribute_checker.rb

Overview

Habdles unknown method calls with arguments as variable sets

Defined Under Namespace

Modules: DefaultAttributeResolver Classes: Log, NovaDslCommand

Class Method Summary collapse

Class Method Details

.system_call(args = {}, &block) ⇒ Object

Wraps out the system calls, accepted only 0 results, otherwise NovaCallError raised

Parameters:

  • block (Proc)

    system call ‘system’ or whateveg

Returns:

  • (Object)

    console output of the call within block



11
12
13
14
15
16
17
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
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/common/helpers.rb', line 11

def self.system_call(args ={}, &block)
  LOGGER.debug("Calling system calls with parameters: #{args}")
  type_checker args[:source], String
  src = "source #{args[:source]} && " unless args[:source].nil?
  src ||= ''

  result = []
  ok_count = 0
  fail_count = 0

  if block.nil?
    LOGGER.error("'system_call_checker' has received empty block, raising Runtime EX ")
    raise "Empty block has been given to 'system_call_checker'"
  end

  block_commands = yield
  block_commands = block_commands.split("\n").map { |x| x.strip }
  LOGGER.debug("Calling system call with args: #{args.inspect} \n commands: #{block_commands*"\n"}")

  result_stdout = nil
  result_stderr = nil
  proc_pid = nil
  block_commands.each do |cmd|
    composite = src + cmd
    status = Open4::popen4(composite) do |pid, stdin, stdout, stderr|
      LOGGER.debug("Command '#{composite}' is running as pid: #{pid} ")
      proc_pid = pid
      result_stdout = stdout.read.strip
      result_stderr = stderr.read.strip
    end

    LOGGER.debug("Process #{proc_pid} finished, code: #{status.exitstatus}")
    if status.success?; ok_count+=1 else fail_count+=1 end
    LOGGER.error("#{result_stderr}") unless status.success?

    cmd_result = {}
    cmd_result[:status] = status.exitstatus
    cmd_result[:out] = status.success? ? result_stdout : result_stderr

    result << cmd_result
  end
  LOGGER.debug("#{result.size} commands executed, ok: #{ok_count}, failed: #{fail_count}")

  result
end

.type_checker(value, clazz) ⇒ Object

Raises:

  • (TypeError)


57
58
59
60
61
# File 'lib/common/helpers.rb', line 57

def self.type_checker(value, clazz)
  return value if value.nil?
  raise TypeError.new("Waiting #{clazz} class but received #{value.class} ") unless value.is_a?(clazz)
  value
end