Class: VagrantDockerCertificatesManager::Actions::Uninstall

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-docker-certificates-manager/actions/uninstall.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Uninstall

Returns a new instance of Uninstall.



14
# File 'lib/vagrant-docker-certificates-manager/actions/uninstall.rb', line 14

def initialize(app, env); @app = app; @env = env; end

Class Method Details

.perform_uninstall(cfg, env) ⇒ Hash

Removes a configured certificate when the current machine is the last owner.

Shared certificates stay installed until every machine recorded in the registry has released ownership.

Parameters:

  • cfg (#cert_path, #cert_name)

    Vagrant certificate configuration.

  • env (Hash, nil)

    Vagrant environment hash.

Returns:

  • (Hash)

    Normalized result with code, status, data, or error.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vagrant-docker-certificates-manager/actions/uninstall.rb', line 62

def self.perform_uninstall(cfg, env)
  fp_entry = Registry.find_by_path(cfg.cert_path)
  unless fp_entry
    return({ code: 1, status: "error",
             error: UiHelpers.t("errors.not_found_for_remove", path: cfg.cert_path) })
  end
  fp, rec = fp_entry
  mid = env && env[:machine]&.id

  if mid && Registry.release(fp, mid)
    return({ code: 0, status: "success", data: { kept: true, cert: rec["name"] } })
  end

  os = OS.detect
  ok = case os
       when :mac     then OS.mac_remove_by_fp(fp)
       when :linux   then OS.linux_uninstall_cert(rec["name"], nss: cfg.manage_nss_browsers,
                                                               firefox: cfg.manage_firefox)
       when :windows then OS.win_remove_by_fp(fp, disable_firefox: !Registry.others_for_os?(fp, "windows"))
       else return({ code: 2, status: "error", error: UiHelpers.t("errors.os_unsupported") })
       end
  Registry.untrack(fp) if ok
  ok ? { code: 0, status: "success" } : { code: 4, status: "error", error: UiHelpers.t("errors.remove_failed") }
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant-docker-certificates-manager/actions/uninstall.rb', line 16

def call(env)
  cfg = env[:machine].config.docker_certificates
  UiHelpers.set_locale!(cfg.locale || "en")
  if cfg.remove_on_destroy
    Ui.say(env, :info, "uninstall.start", name: cfg.cert_name)
    result = self.class.perform_uninstall(cfg, env)
    Ui.say(env, result[:status] == "success" ? :info : :warn,
           result[:status] == "success" ? "uninstall.success" : "uninstall.fail",
           name: cfg.cert_name)
  end
  purge_generated(cfg, env)
  @app.call(env)
end

#env_flag?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/vagrant-docker-certificates-manager/actions/uninstall.rb', line 50

def env_flag?(name)
  ENV[name].to_s == "1"
end

#purge_generated(cfg, env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vagrant-docker-certificates-manager/actions/uninstall.rb', line 30

def purge_generated(cfg, env)
  # Generated material is purged only through explicit env flags to avoid
  # deleting a CA/server certificate that may be shared outside this action.
  files = []
  files += [Generator::CA_CERT, Generator::CA_KEY, Generator::CRL] if env_flag?("VDCM_PURGE_CA_ON_DESTROY")
  files += [Generator::SRV_CRT, Generator::SRV_KEY]                if env_flag?("VDCM_PURGE_SERVER_ON_DESTROY")
  return if files.empty?

  dir = cfg.cert_dir
  files.uniq.each do |name|
    path = File.join(dir, name)
    next unless File.exist?(path)

    File.delete(path)
    Ui.say(env, :info, "purge.removed", path: path)
  rescue StandardError => e
    Ui.say(env, :warn, "purge.failed", path: path, error: e.message)
  end
end