Skip to content

Instantly share code, notes, and snippets.

@clarkdave
Last active March 12, 2018 04:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save clarkdave/5477434 to your computer and use it in GitHub Desktop.
Save clarkdave/5477434 to your computer and use it in GitHub Desktop.
A Chef recipe for creating EBS volumes. See: http://clarkdave.net/2013/04/managing-ebs-volumes-with-chef/
# The database recipe should be included by any server running a DB. It creates
# a /data directory and, if on EC2, will mount an EBS volume here
directory '/data' do
mode '0755'
end
if node[:app][:ec2] || node[:cloud][:provider] == 'ec2'
aws = data_bag_item('aws', 'main')
include_recipe 'aws'
if node[:app][:ebs][:raid]
aws_ebs_raid 'data_volume_raid' do
mount_point '/data'
disk_count 2
disk_size node[:app][:ebs][:size]
level 10
filesystem 'ext4'
action :auto_attach
end
else
# get a device id to use
devices = Dir.glob('/dev/xvd?')
devices = ['/dev/xvdf'] if devices.empty?
devid = devices.sort.last[-1,1].succ
# save the device used for data_volume on this node -- this volume will now always
# be attached to this device
node.set_unless[:aws][:ebs_volume][:data_volume][:device] = "/dev/xvd#{devid}"
device_id = node[:aws][:ebs_volume][:data_volume][:device]
# no raid, so just mount and format a single volume
aws_ebs_volume 'data_volume' do
aws_access_key aws['aws_access_key_id']
aws_secret_access_key aws['aws_secret_access_key']
size node[:app][:ebs][:size]
device device_id.gsub('xvd', 'sd') # aws uses sdx instead of xvdx
action [:create, :attach]
end
# wait for the drive to attach, before making a filesystem
ruby_block "sleeping_data_volume" do
block do
timeout = 0
until File.blockdev?(device_id) || timeout == 1000
Chef::Log.debug("device #{device_id} not ready - sleeping 10s")
timeout += 10
sleep 10
end
end
end
# create a filesystem
execute 'mkfs' do
command "mkfs -t ext4 #{device_id}"
end
mount '/data' do
device device_id
fstype 'ext4'
options 'noatime,nobootwait'
action [:enable, :mount]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment