class Simp::BeakerHelpers::Snapshot

Helpers for managing Vagrant snapshots

Constants

BASE_NAME

The name of the base snapshot that is created if no snapshots currently exist

Public Class Methods

exist?(host, name) click to toggle source

Whether or not a named snapshot exists

@param host [Beaker::Host]

The SUT to work on

@param snapshot_name [String]

The string to add to the snapshot

@return [Boolean]

# File lib/simp/beaker_helpers/snapshot.rb, line 49
def self.exist?(host, name)
  list(host).include?(name)
end
list(host) click to toggle source

List all snapshots for the given host

@parma host [Beaker::Host]

The SUT to work on

@return [Array]

A list of snapshot names for the host
# File lib/simp/beaker_helpers/snapshot.rb, line 60
def self.list(host)
  output = []
  vdir = vagrant_dir(host)

  if vdir
    Dir.chdir(vdir) do
      output = %x(vagrant snapshot list #{host.name}).lines
      output.map! do |x|
        x.split(/^#{host.name}_/).last.split(':').first.delete('==>').strip
      end
    end
  end

  output
end
restore(host, snapshot_name) click to toggle source

Restore a snapshot

@param host [Beaker::Host]

The SUT to work on

@param snapshot_name [String]

The name that was added to the snapshot
# File lib/simp/beaker_helpers/snapshot.rb, line 84
def self.restore(host, snapshot_name)
  if enabled?
    vdir = vagrant_dir(host)

    if vdir
      Dir.chdir(vdir) do
        snap = "#{host.name}_#{snapshot_name}"

        output = %x(vagrant snapshot restore #{host.name} "#{snap}" 2>&1)

        if (output =~ /error/i) && (output =~ /child/)
          raise output
        end

        if (output =~ /snapshot.*not found/)
          raise output
        end

        logger.notify(output)

        retry_on(
          host,
          %(echo "restoring snapshot '#{snap}'" > /dev/null),
          :max_retries => 30,
          :retry_interval => 1
        )
      end
    end
  end
end
restore_to_base(host) click to toggle source

Restore all the way back to the base image

@param host [Beaker::Host]

The SUT to work on
# File lib/simp/beaker_helpers/snapshot.rb, line 120
def self.restore_to_base(host)
  if exist?(host, BASE_NAME)
    restore(host, BASE_NAME)
  else
    save(host, BASE_NAME)
  end
end
save(host, snapshot_name) click to toggle source

Save a snapshot

@param host [Beaker::Host]

The SUT to work on

@param snapshot_name [String]

The string to add to the snapshot
# File lib/simp/beaker_helpers/snapshot.rb, line 15
def self.save(host, snapshot_name)
  if enabled?
    vdir = vagrant_dir(host)

    if vdir
      Dir.chdir(vdir) do
        save(host, BASE_NAME) unless exist?(host, BASE_NAME)

        snap = "#{host.name}_#{snapshot_name}"

        output = %x(vagrant snapshot save --force #{host.name} "#{snap}")

        logger.notify(output)

        retry_on(
          host,
          %(echo "saving snapshot '#{snap}'" > /dev/null),
          :max_retries => 30,
          :retry_interval => 1
        )
      end
    end
  end
end

Private Class Methods

enabled?() click to toggle source
# File lib/simp/beaker_helpers/snapshot.rb, line 130
def self.enabled?
  enabled = ENV['BEAKER_simp_snapshot'] == 'yes'

  unless enabled
    logger.warn('Snapshotting not enabled, set BEAKER_simp_snapshot=yes to enable')
  end

  return enabled
end
vagrant_dir(host) click to toggle source
# File lib/simp/beaker_helpers/snapshot.rb, line 140
def self.vagrant_dir(host)
  tgt_dir = nil

  if host && host.options && host.options[:hosts_file]
    vdir = File.join('.vagrant', 'beaker_vagrant_files', File.basename(host.options[:hosts_file]))

    if File.directory?(vdir)
      tgt_dir = vdir
    else
      logger.notify("Could not find local vagrant dir at #{vdir}")
    end
  end

  return tgt_dir
end