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

Added Windows Support #259

Merged
merged 5 commits into from
Jan 20, 2016
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
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ Style/GuardClause:
Enabled: false
Style/PercentLiteralDelimiters:
Enabled: false
Style/ModuleFunction:
Enabled: false
30 changes: 24 additions & 6 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
#
# Copyright 2014-2016, Bloomberg Finance L.P.
#
::Chef::Node.send(:include, ConsulCookbook::Helpers)

This comment was marked as outdated.

This comment was marked as outdated.


# Only used on Linux
default['consul']['service_name'] = 'consul'
default['consul']['service_user'] = 'consul'
default['consul']['service_group'] = 'consul'

default['consul']['config']['path'] = '/etc/consul.json'
default['consul']['config']['bag_name'] = 'secrets'
default['consul']['config']['bag_item'] = 'consul'
default['consul']['config']['data_dir'] = '/var/lib/consul'
default['consul']['config']['ca_file'] = '/etc/consul/ssl/CA/ca.crt'
default['consul']['config']['cert_file'] = '/etc/consul/ssl/certs/consul.crt'

default['consul']['config']['path'] = join_path config_prefix_path, 'consul.json'
default['consul']['config']['data_dir'] = join_path data_prefix_path, 'data'
default['consul']['config']['ca_file'] = join_path config_prefix_path, 'ssl', 'CA', 'ca.crt'
default['consul']['config']['cert_file'] = join_path config_prefix_path, 'ssl', 'certs', 'consul.crt'
default['consul']['config']['key_file'] = join_path config_prefix_path, 'ssl', 'private', 'consul.key'

