Skip to content

Commit

Permalink
Introduce neutron::db class
Browse files Browse the repository at this point in the history
Current modules[1][2][3] implements a
<component>::db class that is not implemented in neutron.
This commit aims to apply here the same logic.

[1] https://github.com/openstack/puppet-nova/blob/master/manifests/db.pp
[2]
https://github.com/openstack/puppet-designate/blob/master/manifests/db.pp
[3]
https://github.com/openstack/puppet-ceilometer/blob/master/manifests/db.pp

Change-Id: I4169145d9a75bcbf607ace426dca671e0eec0261
  • Loading branch information
Spredzy committed Sep 15, 2015
1 parent 424ea9a commit 7b54f89
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 69 deletions.
97 changes: 97 additions & 0 deletions manifests/db.pp
@@ -0,0 +1,97 @@
# == Class: neutron::db
#
# Configure the Neutron database
#
# === Parameters
#
# [*database_connection*]
# Url used to connect to database.
# (Optional) Defaults to 'sqlite:////var/lib/neutron/ovs.sqlite'.
#
# [*database_idle_timeout*]
# Timeout when db connections should be reaped.
# (Optional) Defaults to 3600.
#
# [*database_min_pool_size*]
# Minimum number of SQL connections to keep open in a pool.
# (Optional) Defaults to 1.
#
# [*database_max_pool_size*]
# Maximum number of SQL connections to keep open in a pool.
# (Optional) Defaults to 10.
#
# [*database_max_retries*]
# Maximum db connection retries during startup.
# Setting -1 implies an infinite retry count.
# (Optional) Defaults to 10.
#
# [*database_retry_interval*]
# Interval between retries of opening a sql connection.
# (Optional) Defaults to 10.
#
# [*database_max_overflow*]
# If set, use this value for max_overflow with sqlalchemy.
# (Optional) Defaults to 20.
#
class neutron::db (
$database_connection = 'sqlite:////var/lib/neutron/ovs.sqlite',
$database_idle_timeout = 3600,
$database_min_pool_size = 1,
$database_max_pool_size = 10,
$database_max_retries = 10,
$database_retry_interval = 10,
$database_max_overflow = 20,
) {

# NOTE(spredzy): In order to keep backward compatibility we rely on the pick function
# to use neutron::<myparam> if neutron::db::<myparam> isn't specified.
$database_connection_real = pick($::neutron::server::database_connection, $database_connection)
$database_idle_timeout_real = pick($::neutron::server::database_idle_timeout, $database_idle_timeout)
$database_min_pool_size_real = pick($::neutron::server::database_min_pool_size, $database_min_pool_size)
$database_max_pool_size_real = pick($::neutron::server::database_max_pool_size, $database_max_pool_size)
$database_max_retries_real = pick($::neutron::server::database_max_retries, $database_max_retries)
$database_retry_interval_real = pick($::neutron::server::database_retry_interval, $database_retry_interval)
$database_max_overflow_real = pick($::neutron::server::database_max_overflow, $database_max_overflow)

validate_re($database_connection_real,
'(sqlite|mysql|postgresql):\/\/(\S+:\S+@\S+\/\S+)?')

if $database_connection_real {
case $database_connection_real {
/^mysql:\/\//: {
$backend_package = false
require 'mysql::bindings'
require 'mysql::bindings::python'
}
/^postgresql:\/\//: {
$backend_package = $::neutron::params::psycopg_package_name
}
/^sqlite:\/\//: {
$backend_package = $::neutron::params::sqlite_package_name
}
default: {
fail('Unsupported backend configured')
}
}

if $backend_package and !defined(Package[$backend_package]) {
package {'neutron-backend-package':
ensure => present,
name => $backend_package,
tag => 'openstack',
}
}

neutron_config {
'database/connection': value => $database_connection_real, secret => true;
'database/idle_timeout': value => $database_idle_timeout_real;
'database/min_pool_size': value => $database_min_pool_size_real;
'database/max_retries': value => $database_max_retries_real;
'database/retry_interval': value => $database_retry_interval_real;
'database/max_pool_size': value => $database_max_pool_size_real;
'database/max_overflow': value => $database_max_overflow_real;
}

}

}
5 changes: 5 additions & 0 deletions manifests/params.pp
Expand Up @@ -75,6 +75,9 @@

$kernel_headers = "linux-headers-${::kernelrelease}"

