Class: Pauper

Inherits:
Object
  • Object
show all
Defined in:
lib/pauper.rb,
lib/pauper/version.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

DEFAULT_PAUPERFILE =
'./Pauperfile'
DEFAULT_VM_PATH =
File.expand_path('/var/lib/lxc/')
SUPPORTED_RELEASES =
[:lucid, :precise]
VERSION =
'0.2.3'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ignore_network = false, pauperfile = nil) ⇒ Pauper

Returns a new instance of Pauper.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pauper.rb', line 25

def initialize(ignore_network = false, pauperfile = nil)
  pauperfile = DEFAULT_PAUPERFILE if pauperfile.nil?
  if root?
    puts "Don't run as root!"
    exit
  end

  @pauper_config = Pauper::Config.new(pauperfile)
  setup_network unless ignore_network
  check_for_new_version
end

Class Method Details

.grok_release(release_s) ⇒ Object



70
71
72
73
74
# File 'lib/pauper.rb', line 70

def self.grok_release(release_s)
  release = release_s.intern
  raise "Unsupported release '#{release_s}'" unless SUPPORTED_RELEASES.include?(release)
  release
end

.osx?Boolean

Returns:

  • (Boolean)


469
470
471
# File 'lib/pauper.rb', line 469

def self.osx?
  RUBY_PLATFORM.include? 'darwin'
end

.versionObject



66
67
68
# File 'lib/pauper.rb', line 66

def self.version
  puts "Pauper v#{VERSION}"
end

Instance Method Details

#bootstrap(release) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pauper.rb', line 95

def bootstrap(release)
  release = Pauper.grok_release(release)
  base_name = get_base_name(release)

  raise "Base '#{base_name}' already exists!" if vm_exists?(base_name)

  ip = get_base_ip(base_name)
  ssh_clean_known_hosts(base_name, ip)
  @pauper_config.config[:nodes].each { |n| ssh_clean_known_hosts(n.name, "#{@pauper_config.config[:subnet]}.#{n.config[:last_octet]}") }

  lxc_pauper_template
  system("sudo touch /var/lib/lxc/lxc.conf")
  system("sudo lxc-create -n #{base_name} -t pauper -f /var/lib/lxc/lxc.conf -- -a amd64 --auth-key #{public_ssh_key} -r #{release}")
  mac = generate_mac

  lxc = LXC.new(base_name)
  lxc.interface = @pauper_config.config[:bridge]
  lxc.mac = mac
  lxc.save

  prepare_fstab(base_name, release)
  prepare_vm_network(base_name,ip)
  prepare_base_cache(release)

  puts "Installing chef..."
  cmd "sudo chroot /var/lib/lxc/#{base_name}/rootfs/ bash -c 'wget #{@pauper_config.config[:chef_download_url][release]}/#{@pauper_config.config[:chef_deb_name][release]} && dpkg -i #{@pauper_config.config[:chef_deb_name][release]}; rm #{@pauper_config.config[:chef_deb_name][release]}'"

  start_node(base_name)
  sleep 3

  chef_node = "#{base_name}#{@pauper_config.config[:node_suffix]}"
  cmd "knife bootstrap --bootstrap-version chef-full -N #{chef_node} -E #{@pauper_config.config[:chef_environment]} -x root -r \"#{@pauper_config.config[:default_run_list].join(",")}\" #{ip}"

  stop_node(base_name)
end

#check_for_new_versionObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pauper.rb', line 37

def check_for_new_version
  begin
    settings = pauper_settings()
  rescue NoMethodError
    settings = {skip_version_check: false}
  end
  unless settings[:skip_version_check]
    current_version = Gem.loaded_specs['pauper'].version
    latest_version = Gem.latest_version_for('pauper')
    if current_version < latest_version
      puts
      puts "***************************************************************"
      puts "It's your lucky day! There's a new version of Pauper available."
      puts "You're on #{current_version}. Latest is #{latest_version}."
      puts "***************************************************************"
      puts
      exit 1
    end
  end
end

#configObject



62
63
64
# File 'lib/pauper.rb', line 62

def config
  @pauper_config
end

#create(node_name) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/pauper.rb', line 192

def create(node_name)
  node_config = get_node_config(node_name)

  raise "VM already exists!" if vm_exists?(node_name)

  release = node_config.config[:release]
  base_name = get_base_name(release)

  unless vm_exists?(base_name)
    puts "Bootstrapping base '#{base_name}' for release '#{release}'"
    bootstrap(release)
  end

  mac = generate_mac
  ip = "#{@pauper_config.config[:subnet]}.#{node_config.config[:last_octet]}"

  puts "Cloning #{base_name}..."
  clone_base(base_name, node_name, ip)

  lxc = LXC.new(node_name)
  lxc.interface = @pauper_config.config[:bridge]
  lxc.mac = mac
  lxc.save

  prepare_fstab(node_name, release)
  prepare_vm_network(node_name,ip)
  reconfigure_ssh(node_name)
  puts "Starting..."
  start_node(node_name)
  sleep 3
  write_hosts
  setup(node_name)
