Created
January 23, 2014 16:42
-
-
Save smashwilson/8582027 to your computer and use it in GitHub Desktop.
This file contains 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 'fog' | |
require 'base64' | |
service = Fog::Compute.new( | |
provider: 'rackspace', | |
rackspace_username: ENV['RAX_USERNAME'], | |
rackspace_api_key: ENV['RAX_API_KEY'], | |
rackspace_region: :ord | |
) | |
# Adjust these settings based on your needs. | |
@swapsize = "1048576" | |
@swappiness = "10" | |
# See: | |
# http://www.rackspace.com/knowledge_center/article/create-a-linux-swap-file | |
enable_swap = Base64.encode64 <<EOF | |
set -e | |
dd if=/dev/zero of=/mnt/swapfile bs=1024 count=#{@swapsize} | |
mkswap /mnt/swapfile | |
swapon /mnt/swapfile | |
echo "/mnt/swapfile none swap sw 0 0" >> /etc/fstab | |
echo "vm.swappiness=#{@swappiness}" >> /etc/sysctl.conf | |
sysctl vm.swappiness=#{@swappiness} | |
EOF | |
# You could also use a keypair. | |
pubkey = Base64.encode64(File.read("#{ENV['HOME']}/.ssh/id_rsa.pub")) | |
# Flavor = 1GB standard instance | |
# Image = Ubuntu 13.10 (Saucy Salamander) | |
# Adjust for your preference. | |
puts "creating server." | |
s = service.servers.create name: 'swappy', | |
flavor_id: 'performance1-1', image_id: 'df27d481-63a5-40ca-8920-3d132ed643d9', personality: [ | |
{ path: '/root/enable_swap.sh', contents: enable_swap }, | |
{ path: '/root/.ssh/authorized_keys', contents: pubkey } | |
] | |
s.wait_for { puts " progress: #{progress}" ; ready? } | |
puts "server created." | |
system "ssh-keyscan #{s.ipv4_address} >> ~/.ssh/known_hosts" | |
system "ssh root@#{s.ipv4_address} bash /root/enable_swap.sh" | |
puts "log in with: ssh root@#{s.ipv4_address}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment