From ed1ffe3b19ea6d73a966deeefe6c69ad91e212f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Bellone?= Date: Wed, 10 Jul 2019 18:50:42 -0300 Subject: [PATCH 1/5] Implemented SystemD support --- manifests/config.pp | 44 ++++++++++++--------- manifests/init.pp | 1 + manifests/params.pp | 1 + manifests/service.pp | 6 +-- templates/systemd/carbon-aggregator.service | 24 +++++++++++ templates/systemd/carbon-cache.service | 22 +++++++++++ templates/systemd/graphite-web.service | 21 ++++++++++ 7 files changed, 98 insertions(+), 21 deletions(-) create mode 100755 templates/systemd/carbon-aggregator.service create mode 100755 templates/systemd/carbon-cache.service create mode 100644 templates/systemd/graphite-web.service diff --git a/manifests/config.pp b/manifests/config.pp index a86a3e5..551cc6c 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -60,31 +60,39 @@ $gunicorn_bin = 'gunicorn_django' } - file { - [ - '/etc/init.d/carbon-aggregator', - '/etc/init.d/carbon-cache', - '/etc/init.d/graphite-web' - ]: - ensure => link, - target => '/lib/init/upstart-job', - } - - file { '/etc/init/carbon-aggregator.conf': + if $::graphite::service_provider == 'upstart' { + file { + [ + '/etc/init.d/carbon-aggregator', + '/etc/init.d/carbon-cache', + '/etc/init.d/graphite-web' + ]: + ensure => link, + target => '/lib/init/upstart-job', + } + + $base_service_path = '/etc/init/' + $service_suffix = 'conf' + } elsif $::graphite::service_provider == 'systemd' { + $base_service_path = '/etc/systemd/system' + $service_suffix = 'service' + } + + file { "${base_service_path}/carbon-aggregator.${service_suffix}": ensure => present, - content => template('graphite/upstart/carbon-aggregator.conf'), + content => template("graphite/${::graphite::service_provider}/carbon-aggregator.${service_suffix}"), mode => '0444', } - file { '/etc/init/carbon-cache.conf': + file { "${base_service_path}/carbon-cache.${service_suffix}": ensure => present, - content => template('graphite/upstart/carbon-cache.conf'), + content => template("graphite/${::graphite::service_provider}/carbon-cache.${service_suffix}"), mode => '0444', } - file { '/etc/init/graphite-web.conf': + file { "${base_service_path}/graphite-web.${service_suffix}": ensure => present, - content => template('graphite/upstart/graphite-web.conf'), + content => template("graphite/${::graphite::service_provider}/graphite-web.${service_suffix}"), mode => '0444', } @@ -138,8 +146,8 @@ refreshonly => true, require => File["${root_dir}/storage"], subscribe => [ - File['/etc/init/graphite-web.conf'], - File['/etc/init/carbon-cache.conf'], + File["${base_service_path}/graphite-web.${service_suffix}"], + File["${base_service_path}/carbon-cache.${service_suffix}"], File["${root_dir}/storage"], File["${root_dir}/webapp/graphite"], ], diff --git a/manifests/init.pp b/manifests/init.pp index 33b06ac..6f65bd0 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -124,6 +124,7 @@ $time_zone = $graphite::params::time_zone, $django_secret_key = $graphite::params::django_secret_key, $memcache_hosts = $graphite::params::memcache_hosts, + $service_provider = $graphite::params::service_provider, ) inherits graphite::params { validate_string( $admin_password, diff --git a/manifests/params.pp b/manifests/params.pp index 5056919..8b42833 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -14,4 +14,5 @@ $time_zone = 'UTC' $django_secret_key = undef $memcache_hosts = [] + $service_provider = 'upstart' } diff --git a/manifests/service.pp b/manifests/service.pp index bd33ac3..122c9a3 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -12,19 +12,19 @@ ensure => $aggregator_ensure, hasstatus => true, hasrestart => false, - provider => upstart, + provider => $::graphite::service_provider, } -> service { 'carbon-cache': ensure => running, hasstatus => true, hasrestart => false, - provider => upstart, + provider => $::graphite::service_provider, } service { 'graphite-web': ensure => running, hasstatus => true, hasrestart => false, - provider => upstart, + provider => $::graphite::service_provider, } } diff --git a/templates/systemd/carbon-aggregator.service b/templates/systemd/carbon-aggregator.service new file mode 100755 index 0000000..75d22ac --- /dev/null +++ b/templates/systemd/carbon-aggregator.service @@ -0,0 +1,24 @@ +[Unit] +Description=Carbon aggregator service for Graphite +Wants=network-online.target +Wants=local-fs.target + +[Install] +WantedBy=multi-user.target + +[Service] +User=<%= @user %> +Group=<%= @group %> + +Restart=on-failure + +WorkingDirectory=<%= @root_dir %> + +Environment=GRAPHITE_STORAGE_DIR=<%= @root_dir %>/storage +Environment=GRAPHITE_CONF_DIR=<%= @root_dir %>/conf + +ExecStart=<%= @root_dir %>/bin/carbon-aggregator.py \ +<% if @aggregation_rules_ensure == 'present' -%> + --rules='<%= @root_dir %>/conf/aggregation-rules.conf' \ +<% end -%> + --debug start diff --git a/templates/systemd/carbon-cache.service b/templates/systemd/carbon-cache.service new file mode 100755 index 0000000..05fdc6d --- /dev/null +++ b/templates/systemd/carbon-cache.service @@ -0,0 +1,22 @@ +[Unit] +Description=Carbon cache service for Graphite +Wants=network-online.target +Wants=local-fs.target + +[Install] +WantedBy=multi-user.target + +[Service] +User=<%= @user %> +Group=<%= @group %> + +Restart=on-failure + +WorkingDirectory=<%= @root_dir %> + +Environment=GRAPHITE_STORAGE_DIR=<%= @root_dir %>/storage +Environment=GRAPHITE_CONF_DIR=<%= @root_dir %>/conf + +ExecStart=<%= @root_dir %>/bin/carbon-cache.py --debug start + +PIDFile=<%= @root_dir %>/storage/carbon-cache-a.pid diff --git a/templates/systemd/graphite-web.service b/templates/systemd/graphite-web.service new file mode 100644 index 0000000..80f5c58 --- /dev/null +++ b/templates/systemd/graphite-web.service @@ -0,0 +1,21 @@ +[Unit] +Description=Graphite realtime graphing engine +Wants=network-online.target +Wants=local-fs.target + +[Install] +WantedBy=multi-user.target + +[Service] +User=<%= @user %> +Group=<%= @group %> + +Restart=on-failure + +WorkingDirectory=<%= @root_dir %>/webapp + +Environment=GRAPHITE_STORAGE_DIR=<%= @root_dir %>/storage +Environment=GRAPHITE_CONF_DIR=<%= @root_dir %>/conf +Environment=PYTHONPATH=<%= @root_dir %>/lib:<%= @root_dir %>/webapp + +ExecStart=<%= @gunicorn_bin %> -b<%= @bind_address -%>:<%= @port %> -w<%= @worker_processes -%> graphite/settings.py From 1e8566ea90920254f4aa1642bd0924c9f2109296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Bellone?= Date: Wed, 10 Jul 2019 19:04:08 -0300 Subject: [PATCH 2/5] Added documentation for new parameter --- manifests/init.pp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/manifests/init.pp b/manifests/init.pp index 6f65bd0..bdbd4af 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -95,6 +95,9 @@ # [*memcache_hosts*] # Optional: Array of memcached servers to use. Each should be ip:port # +# [*service_provider*] +# Optional: String to use as a provider for the services (default: upstart) +# class graphite( $admin_password = $graphite::params::admin_password, $bind_address = $graphite::params::bind_address, From 858e62c804dfa3a269fb010d8b814d06137348fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Bellone?= Date: Wed, 10 Jul 2019 19:04:23 -0300 Subject: [PATCH 3/5] Added untested tests for systemd --- .../classes/graphite/graphite__config_spec.rb | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/spec/classes/graphite/graphite__config_spec.rb b/spec/classes/graphite/graphite__config_spec.rb index 09f2943..9581cf8 100644 --- a/spec/classes/graphite/graphite__config_spec.rb +++ b/spec/classes/graphite/graphite__config_spec.rb @@ -67,6 +67,58 @@ end end + context "systemd_provider" do + lep(:params) {{ :service_provider => 'systemd' }} + + it { should_not contain_file('/etc/init.d/carbon-aggregator').with_ensure('link'). + with_target('/lib/init/upstart-job') } + it { should_not contain_file('/etc/init.d/carbon-cache').with_ensure('link'). + with_target('/lib/init/upstart-job') } + it { should_not contain_file('/etc/init.d/graphite-web').with_ensure('link'). + with_target('/lib/init/upstart-job') } + + context "root_dir" do + let(:params) {{ :root_dir => '/this/is/root' }} + + describe "carbon-aggregator.service" do + it { should contain_file('/etc/systemd/system/carbon-aggregator.service'). + with_ensure('present'). + with_content(/User=www-data/). + with_content(/Group=www-data/). + with_content(/WorkingDirectory=\/this\/is\/root/). + with_content(/Environment=GRAPHITE_STORAGE_DIR=\/this\/is\/root\/storage/). + with_content(/Environment=GRAPHITE_CONF_DIR=\/this\/is\/root\/conf/). + with_content(/ExecStart=\/this\/is\/root\/bin\/carbon-aggregator.py/). + with_mode('0444') } + end + + describe "carbon-cache.service" do + it { should contain_file('/etc/systemd/system/carbon-cache.service'). + with_ensure('present'). + with_content(/User=www-data/). + with_content(/Group=www-data/). + with_content(/WorkingDirectory=\/this\/is\/root/). + with_content(/Environment=GRAPHITE_STORAGE_DIR=\/this\/is\/root\/storage/). + with_content(/Environment=GRAPHITE_CONF_DIR=\/this\/is\/root\/conf/). + with_content(/ExecStart=\/this\/is\/root\/bin\/carbon-cache.py/). + with_mode('0444') } + end + + describe "graphite-web.service" do + it { should contain_file('/etc/systemd/system//graphite-web.service'). + with_ensure('present'). + with_content(/User=www-data/). + with_content(/Group=www-data/). + with_content(/WorkingDirectory=\/this\/is\/root/). + with_content(/Environment=PYTHONPATH=\/this\/is\/root\/lib:\/this\/is\/root\/webapp/). + with_content(/Environment=GRAPHITE_STORAGE_DIR=\/this\/is\/root\/storage/). + with_content(/Environment=GRAPHITE_CONF_DIR=\/this\/is\/root\/conf/). + with_content(/-b127\.0\.0\.1:8000/). + with_mode('0444') } + end + end + end + context "carbon_content" do let(:params) {{ :root_dir => '/this/is/root', :carbon_content => 'SOMEVAR=SOMECONTENT' }} From f6214deaac5b5f882aa7edbbc4bdad798bd90d66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Bellone?= Date: Wed, 10 Jul 2019 19:07:26 -0300 Subject: [PATCH 4/5] Fixed typo in tests --- spec/classes/graphite/graphite__config_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/classes/graphite/graphite__config_spec.rb b/spec/classes/graphite/graphite__config_spec.rb index 9581cf8..20daa62 100644 --- a/spec/classes/graphite/graphite__config_spec.rb +++ b/spec/classes/graphite/graphite__config_spec.rb @@ -68,7 +68,7 @@ end context "systemd_provider" do - lep(:params) {{ :service_provider => 'systemd' }} + let(:params) {{ :service_provider => 'systemd' }} it { should_not contain_file('/etc/init.d/carbon-aggregator').with_ensure('link'). with_target('/lib/init/upstart-job') } From 0b9e170c6824399efcfe81bbdd3dad431ba3b670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Bellone?= Date: Wed, 10 Jul 2019 19:39:21 -0300 Subject: [PATCH 5/5] Corrected paths for service configuration files --- manifests/config.pp | 2 +- .../classes/graphite/graphite__config_spec.rb | 114 +++++++++--------- 2 files changed, 59 insertions(+), 57 deletions(-) diff --git a/manifests/config.pp b/manifests/config.pp index 551cc6c..cedda32 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -71,7 +71,7 @@ target => '/lib/init/upstart-job', } - $base_service_path = '/etc/init/' + $base_service_path = '/etc/init' $service_suffix = 'conf' } elsif $::graphite::service_provider == 'systemd' { $base_service_path = '/etc/systemd/system' diff --git a/spec/classes/graphite/graphite__config_spec.rb b/spec/classes/graphite/graphite__config_spec.rb index 20daa62..c40db04 100644 --- a/spec/classes/graphite/graphite__config_spec.rb +++ b/spec/classes/graphite/graphite__config_spec.rb @@ -3,67 +3,69 @@ describe 'graphite', :type => :class do let(:facts) { {:osfamily => 'Debian'} } - it { should contain_file('/etc/init.d/carbon-aggregator').with_ensure('link'). - with_target('/lib/init/upstart-job') } - it { should contain_file('/etc/init.d/carbon-cache').with_ensure('link'). - with_target('/lib/init/upstart-job') } - it { should contain_file('/etc/init.d/graphite-web').with_ensure('link'). - with_target('/lib/init/upstart-job') } - - it do should contain_exec('set_graphite_ownership').with( - 'before' => [ 'Service[graphite-web]', 'Service[carbon-cache]' ], - 'refreshonly' => 'true' - ) - end - - context "root_dir" do - let(:params) {{ :root_dir => '/this/is/root' }} + context 'upstart_provider' do + it { should contain_file('/etc/init.d/carbon-aggregator').with_ensure('link'). + with_target('/lib/init/upstart-job') } + it { should contain_file('/etc/init.d/carbon-cache').with_ensure('link'). + with_target('/lib/init/upstart-job') } + it { should contain_file('/etc/init.d/graphite-web').with_ensure('link'). + with_target('/lib/init/upstart-job') } - describe "intial_data.json" do - it { should contain_file('/this/is/root/webapp/graphite/initial_data.json') } + it do should contain_exec('set_graphite_ownership').with( + 'before' => [ 'Service[graphite-web]', 'Service[carbon-cache]' ], + 'refreshonly' => 'true' + ) end - describe "carbon-aggregator.conf" do - it { should contain_file('/etc/init/carbon-aggregator.conf').with_ensure('present'). - with_content(/setuid www-data/). - with_content(/setgid www-data/). - with_content(/chdir '\/this\/is\/root'/). - with_content(/GRAPHITE_STORAGE_DIR='\/this\/is\/root\/storage'/). - with_content(/GRAPHITE_CONF_DIR='\/this\/is\/root\/conf'/). - with_content(/exec \/this\/is\/root\/bin\/carbon-aggregator.py/). - with_mode('0444') } - end + context "root_dir" do + let(:params) {{ :root_dir => '/this/is/root' }} - describe "carbon-cache.conf" do - it { should contain_file('/etc/init/carbon-cache.conf').with_ensure('present'). - with_content(/setuid www-data/). - with_content(/setgid www-data/). - with_content(/chdir '\/this\/is\/root'/). - with_content(/GRAPHITE_STORAGE_DIR='\/this\/is\/root\/storage'/). - with_content(/GRAPHITE_CONF_DIR='\/this\/is\/root\/conf'/). - with_content(/exec \/this\/is\/root\/bin\/carbon-cache.py/). - with_mode('0444') } - end + describe "intial_data.json" do + it { should contain_file('/this/is/root/webapp/graphite/initial_data.json') } + end - describe "graphite-web.conf" do - it { should contain_file('/etc/init/graphite-web.conf').with_ensure('present'). - with_content(/setuid www-data/). - with_content(/setgid www-data/). - with_content(/chdir '\/this\/is\/root\/webapp'/). - with_content(/PYTHONPATH='\/this\/is\/root\/lib:\/this\/is\/root\/webapp'/). - with_content(/GRAPHITE_STORAGE_DIR='\/this\/is\/root\/storage'/). - with_content(/GRAPHITE_CONF_DIR='\/this\/is\/root\/conf'/). - with_content(/-b127\.0\.0\.1:8000/). - with_mode('0444') } - end + describe "carbon-aggregator.conf" do + it { should contain_file('/etc/init/carbon-aggregator.conf').with_ensure('present'). + with_content(/setuid www-data/). + with_content(/setgid www-data/). + with_content(/chdir '\/this\/is\/root'/). + with_content(/GRAPHITE_STORAGE_DIR='\/this\/is\/root\/storage'/). + with_content(/GRAPHITE_CONF_DIR='\/this\/is\/root\/conf'/). + with_content(/exec \/this\/is\/root\/bin\/carbon-aggregator.py/). + with_mode('0444') } + end + + describe "carbon-cache.conf" do + it { should contain_file('/etc/init/carbon-cache.conf').with_ensure('present'). + with_content(/setuid www-data/). + with_content(/setgid www-data/). + with_content(/chdir '\/this\/is\/root'/). + with_content(/GRAPHITE_STORAGE_DIR='\/this\/is\/root\/storage'/). + with_content(/GRAPHITE_CONF_DIR='\/this\/is\/root\/conf'/). + with_content(/exec \/this\/is\/root\/bin\/carbon-cache.py/). + with_mode('0444') } + end - describe "carbon.conf" do - it { should contain_file('/this/is/root/conf/carbon.conf'). - with_content(/USER = www-data/). - with_content(/MAX_CACHE_SIZE = inf/). - with_content(/MAX_UPDATES_PER_SECOND = inf/). - with_content(/MAX_CREATES_PER_MINUTE = inf/). - with_content(/LOCAL_DATA_DIR = \/this\/is\/root\/storage\/whisper\//) } + describe "graphite-web.conf" do + it { should contain_file('/etc/init/graphite-web.conf').with_ensure('present'). + with_content(/setuid www-data/). + with_content(/setgid www-data/). + with_content(/chdir '\/this\/is\/root\/webapp'/). + with_content(/PYTHONPATH='\/this\/is\/root\/lib:\/this\/is\/root\/webapp'/). + with_content(/GRAPHITE_STORAGE_DIR='\/this\/is\/root\/storage'/). + with_content(/GRAPHITE_CONF_DIR='\/this\/is\/root\/conf'/). + with_content(/-b127\.0\.0\.1:8000/). + with_mode('0444') } + end + + describe "carbon.conf" do + it { should contain_file('/this/is/root/conf/carbon.conf'). + with_content(/USER = www-data/). + with_content(/MAX_CACHE_SIZE = inf/). + with_content(/MAX_UPDATES_PER_SECOND = inf/). + with_content(/MAX_CREATES_PER_MINUTE = inf/). + with_content(/LOCAL_DATA_DIR = \/this\/is\/root\/storage\/whisper\//) } + end end end @@ -105,7 +107,7 @@ end describe "graphite-web.service" do - it { should contain_file('/etc/systemd/system//graphite-web.service'). + it { should contain_file('/etc/systemd/system/graphite-web.service'). with_ensure('present'). with_content(/User=www-data/). with_content(/Group=www-data/).