Skip to content

Instantly share code, notes, and snippets.

@barrettclark
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save barrettclark/114a4441a1a542c6daae to your computer and use it in GitHub Desktop.
Save barrettclark/114a4441a1a542c6daae to your computer and use it in GitHub Desktop.
A Rakefile to manage dev instances using Docker, boot2docker, and fig for configuration management.
namespace :boot2docker do
desc 'Start VM from any states'
task :start do
%x(boot2docker start > /dev/null 2>&1)
end
desc 'Setup shell'
task :shellinit => [:start] do
# NOTE: this does not set the env, so we have to do something different
# %x($(boot2docker shellinit > /dev/null 2>&1))
env = %x(boot2docker shellinit 2> /dev/null)
env.split(/\n/).each do |line|
env, setting = line.gsub(/^.* /, '').split(/\=/)
ENV[env] = setting
end
end
desc 'Gracefully shutdown the VM'
task :stop do
%x(boot2docker stop)
end
desc 'Install nsenter'
task :install_nsenter => [:shellinit] do
system("boot2docker ssh '[ -f /var/lib/boot2docker/nsenter ] || docker run --rm -v /var/lib/boot2docker/:/target jpetazzo/nsenter'")
end
desc 'SSH into the given container'
task :ssh, [:container] => :install_nsenter do |t, args|
container_hash = %x(docker ps | grep #{args[:container]} | awk '{ print $1 }')
puts "SSH into container: #{container_hash}"
exec "boot2docker ssh -t sudo /var/lib/boot2docker/docker-enter #{container_hash}"
end
end
namespace :docker do
namespace :maintenance do
desc 'Stop running containers'
task :stop, [:container] => 'boot2docker:shellinit' do |t, args|
# rake docker:maintenance:stop[foo]
if args[:container]
puts "Stopping #{args[:container]}"
%x(docker ps -a | grep #{args[:container]} | awk '{print $1}' | xargs docker stop)
else
puts "Stopping ALL"
%x(docker stop $(docker ps -aq))
end
end
desc 'Remove one or more containers'
task :rm, [:container] => [:stop] do |t, args|
if args[:container]
puts "Removing #{args[:container]}"
%x(docker ps -a | grep #{args[:container]} | awk '{print $1}' | xargs docker rm)
else
puts "Removing ALL"
%x(docker rm $(docker ps -aq))
end
end
desc 'Remove one or more images'
task :rmi, [:container] => [:stop, :rm] do |t, args|
%x(docker rmi $(docker images -a | grep "^<none>" | awk '{print $3}') > /dev/null 2>&1)
end
end
namespace :fig do
desc 'Use fig to build the Dockerfile image'
task :build => ['boot2docker:shellinit'] do
ENV['FIG_PROJECT_NAME'] = File.dirname(__FILE__).split(/\//).last.downcase
system "fig build"
end
desc 'Use fig to run the app as described in fig.yml'
# TODO: clean out old images
task :up => :build do
system "fig up"
end
end
namespace :info do
desc 'List all the container (docker ps -a)'
task :ps => 'boot2docker:shellinit' do
system("docker ps -a")
end
desc 'List all the images on localhost (docker images)'
task :images => 'boot2docker:shellinit' do
system("docker images")
end
# rake docker:hostname[rails-basic-app,3000]
# open $(rake docker:info:hostname[railsbasic_web_1,3000])
desc 'Give the hostname and port for a container'
task :hostname, [:container, :port] => 'boot2docker:shellinit' do |t, args|
require 'json'
docker_host = ENV['DOCKER_HOST'].match(/\/(\d*.\d*.\d*.\d*):/).to_a.last
json = JSON.parse(%x(docker inspect #{args[:container]})).first
if json["NetworkSettings"]["Ports"]
port = args[:port]
host_port = json["NetworkSettings"]["Ports"]["#{port}/tcp"].first["HostPort"]
puts "http://#{docker_host}:#{host_port}"
else
puts "No docker container instance found for #{args[:container]}"
end
end
end
end
namespace :git do
desc 'Stash files'
task :stash do
%x(git stash save)
end
desc 'Pull the freshest code'
task :pull, [:remote, :branch] do |t, args|
# NOTE: when used as a dependency this is pretty sloppy
remote = args[:remote] || 'origin'
branch = args[:branch] || 'master'
%x(git pull --rebase #{remote} #{branch})
end
end
task :default => 'docker:fig:up'
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment