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.



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

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.



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

def id
  @id
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#scriptObject

Returns the value of attribute script.



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

def script
  @script
end

#waitObject

Returns the value of attribute wait.



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

def wait
  @wait
end

Instance Method Details

#capture(cmd) ⇒ Object



92
93
94
95
96
# File 'lib/front/cli/vagrant.rb', line 92

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

#destroyObject



22
23
24
# File 'lib/front/cli/vagrant.rb', line 22

def destroy
  run('destroy -f')
end

#get_log_fileObject



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

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

#reloadObject



26
27
28
# File 'lib/front/cli/vagrant.rb', line 26

def reload
  run('reload')
end

#run(cmd) ⇒ Object



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

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

#run2(cmd) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/front/cli/vagrant.rb', line 75

def run2(cmd)
  options = {}
  options[:chdir] = path
  unless wait
    log_file = "#{path}/front.log"
    options[:out] = log_file
    options[:err] = log_file
  end

  pid = Kernel.spawn("vagrant #{cmd}", options)
  if wait
    Process.wait pid
  else
    Process.detach pid
  end
end

#sshObject



30
31
32
# File 'lib/front/cli/vagrant.rb', line 30

def ssh
  run('ssh')
end

#ssh_configObject



34
35
36
# File 'lib/front/cli/vagrant.rb', line 34

def ssh_config
  capture('ssh-config')
end

#ssh_portObject



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

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

  matches = output.match(re)
  return matches[1]
end

#statusObject



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

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



18
19
20
# File 'lib/front/cli/vagrant.rb', line 18

def up
  run('up')
end