$psycopg_package_name = 'python-psycopg2'
$sqlite_package_name = undef

} elsif($::osfamily == 'Debian') {

$nobody_user_group = 'nogroup'
Expand Down Expand Up @@ -152,6 +155,8 @@
$cliff_package = 'python-cliff'
$kernel_headers = "linux-headers-${::kernelrelease}"

$psycopg_package_name = 'python-psycopg2'
$sqlite_package_name = 'python-pysqlite2'
} else {

fail("Unsupported osfamily ${::osfamily}")
Expand Down
59 changes: 14 additions & 45 deletions manifests/server.pp
Expand Up @@ -75,7 +75,7 @@
#
# [*database_connection*]
# (optional) Connection url for the neutron database.
# (Defaults to 'sqlite:////var/lib/neutron/ovs.sqlite')
# (Defaults to undef)
#
# [*sql_connection*]
# DEPRECATED: Use database_connection instead.
Expand All @@ -85,7 +85,7 @@
#
# [*database_max_retries*]
# (optional) Maximum database connection retries during startup.
# (Defaults to 10)
# (Defaults to undef)
#
# [*sql_max_retries*]
# DEPRECATED: Use database_max_retries instead.
Expand All @@ -96,7 +96,7 @@
# [*database_idle_timeout*]
# (optional) Timeout before idle database connections are reaped.
# Deprecates sql_idle_timeout
# (Defaults to 3600)
# (Defaults to undef)
#
# [*sql_idle_timeout*]
# DEPRECATED: Use database_idle_timeout instead.
Expand All @@ -116,15 +116,15 @@
#
# [*database_min_pool_size*]
# (optional) Minimum number of SQL connections to keep open in a pool.
# Defaults to: 1
# Defaults to: undef.
#
# [*database_max_pool_size*]
# (optional) Maximum number of SQL connections to keep open in a pool.
# Defaults to: 10
# Defaults to: undef.
#
# [*database_max_overflow*]
# (optional) If set, use this value for max_overflow with sqlalchemy.
# Defaults to: 20
# Defaults to: undef.
#
# [*sync_db*]
# (optional) Run neutron-db-manage on api nodes after installing the package.
Expand Down Expand Up @@ -165,9 +165,6 @@
# neutron.scheduler.l3_agent_scheduler.LeastRoutersScheduler to allocate on an L3 agent with the least number of routers bound.
# Defaults to: neutron.scheduler.l3_agent_scheduler.ChanceScheduler
#
# [*mysql_module*]
# (optional) Deprecated. Does nothing.
#
# [*router_distributed*]
# (optional) Setting the "router_distributed" flag to "True" will default to the creation
# of distributed tenant routers.
Expand Down Expand Up @@ -211,13 +208,13 @@
$auth_user = 'neutron',
$auth_uri = false,
$identity_uri = false,
$database_connection = 'sqlite:////var/lib/neutron/ovs.sqlite',
$database_max_retries = 10,
$database_idle_timeout = 3600,
$database_retry_interval = 10,
$database_min_pool_size = 1,
$database_max_pool_size = 10,
$database_max_overflow = 20,
$database_connection = undef,
$database_max_retries = undef,
$database_idle_timeout = undef,
$database_retry_interval = undef,
$database_min_pool_size = undef,
$database_max_pool_size = undef,
$database_max_overflow = undef,
$sync_db = false,
$api_workers = $::processorcount,
$rpc_workers = $::processorcount,
Expand All @@ -234,14 +231,14 @@
$auth_port = '35357',
$auth_protocol = 'http',
$auth_admin_prefix = false,
$mysql_module = undef,
$log_dir = undef,
$log_file = undef,
$report_interval = undef,
$state_path = undef,
$lock_path = undef,
) {

include ::neutron::db
include ::neutron::params
include ::neutron::policy
require keystone::python
Expand Down Expand Up @@ -269,27 +266,6 @@
}
}

if $mysql_module {
warning('The mysql_module parameter is deprecated. The latest 2.x mysql module will be used.')
}

validate_re($database_connection, '(sqlite|mysql|postgresql):\/\/(\S+:\S+@\S+\/\S+)?')

case $database_connection {
/mysql:\/\/\S+:\S+@\S+\/\S+/: {
require 'mysql::bindings'
require 'mysql::bindings::python'
}
/postgresql:\/\/\S+:\S+@\S+\/\S+/: {
$backend_package = 'python-psycopg2'
}
/sqlite:\/\//: {
$backend_package = 'python-pysqlite2'
}
default: {
fail("Invalid database_connection parameter: ${database_connection}")
}
}

if $sync_db {
include ::neutron::db::sync
Expand All @@ -302,13 +278,6 @@
'DEFAULT/router_scheduler_driver': value => $router_scheduler_driver;
'DEFAULT/router_distributed': value => $router_distributed;
'DEFAULT/allow_automatic_l3agent_failover': value => $allow_automatic_l3agent_failover;
'database/connection': value => $database_connection, secret => true;
'database/idle_timeout': value => $database_idle_timeout;
'database/retry_interval': value => $database_retry_interval;
'database/max_retries': value => $database_max_retries;
'database/min_pool_size': value => $database_min_pool_size;
'database/max_pool_size': value => $database_max_pool_size;
'database/max_overflow': value => $database_max_overflow;
}