end

#destroy(node_name) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/pauper.rb', line 280

def destroy(node_name)
  ip = get_base_ip(node_name)
  if ip.nil?
    node_config = get_node_config(node_name)
    ip = node_ip(node_config)
  end

  if vm_exists?(node_name)
    stop(node_name)

    chef_node = "#{node_name}#{@pauper_config.config[:node_suffix]}"
    puts "Removing #{node_name} from Chef..."
    `knife node delete #{chef_node} -y`
    `knife client delete #{chef_node} -y`

    puts "Destroying #{node_name}..."
    cmd "sudo lxc-destroy -n '#{node_name}' >> pauper.log 2>&1"
    ssh_clean_known_hosts(node_name, ip)
    write_hosts
  else
    puts "#{node_name} hasn't even been created yet, thusly you can't destroy it!"
  end
end

#destroy_allObject



413
414
415
416
# File 'lib/pauper.rb', line 413

def destroy_all
  puts "Destroying all nodes..."
  @pauper_config.config[:nodes].each { |n| destroy(n.name) }
end

#get_base_ip(base_name) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/pauper.rb', line 87

def get_base_ip(base_name)
  if base_name == 'base'
    return @pauper_config.config[:subnet] + '.2'
  elsif base_name == 'base-precise'
    return @pauper_config.config[:subnet] + '.3'
  end
end

#get_base_name(release) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/pauper.rb', line 76

def get_base_name(release)
  case Pauper.grok_release(release)
  when :lucid
    'base'
  when :precise
    'base-precise'
  else
    raise "Unsupported release #{release}"
  end
end

#get_share_release(options) ⇒ Object



144
145
146
# File 'lib/pauper.rb', line 144

def get_share_release(options)
  options[:release] || :lucid
end

#haltObject

Should call pauper stop on your vm Assumes that your local /etc/hosts already knows of your dev ip



442
443
444
445
446
447
# File 'lib/pauper.rb', line 442

def halt
  puts "Let me blow things up..."
  cmds = ["cd #{code_dir}/pauper-env",
          "pauper stop"]
  ssh_with_sudo hosts_dev_ip, cmds
end

#prepare_base_cache(release) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/pauper.rb', line 177

def prepare_base_cache(release)
  base_name = get_base_name(release)
  shares = @pauper_config.config[:shares]
  shares.each do |share_name, guest_path, host_path, options|
    next if get_share_release(options) != release
    cmd "sudo mkdir -p #{host_path}"
    cmd "sudo mkdir -p /var/lib/lxc/#{base_name}/rootfs#{guest_path}"
  end
  cmd "sudo mkdir -p /var/cache/lxc/#{release}/cache/apt/partial"
end

#prepare_fstab(node_name, release) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/pauper.rb', line 148

def prepare_fstab(node_name, release)
  File.open('.tmp.fstab','w') do |f|
    @pauper_config.config[:shares].each do |share_name, guest_path, host_path, options|
      next if get_share_release(options) != release
      f.puts "#{host_path} #{DEFAULT_VM_PATH}/#{node_name}/rootfs/#{guest_path} none defaults,bind 0 0"
    end
  end
  system "sudo mv .tmp.fstab #{DEFAULT_VM_PATH}/#{node_name}/fstab"
end

#prepare_vm_network(node_name, ip) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/pauper.rb', line 158

def prepare_vm_network(node_name,ip)
  host_ip = "#{@pauper_config.config[:subnet]}.1"
  File.open('.tmp.interfaces', 'w') do |f|
    f.puts "auto lo"
    f.puts "iface lo inet loopback"
    f.puts ""
    f.puts "auto eth0"
    f.puts "iface eth0 inet static"
    f.puts " address #{ip}"
    f.puts " netmask 255.255.255.0"
    f.puts " gateway #{host_ip}"
  end
  File.open('.tmp.resolv.conf', 'w') do |f|
    f.puts "nameserver #{host_ip}"
  end
  system "sudo mv .tmp.interfaces #{DEFAULT_VM_PATH}/#{node_name}/rootfs/etc/network/interfaces"
  system "sudo mv .tmp.resolv.conf #{DEFAULT_VM_PATH}/#{node_name}/rootfs/etc/resolv.conf"
