Class: Front::CLI::VagrantPool

Inherits:
Object
  • Object
show all
Includes:
Loader
Defined in:
lib/front/cli/vagrant_pool.rb

Constant Summary

Constants included from Loader

Loader::LIB_DIR, Loader::ROOT_DIR

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ VagrantPool

Returns a new instance of VagrantPool.



10
11
12
13
# File 'lib/front/cli/vagrant_pool.rb', line 10

def initialize(size)
  @size = size
  @script = Script.new(get_script_path())
end

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/front/cli/vagrant_pool.rb', line 15

def create
  front_path = get_front_path()
  FileUtils.mkdir_p(front_path)

  get_pool_size().times do |index|
    instance_id = index + 1
    instance_path = get_instance_path(instance_id)

    FileUtils.mkdir_p(instance_path)
    FileUtils.cp get_vagrant_file(), instance_path
  end

  create_front_file
end

#create_front_fileObject



173
174
175
176
177
178
179
180
# File 'lib/front/cli/vagrant_pool.rb', line 173

def create_front_file
  defaults = {}
  defaults['current_id'] = 1
  defaults['pool_size'] = @size

  front_file = get_front_file()
  front_file.create(defaults)
end

#get_current_idObject



137
138
139
# File 'lib/front/cli/vagrant_pool.rb', line 137

def get_current_id
  get_front_file().get_current_id()
end

#get_front_fileObject

helpers



162
163
164
165
166
167
168
169
170
171
# File 'lib/front/cli/vagrant_pool.rb', line 162

def get_front_file
  if @front_file.nil?
    @front_file = Frontfile.new(get_frontfile_path())
    if @front_file.exists?
      @front_file.load()
    end
  end

  @front_file
end

#get_front_pathObject



112
113
114
# File 'lib/front/cli/vagrant_pool.rb', line 112

def get_front_path
  "#{Dir.pwd}/.front"
end

#get_frontfile_pathObject



133
134
135
# File 'lib/front/cli/vagrant_pool.rb', line 133

def get_frontfile_path
  "#{get_front_path()}/Frontfile"
end

#get_instance_path(id) ⇒ Object



120
121
122
# File 'lib/front/cli/vagrant_pool.rb', line 120

def get_instance_path(id)
  "#{get_front_path()}/#{id}"
end

#get_inventory_fileObject



108
109
110
# File 'lib/front/cli/vagrant_pool.rb', line 108

def get_inventory_file
  "#{get_front_path()}/inventory.ini"
end

#get_next_idObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/front/cli/vagrant_pool.rb', line 141

def get_next_id
  current_id = get_current_id()
  if current_id + 1 > get_pool_size()
    next_id = 1
  else
    next_id = current_id + 1
  end

  next_id
end

#get_pool_sizeObject



152
153
154
155
156
157
158
159
# File 'lib/front/cli/vagrant_pool.rb', line 152

def get_pool_size
  front_file = get_front_file()
  if front_file.exists?
    front_file.get_pool_size()
  else
    @size
  end
end

#get_script_pathObject



116
117
118
# File 'lib/front/cli/vagrant_pool.rb', line 116

def get_script_path
  "#{get_front_path()}/pending.sh"
end

#get_vagrant(instance_id) ⇒ Object



103
104
105
106
# File 'lib/front/cli/vagrant_pool.rb', line 103

def get_vagrant(instance_id)
  instance_path = get_instance_path(instance_id)
  Vagrant.new(instance_id, instance_path, @script)
end

#get_vagrant_fileObject



124
125
126
127
128
129
130
131
# File 'lib/front/cli/vagrant_pool.rb', line 124

def get_vagrant_file
  custom_path = "#{Dir.pwd}/Vagrantfile"
  if File.exists?(custom_path)
    return custom_path
  else
    return "#{ROOT_DIR}/Vagrantfile"
  end
end

#loadObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/front/cli/vagrant_pool.rb', line 30

def load
  loaded = false
  first_vagrant = nil
  get_pool_size().times do |index|
    vagrant = get_vagrant(index + 1)
    vagrant.wait = !loaded
    vagrant.up

    unless loaded
      first_vagrant = vagrant
    end

    loaded = true
  end

  update_inventory(first_vagrant)
  @script.run
end

#nextObject



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

def next
  current_id = get_current_id()
  vagrant = get_vagrant(current_id)
  vagrant.wait = false
  vagrant.destroy
  vagrant.up

  next_id = get_next_id()
  vagrant = get_vagrant(next_id)
  puts "Switched to instance \##{next_id}"

  save_front_file(next_id)
  update_inventory(vagrant)
  @script.run()

  next_id
end

#save_front_file(current_id) ⇒ Object



182
183
184
185
186
# File 'lib/front/cli/vagrant_pool.rb', line 182

def save_front_file(current_id)
  front_file = get_front_file()
  front_file.set_current_id(current_id)
  front_file.save()
end

#sshObject



78
79
80
81
# File 'lib/front/cli/vagrant_pool.rb', line 78

def ssh
  vagrant = get_vagrant(get_current_id())
  vagrant.ssh
end

#ssh_configObject



83
84
85
86
# File 'lib/front/cli/vagrant_pool.rb', line 83

def ssh_config
  vagrant = get_vagrant(get_current_id())
  vagrant.ssh_config
end

#statusObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/front/cli/vagrant_pool.rb', line 88

def status
  current_id = get_current_id()
  get_pool_size().times do |index|
    instance_id = index + 1
    if instance_id == current_id
      instance_label = "\##{instance_id}*"
    else
      instance_label = "\##{instance_id} "
    end

    vagrant = get_vagrant(instance_id)
    puts "Instance #{instance_label}: #{vagrant.status}"
  end
end

#unloadObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/front/cli/vagrant_pool.rb', line 49

def unload
  get_pool_size().times do |index|
    instance_id = index + 1
    vagrant = get_vagrant(instance_id)
    puts "Destroying instance \##{instance_id}"
    vagrant.destroy
  end

  FileUtils.rm_rf(get_front_path())
end

#update_inventory(vagrant) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/front/cli/vagrant_pool.rb', line 188

def update_inventory(vagrant)
  ip_address = "127.0.0.#{get_current_id()}"
  port = vagrant.ssh_port()

  item = "#{ip_address}:#{port}"
  File.open(get_inventory_file(), 'w') do |file|
    file.write(item)
  end
end