Class: Vigil::VMBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vigil/vmbuilder.rb

Instance Method Summary collapse

Constructor Details

#initialize(revision) ⇒ VMBuilder

Returns a new instance of VMBuilder.



4
5
6
7
8
9
10
# File 'lib/vigil/vmbuilder.rb', line 4

def initialize(revision)
  @x = Vigil.os
  @plugman = Vigil.plugman
  @revision = revision
  @previous_revision = @revision.previous
  @rebuild = false
end

Instance Method Details

#_build_baseboxObject



42
43
44
45
46
47
48
49
# File 'lib/vigil/vmbuilder.rb', line 42

def _build_basebox
  _setup_iso_cache
  Vagrant.run "basebox build --force --nogui '#{@revision.project_name}'"
  Vagrant.run "basebox validate '#{@revision.project_name}'"
  Vagrant.run "basebox export '#{@revision.project_name}'"
  @x._system "mv #{@revision.project_name}.box #{@revision.base_box_path}"
  Vagrant.run "basebox destroy #{@revision.project_name}"
end

#_build_complete_boxObject



83
84
85
86
87
88
89
90
91
# File 'lib/vigil/vmbuilder.rb', line 83

def _build_complete_box
  Vagrant.run "box add --force '#{@revision.no_gems_box_name}' '#{@revision.no_gems_box_path}'"
  Vagrant.use @revision.no_gems_box_name
  Vagrant.run "up"
  Vagrant.run "ssh -c 'sudo gem install bundler'"
  Vagrant.run "ssh -c 'cd /vagrant/; bundle install'"
  Vagrant.run "package --output #{@revision.complete_box_path}"
  Vagrant.run "box remove '#{@revision.no_gems_box_name}'"
end

#_build_no_gems_boxObject



66
67
68
69
70
71
72
# File 'lib/vigil/vmbuilder.rb', line 66

def _build_no_gems_box
  Vagrant.run "box add --force '#{@revision.base_box_name}' '#{@revision.base_box_path}'"
  Vagrant.use @revision.base_box_name
  Vagrant.run "up"
  Vagrant.run "package --output #{@revision.no_gems_box_path}"
  Vagrant.run "box remove #{@revision.base_box_name}"
end

#_build_vmObject



22
23
24
25
26
27
28
29
30
# File 'lib/vigil/vmbuilder.rb', line 22

def _build_vm
  _setup_basebox
  @plugman.notify(:task_done, 'VM1')
  _setup_no_gems_box
  @plugman.notify(:task_done, 'VM2')
  _setup_complete_box
  @plugman.notify(:task_done, 'VM3')
  @rebuild = false
end

#_changes_relative_to_previous_revision_in?(files) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/vigil/vmbuilder.rb', line 97

def _changes_relative_to_previous_revision_in?(files)
  Git.differs?(@previous_revision.sha, files)
end

#_setup_baseboxObject



32
33
34
35
36
37
38
39
40
# File 'lib/vigil/vmbuilder.rb', line 32

def _setup_basebox
  return if @x.exists? @revision.base_box_path
  if @x.exists?(@previous_revision.base_box_path) and !_changes_relative_to_previous_revision_in?('definitions')
    _use_old_box(:base_box_path)
  else
    _build_basebox
    @rebuild = true
  end
end

#_setup_complete_boxObject



74
75
76
77
78
79
80
81
# File 'lib/vigil/vmbuilder.rb', line 74

def _setup_complete_box
  if @rebuild or !@x.exists?(@previous_revision.complete_box_path) or
      _changes_relative_to_previous_revision_in?('Gemfile*')
    _build_complete_box
  else
    _use_old_box :complete_box_path
  end
end

#_setup_iso_cacheObject



51
52
53
# File 'lib/vigil/vmbuilder.rb', line 51

def _setup_iso_cache
  @x._system "ln -sf #{File.join(Vigil.run_dir, 'iso')}"
end

#_setup_no_gems_boxObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/vigil/vmbuilder.rb', line 55

def _setup_no_gems_box
  return if @x.exists?(@revision.no_gems_box_path)
  if @rebuild or !@x.exists?(@previous_revision.no_gems_box_path) or
      _changes_relative_to_previous_revision_in?('manifests')
    _build_no_gems_box
    @rebuild = true
  else
    _use_old_box :no_gems_box_path
  end
end

#_use_old_box(box) ⇒ Object



93
94
95
# File 'lib/vigil/vmbuilder.rb', line 93

def _use_old_box(box)
  @x.ln @previous_revision.send(box), @revision.send(box)
end

#runObject



12
13
14
15
16
17
18
19
20
# File 'lib/vigil/vmbuilder.rb', line 12

def run
  if @x.exists?(@revision.complete_box_path)
    @plugman.notify(:task_done, 'VM1')
    @plugman.notify(:task_done, 'VM2')
    @plugman.notify(:task_done, 'VM3')
    return
  end
  _build_vm
end