Class: Vagrant::Action::Builtin::BoxRemove
- Inherits:
-
Object
- Object
- Vagrant::Action::Builtin::BoxRemove
- Defined in:
- lib/vagrant/action/builtin/box_remove.rb
Overview
This middleware will remove a box for a given provider.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, env) ⇒ BoxRemove
constructor
A new instance of BoxRemove.
Constructor Details
#initialize(app, env) ⇒ BoxRemove
Returns a new instance of BoxRemove.
11 12 13 14 |
# File 'lib/vagrant/action/builtin/box_remove.rb', line 11 def initialize(app, env) @app = app @logger = Log4r::Logger.new("vagrant::action::builtin::box_remove") 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/vagrant/action/builtin/box_remove.rb', line 16 def call(env) box_name = env[:box_name] box_architecture = env[:box_architecture] if env[:box_architecture] box_provider = env[:box_provider].to_sym if env[:box_provider] box_version = env[:box_version] box_remove_all_versions = env[:box_remove_all_versions] box_remove_all_providers = env[:box_remove_all_providers] box_remove_all_architectures = env[:box_remove_all_architectures] box_info = Util::HashWithIndifferentAccess.new env[:box_collection].all.each do |name, version, provider, architecture| next if name != box_name box_info[version] ||= Util::HashWithIndifferentAccess.new box_info[version][provider] ||= [] box_info[version][provider] << architecture end # If there's no box info, then the box doesn't exist here if box_info.empty? raise Errors::BoxRemoveNotFound, name: box_name end # Filtering only matters if not removing all versions if !box_remove_all_versions # If no version was provided, and not removing all versions, # only allow one version to proceed if !box_version && box_info.size > 1 raise Errors::BoxRemoveMultiVersion, name: box_name, versions: box_info.keys.sort.map { |k| " * #{k}" }.join("\n") end # If a version was provided, make sure it exists if box_version if !box_info.keys.include?(box_version) raise Errors::BoxRemoveVersionNotFound, name: box_name, version: box_version, versions: box_info.keys.sort.map { |k| " * #{k}" }.join("\n") else box_info.delete_if { |k, _| k != box_version } end end # Only a single version remains box_version = box_info.keys.first # Further filtering only matters if not removing all providers if !box_remove_all_providers # If no provider was given, check if there are more # than a single provider for the version if !box_provider && box_info.values.first.size > 1 raise Errors::BoxRemoveMultiProvider, name: box_name, version: box_version, providers: box_info.values.first.keys.map(&:to_s).sort.join(", ") end # If a provider was given, check the version has it if box_provider if !box_info.values.first.key?(box_provider) raise Errors::BoxRemoveProviderNotFound, name: box_name, version: box_version, provider: box_provider.to_s, providers: box_info.values.first.keys.map(&:to_s).sort.join(", ") else box_info.values.first.delete_if { |k, _| k.to_s != box_provider.to_s } end end # Only a single provider remains box_provider = box_info.values.first.keys.first # Further filtering only matters if not removing all architectures if !box_remove_all_architectures # If no architecture was given, check if there are more # than a single architecture for the provider in version if !box_architecture && box_info.values.first.values.first.size > 1 raise Errors::BoxRemoveMultiArchitecture, name: box_name, version: box_version, provider: box_provider.to_s, architectures: box_info.values.first.values.first.sort.join(", ") end # If architecture was given, check the provider for the version has it if box_architecture if !box_info.values.first.values.first.include?(box_architecture) raise Errors::BoxRemoveArchitectureNotFound, name: box_name, version: box_version, provider: box_provider.to_s, architecture: box_architecture, architectures: box_info.values.first.values.first.sort.join(", ") else box_info.values.first.values.first.delete_if { |v| v != box_architecture } end end end end end box_info.each do |version, provider_info| provider_info.each do |provider, architecture_info| provider = provider.to_sym architecture_info.each do |architecture| box = env[:box_collection].find( box_name, provider, version, architecture ) # Verify that this box is not in use by an active machine, # otherwise warn the user. users = box.in_use?(env[:machine_index]) || [] users = users.find_all { |u| u.valid?(env[:home_path]) } if !users.empty? # Build up the output to show the user. users = users.map do |entry| "#{entry.name} (ID: #{entry.id})" end.join("\n") force_key = :force_confirm_box_remove = I18n.t( "vagrant.commands.box.remove_in_use_query", name: box.name, architecture: box.architecture, provider: box.provider, version: box.version, users: users) + " " # Ask the user if we should do this stack = Builder.new.tap do |b| b.use Confirm, , force_key end # Keep used boxes, even if "force" is applied keep_used_boxes = env[:keep_used_boxes] result = env[:action_runner].run(stack, env) if !result[:result] || keep_used_boxes # They said "no", so continue with the next box next end end env[:ui].info(I18n.t("vagrant.commands.box.removing", name: box.name, architecture: box.architecture, provider: box.provider, version: box.version)) box.destroy! env[:box_collection].clean(box.name) # Passes on the removed box to the rest of the middleware chain env[:box_removed] = box end end end @app.call(env) end |