Module: Cerberus::Utils

Defined in:
lib/cerberus/utils.rb

Instance Method Summary collapse

Instance Method Details

#dump_yml(file, what, overwrite = true) ⇒ Object



24
25
26
27
28
29
# File 'lib/cerberus/utils.rb', line 24

def dump_yml(file, what, overwrite = true)
  if overwrite or not File.exists?(file)
    FileUtils.mkpath(File.dirname(file))
    File.open(file, 'w') {|f| YAML::dump(what, f) } 
  end
end

#exec_successful?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/cerberus/utils.rb', line 44

def exec_successful?(cmd)
  begin
    `#{cmd}`
    return true if $?.exitstatus == 0
  rescue
    # if anything bad happens, return false
  end
  return false
end

#interpret_state(state, with_default = true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cerberus/utils.rb', line 54

def interpret_state(state, with_default = true)
   case 
   when state == 'all'
     [:setup, :successful, :revival, :broken, :failed]
   when state == 'none'
     []
   when state == 'default' && with_default
     [:setup, :revival, :broken, :failed] #the same as 'all' except successful
   else
     state.scan(/\w+/).map{|s| s.to_sym}
   end
end

#load_yml(file_name, default = {}) ⇒ Object



31
32
33
# File 'lib/cerberus/utils.rb', line 31

def load_yml(file_name, default = {})
  File.exists?(file_name) ? YAML::load(IO.read(file_name)) : default
end

#osObject



15
16
17
18
19
20
21
22
# File 'lib/cerberus/utils.rb', line 15

def os
  case RUBY_PLATFORM
  when /mswin/
    :windows
  else
    :unix
  end
end

#say(msg) ⇒ Object



10
11
12
13
# File 'lib/cerberus/utils.rb', line 10

def say(msg)
  STDERR << msg << "\n"
  exit 1
end

#silence_stream(stream) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/cerberus/utils.rb', line 35

def silence_stream(stream)
  old_stream = stream.dup
  stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
  stream.sync = true
  yield
ensure
  stream.reopen(old_stream)
end