class TestLab::Provisioner::NFSMount

NFSMount Provisioner Class

@author Zachary Patten <zachary AT jovelabs DOT com>

Public Class Methods

new(config={}, ui=nil) click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 17
def initialize(config={}, ui=nil)
  @config  = (config || Hash.new)
  @ui      = (ui     || TestLab.ui)
  @command = ZTK::Command.new(:ui => @ui, :silence => true, :ignore_exit_status => true)

  @config[:nfs_mounts] ||= Array.new

  @ui.logger.debug { "config(#{@config.inspect})" }
end

Public Instance Methods

on_container_deprovision(container) click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 47
def on_container_deprovision(container)
  remove_nfs_mounts(container)

  true
end
on_container_destroy(container)
on_container_down(container)
on_container_provision(container) click to toggle source

NFSMount: Container Provision

# File lib/testlab/provisioners/nfs_mount.rb, line 28
def on_container_provision(container)
  container.exec(%(sudo dpkg --status nfs-common || sudo apt-get -qy install nfs-common))

  add_nfs_mounts(container)
  container_mount(container)

  true
end
on_container_up(container) click to toggle source

NFSMount: Container Up

# File lib/testlab/provisioners/nfs_mount.rb, line 38
def on_container_up(container)
  (container.exec(%(sudo dpkg --status nfs-common), :ignore_exit_status => true).exit_code == 0) or return false

  add_nfs_mounts(container)
  container_mount(container)

  true
end

Private Instance Methods

add_nfs_mounts(container) click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 57
      def add_nfs_mounts(container)
        script = <<-EOF
#{service_check}
grep '#{def_tag(container)}' /etc/exports && exit 0
cat <<EOI | #{sudo} tee -a /etc/exports
#{def_tag(container)}
#{mount_blob(container)}
#{end_tag(container)}
EOI
#{restart_service_command}
        EOF

        tempfile = Tempfile.new('script')
        tempfile.write(script)
        tempfile.flush

        command = %(/bin/bash -x #{tempfile.path})

        @command.exec(command)
      end
container_mount(container) click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 104
def container_mount(container)
  @config[:nfs_mounts].each do |nfs_mount|
    container.exec(%(sudo mkdir -p #{nfs_mount[2]}))
    container.exec(%(sudo mount -vt nfs -o 'nfsvers=3' #{nfs_mount[0]}:#{nfs_mount[1]} #{nfs_mount[2]}), :ignore_exit_status => true)
  end
end
def_tag(container) click to toggle source

NFS Exports Start Definition Tag

# File lib/testlab/provisioners/nfs_mount.rb, line 130
def def_tag(container)
  "#TESTLAB-NFS-EXPORTS-DEF-#{container.id.to_s.upcase}"
end
end_tag(container) click to toggle source

NFS Exports End Definition Tag

# File lib/testlab/provisioners/nfs_mount.rb, line 135
def end_tag(container)
  "#TESTLAB-NFS-EXPORTS-END-#{container.id.to_s.upcase}"
end
mount_blob(container) click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 82
def mount_blob(container)
  mount_entries = Array.new
  @config[:nfs_mounts].each do |nfs_mount|
    mount_entries << case RUBY_PLATFORM
    when /darwin/ then
      %(#{nfs_mount[1]})
    when /linux/ then
      %(#{nfs_mount[1]} *(rw,sync,no_subtree_check))
    end
  end
  mount_entries.join("\n")
end
remove_nfs_mounts(container) click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 78
def remove_nfs_mounts(container)
  @command.exec(sed_exports(container))
end
restart_service_command() click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 111
def restart_service_command
  case RUBY_PLATFORM
  when /darwin/ then
    %(#{sudo} nfsd restart ; sleep 10)
  when /linux/ then
    %(#{sudo} service nfs-kernel-server reload || #{sudo} service nfs-kernel-server restart)
  end
end
sed_exports(container) click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 95
def sed_exports(container)
  case RUBY_PLATFORM
  when /darwin/ then
    %(#{sudo} sed -i '' '/#{def_tag(container)}/,/#{end_tag(container)}/d' /etc/exports)
  when /linux/ then
    %(#{sudo} sed -i '/#{def_tag(container)}/,/#{end_tag(container)}/d' /etc/exports)
  end
end
service_check() click to toggle source
# File lib/testlab/provisioners/nfs_mount.rb, line 120
def service_check
  case RUBY_PLATFORM
  when /darwin/ then
    %(#{sudo} nfsd enable)
  when /linux/ then
    %((#{sudo} dpkg --status nfs-kernel-server || #{sudo} apt-get -qy install nfs-kernel-server) && #{sudo} service nfs-kernel-server start)
  end
end