Skip to content

Instantly share code, notes, and snippets.

@zparnold
Created January 15, 2018 20:14
Show Gist options
  • Save zparnold/99d142d555a82017f5c54714ef11ec9a to your computer and use it in GitHub Desktop.
Save zparnold/99d142d555a82017f5c54714ef11ec9a to your computer and use it in GitHub Desktop.
If run as root on a server it will update the Yum repos to the latest, and then cleans up after itself
require 'tempfile'
require 'fileutils'
require 'open3'
def edit_file(location, update_config)
temp_file = Tempfile.new('newConfig')
begin
File.open(location, 'r') do |file|
file.each_line do |line|
if line.include? "="
str_array = line.split("=")
if str_array[0] == "mirrorlist" && update_config == true
temp_file.puts "mirrorlist=http://repo.$awsregion.$awsdomain/latest/updates/mirror.list-$guid"
elsif str_array[0] == "mirrorlist" && update_config == false
temp_file.puts "mirrorlist=http://repo.$awsregion.$awsdomain/$releasever/updates/mirror.list-$guid"
else
temp_file.puts line
end
else
temp_file.puts line
end
end
end
temp_file.close
FileUtils.mv(temp_file.path, location)
ensure
temp_file.close
temp_file.unlink
end
end
def run_command(cmd_text)
Open3.popen3(cmd_text) do |stdin, stdout, stderr, wait_thr|
exit_status = wait_thr.value
if exit_status != 0
puts stderr.read
abort
else
puts stdout.read
end
end
end
# In order we want to update the yum repos
# Then update the kernel
# Then revert the config
edit_file("/etc/yum.repos.d/amzn-main.repo",true)
edit_file("/etc/yum.repos.d/amzn-updates.repo",true)
run_command("yum update kernel -y")
edit_file("/etc/yum.repos.d/amzn-main.repo",false)
edit_file("/etc/yum.repos.d/amzn-updates.repo",false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment