Class: Vagrant_Rbapi
- Inherits:
-
Object
- Object
- Vagrant_Rbapi
- Defined in:
- lib/vagrant_rbapi.rb
Instance Method Summary collapse
- #destroy ⇒ Object
- #halt ⇒ Object
-
#initialize(vagrant_environment) ⇒ Vagrant_Rbapi
constructor
A new instance of Vagrant_Rbapi.
- #scp(direction, recursive, source, destination) ⇒ Object
- #ssh(cmd) ⇒ Object
- #ssh_config ⇒ Object
- #status ⇒ Object
- #up(provider = 'virtualbox') ⇒ Object
- #vagrant_bin ⇒ Object
- #vagrant_cmd(cmd) ⇒ Object
Constructor Details
#initialize(vagrant_environment) ⇒ Vagrant_Rbapi
Returns a new instance of Vagrant_Rbapi.
9 10 11 |
# File 'lib/vagrant_rbapi.rb', line 9 def initialize(vagrant_environment) Dir.chdir(vagrant_environment) end |
Instance Method Details
#destroy ⇒ Object
49 50 51 52 |
# File 'lib/vagrant_rbapi.rb', line 49 def destroy raise VagrantRbapi::BoxNotCreated if self.status == 'not created' vagrant_cmd(['destroy', '--force']) end |
#halt ⇒ Object
44 45 46 47 |
# File 'lib/vagrant_rbapi.rb', line 44 def halt raise VagrantRbapi::BoxNotRunning if self.status != 'running' vagrant_cmd(['halt', '--force']) end |
#scp(direction, recursive, source, destination) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/vagrant_rbapi.rb', line 74 def scp(direction, recursive, source, destination) raise VagrantRbapi::BoxNotRunning if self.status != 'running' config = ssh_config if direction == :upload Net::SCP.start(config[0], config[1], port: config[2], key_data: [File.read(config[3])]) do |scp| scp.upload!(source, destination, recursive: recursive) end elsif direction == :download Net::SCP.start(config[0], config[1], port: config[2], key_data: [File.read(config[3])]) do |scp| scp.download!(source, destination, recursive: recursive) end end end |
#ssh(cmd) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/vagrant_rbapi.rb', line 64 def ssh(cmd) raise VagrantRbapi::BoxNotRunning if self.status != 'running' config = ssh_config out = Net::SSH.start(config[0], config[1], port: config[2], key_data: [File.read(config[3])]) do |ssh| ssh.exec!(cmd) end out.strip! unless out.nil? return out end |
#ssh_config ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/vagrant_rbapi.rb', line 54 def ssh_config raise VagrantRbapi::BoxNotRunning if self.status != 'running' out = vagrant_cmd(['ssh-config']) hostname = out[/HostName (.*)$/, 1].strip user = out[/User (.*)$/, 1].strip port = out[/Port (.*)$/, 1].strip identityfile = out[/IdentityFile (.*)$/, 1].strip return hostname, user, port, identityfile end |
#status ⇒ Object
33 34 35 36 37 |
# File 'lib/vagrant_rbapi.rb', line 33 def status out = vagrant_cmd(['status']) status = out[/default(.*)\(/, 1].strip return status end |
#up(provider = 'virtualbox') ⇒ Object
39 40 41 42 |
# File 'lib/vagrant_rbapi.rb', line 39 def up(provider = 'virtualbox') raise VagrantRbapi::BoxAlreadyRunning if self.status == 'running' vagrant_cmd(['up', "--provider=#{provider}"]) end |
#vagrant_bin ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/vagrant_rbapi.rb', line 13 def vagrant_bin ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir| if File.executable?(File.join(dir, 'vagrant')) return File.join(dir, 'vagrant') end end end |
#vagrant_cmd(cmd) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/vagrant_rbapi.rb', line 21 def vagrant_cmd(cmd) cmd = cmd.unshift(vagrant_bin).join(' ') out, err, val = '', '', '' Open3.popen3(ENV, cmd) do |stdin, stdout, stderr, wait_thr| out = stdout.read.to_s err = stderr.read.to_s val = wait_thr.value.to_s.split.last.to_i end raise VagrantRbapi::CommandReturnedNonZero unless val == 0 return out end |