Skip to content

Commit

Permalink
Merge pull request #403 from iwagnerclgx/master
Browse files Browse the repository at this point in the history
Added experimental Windows support
  • Loading branch information
solarkennedy committed Jan 27, 2018
2 parents df6e3ca + 64503a0 commit 2049800
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 15 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ new versions of consul. Pin to the version that works for your setup!
* If installing from docker, you *must* ensure puppetlabs-docker_platform module is available.
* Optionally installs a user to run it under
* Installs a configuration file (/etc/consul/config.json)
* Manages the consul service via upstart, sysv, or systemd
* Manages the consul service via upstart, sysv, systemd, or nssm.
* Optionally installs the Web UI

## Usage
Expand Down Expand Up @@ -281,6 +281,30 @@ The optional parameters only need to be specified if you require changes from de
Depends on the JSON gem, or a modern ruby. (Ruby 1.8.7 is not officially supported)
Depending on the version of puppetserver deployed it may not be new enough (1.8.0 is too old, 2.0.3 is known to work).

## Windows Experimental Support

Windows service support is provided by [NSSM](https://nssm.cc), which is expected to be installed separately. The following caveats apply:

* The user and group parameter must be different (Administrator/Administrators recommended).
* The NSSM executable must be passed as a parameter like in the following example:

```puppet
class { '::consul':
nssm_exec => 'C:/Program Files/nssm/nssm-2.24/win64/nssm.exe',
bin_dir => 'C:/Consul',
user => 'Administrator',
group => 'Administrators',
config_hash => {
'bootstrap_expect' => 1,
'data_dir' => 'C:/Consul',
'datacenter' => 'dc1',
'log_level' => 'INFO',
'node_name' => 'server',
'server' => true,
}
}
```

## Consul Template

[Consul Template](https://github.com/hashicorp/consul-template) is a piece of
Expand Down
4 changes: 4 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
# [*manage_user*]
# Whether to create/manage the user that should own consul's configuration files.
#
# [*nssm_exec*]
# Location of nssm windows binary for service management
#
# [*os*]
# OS component in the name of the archive file containing the consul binary.
#
Expand Down Expand Up @@ -157,6 +160,7 @@
Boolean $manage_group = $consul::params::manage_group,
Boolean $manage_service = $consul::params::manage_service,
Boolean $manage_user = $consul::params::manage_user,
Optional[String] $nssm_exec = undef,
$os = $consul::params::os,
$package_ensure = $consul::params::package_ensure,
$package_name = $consul::params::package_name,
Expand Down
38 changes: 28 additions & 10 deletions manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
#
class consul::install {

case $::operatingsystem {
'windows': {
$binary_name = 'consul.exe'
$binary_mode = '0775'
$data_dir_mode = '775'
$binary_owner = 'Administrators'
$binary_group = 'Administrators'
}
default: {
$binary_name = 'consul'
$binary_mode = '0555'
$data_dir_mode = '755'
# 0 instead of root because OS X uses "wheel".
$binary_owner = 'root'
$binary_group = 0
}
}

if $consul::data_dir {
file { $consul::data_dir:
ensure => 'directory',
Expand Down Expand Up @@ -33,27 +51,27 @@
$install_path,
"${install_path}/consul-${consul::version}"]:
ensure => directory,
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0555';
owner => $binary_owner,
group => $binary_group,
mode => $binary_mode,
}
-> archive { "${install_path}/consul-${consul::version}.${consul::download_extension}":
ensure => present,
source => $consul::real_download_url,
proxy_server => $consul::proxy_server,
extract => true,
extract_path => "${install_path}/consul-${consul::version}",
creates => "${install_path}/consul-${consul::version}/consul",
creates => "${install_path}/consul-${consul::version}/${binary_name}",
}
-> file {
"${install_path}/consul-${consul::version}/consul":
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0555';
"${consul::bin_dir}/consul":
"${install_path}/consul-${consul::version}/${binary_name}":
owner => $binary_owner,
group => $binary_group,
mode => $binary_mode;
"${consul::bin_dir}/${binary_name}":
ensure => link,
notify => $do_notify_service,
target => "${install_path}/consul-${consul::version}/consul";
target => "${install_path}/consul-${consul::version}/${binary_name}";
}
}
'package': {
Expand Down
12 changes: 8 additions & 4 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@
$watches = {}

case $::architecture {
'x86_64', 'amd64': { $arch = 'amd64' }
'i386': { $arch = '386' }
/^arm.*/: { $arch = 'arm' }
default: {
'x86_64', 'x64', 'amd64': { $arch = 'amd64' }
'i386': { $arch = '386' }
/^arm.*/: { $arch = 'arm' }
default: {
fail("Unsupported kernel architecture: ${::architecture}")
}
}

$config_dir = $::osfamily ? {
'FreeBSD' => '/usr/local/etc/consul.d',
'windows' => 'c:/Consul/config',
default => '/etc/consul'
}

Expand Down Expand Up @@ -112,6 +113,9 @@
} elsif $::operatingsystem == 'FreeBSD' {
$init_style = 'freebsd'
$shell = '/usr/sbin/nologin'
} elsif $::operatingsystem == 'windows' {
$init_style = 'unmanaged'
$shell = undef
} else {
fail('Cannot determine init_style, unsupported OS')
}
Expand Down
6 changes: 6 additions & 0 deletions manifests/run_service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
}

if $consul::manage_service == true and $consul::install_method != 'docker' {
if $::operatingsystem == 'windows' {
class { 'consul::windows_service':
before => Service['consul'],
}
}

service { 'consul':
ensure => $consul::service_ensure,
name => $service_name,
Expand Down
53 changes: 53 additions & 0 deletions manifests/windows_service.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# == Class consul::windows_service
#
# Installs consul windows server
# == Parameters
#
# [*nssm_version*]
# nssm version to download
#
# [*nssm_download_url*]
# nssm version to download
#
# [*nssm_download_url_base*]
# nssm version to download
#
class consul::windows_service {

$app_dir = regsubst($consul::bin_dir, '\/', '\\', 'G')
$app_exec = "${app_dir}\\consul.exe"
$agent_args = regsubst($consul::config_dir, '\/', '\\', 'G')
$app_args = "agent -config-dir=${agent_args}"
$app_log_path = "${app_dir}\\logs"
$app_log_file = 'consul.log'
$app_log = "${app_log_path}//${app_log_file}"

include '::archive'

file { $app_log_path:
ensure => 'directory',
owner => 'Administrator',
group => 'Administrators',
mode => '0755',
}
-> exec { 'consul_service_install':
cwd => $consul::bin_dir,
command => "&'${consul::nssm_exec}' install Consul ${app_exec}",
unless => 'if((get-service -name consul -ErrorAction SilentlyContinue).count -ne 1){exit 1}',
logoutput => true,
provider => 'powershell',
notify => Exec['consul_service_set_parameters']
}
file { "${consul::bin_dir}/set_service_parameters.ps1":
ensure => 'present',
content => template('consul/set_service_parameters.ps1.erb'),
notify => Exec['consul_service_set_parameters']
}
-> exec { 'consul_service_set_parameters':
cwd => $consul::bin_dir,
command => "${consul::bin_dir}/set_service_parameters.ps1",
refreshonly => true,
logoutput => true,
provider => 'powershell',
}
}
11 changes: 11 additions & 0 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
{
"name": "camptocamp/systemd",
"version_requirement": ">= 1.1.1 < 2.0.0"
},
{
"name": "puppetlabs/powershell",
"version_requirement": ">= 2.1.3 < 3.0.0"
}
],
"operatingsystem_support": [
Expand Down Expand Up @@ -119,6 +123,13 @@
"11",
"12"
]
},
{
"operatingsystem": "Windows",
"operatingsystemrelease": [
"Server 2012 R2",
"Server 2016"
]
}
]
}
14 changes: 14 additions & 0 deletions templates/set_service_parameters.ps1.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
& '<%= scope['consul::nssm_exec'] %>' set Consul AppParameters "<%= @app_args %>"
& '<%= scope['consul::nssm_exec'] %>' set Consul AppDirectory "<%= @app_dir %>"
& '<%= scope['consul::nssm_exec'] %>' set Consul AppStopMethodConsole 15000
& '<%= scope['consul::nssm_exec'] %>' set Consul AppStopMethodSkip 2
& '<%= scope['consul::nssm_exec'] %>' set Consul AppStopMethodSkip 4
& '<%= scope['consul::nssm_exec'] %>' set Consul AppStderr "<%= @app_log %>"
& '<%= scope['consul::nssm_exec'] %>' set Consul AppStdout "<%= @app_log %>"
& '<%= scope['consul::nssm_exec'] %>' set Consul AppRotateFiles 1
& '<%= scope['consul::nssm_exec'] %>' set Consul AppRotateOnline 0
& '<%= scope['consul::nssm_exec'] %>' set Consul AppRotateBytes 1024000
& '<%= scope['consul::nssm_exec'] %>' set Consul AppRestartDelay 0
& '<%= scope['consul::nssm_exec'] %>' set Consul AppExit Default Exit
& '<%= scope['consul::nssm_exec'] %>' set Consul Start SERVICE_AUTO_START
exit 0

0 comments on commit 2049800

Please sign in to comment.