Skip to content

Commit

Permalink
Added support for Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
yachub committed Feb 17, 2021
1 parent fe840fb commit 74a0c25
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 2 deletions.
4 changes: 4 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
# [*archive_location*]
# String. Alternate archive location. E.g. an interal mirror.
#
# [*archive_version*]
# String. Specify a telegraf archive version. E.g. 1.17.2.
#
# [*archive_install_dir*]
# String. Location to extract archive to must be an absolute path
#
Expand Down Expand Up @@ -134,6 +137,7 @@
Boolean $manage_archive = $telegraf::params::manage_archive,
Optional[String] $repo_location = $telegraf::params::repo_location,
Optional[String] $archive_location = $telegraf::params::archive_location,
Optional[String[1]] $archive_version = $telegraf::params::archive_version,
Optional[String] $archive_install_dir = $telegraf::params::archive_install_dir,
Boolean $purge_config_fragments = $telegraf::params::purge_config_fragments,
String $repo_type = $telegraf::params::repo_type,
Expand Down
72 changes: 71 additions & 1 deletion manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,76 @@
assert_private()

case $facts['os']['family'] {
'Darwin': {
if $telegraf::manage_archive {
if $telegraf::manage_user {
group { $telegraf::config_file_group:
ensure => present,
}

user { $telegraf::config_file_owner:
ensure => present,
gid => $telegraf::config_file_group,
}
}

$install_dir_deps = [
'/usr/local/bin',
'/usr/local/etc',
'/usr/local/opt',
'/usr/local/var',
'/usr/local/var/log',
]

file { $install_dir_deps:
ensure => directory,
}

file { "${telegraf::archive_install_dir}-${telegraf::archive_version}":
ensure => directory,
owner => $telegraf::config_file_owner,
group => $telegraf::config_file_group,
require => File[$install_dir_deps],
}

archive { "/tmp/telegraf-${telegraf::archive_version}.tar.gz":
ensure => present,
extract => true,
extract_path => "${telegraf::archive_install_dir}-${telegraf::archive_version}",
extract_command => 'tar xfz %s --strip-components=2',
source => "https://dl.influxdata.com/telegraf/releases/telegraf-${telegraf::archive_version}_darwin_amd64.tar.gz",
creates => "${telegraf::archive_install_dir}-${$telegraf::archive_version}/usr/bin/telegraf",
user => $telegraf::config_file_owner,
group => $telegraf::config_file_group,
cleanup => true,
require => File["${telegraf::archive_install_dir}-${telegraf::archive_version}"],
}

file {
default:
ensure => link,
require => Archive["/tmp/telegraf-${telegraf::archive_version}.tar.gz"],
;
'/usr/local/bin/telegraf':
target => "${telegraf::archive_install_dir}-${telegraf::archive_version}/usr/bin/telegraf",
;
'/usr/local/etc/telegraf':
target => "${telegraf::archive_install_dir}-${telegraf::archive_version}/etc/telegraf",
;
'/usr/local/var/log/telegraf':
target => "${telegraf::archive_install_dir}-${telegraf::archive_version}/var/log/telegraf",
;
}

file { '/Library/LaunchDaemons/telegraf.plist':
ensure => file,
content => epp('telegraf/telegraf.plist.epp', {
'config_file_owner' => $telegraf::config_file_owner,
'config_file_group' => $telegraf::config_file_group,
}),
}
}
}
'Debian': {
if $telegraf::manage_repo {
if $facts['os']['name'] == 'Raspbian' {
Expand Down Expand Up @@ -91,7 +161,7 @@
# repo is not applicable to windows
}
default: {
fail('Only RedHat, CentOS, OracleLinux, Debian, Ubuntu and Windows repositories and Suse archives are supported at this time')
fail('Only RedHat, CentOS, OracleLinux, Debian, Ubuntu, Darwin and Windows repositories and Suse archives are supported at this time')
}
}

Expand Down
25 changes: 24 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@
#
class telegraf::params {
case $facts['os']['family'] {
'Darwin': {
$config_file = '/usr/local/etc/telegraf/telegraf.conf'
$config_file_owner = 'telegraf'
$config_file_group = 'telegraf'
$config_file_mode = '0640'
$config_folder = '/usr/local/etc/telegraf/telegraf.d'
$config_folder_mode = '0770'
$logfile = '/usr/local/var/log/telegraf/telegraf.log'
$manage_repo = false
$manage_archive = true
$manage_user = true
$archive_install_dir = '/usr/local/opt/telegraf'
$archive_version = '1.17.2'
$archive_location = "https://dl.influxdata.com/telegraf/releases/telegraf-${archive_version}_darwin_amd64.tar.gz"
$repo_location = 'https://repos.influxdata.com/'
$service_enable = true
$service_ensure = running
$service_hasstatus = true
$service_restart = 'pkill -HUP telegraf'
}
'windows': {
$config_file = 'C:/Program Files/telegraf/telegraf.conf'
$config_file_owner = 'Administrator'
Expand All @@ -17,6 +37,7 @@
$manage_user = false
$archive_install_dir = undef
$archive_location = undef
$archive_version = undef
$repo_location = undef
$service_enable = true
$service_ensure = running
Expand All @@ -35,7 +56,8 @@
$manage_archive = true
$manage_user = true
$archive_install_dir = '/opt/telegraf'
$archive_location = 'https://dl.influxdata.com/telegraf/releases/telegraf-1.15.2_linux_amd64.tar.gz'
$archive_version = '1.15.2'
$archive_location = "https://dl.influxdata.com/telegraf/releases/telegraf-${archive_version}_linux_amd64.tar.gz"
$repo_location = 'https://repos.influxdata.com/'
$service_enable = true
$service_ensure = running
Expand All @@ -55,6 +77,7 @@
$manage_user = false
$archive_install_dir = undef
$archive_location = undef
$archive_version = undef
$repo_location = 'https://repos.influxdata.com/'
$service_enable = true
$service_ensure = running
Expand Down
6 changes: 6 additions & 0 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
"operatingsystemrelease": [
"12"
]
},
{
"operatingsystem": "Darwin",
"operatingsystemrelease": [
"18"
]
}
],
"dependencies": [
Expand Down
39 changes: 39 additions & 0 deletions spec/classes/telegraf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,21 @@
config_folder_mode
archive_install_dir
archive_location
archive_version
repo_location
service_restart
]
)
end
when 'Darwin'
it do
is_expected.to contain_class('telegraf').with(
config_file_mode: '0640',
config_folder_mode: '0770',
archive_install_dir: '/usr/local/opt/telegraf',
archive_version: '1.17.2'
)
end
when 'Suse'
it do
is_expected.to contain_class('telegraf').with(
Expand All @@ -116,6 +126,7 @@
%w[
archive_install_dir
archive_location
archive_version
]
)
end
Expand All @@ -128,6 +139,12 @@
is_expected.to contain_file('C:/Program Files/telegraf/telegraf.d').
with_purge(false)
}
when 'Darwin'
it { is_expected.to contain_file('/usr/local/etc/telegraf/telegraf.conf') }
it {
is_expected.to contain_file('/usr/local/etc/telegraf/telegraf.d').
with_purge(false)
}
else
it { is_expected.to contain_file('/etc/telegraf/telegraf.conf') }
it {
Expand All @@ -138,6 +155,22 @@
case facts[:osfamily]
when 'Suse'
it { is_expected.to contain_archive('/tmp/telegraf.tar.gz') }
it { is_expected.to contain_file('/etc/telegraf').with_ensure('directory') }
it { is_expected.to contain_file('/opt/telegraf').with_ensure('directory') }
it { is_expected.to contain_file('/var/log/telegraf').with_ensure('directory') }
it { is_expected.to contain_file('/etc/systemd/system/telegraf.service') }
when 'Darwin'
it { is_expected.to contain_archive('/tmp/telegraf-1.17.2.tar.gz') }
it { is_expected.to contain_file('/usr/local/bin').with_ensure('directory') }
it { is_expected.to contain_file('/usr/local/etc').with_ensure('directory') }
it { is_expected.to contain_file('/usr/local/opt').with_ensure('directory') }
it { is_expected.to contain_file('/usr/local/var').with_ensure('directory') }
it { is_expected.to contain_file('/usr/local/var/log').with_ensure('directory') }
it { is_expected.to contain_file('/usr/local/opt/telegraf-1.17.2').with_ensure('directory') }
it { is_expected.to contain_file('/usr/local/var/log/telegraf').with_ensure('link') }
it { is_expected.to contain_file('/usr/local/bin/telegraf').with_ensure('link') }
it { is_expected.to contain_file('/usr/local/etc/telegraf').with_ensure('link') }
it { is_expected.to contain_file('/Library/LaunchDaemons/telegraf.plist') }
else
it { is_expected.to contain_package('telegraf') }
end
Expand All @@ -152,6 +185,12 @@
}
end

case facts[:osfamily]
when 'Darwin', 'Suse'
it { is_expected.to contain_user('telegraf') }
it { is_expected.to contain_group('telegraf') }
end

describe 'allow custom repo_type' do
let(:params) { { repo_type: 'unstable' } }

Expand Down
2 changes: 2 additions & 0 deletions spec/defines/aggregator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
case facts[:kernel]
when 'windows'
let(:filename) { "C:/Program Files/telegraf/telegraf.d/#{title}.conf" }
when 'Darwin'
let(:filename) { "/usr/local/etc/telegraf/telegraf.d/#{title}.conf" }
else
let(:filename) { "/etc/telegraf/telegraf.d/#{title}.conf" }
end
Expand Down
6 changes: 6 additions & 0 deletions spec/defines/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
case facts[:kernel]
when 'windows'
let(:filename) { "C:/Program Files/telegraf/telegraf.d/#{title}.conf" }
when 'Darwin'
let(:filename) { "/usr/local/etc/telegraf/telegraf.d/#{title}.conf" }
else
let(:filename) { "/etc/telegraf/telegraf.d/#{title}.conf" }
end
Expand Down Expand Up @@ -67,6 +69,8 @@
case facts[:kernel]
when 'windows'
let(:filename) { "C:/Program Files/telegraf/telegraf.d/#{title}.conf" }
when 'Darwin'
let(:filename) { "/usr/local/etc/telegraf/telegraf.d/#{title}.conf" }
else
let(:filename) { "/etc/telegraf/telegraf.d/#{title}.conf" }
end
Expand Down Expand Up @@ -104,6 +108,8 @@
case facts[:kernel]
when 'windows'
let(:filename) { "C:/Program Files/telegraf/telegraf.d/#{title}.conf" }
when 'Darwin'
let(:filename) { "/usr/local/etc/telegraf/telegraf.d/#{title}.conf" }
else
let(:filename) { "/etc/telegraf/telegraf.d/#{title}.conf" }
end
Expand Down
4 changes: 4 additions & 0 deletions spec/defines/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
case facts[:kernel]
when 'windows'
let(:filename) { "C:/Program Files/telegraf/telegraf.d/#{title}.conf" }
when 'Darwin'
let(:filename) { "/usr/local/etc/telegraf/telegraf.d/#{title}.conf" }
else
let(:filename) { "/etc/telegraf/telegraf.d/#{title}.conf" }
end
Expand Down Expand Up @@ -77,6 +79,8 @@
case facts[:kernel]
when 'windows'
let(:filename) { "C:/Program Files/telegraf/telegraf.d/#{title}.conf" }
when 'Darwin'
let(:filename) { "/usr/local/etc/telegraf/telegraf.d/#{title}.conf" }
else
let(:filename) { "/etc/telegraf/telegraf.d/#{title}.conf" }
end
Expand Down
3 changes: 3 additions & 0 deletions spec/hieradata/test/darwin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
telegraf::archive_install_dir: '/usr/local/opt/telegraf'
telegraf::archive_version: '1.17.2'
36 changes: 36 additions & 0 deletions templates/telegraf.plist.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%- | String $config_file_owner,
String $config_file_group
| -%>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>Label</key>
<string>telegraf</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/telegraf</string>
<string>-config</string>
<string>/usr/local/etc/telegraf/telegraf.conf</string>
<string>-config-directory</string>
<string>/usr/local/etc/telegraf/telegraf.d</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local/var/log/telegraf</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/telegraf/telegraf.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/telegraf/telegraf.log</string>
<key>UserName</key>
<string><%= $config_file_owner %></string>
<key>GroupName</key>
<string><%= $config_file_group %></string>
</dict>
</plist>

0 comments on commit 74a0c25

Please sign in to comment.