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

Gracefully fall back to other strategies when MAC address cannot be determined #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nwallace
Copy link

@nwallace nwallace commented Jan 3, 2024

Often in containerized *nix environments, neither ip nor ifconfig will be present, which means that in these environments, the MAC address cannot be determined. In such a scenario, certain strategies would fail to generate a UUID.

This change makes the code fall back to other strategies without raising errors when the MAC address cannot be determined.

Fixes #53

@@ -213,7 +213,8 @@ def pending_if_root_required
:openbsd => openbsd_sample,
:linux => linux_sample,
:linux2 => linux_sample_2,
:linuxip => linux_ip_sample
:linuxip => linux_ip_sample,
:docker => nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

@@ -213,7 +213,8 @@ def pending_if_root_required
:openbsd => openbsd_sample,
:linux => linux_sample,
:linux2 => linux_sample_2,
:linuxip => linux_ip_sample
:linuxip => linux_ip_sample,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

@@mac_address = UUID.first_mac(UUID.ifconfig(:all))
elsif ifconfig_output=UUID.ifconfig(:all)
# linux, bsd, macos, solaris
@@mac_address = UUID.first_mac ifconfig_output
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/ClassVars: Replace class var @@mac_address with a class instance var.

@@ -688,8 +686,9 @@ def self.mac_address
@@mac_address = UUID.first_mac `ipconfig /all`
rescue
end
else # linux, bsd, macos, solaris
@@mac_address = UUID.first_mac(UUID.ifconfig(:all))
elsif ifconfig_output=UUID.ifconfig(:all)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

@@ -679,6 +676,7 @@ def self.first_mac(instring)
# Returns nil if a MAC address could not be found.
def self.mac_address
if !defined?(@@mac_address)
@@mac_address = nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/ClassVars: Replace class var @@mac_address with a class instance var.

command =
if ifconfig_path=UUID.ifconfig_path
"#{ifconfig_path}#{all ? " -a" : ""}"
elsif ip_path=UUID.ip_path
Copy link
Collaborator

Choose a reason for hiding this comment

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

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

return `#{ifconfig_path} #{all_switch}` if not ifconfig_path == nil
command =
if ifconfig_path=UUID.ifconfig_path
"#{ifconfig_path}#{all ? " -a" : ""}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/StringLiteralsInInterpolation: Prefer single-quoted strings inside interpolations.

all_switch = all == nil ? "" : "-a"
return `#{ifconfig_path} #{all_switch}` if not ifconfig_path == nil
command =
if ifconfig_path=UUID.ifconfig_path
Copy link
Collaborator

Choose a reason for hiding this comment

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

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing ip & ifconfig causes mysterious errors
2 participants