Class: VagrantTrellisCert::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_trellis_cert/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



8
9
10
# File 'lib/vagrant_trellis_cert/command.rb', line 8

def self.synopsis
  "trust all Trellis self-signed certificates"
end

Instance Method Details

#canonical_hostsObject



48
49
50
51
52
53
54
55
56
# File 'lib/vagrant_trellis_cert/command.rb', line 48

def canonical_hosts
  site_hosts.map do |host|
    if !host.is_a?(Hash) || !host.has_key?("canonical")
      fail_with_message File.read(File.join(@path, "roles/common/templates/site_hosts.j2")).sub!("{{ env }}", "development").gsub!(/com$/, "dev")
    end

    host.fetch("canonical")
  end
end

#config_fileObject



72
73
74
# File 'lib/vagrant_trellis_cert/command.rb', line 72

def config_file
  File.join(@path, "group_vars", "development", "wordpress_sites.yml")
end

#executeObject



12
13
14
15
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
# File 'lib/vagrant_trellis_cert/command.rb', line 12

def execute
  options = {}
  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant trellis-cert [--path <path>]"
    o.separator ""
    o.version = VagrantTrellisCert::Identity.version
    o.program_name = "vagrant trellis-cert"

    o.on("--path <path>", String, "Path to the Trellis root") do |path|
      options[:path] = path
    end
  end
  argv = parse_options(opts)

  @path = options[:path] || "."

  FileUtils.rm_rf(tmp_path)
  FileUtils.mkdir_p(tmp_path)

  canonical_hosts.each do |host|
    system("openssl s_client -showcerts -connect #{host}:443 </dev/null 2>/dev/null | openssl x509 -outform PEM > #{tmp_path}/#{host}.pem 2>/dev/null")
  end

  canonical_hosts.each do |host|
    success = system("security add-trusted-cert -k ~/Library/Keychains/login.keychain #{tmp_path}/#{host}.pem >/dev/null 2>/dev/null")

    if success
      @env.ui.success("#{host} certificate imported successfully")
    else
      @env.ui.error("#{host} certificate import failed")
    end
  end

  FileUtils.rm_rf(tmp_path)
end

#fail_with_message(msg) ⇒ Object



76
77
78
# File 'lib/vagrant_trellis_cert/command.rb', line 76

def fail_with_message(msg)
  fail Vagrant::Errors::VagrantError.new, msg
end

#site_hostsObject



58
59
60
# File 'lib/vagrant_trellis_cert/command.rb', line 58

def site_hosts
  sites.flat_map { |(_name, site)| site["site_hosts"] }
end

#sitesObject



62
63
64
65
66
67
68
69
70
# File 'lib/vagrant_trellis_cert/command.rb', line 62

def sites
  unless File.exists?(config_file)
    fail_with_message "#{config_file} was not found. Please run `$ vagrant trellis-cert` with `--path` option"
  end

  YAML.load_file(config_file)["wordpress_sites"].tap do |sites|
    fail_with_message "No sites found in #{config_file}." if sites.to_h.empty?
  end
end

#tmp_pathObject



80
81
82
# File 'lib/vagrant_trellis_cert/command.rb', line 80

def tmp_path
  "#{@env.tmp_path}/#{VagrantTrellisCert::Identity.name}"
end