vagrant-dsl 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +118 -0
- data/Rakefile +1 -0
- data/lib/vagrant/dsl/version.rb +5 -0
- data/lib/vagrant/dsl.rb +123 -0
- data/vagrant-dsl.gemspec +21 -0
- metadata +69 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Erik Hollensbe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# Vagrant::DSL
|
2
|
+
|
3
|
+
Tools for programmatically driving vagrant in rake and in ruby scripts.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'vagrant-dsl'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install vagrant-dsl
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
vagrant-dsl provides several top-level primitives for use in normal scripts and
|
22
|
+
in Rakefiles. This provides a few advantages from an application speed
|
23
|
+
perspective over shelling out, and also provides us with a little more
|
24
|
+
flexibility configuring metadata about each VM.
|
25
|
+
|
26
|
+
Here's an example:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
#!ruby
|
30
|
+
|
31
|
+
# These two are equivalent, see below
|
32
|
+
vagrant_box 'list'
|
33
|
+
vagrant "box", "list"
|
34
|
+
|
35
|
+
#
|
36
|
+
# This brings up a box, executes ls on a ssh into that box, and destroys a box.
|
37
|
+
# All the vagrant chatter is presented as strings to stdout and stderr
|
38
|
+
# respectively (which is nice if you have, say, a test suite associated with
|
39
|
+
# this function)
|
40
|
+
|
41
|
+
status = nil
|
42
|
+
stdout, stderr = vagrant_capture do
|
43
|
+
if vagrant_up
|
44
|
+
status = vagrant_ssh %w[-c ls]
|
45
|
+
vagrant_destroy '-f'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# we only care about seeing output if we have a problem
|
50
|
+
unless status == 0
|
51
|
+
puts "There was an error! Here's the output from vagrant:"
|
52
|
+
puts "--- stdout ---"
|
53
|
+
puts stdout
|
54
|
+
puts "--- stderr ---"
|
55
|
+
puts stderr
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
In case it needs re-iterating, you can supply any one-level command as
|
60
|
+
`vagrant_$command`, which is just sugar for `vagrant($command, *args)`. These
|
61
|
+
are generated at require-time and will include any integrated Vagrant command
|
62
|
+
plugins you have installed.
|
63
|
+
|
64
|
+
|
65
|
+
## Integration
|
66
|
+
|
67
|
+
Vagrant::DSL also integrates with a few external dependencies:
|
68
|
+
|
69
|
+
* If Rake is already required (for example, through launching a Rakefile),
|
70
|
+
Vagrant::DSL will be available to task, namespace, etc.
|
71
|
+
* Vagrant::Prison is a way to sandbox vagrant runs. You can use it with the
|
72
|
+
`vagrant_prison` keyword like so:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
#!ruby
|
76
|
+
vagrant_prison do
|
77
|
+
configure do |config|
|
78
|
+
config.vm.box = "ubuntu"
|
79
|
+
|
80
|
+
config.vm.define :test, :primary => true do |test_config|
|
81
|
+
test_config.vm.network :hostonly, "192.168.33.10"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
stdout, stderr = vagrant_capture do
|
85
|
+
vagrant_up
|
86
|
+
vagrant_destroy '-f'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
Note that you can use all the base DSL methods inside `vagrant_prison`.
|
92
|
+
`vagrant_prison` takes three arguments supplied as key/value arguments:
|
93
|
+
|
94
|
+
* `:dir` is a directory name that will be created, populated, and destroyed if
|
95
|
+
`:auto_destroy` is set.
|
96
|
+
* If `:auto_destroy` is set to true, as soon as the prison expires (either
|
97
|
+
through termination of the script or garbage collection) vagrant will be
|
98
|
+
asked to destroy the VMs and the directory will be removed.
|
99
|
+
* If `:ui_class` is supplied, you can drive the Vagrant user interface with
|
100
|
+
custom UI classes.
|
101
|
+
|
102
|
+
The `configure` method is basically what you would normally put in your
|
103
|
+
`Vagrantfile`. If you don't like the block form or would rather use a corpus of
|
104
|
+
pre-generated content, it also accepts a string.
|
105
|
+
|
106
|
+
See the docs for
|
107
|
+
[Vagrant::Prison](http://rubydoc.info/gems/vagrant-prison/0.0.1/frames) for
|
108
|
+
more information.
|
109
|
+
|
110
|
+
If you wish to use Vagrant::DSL in your own classes/modules, just include it.
|
111
|
+
|
112
|
+
## Contributing
|
113
|
+
|
114
|
+
1. Fork it
|
115
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
116
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
117
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
118
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/vagrant/dsl.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'vagrant/dsl/version'
|
2
|
+
require 'vagrant/prison'
|
3
|
+
require 'stringio'
|
4
|
+
require 'forwardable'
|
5
|
+
|
6
|
+
module Vagrant
|
7
|
+
module UI
|
8
|
+
class Captured < Interface
|
9
|
+
include Util::SafePuts
|
10
|
+
|
11
|
+
attr_reader :stdout
|
12
|
+
attr_reader :stderr
|
13
|
+
|
14
|
+
def initialize(resource)
|
15
|
+
super
|
16
|
+
@stdout = StringIO.new("", 'w+')
|
17
|
+
@stderr = StringIO.new("", 'w+')
|
18
|
+
end
|
19
|
+
|
20
|
+
def ask(*args)
|
21
|
+
super
|
22
|
+
raise Errors::UIExpectsTTY
|
23
|
+
end
|
24
|
+
|
25
|
+
[:info, :warn, :error, :success].each do |method|
|
26
|
+
class_eval <<-EOF
|
27
|
+
def #{method}(message, *args)
|
28
|
+
super
|
29
|
+
say("#{method.to_s}", message, *args)
|
30
|
+
end
|
31
|
+
EOF
|
32
|
+
end
|
33
|
+
|
34
|
+
def say(type, message, opts=nil)
|
35
|
+
safe_puts(
|
36
|
+
message,
|
37
|
+
:io => type == :error ? @stderr : @stdout,
|
38
|
+
:printer => :puts
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module DSL
|
45
|
+
module GenericMixin
|
46
|
+
|
47
|
+
attr_writer :ui_class
|
48
|
+
attr_accessor :env
|
49
|
+
|
50
|
+
def construct_env
|
51
|
+
Vagrant::Environment.new(:ui_class => ui_class)
|
52
|
+
end
|
53
|
+
|
54
|
+
def ui_class
|
55
|
+
@ui_class || Vagrant::UI::Basic
|
56
|
+
end
|
57
|
+
|
58
|
+
def vagrant_capture
|
59
|
+
old_ui_class = ui_class
|
60
|
+
self.ui_class = Vagrant::UI::Captured
|
61
|
+
self.env = construct_env
|
62
|
+
yield
|
63
|
+
stdout = env.ui.stdout.string
|
64
|
+
stderr = env.ui.stderr.string
|
65
|
+
self.ui_class = old_ui_class
|
66
|
+
self.env = construct_env
|
67
|
+
|
68
|
+
return stdout, stderr
|
69
|
+
end
|
70
|
+
|
71
|
+
def vagrant(command, *args, &block)
|
72
|
+
self.env ||= construct_env
|
73
|
+
Vagrant.commands[command.to_sym].new(args, env).execute
|
74
|
+
rescue SystemExit => e # vagrant calls exit a lot
|
75
|
+
return e.status
|
76
|
+
end
|
77
|
+
|
78
|
+
# some sugar to represent the commands
|
79
|
+
Vagrant.commands.to_hash.keys.each do |key|
|
80
|
+
key = key.to_s.tr('-', '_')
|
81
|
+
class_eval <<-EOF
|
82
|
+
def vagrant_#{key}(*args, &block)
|
83
|
+
vagrant("#{key}", *args, &block)
|
84
|
+
end
|
85
|
+
EOF
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class Prison
|
90
|
+
extend Forwardable
|
91
|
+
include GenericMixin
|
92
|
+
|
93
|
+
def_delegator :@config, :configure, :configure
|
94
|
+
|
95
|
+
def initialize(args)
|
96
|
+
@config = Vagrant::Prison.new(args[:dir], args[:auto_destroy])
|
97
|
+
self.ui_class = args[:ui_class]
|
98
|
+
@env = nil
|
99
|
+
end
|
100
|
+
|
101
|
+
def construct_env
|
102
|
+
@config.construct(:ui_class => ui_class)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
include GenericMixin
|
107
|
+
|
108
|
+
def vagrant_prison(args={}, &block)
|
109
|
+
prison = Vagrant::DSL::Prison.new(args)
|
110
|
+
prison.instance_eval(&block)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
class << eval("self", TOPLEVEL_BINDING)
|
116
|
+
include Vagrant::DSL
|
117
|
+
end
|
118
|
+
|
119
|
+
if defined? Rake::DSL
|
120
|
+
module Rake::DSL
|
121
|
+
include Vagrant::DSL
|
122
|
+
end
|
123
|
+
end
|
data/vagrant-dsl.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vagrant/dsl/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "vagrant-dsl"
|
8
|
+
gem.version = Vagrant::DSL::VERSION
|
9
|
+
gem.authors = ["Erik Hollensbe"]
|
10
|
+
gem.email = ["erik+github@hollensbe.org"]
|
11
|
+
gem.description = %q{Tools for programmatically driving vagrant in rake and in ruby scripts}
|
12
|
+
gem.summary = %q{Tools for programmatically driving vagrant in rake and in ruby scripts}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'vagrant-prison', "~> 0.0.1"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vagrant-dsl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Erik Hollensbe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: vagrant-prison
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.0.1
|
30
|
+
description: Tools for programmatically driving vagrant in rake and in ruby scripts
|
31
|
+
email:
|
32
|
+
- erik+github@hollensbe.org
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/vagrant/dsl.rb
|
43
|
+
- lib/vagrant/dsl/version.rb
|
44
|
+
- vagrant-dsl.gemspec
|
45
|
+
homepage: ''
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Tools for programmatically driving vagrant in rake and in ruby scripts
|
69
|
+
test_files: []
|