Class: Servitor::VagrantBox

Inherits:
Object
  • Object
show all
Includes:
ChildProcessHelper
Defined in:
lib/provisioners/vagrant_box.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChildProcessHelper

#execute_child_process, #execute_child_process_and_capture_output, included

Constructor Details

#initialize(name, path = nil) ⇒ VagrantBox

Returns a new instance of VagrantBox.



44
45
46
47
# File 'lib/provisioners/vagrant_box.rb', line 44

def initialize(name, path=nil)
  @name = name
  @path = path || File.join(Servitor.boxes_root, @name)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



42
43
44
# File 'lib/provisioners/vagrant_box.rb', line 42

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



42
43
44
# File 'lib/provisioners/vagrant_box.rb', line 42

def path
  @path
end

Class Method Details

.add(box_name, box_file) ⇒ Object



18
19
20
# File 'lib/provisioners/vagrant_box.rb', line 18

def add(box_name, box_file)
  execute_child_process('vagrant', 'box', 'add', box_name, box_file)
end

.box_file_for(name) ⇒ Object



33
34
35
# File 'lib/provisioners/vagrant_box.rb', line 33

def box_file_for(name)
  File.join(Servitor.boxes_root, "#{name}.box")
end

.box_name_for(requirements) ⇒ Object



37
38
39
# File 'lib/provisioners/vagrant_box.rb', line 37

def box_name_for(requirements)
  "servitor-#{requirements.consistent_hash.to_s(32)}"
end

.define(requirements) {|box| ... } ⇒ Object

Yields:

  • (box)


22
23
24
25
26
27
28
29
30
31
# File 'lib/provisioners/vagrant_box.rb', line 22

def define(requirements)
  name = box_name_for(requirements)
  return name if exists?(name)
  box = VagrantBoxProvisioner.new(requirements).provision
  yield box
  box_file = box.package
  box.destroy
  self.add(name, box_file)
  name
end

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/provisioners/vagrant_box.rb', line 10

def exists?(name)
  !!self.vagrant.boxes.find(name)
end

.vagrantObject



14
15
16
# File 'lib/provisioners/vagrant_box.rb', line 14

def vagrant
  @vagrant ||= Vagrant::Environment.new
end

Instance Method Details

#destroyObject



67
68
69
70
71
# File 'lib/provisioners/vagrant_box.rb', line 67

def destroy
  box_dir do
    execute_child_process('vagrant', 'destroy', '--force')
  end
end

#haltObject



61
62
63
64
65
# File 'lib/provisioners/vagrant_box.rb', line 61

def halt
  box_dir do
    execute_child_process('vagrant', 'halt')
  end
end

#init(base_box) ⇒ Object



49
50
51
52
53
# File 'lib/provisioners/vagrant_box.rb', line 49

def init(base_box)
  box_dir do
    execute_child_process('vagrant', 'init', base_box)
  end
end

#packageObject



73
74
75
76
77
78
79
# File 'lib/provisioners/vagrant_box.rb', line 73

def package
  box_dir do
    box_file = self.class.box_file_for(@name)
    execute_child_process('vagrant', 'package', '--output', box_file)
    return box_file
  end
end

#provisionObject



101
102
103
104
105
# File 'lib/provisioners/vagrant_box.rb', line 101

def provision
  box_dir do
    execute_child_process('vagrant', 'provision')
  end
end

#ruby(script, options = {}) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/provisioners/vagrant_box.rb', line 93

def ruby(script, options={})
  escaped = script.gsub('"', '\"')
  lines = escaped.split("\n")
  cmd = "ruby #{lines.map {|l| "-e \"#{l}\""}.join(' ') }"
  #puts "cmd = "+ cmd
  ssh(cmd, options)
end

#ssh(command, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/provisioners/vagrant_box.rb', line 81

def ssh(command, options={})
  box_dir do
    sudo = options[:sudo] ? 'sudo ' : ''
    args = ['vagrant', 'ssh', options[:vm_name], '-c', "#{sudo}#{command}"].compact
    if options[:capture]
      execute_child_process_and_capture_output(*args)
    else
      execute_child_process(*args)
    end
  end
end

#upObject



55
56
57
58
59
# File 'lib/provisioners/vagrant_box.rb', line 55

def up
  box_dir do
    execute_child_process('vagrant', 'up', '--no-provision')
  end
end