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



167
168
169
170
171
172
173
174
# File 'lib/front/cli/vagrant_pool.rb', line 167

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



131
132
133
# File 'lib/front/cli/vagrant_pool.rb', line 131

def get_current_id
  get_front_file().get_current_id()
end

#get_front_fileObject

helpers



156
157
158
159
160
161
162
163
164
165
# File 'lib/front/cli/vagrant_pool.rb', line 156

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



106
107
108
# File 'lib/front/cli/vagrant_pool.rb', line 106

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

#get_frontfile_pathObject



127
128
129
# File 'lib/front/cli/vagrant_pool.rb', line 127

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

#get_instance_path(id) ⇒ Object



114
115
116
# File 'lib/front/cli/vagrant_pool.rb', line 114

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

#get_inventory_fileObject



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

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

#get_next_idObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/front/cli/vagrant_pool.rb', line 135

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



146
147
148
149
150
151
152
153
# File 'lib/front/cli/vagrant_pool.rb', line 146

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



110
111
112
# File 'lib/front/cli/vagrant_pool.rb', line 110

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

#get_vagrant(instance_id) ⇒ Object



97
98
99
100
# File 'lib/front/cli/vagrant_pool.rb', line 97

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

#get_vagrant_fileObject



118
119
120
121
122
123
124
125
# File 'lib/front/cli/vagrant_pool.rb', line 118

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
# File 'lib/front/cli/vagrant_pool.rb', line 30

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

    loaded = true
  end

  @script.run
end

#nextObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/front/cli/vagrant_pool.rb', line 54

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



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

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

#sshObject



72
73
74
75
# File 'lib/front/cli/vagrant_pool.rb', line 72

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

#ssh_configObject



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

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

#statusObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/front/cli/vagrant_pool.rb', line 82

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



43
44
45
46
47
48
49
50
51
52
# File 'lib/front/cli/vagrant_pool.rb', line 43

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



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

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