Skip to content

Commit d6f62e7

Browse files
author
Henning Block
committed
To be able to create replicaset in an authenticated environment, a switch has been added to first create the replicaset (on first run) and afterwards enable authentication.
This is quite dirty but there does not seem to be another way.
1 parent 62e02d1 commit d6f62e7

File tree

18 files changed

+277
-154
lines changed

18 files changed

+277
-154
lines changed

lib/facter/is_master.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/facter/mongodb_is_master.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'json';
2+
3+
Facter.add('mongodb_is_master') do
4+
setcode do
5+
if Facter::Core::Execution.which('mongo')
6+
mongo_output = Facter::Core::Execution.exec('mongo --quiet --eval "printjson(db.isMaster())" 2>/dev/null')
7+
8+
if mongo_output =~ /Failed to connect to/
9+
'failed_to_connect'
10+
else
11+
['ObjectId','ISODate'].each do |data_type|
12+
mongo_output.gsub!(/#{data_type}\(([^)]*)\)/, '\1')
13+
end
14+
JSON.parse(mongo_output)['ismaster'] ||= false
15+
end
16+
else
17+
'not_installed'
18+
end
19+
end
20+
end
21+

lib/puppet/provider/mongodb_replset/mongo.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def set_members
196196
hostconf = alive_hosts.each_with_index.map do |host,id|
197197
arbiter_conf = ""
198198
if rs_arbiter == host
199-
arbiter_conf = ", arbiterOnly: \"true\""
199+
arbiter_conf = ", arbiterOnly: true"
200200
end
201201
"{ _id: #{id}, host: \"#{host}\"#{arbiter_conf} }"
202202
end.join(',')
@@ -266,6 +266,7 @@ def self.mongo_command(command, host=nil, retries=4)
266266
end
267267

268268
# Dirty hack to remove JavaScript objects
269+
output.gsub!(/Timestamp\(([^,]+?),.*\)/, '\1')
269270
output.gsub!(/\w+\((.+?)\)/, '\1')
270271

271272
#Hack to avoid non-json empty sets

manifests/globals.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
$use_enterprise_repo = undef,
3535

3636
$pidfilepath = undef,
37+
38+
$create_admin = false,
39+
$admin_username = 'admin',
3740
) {
3841

3942
# Setup of the repo only makes sense globally, so we are doing it here.

manifests/params.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
$service_ensure = pick($mongodb::globals::service_ensure, 'running')
99
$service_status = $mongodb::globals::service_status
1010
$restart = true
11-
$create_admin = false
12-
$admin_username = 'admin'
11+
$create_admin = $mongodb::globals::create_admin
12+
$admin_username = $mongodb::globals::admin_username
1313
$store_creds = false
1414
$rcfile = "${::root_home}/.mongorc.js"
1515

manifests/replset.pp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Wrapper class useful for hiera based deployments
22
class mongodb::replset(
3-
$sets = undef
4-
) {
3+
$sets = undef,
4+
$admin_username = $mongodb::params::admin_username
5+
) inherits mongodb::params {
56

67
if $sets {
78
create_resources(mongodb_replset, $sets)
89
}
910

1011
# Order replset before any DB's and shard config
11-
Mongodb_replset <| |> -> Mongodb_database <| |>
12+
Mongodb_replset <| |> -> Mongodb::Db <| |>
1213
Mongodb_replset <| |> -> Mongodb_shard <| |>
1314
Mongodb_replset <| |> -> Mongodb_user <| |>
1415
}

manifests/server.pp

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,17 @@
6969
$ssl_ca = undef,
7070
$restart = $mongodb::params::restart,
7171
$storage_engine = undef,
72+
$version = $mongodb::params::version,
7273

7374
$create_admin = $mongodb::params::create_admin,
7475
$admin_username = $mongodb::params::admin_username,
7576
$admin_password = undef,
7677
$store_creds = $mongodb::params::store_creds,
7778
$admin_roles = ['userAdmin', 'readWrite', 'dbAdmin',
78-
'dbAdminAnyDatabase', 'readAnyDatabase',
79-
'readWriteAnyDatabase', 'userAdminAnyDatabase',
80-
'clusterAdmin', 'clusterManager', 'clusterMonitor',
81-
'hostManager', 'root', 'restore'],
79+
'dbAdminAnyDatabase', 'readAnyDatabase',
80+
'readWriteAnyDatabase', 'userAdminAnyDatabase',
81+
'clusterAdmin', 'clusterManager', 'clusterMonitor',
82+
'hostManager', 'root', 'restore'],
8283

8384
# Deprecated parameters
8485
$master = undef,
@@ -96,14 +97,14 @@
9697
if $restart {
9798
anchor { 'mongodb::server::start': }->
9899
class { 'mongodb::server::install': }->
99-
# If $restart is true, notify the service on config changes (~>)
100+
# If $restart is true, notify the service on config changes (~>)
100101
class { 'mongodb::server::config': }~>
101102
class { 'mongodb::server::service': }->
102103
anchor { 'mongodb::server::end': }
103104
} else {
104105
anchor { 'mongodb::server::start': }->
105106
class { 'mongodb::server::install': }->
106-
# If $restart is false, config changes won't restart the service (->)
107+
# If $restart is false, config changes won't restart the service (->)
107108
class { 'mongodb::server::config': }->
108109
class { 'mongodb::server::service': }->
109110
anchor { 'mongodb::server::end': }
@@ -158,18 +159,43 @@
158159
}
159160
}
160161
}
162+
}
161163

162-
# Wrap the replset class
163-
class { 'mongodb::replset':
164-
sets => $replset_config_REAL
165-
}
166-
Anchor['mongodb::server::end'] -> Class['mongodb::replset']
164+
# Wrap the replset class
165+
class { 'mongodb::replset':
166+
sets => $replset_config_REAL
167+
}
168+
Anchor['mongodb::server::end'] -> Class['mongodb::replset']
169+
170+
# Make sure that the ordering is correct
171+
if $create_admin {
172+
Class['mongodb::replset'] -> Mongodb::Db['admin']
173+
if $::mongodb_is_master == 'not_installed' and $auth == true and $noauth != true and versioncmp($version, '2.6.0') >= 0 {
174+
file_line{ 'enable_authentication' :
175+
ensure => present,
176+
path => $config,
177+
match => 'security.authorization:',
178+
line => 'security.authorization: enabled',
179+
require => [Class['mongodb::replset'], Mongodb::Db['admin'] ]
180+
}
181+
if $keyfile {
182+
file_line{ 'enable_keyfile' :
183+
ensure => present,
184+
path => $config,
185+
line => "security.keyFile: ${keyfile}",
186+
require => [Class['mongodb::replset'], Mongodb::Db['admin']],
187+
notify => Exec['/sbin/restart mongod']
188+
}
189+
}
167190

168-
# Make sure that the ordering is correct
169-
if $create_admin {
170-
Class['mongodb::replset'] -> Mongodb::Db['admin']
191+
exec{ '/sbin/restart mongod':
192+
user => 'root',
193+
refreshonly => true,
194+
cwd => '/tmp',
195+
subscribe => File_line['enable_authentication']
196+
}
171197
}
172-
173198
}
199+
174200
}
175201
}

manifests/server/config.pp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
$storage_engine = $mongodb::server::storage_engine
6565
$version = $mongodb::server::version
6666

67+
if $auth == true and $::mongodb_is_master=='not_installed' {
68+
$real_auth=false
69+
}
70+
else {
71+
$real_auth=$auth
72+
}
73+
6774
File {
6875
owner => $user,
6976
group => $group,

spec/classes/mongos_config_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
{
99
:osfamily => 'Debian',
1010
:operatingsystem => 'Debian',
11+
:root_home => '/root',
12+
:operatingsystemmajrelease => '14.04'
1113
}
1214
end
1315

spec/classes/mongos_install_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
{
99
:osfamily => 'Debian',
1010
:operatingsystem => 'Debian',
11+
:root_home => '/root',
12+
:operatingsystemmajrelease => '14.04'
1113
}
1214
end
1315

spec/classes/mongos_service_spec.rb

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
context 'on Debian with service_manage set to true' do
66
let :facts do
77
{
8-
:osfamily => 'Debian',
9-
:operatingsystem => 'Debian',
10-
:operatingsystemrelease => '7.0',
8+
:osfamily => 'Debian',
9+
:operatingsystem => 'Debian',
10+
:operatingsystemrelease => '7.0',
11+
:root_home => '/root',
12+
:operatingsystemmajrelease => '14.04'
1113
}
1214
end
1315

14-
let :pre_condition do
16+
let :pre_condition do
1517
"class { 'mongodb::mongos':
1618
configdb => ['127.0.0.1:27019'],
1719
}"
18-
end
20+
end
1921

2022
describe 'include init script' do
2123
it { is_expected.to contain_file('/etc/init.d/mongos') }
@@ -30,9 +32,11 @@
3032
context 'on Debian with service_manage set to false' do
3133
let :facts do
3234
{
33-
:osfamily => 'Debian',
34-
:operatingsystem => 'Debian',
35-
:operatingsystemrelease => '7.0',
35+
:osfamily => 'Debian',
36+
:operatingsystem => 'Debian',
37+
:operatingsystemrelease => '7.0',
38+
:operatingsystemmajrelease => '14.04',
39+
:root_home => '/root'
3640
}
3741
end
3842

