]> git.openstreetmap.org Git - chef.git/blob - cookbooks/wordpress/resources/plugin.rb
imagery: restart titiler service to workaround resource leak
[chef.git] / cookbooks / wordpress / resources / plugin.rb
1 #
2 # Cookbook:: wordpress
3 # Resource:: wordpress_plugin
4 #
5 # Copyright:: 2015, 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 unified_mode true
21
22 default_action :create
23
24 property :plugin, :kind_of => String, :name_property => true
25 property :site, :kind_of => String, :required => true
26 property :source, :kind_of => String
27 property :version, :kind_of => String
28 property :repository, :kind_of => String
29 property :revision, :kind_of => String
30 property :reload_apache, :kind_of => [TrueClass, FalseClass], :default => true
31
32 action :create do
33   if new_resource.source
34     remote_directory plugin_directory do
35       cookbook "wordpress"
36       source new_resource.source
37       owner node[:wordpress][:user]
38       group node[:wordpress][:group]
39       mode "755"
40       files_owner node[:wordpress][:user]
41       files_group node[:wordpress][:group]
42       files_mode "755"
43     end
44   else
45     plugin_repository = new_resource.repository || default_repository
46
47     if plugin_repository.end_with?(".git")
48       git plugin_directory do
49         action :sync
50         repository plugin_repository
51         revision new_resource.revision
52         user node[:wordpress][:user]
53         group node[:wordpress][:group]
54       end
55     else
56       subversion plugin_directory do
57         action :sync
58         repository plugin_repository
59         user node[:wordpress][:user]
60         group node[:wordpress][:group]
61         ignore_failure plugin_repository.start_with?("https://plugins.svn.wordpress.org/")
62       end
63     end
64   end
65
66   execute "wp-cli plugin activate #{new_resource.plugin}" do
67     command "/opt/wp-cli/wp --path='#{site_directory}' plugin activate '#{new_resource.plugin}'"
68     user "www-data"
69     group "www-data"
70     not_if "/opt/wp-cli/wp --path='#{site_directory}' plugin is-active '#{new_resource.plugin}'"
71     ignore_failure plugin_repository.start_with?("https://plugins.svn.wordpress.org/")
72   end
73 end
74
75 action :delete do
76   execute "wp-cli plugin deactivate #{new_resource.plugin}" do
77     command "/opt/wp-cli/wp --path='#{site_directory}' plugin deactivate '#{new_resource.plugin}'"
78     user "www-data"
79     group "www-data"
80     only_if "/opt/wp-cli/wp --path='#{site_directory}' plugin is-active '#{new_resource.plugin}'"
81   end
82
83   directory plugin_directory do
84     action :delete
85     recursive true
86   end
87 end
88
89 action_class do
90   def site_directory
91     node[:wordpress][:sites][new_resource.site][:directory]
92   end
93
94   def plugin_directory
95     "#{site_directory}/wp-content/plugins/#{new_resource.plugin}"
96   end
97
98   def default_repository
99     version = new_resource.version ||
100               Chef::Wordpress.current_plugin_version(new_resource.plugin)
101
102     if version =~ /trunk/
103       "https://plugins.svn.wordpress.org/#{new_resource.plugin}/trunk"
104     else
105       "https://plugins.svn.wordpress.org/#{new_resource.plugin}/tags/#{version}"
106     end
107   end
108 end
109
110 def after_created
111   notifies :reload, "service[apache2]" if reload_apache
112 end