Skip to content

Commit

Permalink
Add support for Windows to use sensu::cli class
Browse files Browse the repository at this point in the history
  • Loading branch information
treydock committed Dec 12, 2019
1 parent 806b75e commit 33be195
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 39 deletions.
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,21 @@ This module will install packages, create configuration and start services neces

Plugin sync is required if the custom sensu types and providers are used.

This module has a soft dependency on the [puppetlabs/apt](https://forge.puppet.com/puppetlabs/apt) module (`>= 5.0.1 < 8.0.0`) for systems using `apt`.
#### Soft module dependencies

If using Puppet >= 6.0.0 there is a soft dependency on the [puppetlabs/yumrepo_core](https://forge.puppet.com/puppetlabs/yumrepo_core) module (`>= 1.0.1 < 2.0.0`) for systems using `yum`.
For systems using `apt`:
* [puppetlabs/apt](https://forge.puppet.com/puppetlabs/apt) module (`>= 5.0.1 < 8.0.0`)

If managing Windows there is a soft dependency on the [puppetlabs/chocolatey](https://forge.puppet.com/puppetlabs/chocolatey) module (`>= 3.0.0 < 5.0.0`).
For systems using `yum` and Puppet >= 6.0.0:
* [puppetlabs/yumrepo_core](https://forge.puppet.com/puppetlabs/yumrepo_core) module (`>= 1.0.1 < 2.0.0`)

If managing Windows and defining `package_source`, there is a soft dependency on the [puppet/archive](https://forge.puppet.com/puppet/archive) module (`>= 3.0.0 < 5.0.0`).
For Windows:
* [puppetlabs/chocolatey](https://forge.puppet.com/puppetlabs/chocolatey) module (`>= 3.0.0 < 5.0.0`)
* [puppet/windows_env](https://forge.puppet.com/puppet/windows_env) module (`>= 3.0.0 < 4.0.0`)
* [puppet/archive](https://forge.puppet.com/puppet/archive) module (`>= 3.0. 0 < 5.0.0`)

If managing Windows and defining `service_env_vars` there is a soft depedency on [puppet/windows_env](https://forge.puppet.com/puppet/windows_env) module (`>= 3.0.0 < 4.0.0`)

For PostgreSQL datastore support there is a soft dependency on [puppetlabs/postgresql](https://forge.puppet.com/puppetlabs/postgresql) module (`>= 6.0.0 < 7.0.0`).
For PostgreSQL datastore support:
* [puppetlabs/postgresql](https://forge.puppet.com/puppetlabs/postgresql) module (`>= 6.0.0 < 7.0.0`)

### Beginning with sensu

Expand Down Expand Up @@ -198,6 +202,16 @@ class { '::sensu::cli':

**NOTE**: The `sensu::backend` class calls the `sensu::cli` class so it is only necessary to directly call the `sensu::cli` class on hosts not using the `sensu::backend` class.

For Windows the `install_source` parameter must be provided:

```puppet
class { '::sensu::cli':
install_source => 'https://s3-us-west-2.amazonaws.com/sensu.io/sensu-go/5.14.1/sensu-go_5.14.1_windows_amd64.zip',
url_host => 'sensu-backend.example.com',
password => 'supersecret',
}
```

### Manage Windows Agent

This module supports Windows Sensu Go agent via chocolatey beginning with version 5.12.0.
Expand Down
3 changes: 3 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
agent.vm.network "forwarded_port", host: 3390, guest: 3389, auto_correct: true
agent.vm.provision :shell, :path => "tests/provision_basic_win.ps1"
agent.vm.provision :shell, :inline => '$env:PATH += ";C:\Program Files\Puppet Labs\Puppet\bin" ; iex "puppet apply -v C:/vagrant/tests/sensu-agent.pp"'
agent.vm.provision :shell, :inline => '$env:PATH += ";C:\Program Files\Puppet Labs\Puppet\bin" ; iex "puppet apply -v C:/vagrant/tests/sensu-cli.pp"'
agent.vm.provision :shell, :inline => '$env:PATH += ";C:\Program Files\Puppet Labs\Puppet\bin" ; iex "facter --custom-dir=C:\vagrant\lib\facter sensu_agent"'
end

Expand All @@ -186,6 +187,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
agent.vm.network "forwarded_port", host: 3389, guest: 3389, auto_correct: true
agent.vm.provision :shell, :path => "tests/provision_basic_win.ps1"
agent.vm.provision :shell, :inline => 'iex "puppet apply -v C:/vagrant/tests/sensu-agent.pp"'
agent.vm.provision :shell, :inline => 'iex "puppet apply -v C:/vagrant/tests/sensu-cli.pp"'
agent.vm.provision :shell, :inline => 'iex "facter --custom-dir=C:\vagrant\lib\facter sensu_agent"'
end

Expand Down Expand Up @@ -215,6 +217,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
agent.vm.network "forwarded_port", host: 3391, guest: 3389, auto_correct: true
agent.vm.provision :shell, :path => "tests/provision_basic_win.ps1"
agent.vm.provision :shell, :inline => 'iex "puppet apply -v C:/vagrant/tests/sensu-agent.pp"'
agent.vm.provision :shell, :inline => 'iex "puppet apply -v C:/vagrant/tests/sensu-cli.pp"'
agent.vm.provision :shell, :inline => 'iex "facter --custom-dir=C:\vagrant\lib\facter sensu_agent"'
end

Expand Down
1 change: 1 addition & 0 deletions data/os/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ sensu::agent::package_name: 'sensu-agent'
sensu::agent::package_download_path: 'C:\'
sensu::agent::log_file: 'C:\ProgramData\sensu\log\sensu-agent.log'
sensu::agent::service_name: SensuAgent
sensu::cli::install_path: 'C:\Program Files\Sensu'
8 changes: 4 additions & 4 deletions lib/puppet/provider/sensu_configure/sensuctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def configure_cmd(bootstrap)

def create
begin
configure_cmd(resource[:bootstrap])
output = configure_cmd(resource[:bootstrap])
rescue Puppet::ExecutionFailure => e
File.delete(config_path)
raise Puppet::Error, "sensuctl configure failed\nError message: #{e.message}"
File.delete(config_path) if File.exist?(config_path)
raise Puppet::Error, "sensuctl configure failed\nOutput: #{output}\nError message: #{e.message}"
rescue Exception => e
raise Puppet::Error, "sensuctl configure failed\nError message: #{e.message}"
end
Expand All @@ -71,7 +71,7 @@ def flush
begin
if @property_flush[:trusted_ca_file] == 'absent'
Puppet.info("Deleting #{config_path} to clear trusted-ca-file")
File.delete(config_path)
File.delete(config_path) if File.exist?(config_path)
end
configure_cmd(false)
rescue Exception => e
Expand Down
26 changes: 22 additions & 4 deletions lib/puppet/provider/sensuctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
class Puppet::Provider::Sensuctl < Puppet::Provider
initvars

commands :sensuctl => 'sensuctl'
commands :sensuctl_cmd => 'sensuctl'

class << self
attr_accessor :chunk_size
attr_accessor :path
end

def self.config_path
# https://github.com/sensu/sensu-puppet/issues/1072
# since $HOME is not set in systemd service File.expand_path('~') won't work
home = Etc.getpwuid(Process.uid).dir
if Dir.respond_to?(:home)
home = Dir.home
else
# https://github.com/sensu/sensu-puppet/issues/1072
# since $HOME is not set in systemd service File.expand_path('~') won't work
home = Etc.getpwuid(Process.uid).dir
end
File.join(home, '.config/sensu/sensuctl/cluster')
end
def config_path
Expand Down Expand Up @@ -47,6 +52,19 @@ def convert_boolean_property_value(value)
end
end

def self.sensuctl(args)
sensuctl_cmd = which('sensuctl')
if ! path.nil?
cmd = [path] + args
else
cmd = [sensuctl_cmd] + args
end
execute(cmd)
end
def sensuctl(*args)
self.class.sensuctl(*args)
end

def self.sensuctl_list(command, namespaces = true)
args = [command]
args << 'list'
Expand Down
5 changes: 5 additions & 0 deletions lib/puppet/type/sensuctl_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
desc "sensuctl chunk-size"
end

newparam(:path) do
desc "path to sensuctl"
end

# First collect all types with sensuctl provider that come from this module
# For each sensuctl type, set the class variable 'chunk_size' used by
# each provider to list resources
Expand All @@ -29,6 +33,7 @@ def generate
sensuctl_types.each do |type|
provider_class = Puppet::Type.type(type).provider(:sensuctl)
provider_class.chunk_size = self[:chunk_size]
provider_class.path = self[:path]
end
[]
end
Expand Down
73 changes: 57 additions & 16 deletions manifests/cli.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,35 @@
# Windows MSI packaging and to avoid surprising upgrades.
# @param package_name
# Name of Sensu CLI package.
# @param install_source
# Source of Sensu Go CLI download for installing on Windows.
# Paths with http:// or https:// will be downloaded
# Paths with puppet:// or file:// paths will also be installed.
# @param install_path
# Where to install sensuctl for Windows. Default to `C:\Program Files\Sensu`.
# @param url_host
# Sensu backend host used to configure sensuctl and verify API access.
# @param url_port
# Sensu backend port used to configure sensuctl and verify API access.
# @param password
# Sensu backend admin password used to confiure sensuctl.
# @param bootstrap
# Should sensuctl be bootstrapped
# Should sensuctl be bootstrapped. This is a private parameter used by sensu::backend class.
# @param configure
# Determines if sensuctl should be configured
# @param sensuctl_chunk_size
# Chunk size to use when listing sensuctl resources
#
class sensu::cli (
Optional[String] $version = undef,
String $package_name = 'sensu-go-cli',
Optional[Variant[Stdlib::HTTPSUrl, Stdlib::HTTPUrl, Pattern[/^(file|puppet):/]]] $install_source = undef,
Optional[Stdlib::Absolutepath] $install_path = undef,
String $url_host = $trusted['certname'],
Stdlib::Port $url_port = 8080,
String $password = 'P@ssw0rd!',
Boolean $bootstrap = false,
Boolean $configure = true,
Optional[Integer] $sensuctl_chunk_size = undef,
) {

Expand All @@ -50,23 +61,53 @@

$url = "${url_protocol}://${url_host}:${url_port}"

package { 'sensu-go-cli':
ensure => $_version,
name => $package_name,
require => $::sensu::package_require,
if $facts['os']['family'] == 'windows' {
if ! $install_source {
fail('sensu::cli: install_source is required for Windows')
}
$sensuctl_path = "${install_path}\\sensuctl.exe"
file { $install_path:
ensure => 'directory',
}
archive { 'sensu-go-cli.zip':
path => "${install_path}\\sensu-go-cli.zip",
source => $install_source,
extract => true,
extract_path => $install_path,
creates => "${install_path}\\sensuctl.exe",
cleanup => false,
require => File[$install_path],
}
windows_env { 'sensuctl-path':
ensure => 'present',
variable => 'PATH',
value => $install_path,
mergemode => 'append',
require => Archive['sensu-go-cli.zip'],
before => Sensu_configure['puppet'],
}
} else {
$sensuctl_path = undef
package { 'sensu-go-cli':
ensure => $_version,
name => $package_name,
require => $::sensu::package_require,
}
}

sensuctl_config { 'sensu':
chunk_size => $sensuctl_chunk_size,
}
if $configure {
sensuctl_config { 'sensu':
chunk_size => $sensuctl_chunk_size,
path => $sensuctl_path,
}

sensu_configure { 'puppet':
url => $url,
username => 'admin',
password => $password,
bootstrap => $bootstrap,
bootstrap_password => 'P@ssw0rd!',
trusted_ca_file => $trusted_ca_file,
sensu_configure { 'puppet':
url => $url,
username => 'admin',
password => $password,
bootstrap => $bootstrap,
bootstrap_password => 'P@ssw0rd!',
trusted_ca_file => $trusted_ca_file,
}
}

}
31 changes: 31 additions & 0 deletions spec/acceptance/windows_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
require 'spec_helper_acceptance_windows' if Gem.win_platform?
require 'json'

describe 'sensu::cli class', if: Gem.win_platform? do
context 'default' do
pp = <<-EOS
class { '::sensu': }
class { '::sensu::cli':
install_source => 'https://s3-us-west-2.amazonaws.com/sensu.io/sensu-go/5.14.1/sensu-go_5.14.1_windows_amd64.zip',
url_host => 'localhost',
# Not yet able to run backend in appveyor so configure will not work
configure => false,
}
EOS

unless RSpec.configuration.skip_apply
it 'creates manifest' do
File.open('C:\manifest-cli.pp', 'w') { |f| f.write(pp) }
puts "C:\manifest-cli.pp"
puts File.read('C:\manifest-cli.pp')
end

describe command('puppet apply --debug --detailed-exitcodes C:\manifest-cli.pp') do
its(:exit_status) { is_expected.to eq 256 }
end

it 'has installed sensuctl' do
output = `sensuctl version`
expect(output).to eq(/5\.14\.1/)
end
end
end
end

describe 'sensu::agent class', if: Gem.win_platform? do
context 'default' do
pp = <<-EOS
Expand Down
Loading

0 comments on commit 33be195

Please sign in to comment.