Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ip_route for appliction_console #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/linux_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'linux_admin/registration_system'
require 'linux_admin/rpm'
require 'linux_admin/deb'
require 'linux_admin/homebrew'
require 'linux_admin/version'
require 'linux_admin/yum'
require 'linux_admin/ssh'
Expand Down
6 changes: 5 additions & 1 deletion lib/linux_admin/distro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ def self.ubuntu
@ubuntu ||= Distro.new(:ubuntu, nil, ['ubuntu'], LinuxAdmin::Deb)
end

def self.darwin
@darwin ||= Distro.new(:darwin, "/System/Library/CoreServices/SystemVersion.plist", ['darwin'], LinuxAdmin::Homebrew)
end

def self.all
@distros ||= [rhel, fedora, ubuntu, generic]
@distros ||= [rhel, fedora, ubuntu, darwin, generic]
end

def self.local
Expand Down
22 changes: 22 additions & 0 deletions lib/linux_admin/homebrew.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module LinuxAdmin
class Homebrew < Package
extend Logging

def self.homebrew_cmd
Common.cmd("brew")
end

def self.list_installed
info(nil)
end

def self.info(pkg)
out = Common.run!(homebrew_cmd, :params => ["list", :versions, pkg]).output
out.split("\n").each_with_object({}) do |line, pkg_hash|
name, ver = line.split(" ")[0..1] # only take the latest version
pkg_hash[name] = ver
end
end
end
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look like you have a bonus line at the end here.

2 changes: 2 additions & 0 deletions lib/linux_admin/network_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def self.dist_class(clear_cache = false)
@dist_class ||= begin
if [Distros.rhel, Distros.fedora].include?(Distros.local)
NetworkInterfaceRH
elsif Distros.local == Distros.darwin
NetworkInterfaceDarwin
Comment on lines 16 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is cleaner as:

Suggested change
if [Distros.rhel, Distros.fedora].include?(Distros.local)
NetworkInterfaceRH
elsif Distros.local == Distros.darwin
NetworkInterfaceDarwin
case Distros.local
when Distros.rhel, Distros.fedora
NetworkInterfaceRH
when Distros.darwin
NetworkInterfaceDarwin

else
NetworkInterfaceGeneric
end
Expand Down
19 changes: 19 additions & 0 deletions lib/linux_admin/network_interface/network_interface_darwin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module LinuxAdmin
class NetworkInterfaceDarwin < NetworkInterface
# Runs the command `ip -[4/6] route` and returns the output
#
# @param version [Fixnum] Version of IP protocol (4 or 6)
# @return [String] The command output
# @raise [NetworkInterfaceError] if the command fails
#
# macs use ip route get while others use ip route show
def ip_route(version, route = "default")
output = Common.run!(Common.cmd("ip"), :params => ["--json", "-#{version}", "route", "get", route]).output
return {} if output.blank?

JSON.parse(output).first
rescue AwesomeSpawn::CommandResultError => e
raise NetworkInterfaceError.new(e.message, e.result)
end
end
end
10 changes: 8 additions & 2 deletions spec/distro_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
context "/etc/issue contains '#{i}'" do
before(:each) do
etc_issue_contains(i)
exists("/etc/fedora-release" => false, "/etc/redhat-release" => false)
exists("/etc/fedora-release" => false, "/etc/redhat-release" => false, "/System/Library/CoreServices/SystemVersion.plist" => false)
end

it "returns Distros.#{d}" do
Expand Down Expand Up @@ -43,9 +43,15 @@
end
end

it "returns Distro.darwin" do
etc_issue_contains('')
exists("/etc/fedora-release" => false, "/etc/redhat-release" => false, "/System/Library/CoreServices/SystemVersion.plist" => true)
expect(subject).to eq(LinuxAdmin::Distros.darwin)
end

it "returns Distros.generic" do
etc_issue_contains('')
exists("/etc/fedora-release" => false, "/etc/redhat-release" => false)
exists("/etc/fedora-release" => false, "/etc/redhat-release" => false, "/System/Library/CoreServices/SystemVersion.plist" => false)
expect(subject).to eq(LinuxAdmin::Distros.generic)
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/network_interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@
end
end

context "on darwin systems" do
describe ".dist_class" do
it "returns NetworkInterfaceDarwin" do
allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.darwin)
expect(described_class.dist_class(true)).to eq(LinuxAdmin::NetworkInterfaceDarwin)
end
end

describe ".new" do
before do
allow_any_instance_of(described_class).to receive(:ip_show).and_raise(LinuxAdmin::NetworkInterfaceError.new(nil, nil))
allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.darwin)
described_class.dist_class(true)
allow(Pathname).to receive(:new).and_return(config_file_path)
end

it "creates a NetworkInterfaceDarwin instance" do
expect(described_class.new(device_name)).to be_an_instance_of(LinuxAdmin::NetworkInterfaceDarwin)
end
end
end

context "on other linux systems" do
describe ".dist_class" do
it "returns NetworkInterfaceGeneric" do
Expand Down