if $state_path {
Expand Down
60 changes: 60 additions & 0 deletions spec/classes/neutron_db_spec.rb
@@ -0,0 +1,60 @@
require 'spec_helper'

describe 'neutron::db' do

shared_examples 'neutron::db' do

context 'with default parameters' do

it { is_expected.to contain_neutron_config('database/connection').with_value('sqlite:////var/lib/neutron/ovs.sqlite').with_secret(true) }
it { is_expected.to contain_neutron_config('database/idle_timeout').with_value('3600') }
it { is_expected.to contain_neutron_config('database/min_pool_size').with_value('1') }
it { is_expected.to contain_neutron_config('database/max_retries').with_value('10') }
it { is_expected.to contain_neutron_config('database/retry_interval').with_value('10') }

end

context 'with specific parameters' do
let :params do
{ :database_connection => 'mysql://neutron:neutron@localhost/neutron',
:database_idle_timeout => '3601',
:database_min_pool_size => '2',
:database_max_retries => '11',
:database_retry_interval => '11', }
end

it { is_expected.to contain_neutron_config('database/connection').with_value('mysql://neutron:neutron@localhost/neutron').with_secret(true) }
it { is_expected.to contain_neutron_config('database/idle_timeout').with_value('3601') }
it { is_expected.to contain_neutron_config('database/min_pool_size').with_value('2') }
it { is_expected.to contain_neutron_config('database/max_retries').with_value('11') }
it { is_expected.to contain_neutron_config('database/retry_interval').with_value('11') }

end

context 'with incorrect database_connection string' do
let :params do
{ :database_connection => 'redis://neutron:neutron@localhost/neutron', }
end

it_raises 'a Puppet::Error', /validate_re/
end

end

context 'on Debian platforms' do
let :facts do
{ :osfamily => 'Debian' }
end

it_configures 'neutron::db'
end

context 'on Redhat platforms' do
let :facts do
{ :osfamily => 'RedHat' }
end

it_configures 'neutron::db'
end

end
25 changes: 1 addition & 24 deletions spec/classes/neutron_server_spec.rb
Expand Up @@ -49,17 +49,7 @@
default_params.merge(params)
end

it 'should perform default database configuration of' do
is_expected.to contain_neutron_config('database/connection').with_value(p[:database_connection])
is_expected.to contain_neutron_config('database/connection').with_secret( true )
is_expected.to contain_neutron_config('database/max_retries').with_value(p[:database_max_retries])
is_expected.to contain_neutron_config('database/idle_timeout').with_value(p[:database_idle_timeout])
is_expected.to contain_neutron_config('database/retry_interval').with_value(p[:database_retry_interval])
is_expected.to contain_neutron_config('database/min_pool_size').with_value(p[:database_min_pool_size])
is_expected.to contain_neutron_config('database/max_pool_size').with_value(p[:database_max_pool_size])
is_expected.to contain_neutron_config('database/max_overflow').with_value(p[:database_max_overflow])
end

it { is_expected.to contain_class('neutron::db') }
it { is_expected.to contain_class('neutron::params') }
it { is_expected.to contain_class('neutron::policy') }

Expand Down Expand Up @@ -242,17 +232,6 @@
end
end

shared_examples_for 'a neutron server with database_connection specified' do
before do
params.merge!(
:database_connection => 'sqlite:////var/lib/neutron/ovs-TEST_parameter.sqlite'
)
end
it 'configures database connection' do
is_expected.to contain_neutron_config('database/connection').with_value(params[:database_connection])
end
end

describe "with custom keystone auth_uri" do
let :facts do
default_facts.merge({ :osfamily => 'RedHat' })
Expand Down Expand Up @@ -341,7 +320,6 @@
it_configures 'a neutron server with broken authentication'
it_configures 'a neutron server with auth_admin_prefix set'
it_configures 'a neutron server with some incorrect auth_admin_prefix set'
it_configures 'a neutron server with database_connection specified'
it_configures 'a neutron server without database synchronization'
end

Expand All @@ -360,7 +338,6 @@
it_configures 'a neutron server with broken authentication'
it_configures 'a neutron server with auth_admin_prefix set'
it_configures 'a neutron server with some incorrect auth_admin_prefix set'
it_configures 'a neutron server with database_connection specified'
it_configures 'a neutron server without database synchronization'
end
end

0 comments on commit 7b54f89

Please sign in to comment.