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

0 commit comments

Comments
 (0)