From c0623c010546305ad109b5f85e8ae8dc44626eb7 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Mon, 27 Apr 2015 17:24:42 +0100 Subject: [PATCH 01/22] First stab at putting together LWRP for installing plugins. --- providers/plugin.rb | 71 +++++++++++++++++++++++++++++++++++++++++++++ resources/plugin.rb | 11 +++++++ 2 files changed, 82 insertions(+) create mode 100644 providers/plugin.rb create mode 100644 resources/plugin.rb diff --git a/providers/plugin.rb b/providers/plugin.rb new file mode 100644 index 0000000..fd5ce48 --- /dev/null +++ b/providers/plugin.rb @@ -0,0 +1,71 @@ + +def whyrun_supported? + true +end + +action :install do + + if @current_resource.exists + Chef::Log.info "Plugin #{@new_resource} already exists - nothing to do." + else + converge_by("Add plugin #{@new_resource}") do + add_plugin + end + end +end + +def add_plugin + download_and_extract(@new_resource.url, @new_resource.plugin_name) + + # TODO: update/freshen the files in the plugins directory + bash "overwrite-plugin" do + cwd "#{node['wordpress']['dir']}/wp-content/plugins" + code "unzip xf #{Chef::Config[:file_cache_path]}/#{title}.#{file_extension}" + end +end + +def download_and_extract(url, title) + Chef::Log.info "Downloading #{title} from #{url}..." + # Work out the extension of the plugin archive + file_extension=url.split('.').last + + # Retrieve the file + remote_file "#{Chef::Config[:file_cache_path]}/#{title}.#{file_extension}" do + source url + end + + # Extract the archive - assuming zip file for now (most WP plugins ship this way) + bash "extract-plugin" do + cwd "#{Chef::Config[:file_cache_path]}/#{title}" + code "unzip xf ../#{title}.#{file_extension}" + end +end + +def load_current_resource + @current_resource = Chef::Resource::WordpressPlugin.new(@new_resource.name) + @current_resource.name(@new_resource.name) + @current_resource.plugin_name(@new_resource.plugin_name) + @current_resource.url(@new_resource.url) + + if plugin_exists?(@current_resource.url, @current_resource.plugin_name) + @current_resource.exists = true + end + + @current_resource +end + +def plugin_exists?(url, title) + + Chef::Log.info "Checking existance of #{title} from #{url}..." + + download_and_extract(url, title) + + plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{title}.#{file_extension} |grep " [^/]*/$" |awk '{print $4}'` + + if Dir.exist?(plugin_dir) + return true + end + + return false +end + diff --git a/resources/plugin.rb b/resources/plugin.rb new file mode 100644 index 0000000..509613c --- /dev/null +++ b/resources/plugin.rb @@ -0,0 +1,11 @@ + +actions :install +default_action :install + +attribute :plugin_name, :name_attribute => true, :kind_of => String, + :required => true + +attribute :url, :kind_of => String, :default => nil, :required => true + +attr_accessor :exists + From 60e04ae1f547da95728c37ac2c74843b1e60dab7 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Tue, 28 Apr 2015 10:58:44 +0100 Subject: [PATCH 02/22] Create attribute to hold plugin list. --- attributes/default.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/attributes/default.rb b/attributes/default.rb index 873753d..9feacb9 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -101,3 +101,6 @@ end default['wordpress']['php_options'] = { 'php_admin_value[upload_max_filesize]' => '50M', 'php_admin_value[post_max_size]' => '55M' } + +default['wordpress']['plugins'] = {} + From c2b0a59e8a74b7cf9e3a88f12e2aed460384d6e4 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Tue, 28 Apr 2015 10:59:08 +0100 Subject: [PATCH 03/22] Updated readme with new plugin. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1a99f24..f54293a 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ Attributes * `node['wordpress']['server_name']` - Hostname used for setting up the Virtual Host configuration for your WordPress site * `node['wordpress']['php_options']` - Additional PHP settings for the installation. +* `node['wordpress']['plugins']` - Hash of plugin names and URLs to be installed. Usage ===== From ee6a9f29453456a128030a7b87cd317c3730534a Mon Sep 17 00:00:00 2001 From: Peter Green Date: Tue, 28 Apr 2015 11:05:47 +0100 Subject: [PATCH 04/22] Added recipe to handle plugin install. --- recipes/plugins.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 recipes/plugins.rb diff --git a/recipes/plugins.rb b/recipes/plugins.rb new file mode 100644 index 0000000..d4e1df5 --- /dev/null +++ b/recipes/plugins.rb @@ -0,0 +1,27 @@ +# +# Cookbook Name:: wordpress +# Recipe:: plugins +# +# Copyright 2009-2010, Opscode, Inc. +# +# 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. +# + +node['wordpress']['plugins'].each do |plugin, url| + + wordpress_plugin plugin do + url url + end + +end + From 208d4fc33f43263b4537b62044220a73674bd018 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Tue, 28 Apr 2015 11:07:15 +0100 Subject: [PATCH 05/22] Inlude the plugin recipe after installing application. --- recipes/app.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/recipes/app.rb b/recipes/app.rb index b3d9f41..35a15bb 100644 --- a/recipes/app.rb +++ b/recipes/app.rb @@ -88,3 +88,6 @@ group node['wordpress']['install']['group'] action :create end + +include_recipe "wordpress::plugins" + From 43e643f2e3cb577cbf4f90a31afc16e85ebb23ac Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 12:33:54 +0100 Subject: [PATCH 06/22] Removed the code that checks for file extension - pretty much all WP plugins are zips. --- providers/plugin.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index fd5ce48..4eb6995 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -20,24 +20,22 @@ def add_plugin # TODO: update/freshen the files in the plugins directory bash "overwrite-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "unzip xf #{Chef::Config[:file_cache_path]}/#{title}.#{file_extension}" + code "unzip xf #{Chef::Config[:file_cache_path]}/#{title}.zip" end end def download_and_extract(url, title) Chef::Log.info "Downloading #{title} from #{url}..." - # Work out the extension of the plugin archive - file_extension=url.split('.').last # Retrieve the file - remote_file "#{Chef::Config[:file_cache_path]}/#{title}.#{file_extension}" do + remote_file "#{Chef::Config[:file_cache_path]}/#{title}.zip" do source url end # Extract the archive - assuming zip file for now (most WP plugins ship this way) bash "extract-plugin" do cwd "#{Chef::Config[:file_cache_path]}/#{title}" - code "unzip xf ../#{title}.#{file_extension}" + code "unzip xf ../#{title}.zip" end end @@ -60,7 +58,7 @@ def plugin_exists?(url, title) download_and_extract(url, title) - plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{title}.#{file_extension} |grep " [^/]*/$" |awk '{print $4}'` + plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{title}.zip |grep " [^/]*/$" |awk '{print $4}'` if Dir.exist?(plugin_dir) return true From 145ce4e5aa781d6a9f4d100814ac953bc3e66d03 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 12:38:28 +0100 Subject: [PATCH 07/22] Changing use of 'title' to 'name' for consistency. --- providers/plugin.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 4eb6995..06f2a4c 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -20,22 +20,22 @@ def add_plugin # TODO: update/freshen the files in the plugins directory bash "overwrite-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "unzip xf #{Chef::Config[:file_cache_path]}/#{title}.zip" + code "unzip xf #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" end end -def download_and_extract(url, title) - Chef::Log.info "Downloading #{title} from #{url}..." +def download_and_extract(url, name) + Chef::Log.info "Downloading #{name} from #{url}..." # Retrieve the file - remote_file "#{Chef::Config[:file_cache_path]}/#{title}.zip" do + remote_file "#{Chef::Config[:file_cache_path]}/#{name}.zip" do source url end # Extract the archive - assuming zip file for now (most WP plugins ship this way) bash "extract-plugin" do - cwd "#{Chef::Config[:file_cache_path]}/#{title}" - code "unzip xf ../#{title}.zip" + cwd "#{Chef::Config[:file_cache_path]}/#{name}" + code "unzip xf ../#{name}.zip" end end @@ -52,13 +52,13 @@ def load_current_resource @current_resource end -def plugin_exists?(url, title) +def plugin_exists?(url, name) - Chef::Log.info "Checking existance of #{title} from #{url}..." + Chef::Log.info "Checking existance of #{name} from #{url}..." - download_and_extract(url, title) + download_and_extract(url, name) - plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{title}.zip |grep " [^/]*/$" |awk '{print $4}'` + plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{name}.zip |grep " [^/]*/$" |awk '{print $4}'` if Dir.exist?(plugin_dir) return true From 7f3eec973beb4f2866e0b436510c6eccc0b2edf9 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 12:43:23 +0100 Subject: [PATCH 08/22] Ensure unzip is installed in order to be able to extract plugins. --- recipes/plugins.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/recipes/plugins.rb b/recipes/plugins.rb index d4e1df5..82f0edd 100644 --- a/recipes/plugins.rb +++ b/recipes/plugins.rb @@ -17,6 +17,8 @@ # limitations under the License. # +package "unzip" + node['wordpress']['plugins'].each do |plugin, url| wordpress_plugin plugin do From 066a1cc23670716611bb61b66cb80486914a06ca Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 13:04:34 +0100 Subject: [PATCH 09/22] Fixed the attempt to chdir into a non-existent directory. --- providers/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 06f2a4c..819b0d3 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -34,7 +34,7 @@ def download_and_extract(url, name) # Extract the archive - assuming zip file for now (most WP plugins ship this way) bash "extract-plugin" do - cwd "#{Chef::Config[:file_cache_path]}/#{name}" + cwd "#{Chef::Config[:file_cache_path]}" code "unzip xf ../#{name}.zip" end end From a22c1ef3d7ac04d624dc48e67afdc827798d1527 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 13:09:00 +0100 Subject: [PATCH 10/22] Adding all-important dash to unzip arguments. --- providers/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 819b0d3..8896bce 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -20,7 +20,7 @@ def add_plugin # TODO: update/freshen the files in the plugins directory bash "overwrite-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "unzip xf #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" + code "unzip -xf #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" end end From 0b0a2f8bae85cfb7dac6bce7a1ec00d938a7a505 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 13:11:59 +0100 Subject: [PATCH 11/22] Adding all-important dash to unzip arguments. --- providers/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 8896bce..c961ef8 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -35,7 +35,7 @@ def download_and_extract(url, name) # Extract the archive - assuming zip file for now (most WP plugins ship this way) bash "extract-plugin" do cwd "#{Chef::Config[:file_cache_path]}" - code "unzip xf ../#{name}.zip" + code "unzip -xf ../#{name}.zip" end end From 16f337215ebefd67f3d849da314e4839afea21cd Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 13:16:49 +0100 Subject: [PATCH 12/22] Correcting the path to zip file. --- providers/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index c961ef8..41a1b13 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -35,7 +35,7 @@ def download_and_extract(url, name) # Extract the archive - assuming zip file for now (most WP plugins ship this way) bash "extract-plugin" do cwd "#{Chef::Config[:file_cache_path]}" - code "unzip -xf ../#{name}.zip" + code "unzip -xf #{name}.zip" end end From e509e7b6417d60d350aa3b15384c9ef5b6b68e1b Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 15:43:39 +0100 Subject: [PATCH 13/22] Should be using 'unzip -u' instead of 'unzip -f'. --- providers/plugin.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 41a1b13..dcbde12 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -20,7 +20,7 @@ def add_plugin # TODO: update/freshen the files in the plugins directory bash "overwrite-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "unzip -xf #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" + code "unzip -u #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" end end @@ -35,7 +35,7 @@ def download_and_extract(url, name) # Extract the archive - assuming zip file for now (most WP plugins ship this way) bash "extract-plugin" do cwd "#{Chef::Config[:file_cache_path]}" - code "unzip -xf #{name}.zip" + code "unzip -u #{name}.zip" end end From 5c59a145352c0a361e56e2fc0ebfb83bbc9ba467 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 13 May 2015 16:46:11 +0100 Subject: [PATCH 14/22] Change ownership of plugin. --- providers/plugin.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index dcbde12..5d7e3cc 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -17,11 +17,21 @@ def whyrun_supported? def add_plugin download_and_extract(@new_resource.url, @new_resource.plugin_name) - # TODO: update/freshen the files in the plugins directory + plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip |grep " [^/]*/$" |awk '{print $4}'` + bash "overwrite-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" code "unzip -u #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" end + + bash "change-plugin-ownership" do + cwd "#{node['wordpress']['dir']}/wp-content/plugins" + code "chown -R #{node['wordpress']['install']['user']}:#{node['wordpress']['install']['group']} ./#{plugin_dir}" + not_if do + Etc.getpwuid(File.stat("#{node['wordpress']['dir']}/wp-content/plugins/#{plugin_dir}").uid) == node['wordpress']['install']['user'] && + Etc.getgrgid(File.stat("#{node['wordpress']['dir']}/wp-content/plugins/#{plugin_dir}").gid) == node['wordpress']['install']['group'] + end + end end def download_and_extract(url, name) From 6cf0feddc68831eefcf08e26fac2c43f3100911f Mon Sep 17 00:00:00 2001 From: Peter Green Date: Wed, 20 May 2015 13:27:09 +0100 Subject: [PATCH 15/22] Created a find_plugin_dir method and removed (failed) attempts at making changing ownership idempotent. --- providers/plugin.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 5d7e3cc..e1729dd 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -17,20 +17,17 @@ def whyrun_supported? def add_plugin download_and_extract(@new_resource.url, @new_resource.plugin_name) - plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip |grep " [^/]*/$" |awk '{print $4}'` + plugin_dir = find_plugin_dir bash "overwrite-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "unzip -u #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" + code "unzip -uo #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" end + #TODO: make this idempotent bash "change-plugin-ownership" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" code "chown -R #{node['wordpress']['install']['user']}:#{node['wordpress']['install']['group']} ./#{plugin_dir}" - not_if do - Etc.getpwuid(File.stat("#{node['wordpress']['dir']}/wp-content/plugins/#{plugin_dir}").uid) == node['wordpress']['install']['user'] && - Etc.getgrgid(File.stat("#{node['wordpress']['dir']}/wp-content/plugins/#{plugin_dir}").gid) == node['wordpress']['install']['group'] - end end end @@ -45,10 +42,16 @@ def download_and_extract(url, name) # Extract the archive - assuming zip file for now (most WP plugins ship this way) bash "extract-plugin" do cwd "#{Chef::Config[:file_cache_path]}" - code "unzip -u #{name}.zip" + code "unzip -o #{name}.zip" end end +def find_plugin_dir + find_dir = Mixlib::ShellOut.new("unzip -l #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip |grep ' [^/]*/$' |awk '{print $4}'") + find_dir.run_command + find_dir.stdout.chomp +end + def load_current_resource @current_resource = Chef::Resource::WordpressPlugin.new(@new_resource.name) @current_resource.name(@new_resource.name) @@ -68,7 +71,7 @@ def plugin_exists?(url, name) download_and_extract(url, name) - plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{name}.zip |grep " [^/]*/$" |awk '{print $4}'` + plugin_dir = find_plugin_dir if Dir.exist?(plugin_dir) return true From 4faf5ac45973567d50d3c75c060ceedb7a7de782 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Thu, 21 May 2015 11:37:22 +0100 Subject: [PATCH 16/22] Revert "Created a find_plugin_dir method and removed (failed) attempts at making changing ownership idempotent." This reverts commit 6cf0feddc68831eefcf08e26fac2c43f3100911f. --- providers/plugin.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index e1729dd..5d7e3cc 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -17,17 +17,20 @@ def whyrun_supported? def add_plugin download_and_extract(@new_resource.url, @new_resource.plugin_name) - plugin_dir = find_plugin_dir + plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip |grep " [^/]*/$" |awk '{print $4}'` bash "overwrite-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "unzip -uo #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" + code "unzip -u #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" end - #TODO: make this idempotent bash "change-plugin-ownership" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" code "chown -R #{node['wordpress']['install']['user']}:#{node['wordpress']['install']['group']} ./#{plugin_dir}" + not_if do + Etc.getpwuid(File.stat("#{node['wordpress']['dir']}/wp-content/plugins/#{plugin_dir}").uid) == node['wordpress']['install']['user'] && + Etc.getgrgid(File.stat("#{node['wordpress']['dir']}/wp-content/plugins/#{plugin_dir}").gid) == node['wordpress']['install']['group'] + end end end @@ -42,16 +45,10 @@ def download_and_extract(url, name) # Extract the archive - assuming zip file for now (most WP plugins ship this way) bash "extract-plugin" do cwd "#{Chef::Config[:file_cache_path]}" - code "unzip -o #{name}.zip" + code "unzip -u #{name}.zip" end end -def find_plugin_dir - find_dir = Mixlib::ShellOut.new("unzip -l #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip |grep ' [^/]*/$' |awk '{print $4}'") - find_dir.run_command - find_dir.stdout.chomp -end - def load_current_resource @current_resource = Chef::Resource::WordpressPlugin.new(@new_resource.name) @current_resource.name(@new_resource.name) @@ -71,7 +68,7 @@ def plugin_exists?(url, name) download_and_extract(url, name) - plugin_dir = find_plugin_dir + plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{name}.zip |grep " [^/]*/$" |awk '{print $4}'` if Dir.exist?(plugin_dir) return true From 4ffeb3742d130f7f44ec2bc0fe22b4118402a297 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Thu, 21 May 2015 11:37:36 +0100 Subject: [PATCH 17/22] Revert "Change ownership of plugin." This reverts commit 5c59a145352c0a361e56e2fc0ebfb83bbc9ba467. --- providers/plugin.rb | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 5d7e3cc..dcbde12 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -17,21 +17,11 @@ def whyrun_supported? def add_plugin download_and_extract(@new_resource.url, @new_resource.plugin_name) - plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip |grep " [^/]*/$" |awk '{print $4}'` - + # TODO: update/freshen the files in the plugins directory bash "overwrite-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" code "unzip -u #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" end - - bash "change-plugin-ownership" do - cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "chown -R #{node['wordpress']['install']['user']}:#{node['wordpress']['install']['group']} ./#{plugin_dir}" - not_if do - Etc.getpwuid(File.stat("#{node['wordpress']['dir']}/wp-content/plugins/#{plugin_dir}").uid) == node['wordpress']['install']['user'] && - Etc.getgrgid(File.stat("#{node['wordpress']['dir']}/wp-content/plugins/#{plugin_dir}").gid) == node['wordpress']['install']['group'] - end - end end def download_and_extract(url, name) From 84df6f11ca9dcd6f663ba39b21596e04a84deb17 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Thu, 21 May 2015 11:51:02 +0100 Subject: [PATCH 18/22] Trying to install plugin in a specific directory (based on name), using a bash trick gleaned from SuperUser.com to mimic tars --strip-component functionality. --- providers/plugin.rb | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index dcbde12..3b278bc 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -15,27 +15,15 @@ def whyrun_supported? end def add_plugin - download_and_extract(@new_resource.url, @new_resource.plugin_name) - - # TODO: update/freshen the files in the plugins directory - bash "overwrite-plugin" do - cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "unzip -u #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" - end -end - -def download_and_extract(url, name) - Chef::Log.info "Downloading #{name} from #{url}..." - # Retrieve the file remote_file "#{Chef::Config[:file_cache_path]}/#{name}.zip" do source url end - - # Extract the archive - assuming zip file for now (most WP plugins ship this way) - bash "extract-plugin" do - cwd "#{Chef::Config[:file_cache_path]}" - code "unzip -u #{name}.zip" + + # TODO: update/freshen the files in the plugins directory + bash "install-plugin" do + cwd "#{node['wordpress']['dir']}/wp-content/plugins" + code "temp=(mktemp -d) && unzip -d $temp #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip && mkdir #{name} && mv $temp/*/* #{name} && rmdir $temp/* $temp" end end @@ -52,15 +40,9 @@ def load_current_resource @current_resource end -def plugin_exists?(url, name) - - Chef::Log.info "Checking existance of #{name} from #{url}..." - - download_and_extract(url, name) - - plugin_dir = `unzip -l #{Chef::Config[:file_cache_path]}/#{name}.zip |grep " [^/]*/$" |awk '{print $4}'` +def plugin_exists?(name) - if Dir.exist?(plugin_dir) + if Dir.exist?("#{node['wordpress']['dir']}/wp-content/plugins/#{name}") return true end From 2a9736c8ba6d0542d02ee820aa0461c2fe4bf028 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Thu, 21 May 2015 12:03:09 +0100 Subject: [PATCH 19/22] Correcting call to plugin_exists function. --- providers/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 3b278bc..430fb4c 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -33,7 +33,7 @@ def load_current_resource @current_resource.plugin_name(@new_resource.plugin_name) @current_resource.url(@new_resource.url) - if plugin_exists?(@current_resource.url, @current_resource.plugin_name) + if plugin_exists?(@current_resource.plugin_name) @current_resource.exists = true end From ace704c59b586d4ecaef565e68cbea255a599c0e Mon Sep 17 00:00:00 2001 From: Peter Green Date: Thu, 21 May 2015 12:05:47 +0100 Subject: [PATCH 20/22] Correcting attempt to use name variable when downloading file. --- providers/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 430fb4c..0a8d4d9 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -16,7 +16,7 @@ def whyrun_supported? def add_plugin # Retrieve the file - remote_file "#{Chef::Config[:file_cache_path]}/#{name}.zip" do + remote_file "#{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" do source url end From 68b08fca8f8739cd9da095dccb26fb8fd2c9a44b Mon Sep 17 00:00:00 2001 From: Peter Green Date: Thu, 21 May 2015 12:09:13 +0100 Subject: [PATCH 21/22] Fixing incorrect use of local variable. --- providers/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 0a8d4d9..7e1bbfa 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -17,7 +17,7 @@ def whyrun_supported? def add_plugin # Retrieve the file remote_file "#{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip" do - source url + source new_resource.url end # TODO: update/freshen the files in the plugins directory From 4524fa8992b1b89738e93b9ea12129f64d5e1540 Mon Sep 17 00:00:00 2001 From: Peter Green Date: Thu, 21 May 2015 12:17:12 +0100 Subject: [PATCH 22/22] Use correct variables for naming plugin directory. --- providers/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/plugin.rb b/providers/plugin.rb index 7e1bbfa..d53b8ee 100644 --- a/providers/plugin.rb +++ b/providers/plugin.rb @@ -23,7 +23,7 @@ def add_plugin # TODO: update/freshen the files in the plugins directory bash "install-plugin" do cwd "#{node['wordpress']['dir']}/wp-content/plugins" - code "temp=(mktemp -d) && unzip -d $temp #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip && mkdir #{name} && mv $temp/*/* #{name} && rmdir $temp/* $temp" + code "temp=(mktemp -d) && unzip -d $temp #{Chef::Config[:file_cache_path]}/#{new_resource.plugin_name}.zip && mkdir #{new_resource.plugin_name} && mv $temp/*/* #{new_resource.plugin_name} && rmdir $temp/* $temp" end end