From 183541d3890cd212db5a581470bdf98c21c40ad7 Mon Sep 17 00:00:00 2001 From: Yanis Guenane Date: Wed, 3 Jun 2015 13:49:07 +0200 Subject: [PATCH] Opencontrail: Create neutron::plugins::opencontrail Create the neutron::plugins::opencontrail class that allows one to configure neutron to work with opencontrail. The parameters have been taken from here[1] [1] https://github.com/Juniper/contrail-neutron-plugin/blob/master/etc/neutron/plugins/opencontrail/ContrailPlugin.ini Change-Id: I080c4aed23ebde320e67c98c44171b18bf2758ad --- .../ini_setting.rb | 22 ++++ .../type/neutron_plugin_opencontrail.rb | 49 +++++++ manifests/config.pp | 6 + manifests/init.pp | 8 +- manifests/params.pp | 6 + manifests/plugins/opencontrail.pp | 120 ++++++++++++++++++ .../neutron_plugins_opencontrail_spec.rb | 93 ++++++++++++++ .../ini_setting_spec.rb | 36 ++++++ 8 files changed, 339 insertions(+), 1 deletion(-) create mode 100644 lib/puppet/provider/neutron_plugin_opencontrail/ini_setting.rb create mode 100644 lib/puppet/type/neutron_plugin_opencontrail.rb create mode 100644 manifests/plugins/opencontrail.pp create mode 100644 spec/classes/neutron_plugins_opencontrail_spec.rb create mode 100644 spec/unit/provider/neutron_plugin_opencontrail/ini_setting_spec.rb diff --git a/lib/puppet/provider/neutron_plugin_opencontrail/ini_setting.rb b/lib/puppet/provider/neutron_plugin_opencontrail/ini_setting.rb new file mode 100644 index 000000000..dd882e5f7 --- /dev/null +++ b/lib/puppet/provider/neutron_plugin_opencontrail/ini_setting.rb @@ -0,0 +1,22 @@ +Puppet::Type.type(:neutron_plugin_opencontrail).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/plugins/opencontrail/ContrailPlugin.ini' + end + +end diff --git a/lib/puppet/type/neutron_plugin_opencontrail.rb b/lib/puppet/type/neutron_plugin_opencontrail.rb new file mode 100644 index 000000000..e595eb248 --- /dev/null +++ b/lib/puppet/type/neutron_plugin_opencontrail.rb @@ -0,0 +1,49 @@ +Puppet::Type.newtype(:neutron_plugin_opencontrail) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from ContrailPlugin.ini' + newvalues(/\S+\/\S+/) + end + + autorequire(:file) do + ['/etc/neutron/plugins/opencontrail'] + end + + autorequire(:package) do ['neutron'] 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 + + def is_to_s( currentvalue ) + if resource.secret? + return '[old secret redacted]' + else + return currentvalue + end + end + + def should_to_s( newvalue ) + if resource.secret? + return '[new secret redacted]' + else + return newvalue + end + end + end + + newparam(:secret, :boolean => true) do + desc 'Whether to hide the value from Puppet logs. Defaults to `false`.' + + newvalues(:true, :false) + + defaultto false + end + +end diff --git a/manifests/config.pp b/manifests/config.pp index d2616fe64..927fbf3cc 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -60,6 +60,9 @@ # [**plugin_plumgrid_config**] # (optional) Manage configuration of plugins/plumgrid/plumgrid.ini # +# [**plugin_opencontrail_config**] +# (optional) Manage configuration of plugins/opencontrail/ContrailPlugin.ini +# # [**plugin_ml2_config**] # (optional) Manage configuration of ml2_conf.ini # @@ -84,6 +87,7 @@ class neutron::config ( $plugin_cisco_config = {}, $plugin_midonet_config = {}, $plugin_plumgrid_config = {}, + $plugin_opencontrail_config = {}, $plugin_ml2_config = {}, $plugin_ovs_config = {}, ) { @@ -102,6 +106,7 @@ class neutron::config ( validate_hash($plugin_cisco_config) validate_hash($plugin_midonet_config) validate_hash($plugin_plumgrid_config) + validate_hash($plugin_opencontrail_config) validate_hash($plugin_ml2_config) validate_hash($plugin_ovs_config) @@ -118,6 +123,7 @@ class neutron::config ( create_resources('neutron_plugin_cisco', $plugin_cisco_config) create_resources('neutron_plugin_midonet', $plugin_midonet_config) create_resources('neutron_plugin_plumgrid', $plugin_plumgrid_config) + create_resources('neutron_plugin_opencontrail', $plugin_opencontrail_config) create_resources('neutron_plugin_ml2', $plugin_ml2_config) create_resources('neutron_plugin_ovs', $plugin_ovs_config) } diff --git a/manifests/init.pp b/manifests/init.pp index 3db024682..c1d732e29 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -32,7 +32,13 @@ # [*core_plugin*] # (optional) Neutron plugin provider # Defaults to openvswitch -# Could be bigswitch, brocade, cisco, embrane, hyperv, linuxbridge, midonet, ml2, mlnx, nec, nicira, plumgrid, ryu +# Could be bigswitch, brocade, cisco, embrane, hyperv, linuxbridge, midonet, ml2, mlnx, nec, nicira, plumgrid, ryu, opencontrail (full path) +# +# Example for opencontrail: +# +# class {'neutron' : +# core_plugin => 'neutron.plugins.opencontrail.contrail_plugin:NeutronPluginContrailCoreV2' +# } # # [*service_plugins*] # (optional) Advanced service modules. diff --git a/manifests/params.pp b/manifests/params.pp index 14b76292a..8b817651c 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -31,6 +31,9 @@ class neutron::params { $cisco_config_file = '/etc/neutron/plugins/cisco/cisco_plugins.ini' $cisco_ml2_config_file = '/etc/neutron/plugins/ml2/ml2_conf_cisco.ini' + $opencontrail_plugin_package = 'neutron-plugin-contrail' + $opencontrail_config_file = '/etc/neutron/plugins/opencontrail/ContrailPlugin.ini' + $midonet_server_package = 'python-neutron-plugin-midonet' $midonet_config_file = '/etc/neutron/plugins/midonet/midonet.ini' @@ -107,6 +110,9 @@ class neutron::params { $cisco_config_file = '/etc/neutron/plugins/cisco/cisco_plugins.ini' $cisco_ml2_config_file = '/etc/neutron/plugins/ml2/ml2_conf_cisco.ini' + $opencontrail_plugin_package = 'neutron-plugin-contrail' + $opencontrail_config_file = '/etc/neutron/plugins/opencontrail/ContrailPlugin.ini' + $midonet_server_package = 'python-neutron-plugin-midonet' $midonet_config_file = '/etc/neutron/plugins/midonet/midonet.ini' diff --git a/manifests/plugins/opencontrail.pp b/manifests/plugins/opencontrail.pp new file mode 100644 index 000000000..3b28c0371 --- /dev/null +++ b/manifests/plugins/opencontrail.pp @@ -0,0 +1,120 @@ +# This class installs and configures Opencontrail Neutron Plugin. +# +# === Parameters +# +# [*api_server_ip*] +# IP address of the API Server +# Defaults to undef +# +# [*api_server_port*] +# Port of the API Server. +# Defaults to undef +# +# [*multi_tenancy*] +# Whether to enable multi-tenancy +# Default to undef +# +# [*contrail_extensions*] +# Array of OpenContrail extensions to be supported +# Defaults to undef +# Example: +# +# class {'neutron::plugins::opencontrail' : +# contrail_extensions => ['ipam:neutron_plugin_contrail.plugins.opencontrail.contrail_plugin_ipam.NeutronPluginContrailIpam'] +# } +# +# [*keystone_auth_url*] +# Url of the keystone auth server +# Defaults to undef +# +# [*keystone_admin_user*] +# Admin user name +# Defaults to undef +# +# [*keystone_admin_tenant_name*] +# Admin_tenant_name +# Defaults to undef +# +# [*keystone_admin_password*] +# Admin password +# Defaults to undef +# +# [*keystone_admin_token*] +# Admin token +# Defaults to undef +# +class neutron::plugins::opencontrail ( + $api_server_ip = undef, + $api_server_port = undef, + $multi_tenancy = undef, + $contrail_extensions = undef, + $keystone_auth_url = undef, + $keystone_admin_user = undef, + $keystone_admin_tenant_name = undef, + $keystone_admin_password = undef, + $keystone_admin_token = undef, + $package_ensure = 'present', +) { + + include ::neutron::params + + validate_array($contrail_extensions) + + package { 'neutron-plugin-contrail': + ensure => $package_ensure, + name => $::neutron::params::opencontrail_plugin_package, + tag => 'openstack', + } + + # Although this manifest does not install opencontrail plugin package because it + # is not available in common distro repos, this statement forces you to + # have an orchestrator/wrapper manifest that does that job. + Package[$::neutron::params::opencontrail_plugin_package] -> Neutron_plugin_opencontrail<||> + Neutron_plugin_opencontrail<||> ~> Service['neutron-server'] + + ensure_resource('file', '/etc/neutron/plugins/opencontrail', { + ensure => directory, + owner => 'root', + group => 'neutron', + mode => '0640'} + ) + + # Ensure the neutron package is installed before config is set + # under both RHEL and Ubuntu + if ($::neutron::params::server_package) { + Package['neutron-server'] -> Neutron_plugin_opencontrail<||> + } else { + Package['neutron'] -> Neutron_plugin_opencontrail<||> + } + + if $::osfamily == 'Debian' { + file_line { '/etc/default/neutron-server:NEUTRON_PLUGIN_CONFIG': + path => '/etc/default/neutron-server', + match => '^NEUTRON_PLUGIN_CONFIG=(.*)$', + line => "NEUTRON_PLUGIN_CONFIG=${::neutron::params::opencontrail_config_file}", + require => [ Package['neutron-server'], Package[$::neutron::params::opencontrail_plugin_package] ], + notify => Service['neutron-server'], + } + } + + if $::osfamily == 'Redhat' { + file { '/etc/neutron/plugin.ini': + ensure => link, + target => $::neutron::params::opencontrail_config_file, + require => Package[$::neutron::params::opencontrail_plugin_package], + } + } + + neutron_plugin_opencontrail { + 'APISERVER/api_server_ip': value => $api_server_ip; + 'APISERVER/api_server_port': value => $api_server_port; + 'APISERVER/multi_tenancy': value => $multi_tenancy; + 'APISERVER/contrail_extensions': value => join($contrail_extensions, ','); + 'KEYSTONE/auth_url': value => $keystone_auth_url; + 'KEYSTONE/admin_user' : value => $keystone_admin_user; + 'KEYSTONE/admin_tenant_name': value => $keystone_admin_tenant_name; + 'KEYSTONE/admin_password': value => $keystone_admin_password, secret =>true; + 'KEYSTONE/admin_token': value => $keystone_admin_token, secret =>true; + } + +} diff --git a/spec/classes/neutron_plugins_opencontrail_spec.rb b/spec/classes/neutron_plugins_opencontrail_spec.rb new file mode 100644 index 000000000..975e89b21 --- /dev/null +++ b/spec/classes/neutron_plugins_opencontrail_spec.rb @@ -0,0 +1,93 @@ +require 'spec_helper' + +describe 'neutron::plugins::opencontrail' do + + let :pre_condition do + "class { 'neutron::server': auth_password => 'password' } + class { 'neutron': rabbit_password => 'passw0rd' }" + end + + let :default_params do + { :api_server_ip => '10.0.0.1', + :api_server_port => '8082', + :multi_tenancy => 'true', + :contrail_extensions => ['ipam:ipam','policy:policy','route-table'], + :keystone_auth_url => 'http://keystone-server:5000/v2.0', + :keystone_admin_user => 'admin', + :keystone_admin_tenant_name => 'admin', + :keystone_admin_password => 'admin', + :keystone_admin_token => 'token1' + } + end + + let :default_facts do + { :operatingsystem => 'default', + :operatingsystemrelease => 'default' + } + end + + shared_examples_for 'neutron opencontrail plugin' do + + let :params do + {} + end + + before do + params.merge!(default_params) + end + + it 'should perform default configuration of' do + is_expected.to contain_neutron_plugin_opencontrail('APISERVER/api_server_ip').with_value(params[:api_server_ip]) + is_expected.to contain_neutron_plugin_opencontrail('APISERVER/api_server_port').with_value(params[:api_server_port]) + is_expected.to contain_neutron_plugin_opencontrail('APISERVER/multi_tenancy').with_value(params[:multi_tenancy]) + is_expected.to contain_neutron_plugin_opencontrail('APISERVER/contrail_extensions').with_value(params[:contrail_extensions].join(',')) + is_expected.to contain_neutron_plugin_opencontrail('KEYSTONE/auth_url').with_value(params[:keystone_auth_url]) + is_expected.to contain_neutron_plugin_opencontrail('KEYSTONE/admin_user').with_value(params[:keystone_admin_user]) + is_expected.to contain_neutron_plugin_opencontrail('KEYSTONE/admin_tenant_name').with_value(params[:keystone_admin_tenant_name]) + is_expected.to contain_neutron_plugin_opencontrail('KEYSTONE/admin_password').with_value(params[:keystone_admin_password]) + is_expected.to contain_neutron_plugin_opencontrail('KEYSTONE/admin_token').with_value(params[:keystone_admin_token]) + end + + end + + context 'on Debian platforms' do + let :facts do + default_facts.merge({ :osfamily => 'Debian' }) + end + + let :params do + { :contrail_extensions => ['ipam:ipam','policy:policy','route-table'] } + end + + it 'configures /etc/default/neutron-server' do + is_expected.to contain_file_line('/etc/default/neutron-server:NEUTRON_PLUGIN_CONFIG').with( + :path => '/etc/default/neutron-server', + :match => '^NEUTRON_PLUGIN_CONFIG=(.*)$', + :line => 'NEUTRON_PLUGIN_CONFIG=/etc/neutron/plugins/opencontrail/ContrailPlugin.ini', + :require => ['Package[neutron-server]', 'Package[neutron-plugin-contrail]'], + :notify => 'Service[neutron-server]' + ) + end + it_configures 'neutron opencontrail plugin' + end + + context 'on RedHat platforms' do + let :facts do + default_facts.merge({ :osfamily => 'RedHat' }) + end + + let :params do + { :contrail_extensions => ['ipam:ipam','policy:policy','route-table'] } + end + + it 'should create plugin symbolic link' do + is_expected.to contain_file('/etc/neutron/plugin.ini').with( + :ensure => 'link', + :target => '/etc/neutron/plugins/opencontrail/ContrailPlugin.ini', + :require => 'Package[neutron-plugin-contrail]' + ) + end + it_configures 'neutron opencontrail plugin' + end + +end diff --git a/spec/unit/provider/neutron_plugin_opencontrail/ini_setting_spec.rb b/spec/unit/provider/neutron_plugin_opencontrail/ini_setting_spec.rb new file mode 100644 index 000000000..80711e166 --- /dev/null +++ b/spec/unit/provider/neutron_plugin_opencontrail/ini_setting_spec.rb @@ -0,0 +1,36 @@ +$LOAD_PATH.push( + File.join( + File.dirname(__FILE__), + '..', + '..', + '..', + 'fixtures', + 'modules', + 'inifile', + 'lib') +) + +require 'spec_helper' + +provider_class = Puppet::Type.type(:neutron_plugin_opencontrail).provider(:ini_setting) +describe provider_class do + let(:resource ) do + Puppet::Type::Neutron_plugin_opencontrail.new({ + :name => 'DEFAULT/foo', + :value => 'bar', + }) + end + + let (:provider) { resource.provider } + + [ 'RedHat', 'Debian' ].each do |os| + context "on #{os} with default setting" do + it 'it should fall back to default and use ContrailPlugin.ini' do + Facter.fact(:operatingsystem).stubs(:value).returns("#{os}") + expect(provider.section).to eq('DEFAULT') + expect(provider.setting).to eq('foo') + expect(provider.file_path).to eq('/etc/neutron/plugins/opencontrail/ContrailPlugin.ini') + end + end + end +end