default['consul']['config']['client_addr'] = '0.0.0.0'
default['consul']['config']['key_file'] = '/etc/consul/ssl/private/consul.key'
default['consul']['config']['ports'] = {
'dns' => 8600,
'http' => 8500,
Expand All @@ -27,13 +32,26 @@

default['consul']['diplomat_version'] = nil

default['consul']['service']['config_dir'] = join_path config_prefix_path, 'conf.d'
default['consul']['service']['data_dir'] = join_path data_prefix_path, 'data'

default['consul']['service']['install_method'] = 'binary'
default['consul']['service']['config_dir'] = '/etc/consul'
default['consul']['service']['binary_url'] = "https://releases.hashicorp.com/consul/%{version}/%{filename}.zip" # rubocop:disable Style/StringLiterals

default['consul']['service']['source_url'] = 'https://github.com/hashicorp/consul'

default['consul']['version'] = '0.6.2'

# Windows only
default['consul']['service']['nssm_params'] = {
'AppDirectory' => join_path(data_prefix_path, 'data'),
'AppStdout' => join_path(config_prefix_path, 'stdout.log'),
'AppStderr' => join_path(config_prefix_path, 'error.log'),
'AppRotateFiles' => 1,
'AppRotateOnline' => 1,
'AppRotateBytes' => 20_000_000
}

default['consul']['checksums'] = {
'consul_0.5.0_darwin_amd64' => '24d9758c873e9124e0ce266f118078f87ba8d8363ab16c2e59a3cd197b77e964',
'consul_0.5.0_linux_386' => '4b6147c30596a30361d4753d409f8a1af9518f54f5ed473a4c4ac973738ac0fd',
Expand Down
50 changes: 32 additions & 18 deletions libraries/consul_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
# Copyright 2014-2016, Bloomberg Finance L.P.
#
require 'poise'
require_relative 'helpers'

module ConsulCookbook
module Resource
# @since 1.0.0
class ConsulConfig < Chef::Resource
include Poise(fused: true)
include ConsulCookbook::Helpers
provides(:consul_config)

# @!attribute path
Expand Down Expand Up @@ -114,49 +116,61 @@ def tls?
[new_resource.ca_file, new_resource.cert_file, new_resource.key_file].each do |filename|
directory ::File.dirname(filename) do
recursive true
owner new_resource.owner
group new_resource.group
mode '0755'
if node['os'].eql? 'linux'

This comment was marked as outdated.

This comment was marked as outdated.

owner new_resource.owner
group new_resource.group
mode '0755'
end
end
end

item = chef_vault_item(new_resource.bag_name, new_resource.bag_item)
file new_resource.ca_file do
content item['ca_certificate']
mode '0644'
owner new_resource.owner
group new_resource.group
if node['os'].eql? 'linux'
owner new_resource.owner
group new_resource.group
mode '0644'
end
end

file new_resource.cert_file do
content item['certificate']
mode '0644'
owner new_resource.owner
group new_resource.group
if node['os'].eql? 'linux'
owner new_resource.owner
group new_resource.group
mode '0644'
end
end

file new_resource.key_file do
sensitive true
content item['private_key']
mode '0640'
owner new_resource.owner
group new_resource.group
if node['os'].eql? 'linux'
owner new_resource.owner
group new_resource.group
mode '0640'
end
end
end

directory ::File.dirname(new_resource.path) do
recursive true
owner new_resource.owner
group new_resource.group
mode '0755'
if node['os'].eql? 'linux'
owner new_resource.owner
group new_resource.group
mode '0755'
end
not_if { ::File.dirname(new_resource.path) == '/etc' }
end

file new_resource.path do
owner new_resource.owner
group new_resource.group
if node['os'].eql? 'linux'
owner new_resource.owner
group new_resource.group
mode '0640'
end
content new_resource.to_json
mode '0640'
end
end
end
Expand Down
19 changes: 12 additions & 7 deletions libraries/consul_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ module Resource
# @since 1.0.0
class ConsulDefinition < Chef::Resource
include Poise(fused: true)
include ConsulCookbook::Helpers
provides(:consul_definition)
default_action(:create)

# @!attribute path
# @return [String]
attribute(:path, kind_of: String, default: lazy { "/etc/consul/#{name}.json" })
attribute(:path, kind_of: String, default: lazy { join_path config_prefix_path, "#{name}.json" })

# @!attribute user
# @return [String]
Expand Down Expand Up @@ -44,16 +45,20 @@ def to_json
notifying_block do
directory ::File.dirname(new_resource.path) do
recursive true
owner new_resource.user
group new_resource.group
mode '0755'
if node['os'].eql? 'linux'
owner new_resource.user
group new_resource.group
mode '0755'
end
end

file new_resource.path do
owner new_resource.user
group new_resource.group
content new_resource.to_json
mode '0640'
if node['os'].eql? 'linux'
owner new_resource.user
group new_resource.group
mode '0640'
end
end
end
end
Expand Down
135 changes: 11 additions & 124 deletions libraries/consul_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
# Copyright 2014-2016, Bloomberg Finance L.P.
#
require 'poise_service/service_mixin'
require_relative 'helpers'

module ConsulCookbook
module Resource
# Resource for managing the Consul service on an instance.
# @since 1.0.0
class ConsulService < Chef::Resource
include Poise
provides(:consul_service)
provides :consul_service
actions :enable, :disable
include PoiseService::ServiceMixin
include ConsulCookbook::Helpers

# @!attribute version
# @return [String]
Expand All @@ -25,11 +28,11 @@ class ConsulService < Chef::Resource

# @!attribute install_path
# @return [String]
attribute(:install_path, kind_of: String, default: '/srv')
attribute(:install_path, kind_of: String, default: lazy { windows? ? config_prefix_path : '/srv' })

# @!attribute config_file
# @return [String]
attribute(:config_file, kind_of: String, default: '/etc/consul.json')
attribute(:config_file, kind_of: String, default: lazy { node['consul']['config']['path'] })

# @!attribute user
# @return [String]
Expand Down Expand Up @@ -57,131 +60,15 @@ class ConsulService < Chef::Resource

# @!attribute data_dir
# @return [String]
attribute(:data_dir, kind_of: String, default: '/var/lib/consul')
attribute(:data_dir, kind_of: String, default: lazy { node['consul']['service']['data_dir'] })

# @!attribute config_dir
# @return [String]
attribute(:config_dir, kind_of: String, default: '/etc/consul')
attribute(:config_dir, kind_of: String, default: lazy { node['consul']['config']['config_dir'] })

def default_environment
{
'GOMAXPROCS' => [node['cpu']['total'], 2].max.to_s,
'PATH' => '/usr/local/bin:/usr/bin:/bin'
}
end

def command
"/usr/bin/env consul agent -config-file=#{config_file} -config-dir=#{config_dir}"
end

def binary_checksum
node['consul']['checksums'].fetch(binary_filename)
end

def binary_filename
arch = node['kernel']['machine'] =~ /x86_64/ ? 'amd64' : '386'
['consul', version, node['os'], arch].join('_')
end
end
end

module Provider
# Provider for managing the Consul service on an instance.
# @since 1.0.0
class ConsulService < Chef::Provider
include Poise
provides(:consul_service)
include PoiseService::ServiceMixin

def action_enable
notifying_block do
package new_resource.package_name do
version new_resource.version unless new_resource.version.nil?
only_if { new_resource.install_method == 'package' }
end

if node['platform'] == 'windows'
include_recipe 'chocolatey::default'

chocolatey new_resource.package_name do
version new_resource.version
end
end

if new_resource.install_method == 'binary'
artifact = libartifact_file "consul-#{new_resource.version}" do
artifact_name 'consul'
artifact_version new_resource.version
install_path new_resource.install_path
remote_url new_resource.binary_url % { version: new_resource.version, filename: new_resource.binary_filename }
remote_checksum new_resource.binary_checksum
end

link '/usr/local/bin/consul' do
to ::File.join(artifact.current_path, 'consul')
end
end

if new_resource.install_method == 'source'
include_recipe 'golang::default'

source_dir = directory ::File.join(new_resource.install_path, 'consul', 'src') do
recursive true
owner new_resource.user
group new_resource.group
mode '0755'
end

git ::File.join(source_dir.path, "consul-#{new_resource.version}") do
repository new_resource.source_url
reference new_resource.version
action :checkout
end

golang_package 'github.com/hashicorp/consul' do
action :install
end

directory ::File.join(new_resource.install_path, 'bin') do
recursive true
owner new_resource.user
group new_resource.group
mode '0755'
end

link ::File.join(new_resource.install_path, 'bin', 'consul') do
to ::File.join(source_dir.path, "consul-#{new_resource.version}", 'consul')
end
end

[new_resource.data_dir, new_resource.config_dir].each do |dirname|
directory dirname do
recursive true
owner new_resource.user
group new_resource.group
mode '0755'
end
end
end
super
end

def action_disable
notifying_block do
file new_resource.config_file do
action :delete
end
end
super
end

def service_options(service)
service.command(new_resource.command)
service.directory(new_resource.data_dir)
service.user(new_resource.user)
service.environment(new_resource.environment)
service.restart_on_update(true)
end
# @!attribute nssm_params
# @return [String]
attribute(:nssm_params, kind_of: Hash, default: lazy { node['consul']['service']['nssm_params'] })
end
end
end
Loading