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