implement interface for linux

This commit is contained in:
Christoph Hartmann 2015-10-07 23:47:57 +02:00
parent 4d4b79b164
commit 4223d5b1ef
2 changed files with 52 additions and 0 deletions

View file

@ -0,0 +1,51 @@
# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter
# Usage:
# describe interface('eth0') do
# it { should exist }
# it { should be_up }
# its(:speed) { should eq 1000 }
# end
require 'utils/convert'
class NetworkInterface < Vulcano.resource(1)
include Converter
name 'interface'
def initialize(iface)
@iface = iface
@cache = nil
end
def exists?
!interface_info.nil?
end
def up?
return false if interface_info.nil? || !interface_info.key?('operstate')
key, _value = interface_info['operstate'].first
key == 'up'
end
def speed
return nil if interface_info.nil? || !interface_info.key?('speed')
key, _value = interface_info['speed'].first
convert_to_i(key)
end
private
def interface_info
return @cache if !@cache.nil?
# will return "[mtu]\n1500\n[type]\n1"
cmd = vulcano.command("find /sys/class/net/#{@iface}/ -type f -maxdepth 1 -exec sh -c 'echo \"[$(basename {})]\"; cat {} || echo -n' \\;")
return nil if cmd.exit_status.to_i != 0
# parse values, we only recieve values, therefore we threat them as keys
@cache = SimpleConfig.new(cmd.stdout.chomp).params
end
end

View file

@ -34,6 +34,7 @@ require 'resources/gem'
require 'resources/group'
require 'resources/group_policy'
require 'resources/inetd_conf'
require 'resources/interface'
require 'resources/json'
require 'resources/kernel_module'
require 'resources/kernel_parameter'