]> git.openstreetmap.org Git - chef.git/blob - cookbooks/otrs/recipes/default.rb
Preserve some configuration files over OTRS upgrades
[chef.git] / cookbooks / otrs / recipes / default.rb
1 #
2 # Cookbook Name:: otrs
3 # Recipe:: default
4 #
5 # Copyright 2012, 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 include_recipe "tools"
21 include_recipe "postgresql"
22 include_recipe "apache"
23
24 passwords = data_bag_item("otrs", "passwords")
25
26 package "libapache2-mod-perl2"
27 package "libapache2-reload-perl"
28
29 package "libgd-gd2-perl"
30 package "libgd-graph-perl"
31 package "libgd-text-perl"
32 package "libjson-xs-perl"
33 package "libmail-imapclient-perl"
34 package "libnet-ldap-perl"
35 package "libpdf-api2-perl"
36 package "libsoap-lite-perl"
37 package "libyaml-libyaml-perl"
38 package "libcrypt-eksblowfish-perl"
39
40 apache_module "headers"
41
42 version = node[:otrs][:version]
43 user = node[:otrs][:user]
44 database_cluster = node[:otrs][:database_cluster]
45 database_name = node[:otrs][:database_name]
46 database_user = node[:otrs][:database_user]
47 database_password = passwords[node[:otrs][:database_password]]
48 site = node[:otrs][:site]
49
50 old_installation = begin
51                      File.readlink("/opt/otrs")
52                    rescue StandardError
53                      nil
54                    end
55
56 postgresql_user database_user do
57   cluster database_cluster
58   password database_password
59 end
60
61 postgresql_database database_name do
62   cluster database_cluster
63   owner database_user
64 end
65
66 remote_file "#{Chef::Config[:file_cache_path]}/otrs-#{version}.tar.bz2" do
67   source "https://ftp.otrs.org/pub/otrs/otrs-#{version}.tar.bz2"
68   not_if { File.exist?("/opt/otrs-#{version}") }
69 end
70
71 execute "untar-otrs-#{version}" do
72   command "tar jxf #{Chef::Config[:file_cache_path]}/otrs-#{version}.tar.bz2"
73   cwd "/opt"
74   user "root"
75   group "root"
76   not_if { File.exist?("/opt/otrs-#{version}") }
77 end
78
79 config = edit_file "/opt/otrs-#{version}/Kernel/Config.pm.dist" do |line|
80   line.gsub!(/^( *)\$Self->{Database} = 'otrs'/, "\\1$Self->{Database} = '#{database_name}'")
81   line.gsub!(/^( *)\$Self->{DatabaseUser} = 'otrs'/, "\\1$Self->{DatabaseUser} = '#{database_user}'")
82   line.gsub!(/^( *)\$Self->{DatabasePw} = 'some-pass'/, "\\1$Self->{DatabasePw} = '#{database_password}'")
83   line.gsub!(/^( *)\$Self->{Database} = 'otrs'/, "\\1$Self->{Database} = '#{database_name}'")
84   line.gsub!(/^( *\$Self->{DatabaseDSN} = "DBI:mysql:)/, "#\\1")
85   line.gsub!(/^#( *\$Self->{DatabaseDSN} = "DBI:Pg:.*;host=)/, "\\1")
86   line.gsub!(/^( *)# (\$Self->{CheckMXRecord} = 0)/, "\\1\\2")
87
88   line
89 end
90
91 file "/opt/otrs-#{version}/Kernel/Config.pm" do
92   owner user
93   group "www-data"
94   mode 0o664
95   content config
96 end
97
98 generic_agent = edit_file "/opt/otrs-#{version}/Kernel/Config/GenericAgent.pm.dist" do |line|
99   line
100 end
101
102 file "/opt/otrs-#{version}/Kernel/Config/GenericAgent.pm" do
103   owner user
104   group "www-data"
105   mode 0o664
106   content generic_agent
107 end
108
109 link "/opt/otrs-#{version}/Kernel/Config/Files/ZZZAuto.pm" do
110   to "#{old_installation}/Kernel/Config/Files/ZZZAuto.pm"
111   link_type :hard
112 end
113
114 link "/opt/otrs-#{version}/var/log/TicketCounter.log" do
115   to "#{old_installation}/var/log/TicketCounter.log"
116   link_type :hard
117 end
118
119 link "/opt/otrs" do
120   to "/opt/otrs-#{version}"
121 end
122
123 execute "/opt/otrs/bin/otrs.SetPermissions.pl" do
124   action :run
125   command "/opt/otrs/bin/otrs.SetPermissions.pl --otrs-user=#{user} --web-user=www-data --otrs-group=www-data --web-group=www-data /opt/otrs-#{version}"
126   user "root"
127   group "root"
128   only_if { File.stat("/opt/otrs/README.md").uid != Etc.getpwnam("otrs").uid }
129 end
130
131 execute "/opt/otrs/bin/otrs.RebuildConfig.pl" do
132   action :run
133   command "/opt/otrs/bin/otrs.RebuildConfig.pl"
134   user "root"
135   group "root"
136   not_if { File.exist?("/opt/otrs/Kernel/Config/Files/ZZZAAuto.pm") }
137 end
138
139 execute "/opt/otrs/bin/Cron.sh" do
140   action :nothing
141   command "/opt/otrs/bin/Cron.sh restart"
142   user "otrs"
143   group "otrs"
144 end
145
146 Dir.glob("/opt/otrs/var/cron/*.dist") do |distname|
147   name = distname.sub(".dist", "")
148
149   file name do
150     owner "otrs"
151     group "www-data"
152     mode 0o664
153     content IO.read(distname)
154     notifies :run, "execute[/opt/otrs/bin/Cron.sh]"
155   end
156 end
157
158 ssl_certificate site do
159   domains site
160   notifies :reload, "service[apache2]"
161 end
162
163 apache_site site do
164   template "apache.erb"
165 end
166
167 template "/etc/sudoers.d/otrs" do
168   source "sudoers.erb"
169   owner "root"
170   group "root"
171   mode 0o440
172 end
173
174 template "/etc/cron.daily/otrs-backup" do
175   source "backup.cron.erb"
176   owner "root"
177   group "root"
178   mode 0o755
179 end