Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consul_ui resource #218

Merged
merged 5 commits into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions libraries/consul_ui.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/cookbooks/consul_spec/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name 'consul_spec'

depends 'consul'
6 changes: 6 additions & 0 deletions test/cookbooks/consul_spec/recipes/consul_ui.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
consul_ui 'myconsul-ui' do
owner 'myconsul'
group 'myconsul'
version '0.5.1'
install_path '/opt'
end
16 changes: 16 additions & 0 deletions test/spec/libraries/consul_ui_spec.rb
Original file line number Diff line number Diff line change
@@ -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