class VagrantPlugins::Openstack::Action::SnapshotSave

Public Class Methods

new(app, _env, retry_interval = 3) click to toggle source
# File lib/vagrant-openstack-provider/action/snapshot_save.rb, line 7
def initialize(app, _env, retry_interval = 3)
  @app = app
  @retry_interval = retry_interval
end

Public Instance Methods

call(env) click to toggle source
# File lib/vagrant-openstack-provider/action/snapshot_save.rb, line 12
def call(env)
  nova = env[:openstack_client].nova
  config = env[:machine].provider_config

  env[:ui].info(I18n.t('vagrant.actions.vm.snapshot.saving',
                       name: env[:snapshot_name]))

  nova.create_snapshot(
    env,
    env[:machine].id, env[:snapshot_name])

  image = nova.list_snapshots(env, env[:machine].id).find { |i| i.name == env[:snapshot_name] }

  timeout(config.server_create_timeout, Errors::Timeout) do
    loop do
      image_status = nova.get_image_details(env, image.id)

      break if image_status['status'] == 'ACTIVE'

      unless image_status['progress'].nil?
        env[:ui].clear_line
        env[:ui].report_progress(image_status['progress'], 100, false)
      end

      sleep @retry_interval
    end
  end

  # Clear progress output.
  env[:ui].clear_line

  env[:ui].success(I18n.t('vagrant.actions.vm.snapshot.saved',
                          name: env[:snapshot_name]))

  @app.call env
end