end

#public_ssh_keyObject



140
141
142
# File 'lib/pauper.rb', line 140

def public_ssh_key
  ssh_key + ".pub"
end

#reconfigure_ssh(node_name) ⇒ Object



188
189
190
# File 'lib/pauper.rb', line 188

def reconfigure_ssh(node_name)
  cmd "sudo chroot /var/lib/lxc/#{node_name}/rootfs/ /usr/sbin/dpkg-reconfigure openssh-server"
end

#reloadObject

runs the same commands as doing a halt + up but it does it in

one SSH session rather than two... also takes a nap in the middle

Assumes that your local /etc/hosts already knows of your dev ip



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/pauper.rb', line 452

def reload
  puts "Let me blow things up..."
  ip = hosts_dev_ip
  cmds = ["cd #{code_dir}/pauper-env",
          "pauper stop",
          "sleep 2", #napping
          "echo '*-*-*>KABOOM<*-*-*'",
          "pauper start",
          "pauper write_hosts",
          "sudo iptables -F"]
  ssh_with_sudo ip, cmds
  if Pauper.osx?
    setup_osx_routes(ip)
    write_osx_hosts(ip)
  end
end

#resume(node_name) ⇒ Object



326
327
328
329
330
331
332
333
# File 'lib/pauper.rb', line 326

def resume(node_name)
  if vm_frozen?(node_name)
    puts "Resuming #{node_name}..."
    resume_node(node_name)
  else
    puts "wtf, #{node_name} is not suspended."
  end
end

#resume_allObject



403
404
405
406
# File 'lib/pauper.rb', line 403

def resume_all
  puts "Resuming all nodes..."
  @pauper_config.config[:nodes].each { |n| resume(n.name) }
end

#root?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/pauper.rb', line 58

def root?
  Process.euid == 0
end

#setup(node_name) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/pauper.rb', line 226

def setup(node_name)
  node_config = get_node_config(node_name)
  release = node_config.config[:release]
  config = @pauper_config.config
  puts "Setting up #{node_name}..."
  client_erb = ERB.new <<EOF
node_name              "<%= chef_node %>"
chef_server_url        "<%= config[:chef_server_url] %>"
chef_download_url      "<%= config[:chef_download_url][release] %>"
chef_deb_name          "<%= config[:chef_deb_name][release] %>"
validation_client_name "<%= config[:validation_client_name] %>"
environment            "<%= config[:chef_environment] %>"
verbose_logging        false
json_attribs           "/etc/chef/client-config.json"
EOF
  chef_node = "#{node_name}#{config[:node_suffix]}"
  client_rb_data = client_erb.result(OpenStruct.new(:config => config, :chef_node => chef_node).send(:binding))
  tmp_client_rb_path = ".tmp.#{node_name}.client.rb"
  File.open(tmp_client_rb_path,'w') do |f|
    f.puts client_rb_data
  end

  ip = node_ip(node_config)

  chef_attribs = {
    :run_list => config[:default_run_list] + node_config.config[:run_list],
    :ip => {
      :private => ip,
      :private_netmask => '255.255.255.0'
    }
  }.merge(config[:chef_options]).merge(node_config.config[:chef_options])

  puts "Uploading Chef files..."
  Net::SCP.start ip, username do |scp|
    scp.upload! tmp_client_rb_path, "client.rb"
    scp.upload! config[:validation_key_path], "validation.pem"
    scp.upload! StringIO.new(chef_attribs.to_json), "client-config.json"
  end

  FileUtils.rm(tmp_client_rb_path)

  puts "Connecting over SSH..."
  Net::SSH.start ip, username do |ssh|
    ssh_exec ssh, "sudo mv client.rb /etc/chef/"
    ssh_exec ssh, "sudo mv validation.pem /etc/chef/"
    ssh_exec ssh, "sudo mv client-config.json /etc/chef/"
    ssh_exec ssh, "sudo touch /etc/chef/disabled"

    ssh.exec! "sudo /usr/bin/chef-client" do |channel, stream, data|
      print data
    end
  end
end

#setup_allObject



418
419
420
421
# File 'lib/pauper.rb', line 418

def setup_all
  puts "Setting up all nodes..."
  @pauper_config.config[:nodes].each { |n| setup(n.name) }
end

#setup_osx_dhcpd(mac, dev_ip) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
# File 'lib/pauper.rb', line 373

