Last active
February 4, 2018 06:27
-
-
Save trueheart78/2bfd7d6de35d8a065cbe5cc3ef922797 to your computer and use it in GitHub Desktop.
Detect and download the most recent stable Ubuntu kernel version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'net/http' | |
require 'uri' | |
require 'byebug' | |
require 'open-uri' | |
class LinuxKernel | |
OFFICIAL_KERNEL_URL = 'https://www.kernel.org/'.freeze | |
UBUNTU_KERNEL_BASE_URL = 'http://kernel.ubuntu.com/~kernel-ppa/mainline/'.freeze | |
def initialize | |
@sections = {} | |
end | |
def exists? | |
File.directory? version | |
end | |
def version | |
return @version if @version | |
@version = Nokogiri::HTML(official_web_page).css('td#latest_link a').text | |
end | |
def available? | |
Net::HTTP.get_response(ubuntu_kernel_uri).is_a? Net::HTTPSuccess | |
end | |
def links_available? | |
files.all? do |kernel_file| | |
Net::HTTP.get_response(kernel_file[:uri]).is_a? Net::HTTPSuccess | |
end | |
end | |
def download! | |
Dir.mkdir version | |
files.each do |kernel_file| | |
print "- #{kernel_file[:basename]}" | |
download = open kernel_file[:uri] | |
IO.copy_stream(download, "#{version}/#{kernel_file[:basename]}") | |
puts " [downloaded]" | |
end | |
save_install_command | |
end | |
private | |
attr_reader :sections | |
def links | |
return @links if @links | |
detect_url_parts | |
@links = [ | |
"linux-headers-#{sections[:first]}_#{sections[:second]}_all.deb", | |
"linux-headers-#{sections[:first]}-generic_#{sections[:second]}_amd64.deb", | |
"linux-image-#{sections[:first]}-generic_#{sections[:second]}_amd64.deb" | |
] | |
end | |
def files | |
links.map do |file| | |
{ | |
basename: file, | |
uri: ubuntu_kernel_uri.tap { |uri| uri.path << file } | |
} | |
end | |
end | |
def save_install_command | |
File.open("#{version}/install_command.txt", "w") do |file| | |
file.puts 'sudo dpkg -i *.deb' | |
end | |
end | |
def detect_url_parts | |
page = Nokogiri::HTML ubuntu_kernel_page | |
link_text = page.css('a:contains("generic")').css('a:contains("amd64")').first.text | |
link_match = link_text.match(/linux-headers-(.+)-generic_(.+)_amd64/) | |
if link_match | |
@sections[:first] = link_match[1] | |
@sections[:second] = link_match[2] | |
end | |
end | |
def official_web_page | |
@official_web_page ||= Net::HTTP.get official_uri | |
end | |
def official_uri | |
URI.parse OFFICIAL_KERNEL_URL | |
end | |
def ubuntu_kernel_page | |
@ubuntu_kernel_page ||= Net::HTTP.get ubuntu_kernel_uri | |
end | |
def ubuntu_kernel_uri | |
URI.parse "#{UBUNTU_KERNEL_BASE_URL}v#{version}/" | |
end | |
end | |
linux_kernel = LinuxKernel.new | |
if linux_kernel.exists? | |
puts "You already downloaded the recent stable kernel [#{linux_kernel.version}]" | |
elsif linux_kernel.available? | |
puts "Downloading v#{linux_kernel.version}" | |
if linux_kernel.links_available? | |
linux_kernel.download! | |
puts "Done!" | |
puts "" | |
puts "Installing: sudo dpkg -i #{linux_kernel.version}/*.deb" | |
else | |
puts "Error checking the status of new kernel files" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: