128 lines
3.9 KiB
Ruby

resource_name :dirsrv_instance
provides :dirsrv_instance
property :instance_name, String, name_property: true
property :hostname, String, required: true
property :admin_password, String, required: true
property :suffix, String, required: true
property :admin_username, String, default: 'admin'
property :bind_dn, String, default: 'cn=Directory Manager'
property :port, Integer, default: 389
action :create do
include_recipe "apt"
package "389-ds-base"
package "python3-lib389"
include_recipe "ulimit"
user_ulimit "dirsrv" do
filehandle_limit 40960
end
config = {
instance_name: new_resource.instance_name,
hostname: new_resource.hostname,
suffix: new_resource.suffix,
port: new_resource.port,
bind_dn: new_resource.bind_dn,
admin_username: new_resource.admin_username,
admin_password: new_resource.admin_password,
base_dir: "/var/lib/dirsrv",
conf_dir: "/etc/dirsrv"
}
inst_dir = "/etc/dirsrv/slapd-#{new_resource.instance_name}"
service_name = "dirsrv@#{new_resource.instance_name}"
cookbook_file "/etc/dirsrv/config/template-initconfig" do
source "template-initconfig"
mode "0644"
owner "dirsrv"
group "dirsrv"
end
cookbook_file "/usr/sbin/ldif2db" do
source "ldif2db"
mode "0755"
owner "root"
group "root"
end
unless ::Dir.exists?(inst_dir)
setup_config = "#{config[:conf_dir]}/setup-#{new_resource.instance_name}.inf"
template setup_config do
source "setup.inf.erb"
mode "0600"
owner "root"
group "root"
sensitive true
variables config
end
execute "setup-#{new_resource.instance_name}" do
command "/usr/share/dirsrv/setup-ds.pl --silent --file #{setup_config}"
creates ::File.join inst_dir, 'dse.ldif'
action :nothing
subscribes :run, "template[#{setup_config}]", :immediately
notifies :restart, "service[#{service_name}]", :immediately
notifies :delete, "template[#{setup_config}]", :immediately
notifies :run, "execute[set base acis]", :delayed
notifies :run, "execute[add users group]", :delayed
notifies :run, "execute[disable anonymous access]", :delayed
end
end
service service_name do
action [:start]
end
# :enable on the dirsrv@master service creates the wrong file
# (/etc/systemd/system/dirsrv.target.wants/dirsrv@master.service), seems to
# be a Chef bug. Let's do it manually
link "/etc/systemd/system/multi-user.target.wants/dirsrv@master.service" do
to "/lib/systemd/system/dirsrv@.service"
end
cookbook_file "#{Chef::Config[:file_cache_path]}/acis.ldif" do
source "acis.ldif"
owner "root"
group "root"
end
execute "set base acis" do
command "ldapmodify -x -w #{new_resource.admin_password} -D '#{new_resource.bind_dn}' -f '#{Chef::Config[:file_cache_path]}/acis.ldif' -p #{new_resource.port} -h localhost"
sensitive true
action :nothing
end
cookbook_file "#{Chef::Config[:file_cache_path]}/users.ldif" do
source "users.ldif"
owner "root"
group "root"
end
execute "add users group" do
command "ldapadd -x -w #{new_resource.admin_password} -D '#{new_resource.bind_dn}' -f '#{Chef::Config[:file_cache_path]}/users.ldif' -p #{new_resource.port} -h localhost"
sensitive true
action :nothing
end
file "#{Chef::Config[:file_cache_path]}/disable_anonymous_access.ldif" do
content <<-EOF
dn: cn=config
changetype: modify
replace: nsslapd-allow-anonymous-access
nsslapd-allow-anonymous-access: off
EOF
owner "root"
group "root"
end
execute "disable anonymous access" do
command "ldapmodify -x -w #{new_resource.admin_password} -D '#{new_resource.bind_dn}' -f '#{Chef::Config[:file_cache_path]}/disable_anonymous_access.ldif' -p #{new_resource.port} -h localhost"
sensitive true
action :nothing
end
end