-
Notifications
You must be signed in to change notification settings - Fork 659
Fix OS detection in DirectorStemcellOwner to read from system files #2640
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,58 @@ | ||
| require 'etc' | ||
|
|
||
| module Bosh::Director | ||
| class DirectorStemcellOwner | ||
| OS_RELEASE_FILE = '/etc/os-release'.freeze | ||
| OPERATING_SYSTEM_FILE = '/var/vcap/bosh/etc/operating_system'.freeze | ||
| STEMCELL_VERSION_FILE = '/var/vcap/bosh/etc/stemcell_version'.freeze | ||
|
|
||
| def stemcell_os | ||
| @stemcell_os ||= os_and_version | ||
| end | ||
|
|
||
| def stemcell_version | ||
| return @stemcell_version unless @stemcell_version.nil? | ||
|
|
||
| stemcell_version_path = '/var/vcap/bosh/etc/stemcell_version' | ||
| return '-' unless File.exist?(stemcell_version_path) | ||
| return '-' unless File.exist?(STEMCELL_VERSION_FILE) | ||
|
|
||
| @stemcell_version = File.read(stemcell_version_path).chomp | ||
| @stemcell_version = File.read(STEMCELL_VERSION_FILE).chomp | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def os_and_version | ||
| results = Etc.uname[:version].scan(/~([^ ]*)-([^ ]*) .*$/)[0] | ||
| return '-' if Array(results).empty? | ||
|
|
||
| os = results[1].downcase | ||
| version_number = results[0] | ||
| version_name = if version_number.start_with?('16.') | ||
| 'xenial' | ||
| elsif version_number.start_with?('14.') | ||
| 'trusty' | ||
| elsif version_number.start_with?('18.') | ||
| 'bionic' | ||
| else | ||
| version_number | ||
| end | ||
|
|
||
| "#{os}-#{version_name}" | ||
| os = read_operating_system | ||
| codename = read_codename | ||
|
|
||
| return '-' if os.nil? || codename.nil? | ||
|
|
||
| "#{os}-#{codename}" | ||
| end | ||
|
|
||
| def read_operating_system | ||
| if File.exist?(OPERATING_SYSTEM_FILE) | ||
| return File.read(OPERATING_SYSTEM_FILE).chomp.downcase | ||
| end | ||
|
|
||
| return nil unless File.exist?(OS_RELEASE_FILE) | ||
|
|
||
| File.readlines(OS_RELEASE_FILE).each do |line| | ||
| if line =~ /^ID=(.+)$/ | ||
| return ::Regexp.last_match(1).strip.delete('"').downcase | ||
| end | ||
| end | ||
|
Comment on lines
+37
to
+41
|
||
|
|
||
| nil | ||
| end | ||
|
|
||
| def read_codename | ||
| return nil unless File.exist?(OS_RELEASE_FILE) | ||
|
|
||
| File.readlines(OS_RELEASE_FILE).each do |line| | ||
| if line =~ /^UBUNTU_CODENAME=(.+)$/ | ||
| return ::Regexp.last_match(1).strip.delete('"') | ||
| end | ||
| end | ||
|
Comment on lines
+49
to
+53
|
||
|
|
||
| nil | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
File.readcall could raise exceptions (e.g.,Errno::ENOENT,Errno::EACCES) if the file becomes inaccessible between the existence check and the read. Consider wrapping this in a rescue block to returnnilon error, similar to howstemcell_versionhandles the file read.