def setup_osx_dhcpd(mac, dev_ip)
  dhcpd = DHCPD.new('/Library/Preferences/VMware Fusion/vmnet8/dhcpd.conf')
  unless dhcpd.config['dev'] &&
    dhcpd.config['dev']['hardware ethernet'] == mac &&
    dhcpd.config['dev']['fixed-address'] == dev_ip

    puts "Writing correct IP/MAC combo into dhcpd.conf file..."
    dhcpd.config['dev'] = { 'hardware ethernet' => mac, 'fixed-address' => dev_ip }
    dhcpd.save
  end
end

#setup_osx_routes(dev_ip) ⇒ Object



365
366
367
368
369
370
371
# File 'lib/pauper.rb', line 365

def setup_osx_routes(dev_ip)
  gateway = `route -n get #{@pauper_config.config[:subnet]}.0`.match(/gateway: (.*)$/).captures[0]
  unless gateway == dev_ip
    puts "Adding route to LXC hosts..."
    system 'sudo', 'route', 'add', "#{@pauper_config.config[:subnet]}.0/24", dev_ip
  end
end

#ssh_keyObject



131
132
133
134
135
136
137
138
# File 'lib/pauper.rb', line 131

def ssh_key
  @ssh_key ||= begin
    key = @pauper_config.config[:bootstrap_ssh_key] || "/home/#{username}/.ssh/id_rsa"
    raise "The private SSH key set in your Pauperfile does not exist." unless File.exist?(key)
    raise "The public SSH key set in your Pauperfile does not exist." unless File.exist?("#{key}.pub")
    key
  end
end

#start(node_name) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/pauper.rb', line 313

def start(node_name)
  if vm_running?(node_name)
    puts "Dude, #{node_name} is already running!"
  elsif vm_exists?(node_name)
    puts "Starting #{node_name}..."
    start_node node_name
  else
    puts "Generating, then starting #{node_name}..."
    create node_name
  end
end

#start_allObject



385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/pauper.rb', line 385

def start_all
  skipped = Array.new
  puts "Starting all nodes..."
  @pauper_config.config[:nodes].each do |n|
    if n.config[:optional]
      skipped << n.name
    else
      start(n.name) unless n.config[:optional]
    end
  end
  puts "Skipped: [#{skipped.join(', ')}]" unless skipped.empty?
end

#stop(node_name) ⇒ Object



304
305
306
307
308
309
310
311
# File 'lib/pauper.rb', line 304

def stop(node_name)
  if vm_running?(node_name)
    puts "Stopping #{node_name}..."
    stop_node(node_name)
  else
    puts "wtf, #{node_name} is not running."
  end
end

#stop_allObject



398
399
400
401
# File 'lib/pauper.rb', line 398

def stop_all
  puts "Stopping all nodes..."
  @pauper_config.config[:nodes].each { |n| stop(n.name) }
end

#suspend(node_name) ⇒ Object



335
336
337
338
339
340
341
342
# File 'lib/pauper.rb', line 335

def suspend(node_name)
  if vm_frozen?(node_name)
    puts "Dude, #{node_name} is already suspended!"
  else
    puts "Suspending #{node_name}..."
    suspend_node(node_name)
  end
end

#suspend_allObject



408
409
410
411
# File 'lib/pauper.rb', line 408

def suspend_all
  puts "Suspending all nodes..."
  @pauper_config.config[:nodes].each { |n| suspend(n.name) }
end

#upObject

Should call pauper stop on your vm and then do the things needed for the

hosts to get setup and made visible

Assumes that your local /etc/hosts already knows of your dev ip



426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/pauper.rb', line 426

def up
  puts "Let me get you setup..."
  ip = hosts_dev_ip
  cmds = ["cd #{code_dir}/pauper-env",
          "pauper start",
          "pauper write_hosts",
          "sudo iptables -F"]
  ssh_with_sudo ip, cmds
  if Pauper.osx?
    setup_osx_routes(ip)
    write_osx_hosts(ip)
  end
end

#write_hostsObject



344
345
346
347
348
349
350
351
352
353
# File 'lib/pauper.rb', line 344

def write_hosts
  puts "Writing /etc/hosts file..."
  hosts = Hosts.new
  hosts.config.clear
  @pauper_config.config[:nodes].each do |node|
    next unless vm_exists?(node.name)
    hosts.config[node_ip(node)] = node.name
  end
  hosts.save(@pauper_config.config[:dev_domain])
end

#write_osx_hosts(dev_ip) ⇒ Object



355
356
357
358
359
360
361
362
363
# File 'lib/pauper.rb', line 355

def write_osx_hosts(dev_ip)
  puts "Writing /etc/hosts file..."
  hosts = Hosts.new
  @pauper_config.config[:nodes].each do |node|
    hosts.config[node_ip(node)] = node.name
  end
  hosts.config[dev_ip] = 'dev'
  hosts.save(@pauper_config.config[:dev_domain])
end