core: Get rid of code that deals with box info on a separate statefile

This commit is contained in:
Fabio Rehm 2013-10-16 10:58:11 -03:00
parent fcfb431362
commit 05a8cf523a
9 changed files with 7 additions and 161 deletions

View File

@ -22,12 +22,10 @@ module Vagrant
autoload :NFS, "vagrant/action/builtin/nfs" autoload :NFS, "vagrant/action/builtin/nfs"
autoload :Provision, "vagrant/action/builtin/provision" autoload :Provision, "vagrant/action/builtin/provision"
autoload :ProvisionerCleanup, "vagrant/action/builtin/provisioner_cleanup" autoload :ProvisionerCleanup, "vagrant/action/builtin/provisioner_cleanup"
autoload :RemoveBoxInfo, "vagrant/action/builtin/remove_box_info"
autoload :SetHostname, "vagrant/action/builtin/set_hostname" autoload :SetHostname, "vagrant/action/builtin/set_hostname"
autoload :SSHExec, "vagrant/action/builtin/ssh_exec" autoload :SSHExec, "vagrant/action/builtin/ssh_exec"
autoload :SSHRun, "vagrant/action/builtin/ssh_run" autoload :SSHRun, "vagrant/action/builtin/ssh_run"
autoload :WaitForCommunicator, "vagrant/action/builtin/wait_for_communicator" autoload :WaitForCommunicator, "vagrant/action/builtin/wait_for_communicator"
autoload :WriteBoxInfo, "vagrant/action/builtin/write_box_info"
end end
module General module General
@ -42,7 +40,6 @@ module Vagrant
def self.action_box_add def self.action_box_add
Builder.new.tap do |b| Builder.new.tap do |b|
b.use Builtin::BoxAdd b.use Builtin::BoxAdd
b.use Builtin::WriteBoxInfo
end end
end end
@ -52,7 +49,6 @@ module Vagrant
def self.action_box_remove def self.action_box_remove
Builder.new.tap do |b| Builder.new.tap do |b|
b.use Builtin::BoxRemove b.use Builtin::BoxRemove
b.use Builtin::RemoveBoxInfo
end end
end end
end end

View File

@ -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

View File

@ -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

View File

@ -1,11 +1,9 @@
require 'optparse' require 'optparse'
require_relative "base"
module VagrantPlugins module VagrantPlugins
module CommandBox module CommandBox
module Command module Command
class Add < Base class Add < Vagrant.plugin("2", :command)
def execute def execute
options = {} options = {}
@ -36,7 +34,7 @@ module VagrantPlugins
provider = nil provider = nil
provider = options[:provider].to_sym if options[:provider] 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_name => argv[0],
:box_provider => provider, :box_provider => provider,
:box_url => argv[1], :box_url => argv[1],

View File

@ -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

View File

@ -1,11 +1,9 @@
require 'optparse' require 'optparse'
require_relative "base"
module VagrantPlugins module VagrantPlugins
module CommandBox module CommandBox
module Command module Command
class List < Base class List < Vagrant.plugin("2", :command)
def execute def execute
options = {} options = {}
@ -51,8 +49,8 @@ module VagrantPlugins
boxes.each do |name, provider, _v1| boxes.each do |name, provider, _v1|
extra = '' extra = ''
if extra_info if extra_info
extra << "\n `- URL: #{box_state_file.box_url(name, provider)}" extra << "\n `- URL: TODO"
extra << "\n `- Date: #{box_state_file.downloaded_at(name, provider)}" extra << "\n `- Date: TODO"
end end
name = name.ljust(longest_box_length) name = name.ljust(longest_box_length)

View File

@ -1,11 +1,9 @@
require 'optparse' require 'optparse'
require_relative "base"
module VagrantPlugins module VagrantPlugins
module CommandBox module CommandBox
module Command module Command
class Remove < Base class Remove < Vagrant.plugin("2", :command)
def execute def execute
opts = OptionParser.new do |o| opts = OptionParser.new do |o|
o.banner = "Usage: vagrant box remove <name> <provider>" o.banner = "Usage: vagrant box remove <name> <provider>"
@ -36,7 +34,7 @@ module VagrantPlugins
argv[1] = providers[0] || "" argv[1] = providers[0] || ""
end end
action(Vagrant::Action.action_box_remove, { @env.action_runner.run(Vagrant::Action.action_box_remove, {
:box_name => argv[0], :box_name => argv[0],
:box_provider => argv[1] :box_provider => argv[1]
}) })

View File

@ -11,7 +11,5 @@ module VagrantPlugins
Command::Root Command::Root
end end
end end
autoload :StateFile, File.expand_path("../state_file", __FILE__)
end end
end end

View File

@ -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