From 05a8cf523ac4eacc8b48b522dc8bf73dd14391e8 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Wed, 16 Oct 2013 10:58:11 -0300 Subject: [PATCH] core: Get rid of code that deals with box info on a separate statefile --- lib/vagrant/action.rb | 4 -- lib/vagrant/action/builtin/remove_box_info.rb | 27 -------- lib/vagrant/action/builtin/write_box_info.rb | 28 --------- plugins/commands/box/command/add.rb | 6 +- plugins/commands/box/command/base.rb | 25 -------- plugins/commands/box/command/list.rb | 8 +-- plugins/commands/box/command/remove.rb | 6 +- plugins/commands/box/plugin.rb | 2 - plugins/commands/box/state_file.rb | 62 ------------------- 9 files changed, 7 insertions(+), 161 deletions(-) delete mode 100644 lib/vagrant/action/builtin/remove_box_info.rb delete mode 100644 lib/vagrant/action/builtin/write_box_info.rb delete mode 100644 plugins/commands/box/command/base.rb delete mode 100644 plugins/commands/box/state_file.rb diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index b67d5fdb5..03ce5a358 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -22,12 +22,10 @@ module Vagrant autoload :NFS, "vagrant/action/builtin/nfs" autoload :Provision, "vagrant/action/builtin/provision" autoload :ProvisionerCleanup, "vagrant/action/builtin/provisioner_cleanup" - autoload :RemoveBoxInfo, "vagrant/action/builtin/remove_box_info" autoload :SetHostname, "vagrant/action/builtin/set_hostname" autoload :SSHExec, "vagrant/action/builtin/ssh_exec" autoload :SSHRun, "vagrant/action/builtin/ssh_run" autoload :WaitForCommunicator, "vagrant/action/builtin/wait_for_communicator" - autoload :WriteBoxInfo, "vagrant/action/builtin/write_box_info" end module General @@ -42,7 +40,6 @@ module Vagrant def self.action_box_add Builder.new.tap do |b| b.use Builtin::BoxAdd - b.use Builtin::WriteBoxInfo end end @@ -52,7 +49,6 @@ module Vagrant def self.action_box_remove Builder.new.tap do |b| b.use Builtin::BoxRemove - b.use Builtin::RemoveBoxInfo end end end diff --git a/lib/vagrant/action/builtin/remove_box_info.rb b/lib/vagrant/action/builtin/remove_box_info.rb deleted file mode 100644 index c0a8d8fba..000000000 --- a/lib/vagrant/action/builtin/remove_box_info.rb +++ /dev/null @@ -1,27 +0,0 @@ -require "log4r" - -module Vagrant - module Action - module Builtin - # This middleware will remove additional information about the base box - # from state file - class RemoveBoxInfo - def initialize(app, env) - @app = app - @logger = Log4r::Logger.new("vagrant::action::builtin::remove_box_info") - end - - def call(env) - box_removed = env[:box_removed] - box_state_file = env[:box_state_file] - - # Mark that we removed the box - @logger.info("Removing the box from the state file...") - box_state_file.remove_box(box_removed) - - @app.call(env) - end - end - end - end -end diff --git a/lib/vagrant/action/builtin/write_box_info.rb b/lib/vagrant/action/builtin/write_box_info.rb deleted file mode 100644 index e8c55989b..000000000 --- a/lib/vagrant/action/builtin/write_box_info.rb +++ /dev/null @@ -1,28 +0,0 @@ -require "log4r" - -module Vagrant - module Action - module Builtin - # This middleware will persist some extra information about the base box - # on a state file - class WriteBoxInfo - def initialize(app, env) - @app = app - @logger = Log4r::Logger.new("vagrant::action::builtin::write_box_info") - end - - def call(env) - box_url = env[:box_url] - box_added = env[:box_added] - box_state_file = env[:box_state_file] - - # Mark that we downloaded the box - @logger.info("Adding the box to the state file...") - box_state_file.add_box(box_added, box_url) - - @app.call(env) - end - end - end - end -end diff --git a/plugins/commands/box/command/add.rb b/plugins/commands/box/command/add.rb index 403dddb79..5a2ff0187 100644 --- a/plugins/commands/box/command/add.rb +++ b/plugins/commands/box/command/add.rb @@ -1,11 +1,9 @@ require 'optparse' -require_relative "base" - module VagrantPlugins module CommandBox module Command - class Add < Base + class Add < Vagrant.plugin("2", :command) def execute options = {} @@ -36,7 +34,7 @@ module VagrantPlugins provider = nil provider = options[:provider].to_sym if options[:provider] - action(Vagrant::Action.action_box_add, { + @env.action_runner.run(Vagrant::Action.action_box_add, { :box_name => argv[0], :box_provider => provider, :box_url => argv[1], diff --git a/plugins/commands/box/command/base.rb b/plugins/commands/box/command/base.rb deleted file mode 100644 index 9b07cfe4d..000000000 --- a/plugins/commands/box/command/base.rb +++ /dev/null @@ -1,25 +0,0 @@ -module VagrantPlugins - module CommandBox - module Command - class Base < Vagrant.plugin("2", :command) - # This is a helper for executing an action sequence with the proper - # environment hash setup so that the plugin specific helpers are - # in. - # - # @param [Object] callable the Middleware callable - # @param [Hash] env Extra environment hash that is merged in. - def action(callable, env=nil) - env = { - :box_state_file => box_state_file - }.merge(env || {}) - - @env.action_runner.run(callable, env) - end - - def box_state_file - @box_state_file ||= StateFile.new(@env.home_path.join("boxes.json")) - end - end - end - end -end diff --git a/plugins/commands/box/command/list.rb b/plugins/commands/box/command/list.rb index ca1b0f3c4..97782d026 100644 --- a/plugins/commands/box/command/list.rb +++ b/plugins/commands/box/command/list.rb @@ -1,11 +1,9 @@ require 'optparse' -require_relative "base" - module VagrantPlugins module CommandBox module Command - class List < Base + class List < Vagrant.plugin("2", :command) def execute options = {} @@ -51,8 +49,8 @@ module VagrantPlugins boxes.each do |name, provider, _v1| extra = '' if extra_info - extra << "\n `- URL: #{box_state_file.box_url(name, provider)}" - extra << "\n `- Date: #{box_state_file.downloaded_at(name, provider)}" + extra << "\n `- URL: TODO" + extra << "\n `- Date: TODO" end name = name.ljust(longest_box_length) diff --git a/plugins/commands/box/command/remove.rb b/plugins/commands/box/command/remove.rb index 6b52f023f..d58f5bcec 100644 --- a/plugins/commands/box/command/remove.rb +++ b/plugins/commands/box/command/remove.rb @@ -1,11 +1,9 @@ require 'optparse' -require_relative "base" - module VagrantPlugins module CommandBox module Command - class Remove < Base + class Remove < Vagrant.plugin("2", :command) def execute opts = OptionParser.new do |o| o.banner = "Usage: vagrant box remove " @@ -36,7 +34,7 @@ module VagrantPlugins argv[1] = providers[0] || "" end - action(Vagrant::Action.action_box_remove, { + @env.action_runner.run(Vagrant::Action.action_box_remove, { :box_name => argv[0], :box_provider => argv[1] }) diff --git a/plugins/commands/box/plugin.rb b/plugins/commands/box/plugin.rb index 58a9ca530..bc19f99f6 100644 --- a/plugins/commands/box/plugin.rb +++ b/plugins/commands/box/plugin.rb @@ -11,7 +11,5 @@ module VagrantPlugins Command::Root end end - - autoload :StateFile, File.expand_path("../state_file", __FILE__) end end diff --git a/plugins/commands/box/state_file.rb b/plugins/commands/box/state_file.rb deleted file mode 100644 index 0ea690da9..000000000 --- a/plugins/commands/box/state_file.rb +++ /dev/null @@ -1,62 +0,0 @@ -require "json" - -module VagrantPlugins - module CommandBox - # This is a helper to deal with the boxes state file that Vagrant - # uses to track the boxes that have been downloaded. - class StateFile - def initialize(path) - @path = path - - @data = {} - @data = JSON.parse(@path.read) if @path.exist? - @data["boxes"] ||= {} - end - - # Add a downloaded box to the state file. - # - # @param [Box] box The Box object that was added - # @param [String] url The URL from where the box was downloaded - def add_box(box, url) - box_key = "#{box.name}-#{box.provider}" - - @data["boxes"][box_key] = { - "url" => url, - "downloaded_at" => Time.now.utc.to_s - } - - save! - end - - def box_url(name, provider) - box_key = "#{name}-#{provider}" - - box_info = @data["boxes"].fetch(box_key, {}) - box_info['url'] || 'Unknown' - end - - def downloaded_at(name, provider) - box_key = "#{name}-#{provider}" - - box_info = @data["boxes"].fetch(box_key, {}) - box_info['downloaded_at'] || 'Unknown' - end - - # Remove a box that has been previously downloaded from the state file. - # - # @param [Box] box The box that was removed. - def remove_box(box) - box_key = "#{box.name}-#{box.provider}" - @data["boxes"].delete(box_key) - save! - end - - # This saves the state back into the state file. - def save! - @path.open("w+") do |f| - f.write(JSON.dump(@data)) - end - end - end - end -end