Class: VagrantDockerNetworksManager::ActionDestroy
- Inherits:
-
Object
- Object
- VagrantDockerNetworksManager::ActionDestroy
- Defined in:
- lib/vagrant-docker-networks-manager/action.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, _env) ⇒ ActionDestroy
constructor
Vagrant middleware entry point that removes owned Docker networks on destroy.
Constructor Details
#initialize(app, _env) ⇒ ActionDestroy
Vagrant middleware entry point that removes owned Docker networks on destroy.
111 |
# File 'lib/vagrant-docker-networks-manager/action.rb', line 111 def initialize(app, _env); @app = app; end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/vagrant-docker-networks-manager/action.rb', line 113 def call(env) UiHelpers.setup_i18n! cfg = env[:machine].config.docker_network UiHelpers.setup_locale_from_config!(cfg) ui = env[:ui] unless Util.docker_available? UiHelpers.error(ui, "#{UiHelpers.e(:error)} #{I18n.t('vdnm.errors.docker_unavailable')}") return @app.call(env) end name = cfg.network_name mid = env[:machine].id unless cfg.cleanup_on_destroy @app.call(env) return end if created_by_this_machine?(env, name) || owned_by_this_machine?(name, mid) UiHelpers.say(ui, "#{UiHelpers.e(:broom)} #{I18n.t('vdnm.messages.remove_network', name: name)}") if Util.docker_network_exists?(name) out, _e, st = Open3.capture3("docker", "network", "inspect", name) if st.success? begin info = JSON.parse(out).first (info["Containers"] || {}).values.each do |c| cn = c["Name"] UiHelpers.say(ui, "#{UiHelpers.e(:ongoing)} #{I18n.t('vdnm.log.disconnect_container', name: cn)}") Util.sh!("network", "disconnect", "--force", name, cn) if ENV["VDNM_DESTROY_WITH_CONTAINERS"] == "1" UiHelpers.say(ui, "#{UiHelpers.e(:ongoing)} #{I18n.t('vdnm.log.remove_container', name: cn)}") Util.sh!("rm", "-f", cn) end end rescue => e UiHelpers.warn(ui, "failed to parse containers for '#{name}': #{e.}") end end if Util.sh!("network", "rm", name) delete_marker(env, name) UiHelpers.say(ui, "#{UiHelpers.e(:success)} #{I18n.t('vdnm.log.ok')}") else UiHelpers.warn(ui, "#{UiHelpers.e(:warning)} #{I18n.t('vdnm.errors.remove_failed')}") end else delete_marker(env, name) UiHelpers.say(ui, "#{UiHelpers.e(:info)} #{I18n.t('vdnm.messages.nothing_to_do')}") end else UiHelpers.say(ui, "#{UiHelpers.e(:broom)} #{I18n.t('vdnm.messages.nothing_to_do')}") end @app.call(env) end |