Skip to content

Instantly share code, notes, and snippets.

@jamesshipton
Last active December 19, 2015 02:48
Show Gist options
  • Save jamesshipton/5885312 to your computer and use it in GitHub Desktop.
Save jamesshipton/5885312 to your computer and use it in GitHub Desktop.
Code to rebuild a Linode with ruby - using createfromdistribution. This code has been designed for use with one linode - if you have more than one it will need to be amended you will need to install the linode gem https://github.com/rick/linode
## Run rake rebuild
#
#task :rebuild do
# DiscogsServer.shutdown
#
# DiscogsServer.delete_disks
#
# DiscogsServer.delete_configs
#
# DiscogsServer.create_disks
#
# DiscogsServer.create_configs
#
# DiscogsServer.boot
#end
require 'linode'
require 'etc'
require 'securerandom'
module DiscogsServer
API_KEY = ENV['API_KEY']
class << self
def shutdown
puts 'Shutdown the server'
linode.shutdown(LinodeID: linodeid)
confirm 'Shutdown' do
(linode.list.first.status == 2)
end
end
def delete_disks
puts 'Delete the disks'
linode.disk.list(LinodeID: linodeid).each do |d|
linode.disk.delete(LinodeID: linodeid, DiskID: d.diskid)
end
confirm 'Disk deletion' do
linode.disk.list(LinodeID: linodeid).empty?
end
end
def delete_configs
puts 'Delete the configs'
linode.config.list(LinodeID: linodeid).each do |c|
linode.config.delete(LinodeID: linodeid, ConfigID: c.configid)
end
end
def create_disks
puts "Create an #{distribution_name} disk image"
boot_disk
puts 'Create a swap disk'
swap_disk
confirm 'Disk boot' do
linode.disk.list(LinodeID: linodeid).all? { |d| d.status == 1 }
end
end
def create_configs
puts 'Create a config profile using the Latest 32 bit Kernel'
linode.config.create(
LinodeID: linodeid,
KernelID: kernelid,
DiskList: "#{boot_disk.diskid},#{swap_disk.diskid},,,,,,,",
Label: 'My discogs-service Profile')
end
def boot
puts 'Boot the server'
linode.boot LinodeID: linodeid
confirm 'Server boot' do
(linode.list.first.status == 1)
end
end
def reboot
puts 'Reboot the server'
linode.reboot LinodeID: linodeid
confirm 'Server reboot' do
(linode.list.first.status == 1)
end
end
private
def connection
@connection ||= Linode.new(api_key: API_KEY)
end
def linode
@linode ||= connection.linode
end
def avail
@avail ||= connection.avail
end
def linodeid
@linodeid ||= linode.list.first.linodeid
end
def distribution_name
'Ubuntu 12.04 LTS 32bit'
end
def distributionid
avail.distributions.select { |d| d.label == distribution_name }.first.distributionid
end
def kernelid
avail.kernels.select { |k| k.label.match %r(^Latest 32 bit) }.first.kernelid
end
def boot_disk
@boot_disk ||= linode.disk.createfromdistribution(
LinodeID: linodeid,
DistributionID: distributionid,
Label: 'discogs-service Disk Image',
Size: 24320,
# We don't really care what this is as we will disable ssh password access
rootPass: SecureRandom.uuid,
rootSSHKey: ssh_key_pub)
end
def swap_disk
@swap_disk ||= linode.disk.create(
LinodeID: linodeid,
Label: '256MB Swap Image',
Type: 'swap',
Size: 256)
end
def confirm(action)
until yield do
puts "#{action} not complete..."
sleep 2
end
puts "#{action} complete"
end
def ssh_key_pub
File.read("#{Dir.home(Etc.getlogin)}/.ssh/id_rsa.pub")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment