Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed May 10, 2015
0 parents commit 0518b0b
Show file tree
Hide file tree
Showing 22 changed files with 353 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
Gemfile.lock
coverage
.ruby-version
*.sublime-*
*.gem
pkg
.vagrant
10 changes: 10 additions & 0 deletions Gemfile
@@ -0,0 +1,10 @@
source "https://rubygems.org"
gemspec

group :development do
gem "vagrant", git: "https://github.com/mitchellh/vagrant.git", tag: "v1.7.2"
end

group :plugins do
gem "vagrant-docker-compose", path: "."
end
5 changes: 5 additions & 0 deletions LICENSE
@@ -0,0 +1,5 @@
Copyright (c) 2015, Leigh McCulloch

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
62 changes: 62 additions & 0 deletions README.md
@@ -0,0 +1,62 @@
# Vagrant Provisioner: Docker Compose

A Vagrant provisioner for [Docker Compose](https://docs.docker.com/compose/). Installs Docker Compose and can also bring up the containers defined by a [docker-compose.yml](https://docs.docker.com/compose/yml/).

## Install

```bash
vagrant plugin install vagrant-docker-compose
```

## Usage

### To install docker and docker-compose

```ruby
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"

config.vm.provision :docker
config.vm.provision :docker_compose
end
```

### To install and run docker-compose on `vagrant up`

```ruby
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"

config.vm.provision :docker
config.vm.provision :docker_compose, yml: "/vagrant/docker-compose.yml", run: "always"
end
```

Equivalent to running:

```bash
docker-compose -f [yml] up
```

### To install, rebuild and run docker-compose on `vagrant up`

```ruby
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"

config.vm.provision :docker
config.vm.provision :docker_compose, yml: "/vagrant/docker-compose.yml", rebuild: true, run: "always"
end
```

Equivalent to running:

```bash
docker-compose -f [yml] rm
docker-compose -f [yml] build
docker-compose -f [yml] up
```

## Example

See `example` in the repository for a full working example.
3 changes: 3 additions & 0 deletions Rakefile
@@ -0,0 +1,3 @@
require "rubygems"
require "bundler/setup"
Bundler::GemHelper.install_tasks
11 changes: 11 additions & 0 deletions example/Vagrantfile
@@ -0,0 +1,11 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"

config.vm.network(:forwarded_port, guest: 8080, host: 8080)

config.vm.provision :docker
config.vm.provision :docker_compose, yml: "/vagrant/docker-compose.yml", rebuild: true, run: "always"
end
3 changes: 3 additions & 0 deletions example/app/Dockerfile
@@ -0,0 +1,3 @@
FROM ubuntu:14.04
WORKDIR /app
CMD while true ; do nc -l 8080 < /app/index.html ; done
9 changes: 9 additions & 0 deletions example/app/index.html
@@ -0,0 +1,9 @@
<html>
<head>
<title>An App</title>
</head>
<body>
<h1>An App</h1>
<p>An app that serves static.</p>
</body>
</html>
1 change: 1 addition & 0 deletions example/app/init.sh
@@ -0,0 +1 @@
while true ; do nc -l 8080 < /app/index.html ; done
10 changes: 10 additions & 0 deletions example/docker-compose.yml
@@ -0,0 +1,10 @@
app:
build: ./app
links:
- redis
ports:
- "8080:8080"
volumes:
- ./app/:/app/
redis:
image: redis
11 changes: 11 additions & 0 deletions lib/vagrant-docker-compose.rb
@@ -0,0 +1,11 @@
require "pathname"

require "vagrant-docker-compose/plugin"

module VagrantPlugins
module DockerComposeProvisioner
def self.source_root
@source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
end
end
end
16 changes: 16 additions & 0 deletions lib/vagrant-docker-compose/cap/linux/docker_compose_install.rb
@@ -0,0 +1,16 @@
module VagrantPlugins
module DockerComposeProvisioner
module Cap
module Linux
module DockerComposeInstall
def self.docker_compose_install(machine)
machine.communicate.tap do |comm|
comm.sudo("curl -L https://github.com/docker/compose/releases/download/1.2.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose")
end
end
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/vagrant-docker-compose/cap/linux/docker_compose_installed.rb
@@ -0,0 +1,19 @@
module VagrantPlugins
module DockerComposeProvisioner
module Cap
module Linux
module DockerComposeInstalled
def self.docker_compose_installed(machine)
paths = [
"/usr/local/bin/docker-compose"
]

paths.all? do |p|
machine.communicate.test("test -f #{p}", sudo: true)
end
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/vagrant-docker-compose/config.rb
@@ -0,0 +1,12 @@
module VagrantPlugins
module DockerComposeProvisioner
class Config < Vagrant.plugin("2", :config)
attr_accessor :yml, :rebuild

def yml=(yml)
raise DockerComposeError, :yml_must_be_absolute if !Pathname.new(yml).absolute?
@yml = yml
end
end
end
end
50 changes: 50 additions & 0 deletions lib/vagrant-docker-compose/docker_compose.rb
@@ -0,0 +1,50 @@
require "pathname"

module VagrantPlugins
module DockerComposeProvisioner
class DockerCompose
def initialize(machine, config)
@machine = machine
@config = config
end

def build
@machine.ui.detail(I18n.t(:docker_compose_build))
@machine.communicate.tap do |comm|
comm.sudo("docker-compose -f \"#{@config.yml}\" build") do |type, data|
handle_comm(type, data)
end
end
end

def rm
@machine.ui.detail(I18n.t(:docker_compose_rm))
@machine.communicate.tap do |comm|
comm.sudo("docker-compose -f \"#{@config.yml}\" rm --force") do |type, data|
handle_comm(type, data)
end
end
end

def up
@machine.ui.detail(I18n.t(:docker_compose_up))
@machine.communicate.tap do |comm|
comm.sudo("docker-compose -f \"#{@config.yml}\" up -d") do |type, data|
handle_comm(type, data)
end
end
end

protected

def handle_comm(type, data)
data.chomp!
return if data.empty?
case type
when :stdout; @machine.ui.detail(data)
when :stderr; @machine.ui.error(data)
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/vagrant-docker-compose/errors/docker_compose_error.rb
@@ -0,0 +1,7 @@
module VagrantPlugins
module DockerComposeProvisioner
class DockerComposeError < Vagrant::Errors::VagrantError
error_namespace("errors")
end
end
end
22 changes: 22 additions & 0 deletions lib/vagrant-docker-compose/installer.rb
@@ -0,0 +1,22 @@
module VagrantPlugins
module DockerComposeProvisioner
class Installer
def initialize(machine)
@machine = machine
end

def ensure_installed
@machine.ui.detail(I18n.t(:checking_installation))

if !@machine.guest.capability(:docker_compose_installed)
@machine.ui.detail(I18n.t(:installing))
@machine.guest.capability(:docker_compose_install)

if !@machine.guest.capability(:docker_compose_installed)
raise DockerComposeError, :install_failed
end
end
end
end
end
end
9 changes: 9 additions & 0 deletions lib/vagrant-docker-compose/locales/en.yml
@@ -0,0 +1,9 @@
en:
errors:
yml_must_be_absolute: Docker Compose YML path must be absolute!
not_supported_on_guest: Not supported on the guest operating system
checking_installation: Checking for Docker Compose installation...
installing: Installing Docker Compose
docker_compose_up: Running docker-compose up...
docker_compose_rm: Running docker-compose rm...
docker_compose_build: Running docker-compose build...
33 changes: 33 additions & 0 deletions lib/vagrant-docker-compose/plugin.rb
@@ -0,0 +1,33 @@
module VagrantPlugins
module DockerComposeProvisioner
class Plugin < Vagrant.plugin("2")
name "docker-compose-provisioner"
description <<-DESC
Provides support for provisioning your virtual machines with Docker-Compose.
DESC

I18n.load_path << File.expand_path("../locales/en.yml", __FILE__)
I18n.reload!

config(:docker_compose, :provisioner) do
require_relative "config"
Config
end

guest_capability("linux", "docker_compose_installed") do
require_relative "cap/linux/docker_compose_installed"
Cap::Linux::DockerComposeInstalled
end

guest_capability("linux", "docker_compose_install") do
require_relative "cap/linux/docker_compose_install"
Cap::Linux::DockerComposeInstall
end

provisioner(:docker_compose) do
require_relative "provisioner"
Provisioner
end
end
end
end
29 changes: 29 additions & 0 deletions lib/vagrant-docker-compose/provisioner.rb
@@ -0,0 +1,29 @@
require_relative "errors/docker_compose_error"
require_relative "installer"
require_relative "docker_compose"

module VagrantPlugins
module DockerComposeProvisioner
class Provisioner < Vagrant.plugin("2", :provisioner)
def initialize(machine, config, installer = nil, docker_compose = nil)
super(machine, config)

@installer = installer || Installer.new(@machine)
@docker_compose = docker_compose || DockerCompose.new(@machine, @config)
end

def provision
@installer.ensure_installed

return unless @config.yml

if @config.rebuild
@docker_compose.rm
@docker_compose.build
end

@docker_compose.up
end
end
end
end
5 changes: 5 additions & 0 deletions lib/vagrant-docker-compose/version.rb
@@ -0,0 +1,5 @@
module VagrantPlugins
module DockerComposeProvisioner
VERSION = "0.0.1"
end
end
19 changes: 19 additions & 0 deletions vagrant-docker-compose.gemspec
@@ -0,0 +1,19 @@
# -*- encoding: utf-8 -*-

lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require "vagrant-docker-compose/version"

Gem::Specification.new do |s|
s.name = "vagrant-docker-compose"
s.version = VagrantPlugins::DockerComposeProvisioner::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Leigh McCulloch"]
s.homepage = "https://github.com/leighmcculloch/vagrant-docker-compose"
s.summary = %q{A Vagrant provisioner for docker compose.}
s.description = %q{A Vagrant provisioner for docker compose.}

s.files = `git ls-files -z`.split("\0")
s.require_path = "lib"
end

2 comments on commit 0518b0b

@leafac
Copy link

@leafac leafac commented on 0518b0b May 25, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leighmcculloch this is exactly what I was looking for and it works great. Thanks for the good job! 👍

@leighmcculloch
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries @leafac 😄

Please sign in to comment.