Class: Front::CLI::Vagrant

Inherits:
Object
  • Object
show all
Defined in:
lib/front/cli/vagrant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, path, script) ⇒ Vagrant

Returns a new instance of Vagrant.



9
10
11
12
13
14
# File 'lib/front/cli/vagrant.rb', line 9

def initialize(id, path, script)
  @id = id
  @path = path
  @wait = true
  @script = script
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/front/cli/vagrant.rb', line 4

def id
  @id
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/front/cli/vagrant.rb', line 5

def path
  @path
end

#scriptObject

Returns the value of attribute script.



7
8
9
# File 'lib/front/cli/vagrant.rb', line 7

def script
  @script
end

#waitObject

Returns the value of attribute wait.



6
7
8
# File 'lib/front/cli/vagrant.rb', line 6

def wait
  @wait
end

Instance Method Details

#capture(cmd) ⇒ Object



74
75
76
77
78
# File 'lib/front/cli/vagrant.rb', line 74

def capture(cmd)
  Dir.chdir(path) do
    `vagrant #{cmd}`
  end
end

#destroyObject



20
21
22
# File 'lib/front/cli/vagrant.rb', line 20

def destroy
  run('destroy -f')
end

#get_log_fileObject



56
57
58
# File 'lib/front/cli/vagrant.rb', line 56

def get_log_file
  "#{path}/front.log"
end

#reloadObject



24
25
26
# File 'lib/front/cli/vagrant.rb', line 24

def reload
  run('reload')
end

#run(cmd) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/front/cli/vagrant.rb', line 60

def run(cmd)
  cmd = "vagrant #{cmd}"
  options = {}
  options[:chdir] = path

  if wait
    pid = Kernel.spawn(cmd, options)
    Process.wait pid
  else
    cmd = "#{cmd} &>> #{get_log_file()} "
    script.enqueue "cd #{path} && #{cmd}"
  end
end

#sshObject



28
29
30
# File 'lib/front/cli/vagrant.rb', line 28

def ssh
  run('ssh')
end

#ssh_configObject



32
33
34
# File 'lib/front/cli/vagrant.rb', line 32

def ssh_config
  capture('ssh-config')
end

#ssh_portObject



36
37
38
39
40
41
42
43
# File 'lib/front/cli/vagrant.rb', line 36

def ssh_port
  output = ssh_config()
  re = /^\s*Port\s*(\d+)$/m

  matches = output.match(re)
  return matches[1] unless matches.nil?
  return '2222'
end

#statusObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/front/cli/vagrant.rb', line 45

def status
  output = capture('status')
  re = /^(\w+\s+\w+ \(\w+\))/m
  matches = output.match(re)
  if matches
    return matches[1]
  else
    'pending'
  end
end

#upObject



16
17
18
# File 'lib/front/cli/vagrant.rb', line 16

def up
  run('up')
end