Class: VagrantWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-wrapper.rb,
lib/vagrant-wrapper/version.rb,
lib/vagrant-wrapper/exceptions.rb

Overview

Author

BinaryBabel OSS (<[email protected]>)

Homepage

www.binarybabel.org

License

MIT

For bugs, docs, updates:

http://code.binbab.org

Copyright 2013 sha1(OWNER) = df334a7237f10846a0ca302bd323e35ee1463931

See LICENSE file for more details.

Defined Under Namespace

Classes: Exceptions

Constant Summary collapse

WRAPPER_MARK =

The string used to detect ourselves and a gem wrapper of ourselves

"vagrant-wrapper"
VERSION =
'2.0.3'
RELEASE_DATE =
'2015-08-04'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ VagrantWrapper

Returns a new instance of VagrantWrapper.



27
28
29
30
31
32
33
34
35
36
# File 'lib/vagrant-wrapper.rb', line 27

def initialize(*args)
  @vagrant_name = windows? ? "vagrant.exe" : "vagrant"
  @vagrant_path = nil
  @search_paths = default_paths + env_paths

  # Optional first parameter sets required version.
  unless args.length < 1 or args[0].nil?
    require_version args[0]
  end
end

Class Method Details

.install_instructionsObject



121
122
123
# File 'lib/vagrant-wrapper.rb', line 121

def self.install_instructions
  "See http://www.vagrantup.com for instructions.\n"
end

.require_or_help_install(version) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/vagrant-wrapper.rb', line 125

def self.require_or_help_install(version)
  begin
    vw = VagrantWrapper.new(version)
  rescue Exceptions::Version => e
    $stderr.print e.message + "\n"
    $stderr.print install_instructions
    exit(1)
  end
  vw
end

Instance Method Details

#default_pathsObject

Default paths to search for the packaged version of Vagrant.

/opt/vagrant/bin
/usr/local/bin
/usr/bin
/bin


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vagrant-wrapper.rb', line 93

def default_paths
  if windows?
    %w{
      C:\HashiCorp\Vagrant\bin
      /c/HashiCorp/Vagrant/bin
    }
  else
    %w{
      /opt/vagrant/bin
      /usr/local/bin
      /usr/bin
      /bin
    }
  end
end

#env_pathsObject

Environment search paths to be used as low priority search.



110
111
112
113
114
115
116
117
118
119
# File 'lib/vagrant-wrapper.rb', line 110

def env_paths
  path = ENV['PATH'].to_s.strip
  return [] if path.empty?
  separator = if windows?
                ';'
              else
                ':'
              end
  path.split(separator)
end

#execute(*args) ⇒ Object

Execute the discovered version of Vagrant. The given arguments (if any) are passed along to the command line.

The vagrant process will replace this process entirely, operating and outputting in an unmodified state.



66
67
68
69
70
71
72
# File 'lib/vagrant-wrapper.rb', line 66

def execute(*args)
  if args.length > 0 && args[0].is_a?(Array)
    send("exec_vagrant", *args[0])
  else
    send("exec_vagrant", *args)
  end
end

#get_output(*args) ⇒ Object

Call the discovered version of Vagrant. The given arguments (if any) are passed along to the command line.

The output will be returned.



53
54
55
56
57
58
59
# File 'lib/vagrant-wrapper.rb', line 53

def get_output(*args)
  if args.length > 0 && args[0].is_a?(Array)
    send("call_vagrant", *args[0])
  else
    send("call_vagrant", *args)
  end
end

#require_version(version) ⇒ Object

Require a specific version (or range of versions). Ex. “>= 1.1”



40
41
42
43
44
45
46
47
# File 'lib/vagrant-wrapper.rb', line 40

def require_version(version)
  version_req = Gem::Requirement.new(version)
  vagrant_ver = vagrant_version
  raise Exceptions::NotInstalled, "Vagrant is not installed." if vagrant_ver.nil?
  unless version_req.satisfied_by?(Gem::Version.new(vagrant_ver))
    raise Exceptions::Version, "Vagrant #{version} is required. You have #{vagrant_ver}."
  end
end

#vagrant_locationObject

Return the filesystem location of the discovered Vagrant install.



75
76
77
# File 'lib/vagrant-wrapper.rb', line 75

def vagrant_location
  find_vagrant
end

#vagrant_versionObject

Return the version of the discovered Vagrant install.



80
81
82
83
84
85
86
# File 'lib/vagrant-wrapper.rb', line 80

def vagrant_version
  ver = call_vagrant "-v"
  unless ver.nil?
    ver = ver[/Vagrant( version)? ([0-9]+(\.[0-9]+)+)/, 2]
  end
  ver
end