From bf34f8631cfae0550dbe082740668df82f0cdfd4 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Thu, 21 Nov 2013 23:33:09 +0100 Subject: [PATCH] Add metering agent support The Ceilometer project is able to get meters from Neutron via the notifications system on how much bandwidth the projects are using. The idea is to meter this as the L3 routers levels. The feature exists since Havana. Signed-off-by: Emilien Macchi implement blueprint metering-agent Change-Id: I62ad9b7455e5f2bc3455b55a346f2515ca1040a3 --- examples/neutron.pp | 2 + .../ini_setting.rb | 22 ++++ .../type/neutron_metering_agent_config.rb | 18 +++ manifests/agents/metering.pp | 106 +++++++++++++++++ manifests/init.pp | 1 + manifests/params.pp | 6 + spec/classes/neutron_agents_metering_spec.rb | 110 ++++++++++++++++++ 7 files changed, 265 insertions(+) create mode 100644 lib/puppet/provider/neutron_metering_agent_config/ini_setting.rb create mode 100644 lib/puppet/type/neutron_metering_agent_config.rb create mode 100644 manifests/agents/metering.pp create mode 100644 spec/classes/neutron_agents_metering_spec.rb diff --git a/examples/neutron.pp b/examples/neutron.pp index 1a4726d24..901e01a9b 100644 --- a/examples/neutron.pp +++ b/examples/neutron.pp @@ -8,6 +8,7 @@ class { 'neutron': rabbit_password => 'password', rabbit_user => 'guest', rabbit_host => 'localhost', + service_plugins => ['neutron.services.metering.metering_plugin.MeteringPlugin'] } # The API server talks to keystone for authorisation @@ -21,6 +22,7 @@ class { 'neutron::agents::dhcp': } class { 'neutron::agents::l3': } class { 'neutron::agents::lbaas': } class { 'neutron::agents::vpnaas': } +class { 'neutron::agents::metering': } # This plugin configures Neutron for OVS on the server # Agent diff --git a/lib/puppet/provider/neutron_metering_agent_config/ini_setting.rb b/lib/puppet/provider/neutron_metering_agent_config/ini_setting.rb new file mode 100644 index 000000000..0abd3db4e --- /dev/null +++ b/lib/puppet/provider/neutron_metering_agent_config/ini_setting.rb @@ -0,0 +1,22 @@ +Puppet::Type.type(:neutron_metering_agent_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:ini_setting).provider(:ruby) +) do + + def section + resource[:name].split('/', 2).first + end + + def setting + resource[:name].split('/', 2).last + end + + def separator + '=' + end + + def file_path + '/etc/neutron/metering_agent.ini' + end + +end diff --git a/lib/puppet/type/neutron_metering_agent_config.rb b/lib/puppet/type/neutron_metering_agent_config.rb new file mode 100644 index 000000000..31b75c84d --- /dev/null +++ b/lib/puppet/type/neutron_metering_agent_config.rb @@ -0,0 +1,18 @@ +Puppet::Type.newtype(:neutron_metering_agent_config) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from metering_agent.ini' + newvalues(/\S+\/\S+/) + end + + newproperty(:value) do + desc 'The value of the setting to be defined.' + munge do |value| + value = value.to_s.strip + value.capitalize! if value =~ /^(true|false)$/i + value + end + end +end diff --git a/manifests/agents/metering.pp b/manifests/agents/metering.pp new file mode 100644 index 000000000..4380d98f3 --- /dev/null +++ b/manifests/agents/metering.pp @@ -0,0 +1,106 @@ +# +# Copyright (C) 2013 eNovance SAS +# +# Author: Emilien Macchi +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# == Class: neutron::agents:metering +# +# Setups Neutron metering agent. +# +# === Parameters +# +# [*package_ensure*] +# (optional) Ensure state for package. Defaults to 'present'. +# +# [*enabled*] +# (optional) Enable state for service. Defaults to 'true'. +# +# [*debug*] +# (optional) Show debugging output in log. Defaults to false. +# +# [*interface_driver*] +# (optional) Defaults to 'neutron.agent.linux.interface.OVSInterfaceDriver'. +# +# [*use_namespaces*] +# (optional) Allow overlapping IP (Must have kernel build with +# CONFIG_NET_NS=y and iproute2 package that supports namespaces). +# Defaults to true. +# +# [*measure_interval*] +# (optional) Interval between two metering measures. +# Defaults to 30. +# +# [*report_interval*] +# (optional) Interval between two metering reports. +# Defaults to 300. +# + +class neutron::agents::metering ( + $package_ensure = present, + $enabled = true, + $debug = false, + $interface_driver = 'neutron.agent.linux.interface.OVSInterfaceDriver', + $use_namespaces = true, + $measure_interval = '30', + $report_interval = '300' +) { + + include neutron::params + + Neutron_config<||> ~> Service['neutron-metering-service'] + Neutron_metering_agent_config<||> ~> Service['neutron-metering-service'] + + # The metering agent loads both neutron.ini and its own file. + # This only lists config specific to the agent. neutron.ini supplies + # the rest. + neutron_metering_agent_config { + 'DEFAULT/debug': value => $debug; + 'DEFAULT/interface_driver': value => $interface_driver; + 'DEFAULT/use_namespaces': value => $use_namespaces; + 'DEFAULT/measure_interval': value => $measure_interval; + 'DEFAULT/report_interval': value => $report_interval; + } + + if $::neutron::params::metering_agent_package { + Package['neutron'] -> Package['neutron-metering-agent'] + Package['neutron-metering-agent'] -> Neutron_config<||> + Package['neutron-metering-agent'] -> Neutron_metering_agent_config<||> + package { 'neutron-metering-agent': + ensure => $package_ensure, + name => $::neutron::params::metering_agent_package, + } + } else { + # Some platforms (RedHat) do not provide a neutron metering agent package. + # The neutron metering agent config file is provided by the neutron package. + Package['neutron'] -> Neutron_metering_agent_config<||> + } + + if $enabled { + $ensure = 'running' + } else { + $ensure = 'stopped' + } + + service { 'neutron-metering-service': + ensure => $ensure, + name => $::neutron::params::metering_agent_service, + enable => $enabled, + require => Class['neutron'], + } + + if $::neutron::service_plugins !~ /neutron\.services\.metering\.metering_plugin\.MeteringPlugin/ { + fail('metering_plugin class should be part of service_plugins in neutron.conf') + } +} diff --git a/manifests/init.pp b/manifests/init.pp index 4d65a1d18..aad9beb27 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -39,6 +39,7 @@ # neutron.services.firewall.fwaas_plugin.FirewallPlugin # neutron.services.loadbalancer.plugin.LoadBalancerPlugin # neutron.services.vpn.plugin.VPNDriverPlugin +# neutron.services.metering.metering_plugin.MeteringPlugin # Defaults to empty # # [*auth_strategy*] diff --git a/manifests/params.pp b/manifests/params.pp index 1d5b82ce8..c9696e5a3 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -30,6 +30,9 @@ class neutron::params { $haproxy_package = 'haproxy' + $metering_agent_package = false + $metering_agent_service = 'neutron-metering-agent' + $vpnaas_agent_package = false $vpnaas_agent_service = 'neutron-vpnaas-agent' $openswan_package = 'openswan' @@ -71,6 +74,9 @@ class neutron::params { $haproxy_package = 'haproxy' + $metering_agent_package = 'neutron-metering-agent' + $metering_agent_service = 'neutron-metering-agent' + $vpnaas_agent_package = 'neutron-plugin-vpn-agent' $vpnaas_agent_service = 'neutron-vpnaas-agent' $openswan_package = 'openswan' diff --git a/spec/classes/neutron_agents_metering_spec.rb b/spec/classes/neutron_agents_metering_spec.rb new file mode 100644 index 000000000..0fa2d4556 --- /dev/null +++ b/spec/classes/neutron_agents_metering_spec.rb @@ -0,0 +1,110 @@ +# +# Copyright (C) 2013 eNovance SAS +# +# Author: Emilien Macchi +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# Unit tests for neutron::plugins::metering class +# + +require 'spec_helper' + +describe 'neutron::agents::metering' do + + let :pre_condition do + "class { 'neutron': + rabbit_password => 'passw0rd', + service_plugins => ['neutron.services.metering.metering_plugin.MeteringPlugin'] }" + end + + let :params do + {} + end + + let :default_params do + { :package_ensure => 'present', + :enabled => true, + :debug => false, + :interface_driver => 'neutron.agent.linux.interface.OVSInterfaceDriver', + :use_namespaces => true, + :measure_interval => '30', + :report_interval => '300' + } + end + + + shared_examples_for 'neutron metering agent' do + let :p do + default_params.merge(params) + end + + it { should include_class('neutron::params') } + + it 'configures metering_agent.ini' do + should contain_neutron_metering_agent_config('DEFAULT/debug').with_value(p[:debug]); + should contain_neutron_metering_agent_config('DEFAULT/interface_driver').with_value(p[:interface_driver]); + should contain_neutron_metering_agent_config('DEFAULT/use_namespaces').with_value(p[:use_namespaces]); + should contain_neutron_metering_agent_config('DEFAULT/measure_interval').with_value(p[:measure_interval]); + should contain_neutron_metering_agent_config('DEFAULT/report_interval').with_value(p[:report_interval]); + end + + it 'installs neutron metering agent package' do + if platform_params.has_key?(:metering_agent_package) + should contain_package('neutron-metering-agent').with( + :name => platform_params[:metering_agent_package], + :ensure => p[:package_ensure] + ) + should contain_package('neutron').with_before(/Package\[neutron-metering-agent\]/) + should contain_package('neutron-metering-agent').with_before(/Neutron_metering_agent_config\[.+\]/) + should contain_package('neutron-metering-agent').with_before(/Neutron_config\[.+\]/) + else + should contain_package('neutron').with_before(/Neutron_metering_agent_config\[.+\]/) + end + end + + it 'configures neutron metering agent service' do + should contain_service('neutron-metering-service').with( + :name => platform_params[:metering_agent_service], + :enable => true, + :ensure => 'running', + :require => 'Class[Neutron]' + ) + end + end + + context 'on Debian platforms' do + let :facts do + { :osfamily => 'Debian' } + end + + let :platform_params do + { :metering_agent_package => 'neutron-metering-agent', + :metering_agent_service => 'neutron-metering-agent' } + end + + it_configures 'neutron metering agent' + end + + context 'on RedHat platforms' do + let :facts do + { :osfamily => 'RedHat' } + end + + let :platform_params do + { :metering_agent_service => 'neutron-metering-agent' } + end + + it_configures 'neutron metering agent' + end +end