Merge pull request #10359 from chrisroberts/e-check-limits

Limit automatic box outdated checks to once per hour
This commit is contained in:
Chris Roberts 2018-11-05 12:46:56 -08:00 committed by GitHub
commit d2d117471a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 3 deletions

View File

@ -40,6 +40,7 @@ module Vagrant
# Have download options specified in the environment override # Have download options specified in the environment override
# options specified for the machine. # options specified for the machine.
download_options = { download_options = {
automatic_check: true,
ca_cert: env[:ca_cert] || machine.config.vm.box_download_ca_cert, ca_cert: env[:ca_cert] || machine.config.vm.box_download_ca_cert,
ca_path: env[:ca_path] || machine.config.vm.box_download_ca_path, ca_path: env[:ca_path] || machine.config.vm.box_download_ca_path,
client_cert: env[:client_cert] || client_cert: env[:client_cert] ||

View File

@ -16,6 +16,9 @@ module Vagrant
class Box class Box
include Comparable include Comparable
# Number of seconds to wait between checks for box updates
BOX_UPDATE_CHECK_INTERVAL = 3600
# The box name. This is the logical name used when adding the box. # The box name. This is the logical name used when adding the box.
# #
# @return [String] # @return [String]
@ -154,6 +157,11 @@ module Vagrant
raise Errors::BoxUpdateNoMetadata, name: @name raise Errors::BoxUpdateNoMetadata, name: @name
end end
if download_options.delete(:automatic_check) && !automatic_update_check_allowed?
@logger.info("Skipping box update check")
return
end
version += ", " if version version += ", " if version
version ||= "" version ||= ""
version += "> #{@version}" version += "> #{@version}"
@ -164,6 +172,25 @@ module Vagrant
[md, newer, newer.provider(@provider)] [md, newer, newer.provider(@provider)]
end end
# Check if a box update check is allowed. Uses a file
# in the box data directory to track when the last auto
# update check was performed and returns true if the
# BOX_UPDATE_CHECK_INTERVAL has passed.
#
# @return [Boolean]
def automatic_update_check_allowed?
check_path = directory.join("box_update_check")
if check_path.exist?
last_check_span = Time.now.to_i - check_path.mtime.to_i
if last_check_span < BOX_UPDATE_CHECK_INTERVAL
@logger.info("box update check is under the interval threshold")
return false
end
end
FileUtils.touch(check_path)
true
end
# This repackages this box and outputs it to the given path. # This repackages this box and outputs it to the given path.
# #
# @param [Pathname] path The full path (filename included) of where # @param [Pathname] path The full path (filename included) of where

View File

@ -119,7 +119,7 @@ describe Vagrant::Action::Builtin::BoxCheckOutdated do
expect(box).to receive(:has_update?).with(machine.config.vm.box_version, expect(box).to receive(:has_update?).with(machine.config.vm.box_version,
{download_options: {download_options:
{ca_cert: nil, ca_path: nil, client_cert: nil, insecure: false}}). {automatic_check: true, ca_cert: nil, ca_path: nil, client_cert: nil, insecure: false}}).
and_return([md, md.version("1.1"), md.version("1.1").provider("virtualbox")]) and_return([md, md.version("1.1"), md.version("1.1").provider("virtualbox")])
expect(app).to receive(:call).with(env).once expect(app).to receive(:call).with(env).once
@ -205,7 +205,7 @@ describe Vagrant::Action::Builtin::BoxCheckOutdated do
it "uses download options from machine" do it "uses download options from machine" do
expect(box).to receive(:has_update?).with(machine.config.vm.box_version, expect(box).to receive(:has_update?).with(machine.config.vm.box_version,
{download_options: {download_options:
{ca_cert: "foo", ca_path: "bar", client_cert: "baz", insecure: true}}) {automatic_check: true, ca_cert: "foo", ca_path: "bar", client_cert: "baz", insecure: true}})
expect(app).to receive(:call).with(env).once expect(app).to receive(:call).with(env).once
@ -215,7 +215,7 @@ describe Vagrant::Action::Builtin::BoxCheckOutdated do
it "overrides download options from machine with options from env" do it "overrides download options from machine with options from env" do
expect(box).to receive(:has_update?).with(machine.config.vm.box_version, expect(box).to receive(:has_update?).with(machine.config.vm.box_version,
{download_options: {download_options:
{ca_cert: "oof", ca_path: "rab", client_cert: "zab", insecure: false}}) {automatic_check: true, ca_cert: "oof", ca_path: "rab", client_cert: "zab", insecure: false}})
env[:ca_cert] = "oof" env[:ca_cert] = "oof"
env[:ca_path] = "rab" env[:ca_path] = "rab"

View File

@ -195,6 +195,28 @@ describe Vagrant::Box, :skip_windows do
end end
end end
context "#automatic_update_check_allowed?" do
it "should return true on intial check" do
expect(subject.automatic_update_check_allowed?).to be_truthy
end
it "should return false on second check" do
expect(subject.automatic_update_check_allowed?).to be_truthy
expect(subject.automatic_update_check_allowed?).to be_falsey
end
it "should use a file to mark last check time" do
expect(FileUtils).to receive(:touch)
subject.automatic_update_check_allowed?
end
it "should return true when time since last check is greater than check interval" do
subject.automatic_update_check_allowed?
stub_const("Vagrant::Box::BOX_UPDATE_CHECK_INTERVAL", -1)
expect(subject.automatic_update_check_allowed?).to be_truthy
end
end
context "#in_use?" do context "#in_use?" do
let(:index) { [] } let(:index) { [] }