From 4927a08ef4ec38e365a7caa35b59eff204c76723 Mon Sep 17 00:00:00 2001 From: Tomasz Setkowski Date: Sat, 12 Sep 2015 16:15:25 +0000 Subject: [PATCH 1/5] #215 consul_ui resource and readme --- README.md | 28 +++++++++++++++ libraries/consul_ui.rb | 82 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 libraries/consul_ui.rb diff --git a/README.md b/README.md index 7b2f5f81..98763308 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,34 @@ 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 '/opt/consul-ui/current/dist' +end +``` + + [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..d0bb2be3 --- /dev/null +++ b/libraries/consul_ui.rb @@ -0,0 +1,82 @@ +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 + artifact = libartifact_file "consul-ui-#{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 + artifact = libartifact_file "consul-ui-#{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 From d09f0cf2a49efc3a2e9318d893cacfdff472b763 Mon Sep 17 00:00:00 2001 From: Tomasz Setkowski Date: Tue, 15 Sep 2015 07:18:15 +0000 Subject: [PATCH 2/5] #215 consul_ui resource chefspec tests --- Berksfile | 4 ++++ libraries/consul_ui.rb | 4 ++-- test/cookbooks/consul_spec/metadata.rb | 3 +++ test/cookbooks/consul_spec/recipes/consul_ui.rb | 6 ++++++ test/spec/libraries/consul_ui_spec.rb | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/cookbooks/consul_spec/metadata.rb create mode 100644 test/cookbooks/consul_spec/recipes/consul_ui.rb create mode 100644 test/spec/libraries/consul_ui_spec.rb diff --git a/Berksfile b/Berksfile index 9042e42c..41c21fbf 100644 --- a/Berksfile +++ b/Berksfile @@ -1,3 +1,7 @@ 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 diff --git a/libraries/consul_ui.rb b/libraries/consul_ui.rb index d0bb2be3..ae6d1b47 100644 --- a/libraries/consul_ui.rb +++ b/libraries/consul_ui.rb @@ -55,7 +55,7 @@ class ConsulUI < Chef::Provider def action_install notifying_block do - artifact = libartifact_file "consul-ui-#{new_resource.version}" do + artifact = libartifact_file "#{new_resource.name}-#{new_resource.version}" do artifact_name new_resource.name artifact_version new_resource.version owner new_resource.owner @@ -69,7 +69,7 @@ def action_install def action_uninstall notifying_block do - artifact = libartifact_file "consul-ui-#{new_resource.version}" do + artifact = libartifact_file "#{new_resource.name}-#{new_resource.version}" do action :delete artifact_name new_resource.name artifact_version new_resource.version 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 From 6b8f6eca727461d3b517651493880b189bbf7e88 Mon Sep 17 00:00:00 2001 From: Tomasz Setkowski Date: Tue, 15 Sep 2015 07:29:20 +0000 Subject: [PATCH 3/5] #215 fix readme --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 98763308..9e476ca6 100644 --- a/README.md +++ b/README.md @@ -164,10 +164,12 @@ It does not change agent's configuration by itself. ```ruby consul_config 'consul' do ... - ui_dir '/opt/consul-ui/current/dist' + 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 From 38761c0310d265629ac07d2f1814af5c85b7cd8f Mon Sep 17 00:00:00 2001 From: Tomasz Setkowski Date: Tue, 15 Sep 2015 07:35:22 +0000 Subject: [PATCH 4/5] #215 style fixes --- libraries/consul_ui.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libraries/consul_ui.rb b/libraries/consul_ui.rb index ae6d1b47..6b347695 100644 --- a/libraries/consul_ui.rb +++ b/libraries/consul_ui.rb @@ -26,7 +26,7 @@ class ConsulUI < Chef::Resource # @!attribute binary_url # @return [String] - attribute(:binary_url, kind_of: String, default: "https://dl.bintray.com/mitchellh/consul/%{filename}.zip") + attribute(:binary_url, kind_of: String, default: 'https://dl.bintray.com/mitchellh/consul/%{filename}.zip') # @!attribute source_url # @return [String] @@ -43,7 +43,6 @@ def binary_checksum def binary_filename [version, 'web_ui'].join('_') end - end end @@ -55,7 +54,7 @@ class ConsulUI < Chef::Provider def action_install notifying_block do - artifact = libartifact_file "#{new_resource.name}-#{new_resource.version}" do + libartifact_file "#{new_resource.name}-#{new_resource.version}" do artifact_name new_resource.name artifact_version new_resource.version owner new_resource.owner @@ -69,7 +68,7 @@ def action_install def action_uninstall notifying_block do - artifact = libartifact_file "#{new_resource.name}-#{new_resource.version}" do + libartifact_file "#{new_resource.name}-#{new_resource.version}" do action :delete artifact_name new_resource.name artifact_version new_resource.version From bfe551cc3572117a87b45dfea9b8f38171d1fde1 Mon Sep 17 00:00:00 2001 From: Tomasz Setkowski Date: Tue, 15 Sep 2015 07:37:44 +0000 Subject: [PATCH 5/5] #215 style fixes --- Berksfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Berksfile b/Berksfile index 41c21fbf..6831b2a6 100644 --- a/Berksfile +++ b/Berksfile @@ -3,5 +3,9 @@ cookbook 'chef-vault', git: 'https://github.com/johnbellone/chef-vault-cookbook' metadata group :test do - cookbook "consul_spec", path: "test/cookbooks/consul_spec" + cookbook 'consul_spec', path: 'test/cookbooks/consul_spec' +end + +group :integration do + cookbook 'consul_spec', path: 'test/cookbooks/consul_spec' end