]> git.openstreetmap.org Git - chef.git/blob - cookbooks/accounts/recipes/default.rb
fa7dc2814c8367705e7eea61284f97470a24a774
[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         cookbook = run_context.cookbook_collection[cookbook_name]
73         files = cookbook.relative_filenames_in_preferred_directory(node, :files, name.to_s)
74         !files.empty?
75       rescue Chef::Exceptions::FileNotFound
76         false
77       end
78     end
79
80     administrators.push(name.to_s) if details[:status] == "administrator"
81   else
82     user name.to_s do
83       action :remove
84     end
85
86     group name.to_s do
87       action :remove
88     end
89   end
90 end
91
92 node[:accounts][:groups].each do |name, details|
93   group name do
94     action :modify
95     members details[:members]
96     append true
97   end
98 end
99
100 group "sudo" do
101   action :manage
102   members administrators.sort
103 end
104
105 group "admin" do
106   action :manage
107   members administrators.sort
108 end
109
110 group "adm" do
111   action :modify
112   members administrators.sort
113 end