Class: Mason::Stacks

Inherits:
Object
  • Object
show all
Defined in:
lib/mason/stacks.rb

Class Method Summary collapse

Class Method Details

.build_vagrantfile(stacks = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mason/stacks.rb', line 94

def self.build_vagrantfile(stacks={})
  data = File.read(vagrantfile_template)
  ip_base = 3
  data.gsub! "BOXES", (stacks.map do |name, box|
    ip_base += 1
    <<-BOX
config.vm.define :#{name} do |config|
  config.vm.box = "#{box}"
  config.vm.base_mac = "080027706AA#{ip_base}"
  config.vm.network :hostonly, "33.33.33.#{ip_base}"
  config.vm.share_folder "share", "/share", "#{share_dir(name)}"
end
    BOX
  end.join("\n").chomp)
  File.open(vagrantfile, "w") do |file|
    file.puts data
  end
end

.create(name, box) ⇒ Object



37
38
39
40
41
# File 'lib/mason/stacks.rb', line 37

def self.create(name, box)
  raise "stack already exists: #{name}" if stacks.keys.include?(name.to_sym)
  raise "vagrant box does not exist: #{box}" unless vagrant_env.boxes.map(&:name).include?(box)
  build_vagrantfile(stacks.update(name => box))
end

.destroy(name) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/mason/stacks.rb', line 43

def self.destroy(name)
  raise "no such stack: #{name}" unless stacks.keys.include?(name.to_sym)
  vm = vms[name.to_sym]
  vm.halt rescue nil
  vm.destroy rescue nil
  s = stacks
  s.delete(name.to_sym)
  build_vagrantfile(s)
end

.down(name) ⇒ Object



67
68
69
70
71
# File 'lib/mason/stacks.rb', line 67

def self.down(name)
  raise "no such stack: #{name}" unless stacks.keys.include?(name.to_sym)
  return if state(name) == :down
  vms[name.to_sym].suspend
end

.load_vagrant!Object



6
7
8
9
10
11
12
# File 'lib/mason/stacks.rb', line 6

def self.load_vagrant!
  require "vagrant"
  if Gem::Version.new(Vagrant::VERSION) < Gem::Version.new("1.0.1")
    raise "mason requires vagrant 1.0.1 or higher"
  end
  build_vagrantfile unless File.exists?(vagrantfile)
end

.run(name, command) ⇒ Object



73
74
75
76
77
78
# File 'lib/mason/stacks.rb', line 73

def self.run(name, command)
  raise "no suck stack: #{name}" unless stacks.keys.include?(name.to_sym)
  vms[name.to_sym].channel.execute(command, :error_check => false) do |type, data|
    print data
  end
end

.share_dir(name) ⇒ Object



88
89
90
91
92
# File 'lib/mason/stacks.rb', line 88

def self.share_dir(name)
  dir = File.expand_path("~/.mason/share/#{name}")
  FileUtils.mkdir_p dir unless File.exists?(dir)
  dir
end

.stacksObject



28
29
30
31
32
33
34
35
# File 'lib/mason/stacks.rb', line 28

def self.stacks
  @stacks ||= begin
    vms.inject({}) do |hash, (name, vm)|
      next(hash) if name == :default
      hash.update(name => vm.box ? vm.box.name : "")
    end
  end
end

.state(name) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/mason/stacks.rb', line 53

def self.state(name)
  raise "no such stack: #{name}" unless stacks.keys.include?(name.to_sym)
  case vms[name.to_sym].state.to_sym
    when :running then :up
    else               :down
  end
end

.up(name) ⇒ Object



61
62
63
64
65
# File 'lib/mason/stacks.rb', line 61

def self.up(name)
  raise "no such stack: #{name}" unless stacks.keys.include?(name.to_sym)
  return if state(name) == :up
  vms[name.to_sym].up
end

.vagrant(args) ⇒ Object



24
25
26
# File 'lib/mason/stacks.rb', line 24

def self.vagrant(args)
  vagrant_env(true).cli(args)
end

.vagrant_env(display = false) ⇒ Object



14
15
16
17
18
# File 'lib/mason/stacks.rb', line 14

def self.vagrant_env(display=false)
  load_vagrant!
  ui = display ? Vagrant::UI::Basic : nil
  Vagrant::Environment.new(:vagrantfile_name => vagrantfile, :ui_class => ui)
end

.vagrantfileObject



80
81
82
# File 'lib/mason/stacks.rb', line 80

def self.vagrantfile
  File.expand_path("~/.mason/Vagrantfile")
end

.vagrantfile_templateObject



84
85
86
# File 'lib/mason/stacks.rb', line 84

def self.vagrantfile_template
  File.expand_path("../../../data/Vagrantfile.template", __FILE__)
end

.vmsObject



20
21
22
# File 'lib/mason/stacks.rb', line 20

def self.vms
  vagrant_env.vms
end