Class: Vagrant::Action::Builtin::BoxCheckOutdated

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/builtin/box_check_outdated.rb

Overview

This middleware checks if there are outdated boxes. By default, it only checks locally, but if box_outdated_refresh is set, it will refresh the metadata associated with a box.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ BoxCheckOutdated

Returns a new instance of BoxCheckOutdated.


10
11
12
13
14
# File 'lib/vagrant/action/builtin/box_check_outdated.rb', line 10

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new(
    "vagrant::action::builtin::box_check_outdated")
end

Instance Method Details

#call(env) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vagrant/action/builtin/box_check_outdated.rb', line 16

def call(env)
  machine = env[:machine]

  if !env[:box_outdated_force]
    if !machine.config.vm.box_check_update
      @logger.debug(
        "Not checking for update: no force and no update config")
      return @app.call(env)
    end
  end

  if !machine.box
    # We don't have a box. Just ignore, we can't check for
    # outdated...
    @logger.warn("Not checking for update, no box")
    return @app.call(env)
  end

  box = machine.box
  if box.version == "0" && !box.
    return @app.call(env)
  end

  constraints = machine.config.vm.box_version
  # Have download options specified in the environment override
  # options specified for the machine.
  download_options = {
    automatic_check: !env[:box_outdated_force],
    ca_cert: env[:ca_cert] || machine.config.vm.box_download_ca_cert,
    ca_path: env[:ca_path] || machine.config.vm.box_download_ca_path,
    client_cert: env[:client_cert] ||
                   machine.config.vm.box_download_client_cert,
    insecure: !env[:insecure].nil? ?
                env[:insecure] : machine.config.vm.box_download_insecure,
    box_extra_download_options: env[:box_extra_download_options] || machine.config.vm.box_extra_download_options,
  }

  env[:ui].output(I18n.t(
    "vagrant.box_outdated_checking_with_refresh",
    name: box.name,
    version: box.version))
  update = nil
  begin
    update = box.has_update?(constraints, download_options: download_options)
  rescue Errors::BoxMetadataDownloadError => e
    env[:ui].warn(I18n.t(
      "vagrant.box_outdated_metadata_download_error",
      message: e.extra_data[:message]))
  rescue Errors::BoxMetadataMalformed  => e
    @logger.warn(e.to_s)
    env[:ui].warn(I18n.t("vagrant.box_malformed_continue_on_update"))
  rescue Errors::VagrantError => e
    raise if !env[:box_outdated_ignore_errors]
    env[:ui].detail(I18n.t(
      "vagrant.box_outdated_metadata_error_single",
      message: e.message))
  end
  env[:box_outdated] = update != nil
  local_update = check_outdated_local(env)
  if update && (local_update.nil? || (local_update.version < update[1].version))
    env[:ui].warn(I18n.t(
      "vagrant.box_outdated_single",
      name: update[0].name,
      provider: box.provider,
      current: box.version,
      latest: update[1].version))
  elsif local_update
    env[:ui].warn(I18n.t(
      "vagrant.box_outdated_local",
      name: local_update.name,
      old: box.version,
      new: local_update.version))
    env[:box_outdated] = true
  else
    env[:box_outdated] = false
  end

  @app.call(env)
end

#check_outdated_local(env) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vagrant/action/builtin/box_check_outdated.rb', line 96

def check_outdated_local(env)
  machine = env[:machine]

  # Make sure we respect the constraints set within the Vagrantfile
  version = machine.config.vm.box_version
  version += ", " if version
  version ||= ""
  version += "> #{machine.box.version}"

  env[:box_collection].find(
    machine.box.name, machine.box.provider, version)
end