@@ -52,9 +56,11 @@
5256
context 'on RedHat with service_manage set to true' do
5357
let :facts do
5458
{
55-
:osfamily => 'RedHat',
56-
:operatingsystem => 'RedHat',
57-
:operatingsystemrelease => '7.0',
59+
:osfamily => 'RedHat',
60+
:operatingsystem => 'RedHat',
61+
:operatingsystemrelease => '7.0',
62+
:operatingsystemmajrelease => '14.04',
63+
:root_home => '/root'
5864
}
5965
end
6066

@@ -81,9 +87,11 @@
8187
context 'on RedHat with service_manage set to false' do
8288
let :facts do
8389
{
84-
:osfamily => 'RedHat',
85-
:operatingsystem => 'RedHat',
86-
:operatingsystemrelease => '7.0',
90+
:osfamily => 'RedHat',
91+
:operatingsystem => 'RedHat',
92+
:operatingsystemrelease => '7.0',
93+
:operatingsystemmajrelease => '14.04',
94+
:root_home => '/root'
8795
}
8896
end
8997

spec/classes/mongos_spec.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
describe 'mongodb::mongos' do
44
let :facts do
55
{
6-
:osfamily => 'Debian',
7-
:operatingsystem => 'Debian',
6+
:osfamily => 'Debian',
7+
:operatingsystem => 'Debian',
8+
:root_home => '/root',
9+
:operatingsystemmajrelease => '14.04',
810
}
911
end
1012

1113
let :params do
1214
{
13-
:configdb => ['127.0.0.1:27019']
15+
:configdb => ['127.0.0.1:27019']
1416
}
1517
end
1618

@@ -21,8 +23,7 @@
2123
end
2224

2325
context 'when deploying on Solaris' do
24-
let :facts do
25-
{ :osfamily => 'Solaris' }
26+
let :facts do {:osfamily => 'Solaris', }
2627
end
2728
it { expect { is_expected.to raise_error(Puppet::Error) } }
2829
end

spec/classes/repo_spec.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
context 'when deploying on Debian' do
66
let :facts do
77
{
8-
:osfamily => 'Debian',
9-
:operatingsystem => 'Debian',
10-
:operatingsystemrelease => '7.0',
11-
:lsbdistid => 'Debian',
8+
:osfamily => 'Debian',
9+
:operatingsystem => 'Debian',
10+
:operatingsystemrelease => '7.0',
11+
:lsbdistid => 'Debian',
12+
:root_home => '/root',
13+
:operatingsystemmajrelease => '7.0'
1214
}
1315
end
1416

@@ -20,9 +22,10 @@
2022
context 'when deploying on CentOS' do
2123
let :facts do
2224
{
23-
:osfamily => 'RedHat',
24-
:operatingsystem => 'CentOS',
25-
:operatingsystemrelease => '7.0',
25+
:osfamily => 'RedHat',
26+
:operatingsystem => 'CentOS',
27+
:operatingsystemrelease => '7.0',
28+
:operatingsystemmajrelease => '7.0'
2629
}
2730
end
2831

@@ -34,28 +37,29 @@
3437
context 'when yumrepo has a proxy set' do
3538
let :facts do
3639
{
37-
:osfamily => 'RedHat',
38-
:operatingsystem => 'RedHat',
39-
:operatingsystemrelease => '7.0',
40+
:osfamily => 'RedHat',
41+
:operatingsystem => 'RedHat',
42+
:operatingsystemrelease => '7.0',
43+
:operatingsystemmajrelease => '7.0'
4044
}
4145
end
4246
let :params do
4347
{
44-
:proxy => 'http://proxy-server:8080',
45-
:proxy_username => 'proxyuser1',
46-
:proxy_password => 'proxypassword1',
48+
:proxy => 'http://proxy-server:8080',
49+
:proxy_username => 'proxyuser1',
50+
:proxy_password => 'proxypassword1',
4751
}
4852
end
4953
it {
5054
is_expected.to contain_class('mongodb::repo::yum')
5155
}
5256
it do
5357
should contain_yumrepo('mongodb').with({
54-
'enabled' => '1',
55-
'proxy' => 'http://proxy-server:8080',
56-
'proxy_username' => 'proxyuser1',
57-
'proxy_password' => 'proxypassword1',
58-
})
58+
'enabled' => '1',
59+
'proxy' => 'http://proxy-server:8080',
60+
'proxy_username' => 'proxyuser1',
61+
'proxy_password' => 'proxypassword1',
62+
})
5963
end
6064
end
6165
end

0 commit comments

Comments
 (0)