Class: Simp::BeakerHelpers::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/simp/beaker_helpers/snapshot.rb

Overview

Helpers for managing Vagrant snapshots

Constant Summary collapse

BASE_NAME =

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

'_simp_beaker_base'.freeze

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
# File 'lib/simp/beaker_helpers/snapshot.rb', line 123

def self.enabled?
  enabled = ENV['BEAKER_simp_snapshot'] == 'yes'

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

  enabled
end

.exist?(host, name) ⇒ Boolean

Whether or not a named snapshot exists

Parameters:

  • host (Beaker::Host)

    The SUT to work on

  • snapshot_name (String)

    The string to add to the snapshot

Returns:

  • (Boolean)


46
47
48
# File 'lib/simp/beaker_helpers/snapshot.rb', line 46

def self.exist?(host, name)
  list(host).include?(name)
end

.list(host) ⇒ Array[String]

List all snapshots for the given host

Returns:

  • (Array[String])

    A list of snapshot names for the host



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simp/beaker_helpers/snapshot.rb', line 57

def self.list(host)
  output = []
  vdir = vagrant_dir(host)

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

  output
end

.restore(host, snapshot_name) ⇒ Object

Restore a snapshot

Parameters:

  • host (Beaker::Host)

    The SUT to work on

  • snapshot_name (String)

    The name that was added to the snapshot



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/simp/beaker_helpers/snapshot.rb', line 81

def self.restore(host, snapshot_name)
  return unless enabled?
  vdir = vagrant_dir(host)

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

    output = `vagrant snapshot restore #{host.name} "#{snap}" 2>&1`

    if (output =~ %r{error}i) && output.include?('child')
      raise output
    end

    if %r{snapshot.*not found}.match?(output)
      raise output
    end

    logger.notify(output)

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

.restore_to_base(host) ⇒ Object

Restore all the way back to the base image

Parameters:

  • host (Beaker::Host)

    The SUT to work on



115
116
117
118
119
120
121
# File 'lib/simp/beaker_helpers/snapshot.rb', line 115

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) ⇒ Object

Save a snapshot

Parameters:

  • host (Beaker::Host)

    The SUT to work on

  • snapshot_name (String)

    The string to add to the snapshot



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simp/beaker_helpers/snapshot.rb', line 14

def self.save(host, snapshot_name)
  return unless enabled?
  vdir = vagrant_dir(host)

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

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

    output = `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

.vagrant_dir(host) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/simp/beaker_helpers/snapshot.rb', line 133

def self.vagrant_dir(host)
  tgt_dir = nil

  if 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

  tgt_dir
end