Module: Avalon::Utils

Included in:
Config, Node
Defined in:
lib/avalon/utils.rb

Instance Method Summary collapse

Instance Method Details

#alarm(message, sound = :failure) ⇒ Object

Helper method: sound alarm with message



34
35
36
37
# File 'lib/avalon/utils.rb', line 34

def alarm message, sound=:failure
  puts message
  play sound 
end

#duration(time_string) ⇒ Object

Helper method: from time string ‘hh:mm:ss’ to duration in minutes



40
41
42
43
44
45
46
47
# File 'lib/avalon/utils.rb', line 40

def duration time_string
  if time_string == 'never'
    'never'
  else
    hour, min, sec = *time_string.split(/:/).map(&:to_i)
    (hour*60.0 + min + sec/60.0).round(2)
  end
end

#find_file(*locations) ⇒ Object



8
9
10
# File 'lib/avalon/utils.rb', line 8

def find_file *locations
  locations.map {|loc| File.expand_path(loc,__FILE__)}.find {|f| File.exist?(f)}
end

#ping(ip) ⇒ Object

Helper method: ping the Node, return ping time in ms



50
51
52
53
54
55
# File 'lib/avalon/utils.rb', line 50

def ping ip
  ping_result = `ping -c 1 #{ip}`
  if ping_result =~ /( | 0.)0% packet loss/
    ping_result.match(/time=([\.\d]*) ms/)[1].to_f.round(1)
  end
end

#play(what) ⇒ Object

Helper method: play a sound file



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/avalon/utils.rb', line 13

def play what
  case Avalon::Config[:alert_sounds]
  when false, :none, :no
  when Hash
    tunes = [Avalon::Config[:alert_sounds][what] || what].compact.flatten

    tunes.each do |tune|
      file = find_file( tune, "../../../sound/#{tune}",
                        "~/.avalon/sound/#{tune}", "/System/Library/Sounds/#{tune}")
      case system
      when 'Darwin'
        `afplay #{file}`
      when 'Linux'
        raise 'Please install sox package: sudo apt-get install sox' if `which sox`.empty?
        `play -q #{file}`
      end
    end
  end
end

#systemObject



4
5
6
# File 'lib/avalon/utils.rb', line 4

def system
  @system ||= `uname -a`.match(/^\w*/).to_s
end