class Kamaze::Project::Tools::Vagrant
Vagrant
based, this class provides a easy and ready to use wrapper around “vagrant“ executable. VM configuration is easyfied by the use of YAML files.
VM configurations are stored in a single directory (default: “vagrant“) and can be overriden by an “.override.yml“ file. A “Vagrantfile“ is generated upon configurations.
Sample use in Rake task:
“`ruby vagrant = project.tools.fetch(:vagrant)
if vagrant.boxes? and vagrant.executable?
file vagrant.vagrantfile => vagrant.sources do vagrant.install end
end “`
This class is almost a facade based on:
@see yaml.org/YAML_for_ruby.html @see friendsofvagrant.github.io/v1/docs/boxes.html
rubocop:disable Style/Documentation
Attributes
Get composer, responsible to read and combine files describing boxes
@return [Composer]
Absolute path to the vagrant executable
@return [String|nil]
Path to files describing boxes (directory)
defaults to “./vagrant“
@return [String]
Get a shell, to execute “vagrant“ commands
@return [Shell]
Template file (used for code generation)
@return [String]
Get path to actual “Vagrantfile“
@return [Pathname]
Get writer, reponsible of “Vagrantfile“ generation
@return [Writer]
Public Instance Methods
Denote “vagrant“ executable is present
@return [Boolean]
# File lib/kamaze/project/tools/vagrant.rb, line 83 def executable? shell.executable? end
Run the vagrant command with given “args“.
@see github.com/ruby/rake/blob/master/lib/rake/file_utils.rb
# File lib/kamaze/project/tools/vagrant.rb, line 90 def execute(*args, &block) shell.execute(*args, &block) end
Install a new Vagrantfile
Vagrant
file is installed with additionnal YAML file, defining “boxes“
@return [self]
# File lib/kamaze/project/tools/vagrant.rb, line 111 def install writer.write(boxes) self end
# File lib/kamaze/project/tools/vagrant.rb, line 117 def method_missing(method, *args, &block) if respond_to_missing?(method) composer.public_send(method, *args, &block) else super end end
# File lib/kamaze/project/tools/vagrant.rb, line 69 def mutable_attributes [:path, :executable, :template, :vagrantfile] end
Get working dir
@return [Pathname]
# File lib/kamaze/project/tools/vagrant.rb, line 76 def pwd Pathname.new(Dir.pwd).realpath end
# File lib/kamaze/project/tools/vagrant.rb, line 125 def respond_to_missing?(method, include_private = false) return true if composer.respond_to?(method, include_private) super end
Run a command remotely on box identified by “box_id“
Sample of use:
“`ruby vagrant.ssh('freebsd', 'rake clobber') “`
# File lib/kamaze/project/tools/vagrant.rb, line 101 def ssh(*args, &block) remote.execute(*args, &block) end
Protected Instance Methods
Get remote shell provider
@return [Remote]
# File lib/kamaze/project/tools/vagrant.rb, line 169 def remote Remote.new(boxes, executable: executable) end
# File lib/kamaze/project/tools/vagrant.rb, line 148 def setup @template ||= Pathname.new(__dir__).join('..', 'resources').to_s @vagrantfile ||= ::Pathname.new(@vagrantfile || pwd.join('Vagrantfile')) @executable ||= :vagrant @path ||= pwd.join('vagrant').to_s setup_compose end
Initialize most of the tools used internally
# File lib/kamaze/project/tools/vagrant.rb, line 158 def setup_compose @composer = Composer.new(@path) @shell = Shell.new(executable: executable) @writer = Writer.new(@template, vagrantfile) self end