Skip to content

Commit

Permalink
Add custom resource for installing the Web UI.
Browse files Browse the repository at this point in the history
Fixes #297.

This was definitely something that was missed from 2.0.
  • Loading branch information
John Bellone authored and John Bellone committed Mar 18, 2016
1 parent 8fc21b8 commit a66a3c7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion libraries/consul_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ConsulConfig < Chef::Resource
attribute(:statsite_addr, kind_of: String)
attribute(:syslog_facility, kind_of: String)
attribute(:ui, equal_to: [true, false], default: false)
attribute(:ui_dir, kind_of: String)
attribute(:ui_dir, kind_of: String, default: lazy { '/var/lib/consul/ui' })
attribute(:verify_incoming, equal_to: [true, false], default: false)
attribute(:verify_outgoing, equal_to: [true, false], default: false)
attribute(:verify_server_hostname, equal_to: [true, false], default: false)
Expand Down
4 changes: 4 additions & 0 deletions libraries/consul_installation_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def action_remove
end
end

def consul_install_path
options[:git_path]
end

def consul_program
::File.join(options[:git_path], 'bin', 'consul')
end
Expand Down
2 changes: 1 addition & 1 deletion libraries/consul_installation_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def action_remove
end

def consul_program
options.fetch(:consul_program, '/usr/local/bin/consul')
options.fetch(:program, '/usr/local/bin/consul')
end
end
end
Expand Down
99 changes: 99 additions & 0 deletions libraries/consul_installation_webui.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#
# Cookbook: consul
# License: Apache 2.0
#
# Copyright 2014-2016, Bloomberg Finance L.P.
#
require 'poise'

module ConsulCookbook
module Provider
# A `consul_installation` provider which manages Consul WebUI
# installation from remote source URL.
# @action create
# @action remove
# @provides consul_installation
# @example
# consul_installation '0.5.0'
# @since 2.0
class ConsulInstallationWebui < Chef::Provider
include Poise(inversion: :consul_installation)
provides(:webui)
inversion_attribute('consul')

# Set the default inversion options.
# @return [Hash]
# @api private
def self.default_inversion_options(node, resource)
archive_basename = binary_basename(node, resource)
super.merge(
version: resource.version,
archive_url: default_archive_url % { version: resource.version, basename: archive_basename },
archive_basename: archive_basename,
archive_checksum: binary_checksum(node, resource),
extract_to: '/opt/consul-webui',
symlink_to: '/var/lib/consul/ui'
)
end

def action_create
archive_url = options[:archive_url] % {
version: options[:version],
basename: options[:archive_basename]
}

notifying_block do
directory ::File.join(options[:extract_to], new_resource.version) do
recursive true
end

zipfile options[:archive_basename] do
path ::File.join(options[:extract_to], new_resource.version)
source archive_url
checksum options[:archive_checksum]
not_if { ::File.exist?(::File.join(path, 'index.html')) }
notifies :create, "link[#{options[:symlink_to]}]", :immediately
end

link options[:symlink_to] do
to ::File.join(options[:extract_to], new_resource.version)
action :nothing
end
end
end

def action_remove
notifying_block do
directory ::File.join(options[:extract_to], new_resource.version) do
recursive true
action :delete
end
end
end

def consul_program
nil
end

def self.default_archive_url
"https://releases.hashicorp.com/consul/%{version}/%{basename}" # rubocop:disable Style/StringLiterals
end

def self.binary_basename(node, resource)
['consul', resource.version, 'web_ui'].join('_').concat('.zip')
end

def self.binary_checksum(_node, resource)
case resource.version
when '0.5.0' then '0081d08be9c0b1172939e92af5a7cf9ba4f90e54fae24a353299503b24bb8be9'
when '0.5.1', '0.5.2' then 'ad883aa52e1c0136ab1492bbcedad1210235f26d59719fb6de3ef6464f1ff3b1'
when '0.6.0' then '73c5e7ee50bb4a2efe56331d330e6d7dbf46335599c028344ccc4031c0c32eb0'
when '0.6.1' then 'afccdd540b166b778c7c0483becc5e282bbbb1ee52335bfe94bf757df8c55efc'
when '0.6.2' then 'f144377b8078df5a3f05918d167a52123089fc47b12fc978e6fb375ae93afc90'
when '0.6.3' then '93bbb300cacfe8de90fb3bd5ede7d37ae6ce014898edc520b9c96a676b2bbb72'
when '0.6.4' then '5f8841b51e0e3e2eb1f1dc66a47310ae42b0448e77df14c83bb49e0e0d5fa4b7'
end
end
end
end
end
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
license 'Apache 2.0'
description 'Application cookbook which installs and configures Consul.'
long_description 'Application cookbook which installs and configures Consul.'
version '2.0.0'
version '2.1.0'

recipe 'consul::default', 'Installs and configures the Consul service.'
recipe 'consul::client_gem', 'Installs the Consul Ruby client as a gem.'
Expand Down
7 changes: 7 additions & 0 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
notifies :reload, "consul_service[#{service_name}]", :delayed
end

consul_installation "Consul WebUI: #{node['consul']['version']}" do
provider :webui
version node['consul']['version']
options symlink_to: config.ui_dir if config.ui_dir
only_if { config.ui == true }
end

install = consul_installation node['consul']['version'] do |r|
if node['consul']['installation']
node['consul']['installation'].each_pair { |k, v| r.send(k, v) }
Expand Down

0 comments on commit a66a3c7

Please sign in to comment.