]> git.openstreetmap.org Git - chef.git/blob - cookbooks/accounts/recipes/default.rb
Fix foodcritic warnings
[chef.git] / cookbooks / accounts / recipes / default.rb
1 #
2 # Cookbook Name:: accounts
3 # Recipe:: default
4 #
5 # Copyright 2010, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 package "zsh" do
21   action :install
22 end
23
24 administrators = []
25
26 search(:accounts, "*:*").each do |account|
27   name = account["id"]
28   details = node[:accounts][:users][name] || {}
29
30   if details[:status]
31     group_members = details[:members] || account["members"] || []
32     user_home = details[:home] || account["home"] || "#{node[:accounts][:home]}/#{name}"
33     manage_user_home = details.fetch(:manage_home, account.fetch("manage_home", node[:accounts][:manage_home]))
34
35     group_members = group_members.collect(&:to_s).sort
36
37     case details[:status]
38     when "role"
39       user_shell = "/usr/sbin/nologin"
40     when "user", "administrator"
41       user_shell = details[:shell] || account["shell"] || node[:accounts][:shell]
42     end
43
44     group name.to_s do
45       gid account["uid"].to_i
46       members group_members & node[:etc][:passwd].keys
47     end
48
49     user name.to_s do
50       uid account["uid"].to_i
51       gid account["uid"].to_i
52       comment account["comment"] if account["comment"]
53       home user_home
54       shell user_shell
55       manage_home manage_user_home
56     end
57
58     remote_directory "/home/#{name}" do
59       path user_home
60       source name.to_s
61       owner name.to_s
62       group name.to_s
63       mode 0o755
64       files_owner name.to_s
65       files_group name.to_s
66       files_mode 0o644
67       only_if do
68         begin
69           cookbook = run_context.cookbook_collection[cookbook_name]
70           files = cookbook.relative_filenames_in_preferred_directory(node, :files, name.to_s)
71           !files.empty?
72         rescue Chef::Exceptions::FileNotFound
73           false
74         end
75       end
76     end
77
78     administrators.push(name.to_s) if details[:status] == "administrator"
79   else
80     user name.to_s do
81       action :remove
82     end
83
84     group name.to_s do
85       action :remove
86     end
87   end
88 end
89
90 node[:accounts][:groups].each do |name, details|
91   group name do
92     action :modify
93     members details[:members]
94     append true
95   end
96 end
97
98 group "sudo" do
99   action :manage
100   members administrators.sort
101 end
102
103 group "admin" do
104   action :manage
105   members administrators.sort
106 end
107
108 group "adm" do
109   action :modify
110   members administrators.sort
111 end