diff --git a/Berksfile b/Berksfile index 9042e42c..6831b2a6 100644 --- a/Berksfile +++ b/Berksfile @@ -1,3 +1,11 @@ source 'https://supermarket.chef.io' cookbook 'chef-vault', git: 'https://github.com/johnbellone/chef-vault-cookbook' metadata + +group :test do + cookbook 'consul_spec', path: 'test/cookbooks/consul_spec' +end + +group :integration do + cookbook 'consul_spec', path: 'test/cookbooks/consul_spec' +end diff --git a/README.md b/README.md index 7b2f5f81..9e476ca6 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,36 @@ into the resource. This could potentially be a *very dangerous* operation. You should absolutely understand what you are doing. By the nature of this command it is _impossible_ for it to be idempotent. +### UI + +`consul_ui` resource can be used to download and extract the +[consul web UI](https://www.consul.io/intro/getting-started/ui.html). +It can be done with a block like this: + +```ruby +consul_ui 'consul-ui' do + owner node['consul']['service_user'] + group node['consul']['service_group'] + version node['consul']['version'] +end +``` + +Assuming consul version `0.5.2` above block would create `/srv/consul-ui/0.5.2` +and symlink `/srv/consul-ui/current`. + +It does not change agent's configuration by itself. +`consul_config` resource should be modified explicitly in order to host the web page. + +```ruby +consul_config 'consul' do + ... + ui_dir '/srv/consul-ui/current/dist' +end +``` + +This is optional, because consul UI can be hosted by any web server. + + [0]: http://blog.vialstudios.com/the-environment-cookbook-pattern/#theapplicationcookbook [1]: http://consul.io [2]: http://blog.vialstudios.com/the-environment-cookbook-pattern#thewrappercookbook diff --git a/libraries/consul_ui.rb b/libraries/consul_ui.rb new file mode 100644 index 00000000..6b347695 --- /dev/null +++ b/libraries/consul_ui.rb @@ -0,0 +1,81 @@ +require 'poise' + +module ConsulCookbook + module Resource + # Resource for managing the Consul web UI installation. + class ConsulUI < Chef::Resource + include Poise + provides(:consul_ui) + default_action(:install) + + # @!attribute version + # @return [String] + attribute(:version, kind_of: String, required: true) + + # @!attribute install_path + # @return [String] + attribute(:install_path, kind_of: String, default: '/srv') + + # @!attribute owner + # @return [String] + attribute(:owner, kind_of: String, default: 'consul') + + # @!attribute group + # @return [String] + attribute(:group, kind_of: String, default: 'consul') + + # @!attribute binary_url + # @return [String] + attribute(:binary_url, kind_of: String, default: 'https://dl.bintray.com/mitchellh/consul/%{filename}.zip') + + # @!attribute source_url + # @return [String] + attribute(:source_url, kind_of: String) + + def default_environment + { GOMAXPROCS: [node['cpu']['total'], 2].max.to_s, PATH: '/usr/local/bin:/usr/bin:/bin' } + end + + def binary_checksum + node['consul']['checksums'].fetch(binary_filename) + end + + def binary_filename + [version, 'web_ui'].join('_') + end + end + end + + module Provider + # Provider for managing the Consul web UI installation. + class ConsulUI < Chef::Provider + include Poise + provides(:consul_ui) + + def action_install + notifying_block do + libartifact_file "#{new_resource.name}-#{new_resource.version}" do + artifact_name new_resource.name + artifact_version new_resource.version + owner new_resource.owner + group new_resource.group + install_path new_resource.install_path + remote_url new_resource.binary_url % { filename: new_resource.binary_filename } + remote_checksum new_resource.binary_checksum + end + end + end + + def action_uninstall + notifying_block do + libartifact_file "#{new_resource.name}-#{new_resource.version}" do + action :delete + artifact_name new_resource.name + artifact_version new_resource.version + install_path new_resource.install_path + end + end + end + end + end +end diff --git a/test/cookbooks/consul_spec/metadata.rb b/test/cookbooks/consul_spec/metadata.rb new file mode 100644 index 00000000..a40a72e5 --- /dev/null +++ b/test/cookbooks/consul_spec/metadata.rb @@ -0,0 +1,3 @@ +name 'consul_spec' + +depends 'consul' diff --git a/test/cookbooks/consul_spec/recipes/consul_ui.rb b/test/cookbooks/consul_spec/recipes/consul_ui.rb new file mode 100644 index 00000000..a52abf4b --- /dev/null +++ b/test/cookbooks/consul_spec/recipes/consul_ui.rb @@ -0,0 +1,6 @@ + consul_ui 'myconsul-ui' do + owner 'myconsul' + group 'myconsul' + version '0.5.1' + install_path '/opt' + end diff --git a/test/spec/libraries/consul_ui_spec.rb b/test/spec/libraries/consul_ui_spec.rb new file mode 100644 index 00000000..0b765be6 --- /dev/null +++ b/test/spec/libraries/consul_ui_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' +require_relative '../../../libraries/consul_ui' + +describe ConsulCookbook::Resource::ConsulUI do + step_into(:consul_ui) + + context 'consul ui install' do + recipe 'consul_spec::consul_ui' + + it do is_expected.to create_libartifact_file('myconsul-ui-0.5.1') + .with(owner: 'myconsul',group: 'myconsul', + remote_url: "https://dl.bintray.com/mitchellh/consul/0.5.1_web_ui.zip", + install_path: '/opt') + end + end +end