]> git.openstreetmap.org Git - chef.git/blob - cookbooks/accounts/recipes/default.rb
250ca9e410a136b406a6c84848109b849d9a0032
[chef.git] / cookbooks / accounts / recipes / default.rb
1 #
2 # Cookbook:: 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 #     https://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"
21
22 administrators = []
23
24 search(:accounts, "*:*").each do |account|
25   name = account["id"]
26   details = node[:accounts][:users][name] || {}
27
28   if details[:status]
29     group_members = details[:members] || account["members"] || []
30     user_home = details[:home] || account["home"] || "#{node[:accounts][:home]}/#{name}"
31     manage_user_home = details.fetch(:manage_home, account.fetch("manage_home", node[:accounts][:manage_home]))
32
33     group_members = group_members.collect(&:to_s).sort
34
35     case details[:status]
36     when "role"
37       user_shell = "/usr/sbin/nologin"
38     when "user", "administrator"
39       user_shell = details[:shell] || account["shell"] || node[:accounts][:shell]
40     end
41
42     available_users = if node[:etc]
43                         node[:etc][:passwd].keys
44                       else
45                         []
46                       end
47
48     group name.to_s do
49       gid account["uid"].to_i
50       members group_members & available_users
51     end
52
53     user name.to_s do
54       uid account["uid"].to_i
55       gid account["uid"].to_i
56       comment account["comment"] if account["comment"]
57       home user_home
58       shell user_shell
59       manage_home manage_user_home
60     end
61
62     remote_directory "/home/#{name}" do
63       path user_home
64       source name.to_s
65       owner name.to_s
66       group name.to_s
67       mode 0o755
68       files_owner name.to_s
69       files_group name.to_s
70       files_mode 0o644
71       only_if do
72         begin
73           cookbook = run_context.cookbook_collection[cookbook_name]
74           files = cookbook.relative_filenames_in_preferred_directory(node, :files, name.to_s)
75           !files.empty?
76         rescue Chef::Exceptions::FileNotFound
77           false
78         end
79       end
80     end
81
82     administrators.push(name.to_s) if details[:status] == "administrator"
83   else
84     user name.to_s do
85       action :remove
86     end
87
88     group name.to_s do
89       action :remove
90     end
91   end
92 end
93
94 node[:accounts][:groups].each do |name, details|
95   group name do
96     action :modify
97     members details[:members]
98     append true
99   end
100 end
101
102 group "sudo" do
103   action :manage
104   members administrators.sort
105 end
106
107 group "admin" do
108   action :manage
109   members administrators.sort
110 end
111
112 group "adm" do
113   action :modify
114   members administrators.sort
115 end