Skip to content

Instantly share code, notes, and snippets.

@nathwill
Created September 12, 2016 17:58
Show Gist options
  • Save nathwill/26aadc9a030d4706a5adf79d57e949ea to your computer and use it in GitHub Desktop.
Save nathwill/26aadc9a030d4706a5adf79d57e949ea to your computer and use it in GitHub Desktop.
simple snapshotting script
#!/opt/chef/embedded/bin/ruby
#
# Create/manage Cinder volume snapshots
#
require 'mixlib/shellout'
require 'fog/openstack'
#
# Assemble our SnapshotRobot
#
class SnapshotRobot
attr_accessor :client, :options, :server, :volume
def initialize(opts)
@client = Fog::Compute.new(
provider: :openstack, openstack_tenant: ENV['OS_TENANT_NAME'],
openstack_username: ENV['OS_USERNAME'], openstack_api_key: ENV['OS_PASSWORD'],
openstack_auth_url: "#{ENV['OS_AUTH_URL']}/tokens"
)
@options = opts
server_id = File.read('/var/lib/cloud/data/instance-id').chomp
@server = @client.servers.get(server_id)
@volume = @client.volumes.detect do |vol|
vol.attachments.first['serverId'] == server.id
end
end
def run!
stop!
snap!
start!
end
private
def stop!
manage_units!(:stop, @options[:services])
manage_units!(:kill, @options[:services]) if @options[:kill]
manage_units!(:stop, @options[:mounts])
end
def snap!
detach_volume!(@server, @volume)
create_snapshot!(@volume)
attach_volume!(@server, @volume)
Mixlib::ShellOut.new('systemctl daemon-reload').run_command
end
def start!
manage_units!(:start, @options[:mounts])
manage_units!(:start, @options[:services])
end
def manage_units!(action, unit_list)
unit_list.each do |unit|
Mixlib::ShellOut
.new("systemctl #{action} #{unit}", timeout: 3_600)
.tap(&:run_command)
.error!
end
end
def detach_volume!(server, volume)
server.detach_volume(volume.id)
volume.wait_for { status == 'available' }
end
def create_snapshot!(volume)
name = "#{@volume.name}-#{Time.now.to_i}"
@client.create_snapshot(volume.id, name, 'Roderick was here.')
@client.snapshots
.find { |snap| snap.name == name }
.wait_for { status == 'available' }
end
def attach_volume!(server, volume)
server.attach_volume(volume.id, '/dev/vdb')
volume.wait_for { status == 'in-use' }
end
end
#
# Turn the SnapshotRobot loose.
# Isn't little Roderick so cute?
#
require 'optparse'
options = {}
optparser = OptionParser.new do |opts|
opts.banner = 'Usage: create-cinder-snapshot.rb [options]'
opts.on('--services x,y,z', Array, 'Services to stop/start.') do |s|
options[:services] = s
end
opts.on('--mounts x,y,z', Array, 'Mount units to umount/mount.') do |m|
options[:mounts] = m
end
opts.on('--kill', 'Kill services after stopping.') do |k|
options[:kill] = k
end
end
optparser.parse!
begin
puts "Starting snapshotting."
roderick = SnapshotRobot.new(options)
roderick.run!
rescue StandardError => error
puts "Failed snapshotting!"
puts "ERROR: #{error.message}"
else
puts "Finished snapshotting."
ensure
puts 'Going into sleep mode.'
end
@nathwill
Copy link
Author

example usage:

[root@ws-gluster0 ~(WDC_STAGING)]# ./snapshot-robot.rb -s glusterd.service -m srv-workspaces.mount,srv-data.mount -k
Starting snapshotting
Finished snapshotting
Going into sleep mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment