-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdefault.rb
444 lines (387 loc) · 13.6 KB
/
default.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# encoding: UTF-8
#
# Cookbook Name:: owncloud
# Recipe:: default
# Author:: Xabier de Zuazo (<xabier@zuazo.org>)
# Copyright:: Copyright (c) 2015 Xabier de Zuazo
# Copyright:: Copyright (c) 2013-2015 Onddo Labs, SL.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
dbtype = node['owncloud']['config']['dbtype']
download_url =
node['owncloud']['download_url'] % { version: node['owncloud']['version'] }
# Sync apt package index
include_recipe 'apt' if platform_family?('debian')
#==============================================================================
# Initialize autogenerated passwords
#==============================================================================
::Chef::Recipe.send(:include, Opscode::OpenSSL::Password)
if Chef::Config[:solo]
if node['owncloud']['config']['dbpassword'].nil? &&
node['owncloud']['config']['dbtype'] != 'sqlite'
fail 'You must set ownCloud\'s database password in chef-solo mode.'
end
if node['owncloud']['admin']['pass'].nil?
fail 'You must set ownCloud\'s admin password in chef-solo mode.'
end
else
unless node['owncloud']['config']['dbtype'] == 'sqlite'
node.set_unless['owncloud']['config']['dbpassword'] = secure_password
node.set_unless['owncloud']['mysql']['server_root_password'] =
secure_password
end
node.set_unless['owncloud']['admin']['pass'] = secure_password
node.save
end
#==============================================================================
# Initialize encrypted attributes
#==============================================================================
Chef::Recipe.send(:include, Chef::EncryptedAttributesHelpers)
# Include the #secure_password method:
Chef::Recipe.send(:include, Opscode::OpenSSL::Password)
self.encrypted_attributes_enabled = node['owncloud']['encrypt_attributes']
admin_pass = encrypted_attribute_write(%w(owncloud admin pass)) do
secure_password
end
dbpass = encrypted_attribute_write(%w(owncloud config dbpassword)) do
secure_password
end
#==============================================================================
# Install PHP
#==============================================================================
# ownCloud requires PHP >= 5.4.0, so in older ubuntu versions we need to add an
# extra repository in order to provide it
apt_repository 'ondrej-php5-oldstable' do
uri 'http://ppa.launchpad.net/ondrej/php5-oldstable/ubuntu'
distribution node['lsb']['codename'] if node['lsb'].is_a?(Hash)
components %w(main)
keyserver 'keyserver.ubuntu.com'
key 'E5267A6C'
deb_src true
only_if do
node['platform'] == 'ubuntu' &&
Chef::VersionConstraint.new('<= 12.04').include?(node['platform_version'])
end
end
include_recipe 'php'
node['owncloud']['packages']['core'].each do |pkg|
package pkg do
action :install
end
end
if node['owncloud']['packages'].key?(dbtype)
node['owncloud']['packages'][dbtype].each do |pkg|
package pkg do
action :install
end
end
end
#==============================================================================
# Set up database
#==============================================================================
if node['owncloud']['manage_database'].nil?
node.default['owncloud']['manage_database'] =
%w(localhost 127.0.0.1).include?(node['owncloud']['config']['dbhost'])
end
case node['owncloud']['config']['dbtype']
when 'sqlite'
# With SQLite the table prefix must be oc_
node.default['owncloud']['config']['dbtableprefix'] = 'oc_'
when 'mysql'
if node['owncloud']['config']['dbport'].nil?
node.default['owncloud']['config']['dbport'] = '3306'
end
if node['owncloud']['manage_database']
# Install MySQL
if Chef::Config[:solo] &&
node['owncloud']['mysql']['server_root_password'].nil?
fail 'You must set the database admin password in chef-solo mode.'
end
def mysql_password(user)
key = "server_#{user}_password"
encrypted_attribute_write(['owncloud', 'mysql', key]) { secure_password }
end
root_password = mysql_password('root')
dbinstance = node['owncloud']['mysql']['instance']
mysql2_chef_gem dbinstance do
action :install
end
mysql_service dbinstance do
data_dir node['owncloud']['mysql']['data_dir']
initial_root_password root_password
bind_address node['owncloud']['config']['dbhost']
port node['owncloud']['config']['dbport'].to_s
run_group node['owncloud']['mysql']['run_group']
run_user node['owncloud']['mysql']['run_user']
version node['owncloud']['mysql']['version']
action [:create, :start]
end
mysql_connection_info = {
host: node['owncloud']['config']['dbhost'],
port: node['owncloud']['config']['dbport'],
username: 'root',
password: root_password
}
mysql_database node['owncloud']['config']['dbname'] do
connection mysql_connection_info
action :create
end
mysql_database_user node['owncloud']['config']['dbuser'] do
connection mysql_connection_info
database_name node['owncloud']['config']['dbname']
host node['owncloud']['config']['dbhost']
password dbpass
privileges [:all]
action :grant
end
end # if manage database
when 'pgsql'
if node['owncloud']['config']['dbport'].nil?
node.default['owncloud']['config']['dbport'] =
node['postgresql']['config']['port']
else
node.default['postgresql']['config']['port'] =
node['owncloud']['config']['dbport']
end
if node['owncloud']['manage_database']
# Install PostgreSQL
if node['postgresql']['password']['postgres'].nil? && Chef::Config[:solo]
fail 'You must set node["postgresql"]["password"]["postgres"] in '\
'chef-solo mode.'
elsif node['postgresql']['password']['postgres'].nil? &&
!Chef::Config[:solo]
node.set['postgresql']['password']['postgres'] = secure_password
node.save
end
# Fix issue: https://github.com/hw-cookbooks/postgresql/issues/249
if node['postgresql']['server']['packages'].is_a?(Array) &&
platform_family?('debian')
pgsql_last_package = node['postgresql']['server']['packages'].last
ruby_block 'Fix postgresql#249' do
block {}
subscribes :run, "package[#{pgsql_last_package}]", :immediately
notifies :run, 'execute[Set locale and Create cluster]', :immediately
action :nothing
end
end
include_recipe 'postgresql::server'
include_recipe 'database::postgresql'
postgresql_connection_info = {
host: node['owncloud']['config']['dbhost'],
port: node['owncloud']['config']['dbport'],
username: 'postgres',
password: node['postgresql']['password']['postgres']
}
postgresql_database node['owncloud']['config']['dbname'] do
connection postgresql_connection_info
action :create
end
postgresql_database_user node['owncloud']['config']['dbuser'] do
connection postgresql_connection_info
database_name node['owncloud']['config']['dbname']
host node['owncloud']['config']['dbhost']
password dbpass
privileges [:all]
action [:create, :grant]
end
end # if manage database
else
fail "Unsupported database type: #{node['owncloud']['config']['dbtype']}"
end
#==============================================================================
# Set up mail transfer agent
#==============================================================================
if node['owncloud']['config']['mail_smtpmode'].eql?('sendmail') &&
node['owncloud']['install_postfix']
include_recipe 'postfix::default'
# Fix Ubuntu 15.04 support:
if node['platform'] == 'ubuntu' && node['platform_version'].to_i >= 15
r = resources(service: 'postfix')
r.provider(Chef::Provider::Service::Debian)
end
end
#==============================================================================
# Download and extract ownCloud
#==============================================================================
directory node['owncloud']['www_dir']
if node['owncloud']['deploy_from_git'] != true
basename = ::File.basename(download_url)
local_file = ::File.join(Chef::Config[:file_cache_path], basename)
# Required on Docker:
package 'tar'
package 'bzip2'
# Prior to Chef 11.6, remote_file does not support conditional get
# so we do a HEAD http_request to mimic it
http_request 'HEAD owncloud' do
message ''
url download_url
if Gem::Version.new(Chef::VERSION) < Gem::Version.new('11.6.0')
action :head
else
action :nothing
end
if File.exist?(local_file)
headers 'If-Modified-Since' => File.mtime(local_file).httpdate
end
notifies :create, 'remote_file[download owncloud]', :immediately
end
remote_file 'download owncloud' do
source download_url
path local_file
if Gem::Version.new(Chef::VERSION) < Gem::Version.new('11.6.0')
action :nothing
else
action :create
end
notifies :run, 'bash[extract owncloud]', :immediately
end
bash 'extract owncloud' do
code <<-EOF
# remove previous installation if any
if [ -d ./owncloud ]
then
pushd ./owncloud >/dev/null
ls | grep -v 'data\\|config' | xargs rm -r
popd >/dev/null
fi
# extract tar file
tar xfj '#{local_file}' --no-same-owner
EOF
cwd node['owncloud']['www_dir']
action :nothing
end
else
if node['owncloud']['git_ref']
git_ref = node['owncloud']['git_ref']
elsif node['owncloud']['version'].eql?('latest')
git_ref = 'master'
else
git_ref = "v#{node['owncloud']['version']}"
end
git 'clone owncloud' do
destination node['owncloud']['dir']
repository node['owncloud']['git_repo']
reference git_ref
enable_submodules true
action :sync
end
end
#==============================================================================
# Set up webserver
#==============================================================================
# Get the webserver used
web_server = node['owncloud']['web_server']
# include the recipe for installing the webserver
case web_server
when 'apache'
include_recipe 'owncloud::_apache'
web_services = %w(apache2)
when 'nginx'
include_recipe 'owncloud::_nginx'
web_services = %w(nginx php-fpm)
else
fail "Web server not supported: #{web_server}"
end
#==============================================================================
# Initialize configuration file and install ownCloud
#==============================================================================
# create required directories
[
::File.join(node['owncloud']['dir'], 'apps'),
::File.join(node['owncloud']['dir'], 'config'),
node['owncloud']['data_dir']
].each do |dir|
directory dir do
if node['owncloud']['skip_permissions'] == false
owner node[web_server]['user']
group node[web_server]['group']
mode 00750
end
action :create
end
end
dbhost =
if node['owncloud']['config']['dbport'].nil?
node['owncloud']['config']['dbhost']
else
[
node['owncloud']['config']['dbhost'],
node['owncloud']['config']['dbport']
].join(':')
end
# create autoconfig.php for the installation
template 'owncloud autoconfig.php' do
path ::File.join(node['owncloud']['dir'], 'config', 'autoconfig.php')
source 'autoconfig.php.erb'
unless node['owncloud']['skip_permissions']
owner node[web_server]['user']
group node[web_server]['group']
mode 00640
end
variables(
dbtype: node['owncloud']['config']['dbtype'],
dbname: node['owncloud']['config']['dbname'],
dbuser: node['owncloud']['config']['dbuser'],
dbpass: dbpass,
dbhost: dbhost,
dbprefix: node['owncloud']['config']['dbtableprefix'],
admin_user: node['owncloud']['admin']['user'],
admin_pass: admin_pass,
data_dir: node['owncloud']['data_dir']
)
not_if do
::File.exist?(::File.join(node['owncloud']['dir'], 'config', 'config.php'))
end
web_services.each do |web_service|
notifies :restart, "service[#{web_service}]", :immediately
end
notifies :run, 'execute[run owncloud setup]', :immediately
end
# install ownCloud
execute 'run owncloud setup' do
cwd node['owncloud']['dir']
command(
"sudo -u '#{node[web_server]['user']}' php -f index.php "\
'| { ! grep -iA2 -e error -e failed -e "No database drivers"; }'
)
action :nothing
end
# Apply the configuration on attributes to config.php
ruby_block 'apply owncloud config' do
block do
self.class.send(:include, OwncloudCookbook::CookbookHelpers)
apply_owncloud_configuration
end
only_if do
::File.exist?(::File.join(node['owncloud']['dir'], 'config', 'config.php'))
end
end
#==============================================================================
# Enable cron for background jobs
#==============================================================================
include_recipe 'cron'
cron_command =
"php -f '#{node['owncloud']['dir']}/cron.php' "\
">> '#{node['owncloud']['data_dir']}/cron.log' 2>&1"
cron 'owncloud cron' do
user node[web_server]['user']
minute node['owncloud']['cron']['min']
hour node['owncloud']['cron']['hour']
day node['owncloud']['cron']['day']
month node['owncloud']['cron']['month']
weekday node['owncloud']['cron']['weekday']
action node['owncloud']['cron']['enabled'] ? :create : :delete
command cron_command
end