From 00c06587499f5af427335d78681f01c7d3254769 Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Wed, 28 Jun 2023 00:21:39 +0100 Subject: [PATCH] wordpress: use wp-cli for initial DB install and activating plugins --- cookbooks/wordpress/resources/plugin.rb | 4 ++-- cookbooks/wordpress/resources/site.rb | 12 ++++++++++++ test/integration/blog/serverspec/wordpress_spec.rb | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/integration/blog/serverspec/wordpress_spec.rb diff --git a/cookbooks/wordpress/resources/plugin.rb b/cookbooks/wordpress/resources/plugin.rb index 03aadec28..fee9fcbca 100644 --- a/cookbooks/wordpress/resources/plugin.rb +++ b/cookbooks/wordpress/resources/plugin.rb @@ -67,7 +67,7 @@ action :create do command "/opt/wp-cli/wp --path=#{site_directory} plugin activate #{new_resource.plugin}" user node[:wordpress][:user] group node[:wordpress][:group] - only_if { ::File.exist?("#{site_directory}/wp-config.php") } + not_if "/opt/wp-cli/wp --path=#{site_directory} plugin is-active #{new_resource.plugin}" end end @@ -76,7 +76,7 @@ action :delete do command "/opt/wp-cli/wp --path=#{site_directory} plugin deactivate #{new_resource.plugin}" user node[:wordpress][:user] group node[:wordpress][:group] - only_if { ::File.exist?("#{site_directory}/wp-config.php") } + only_if "/opt/wp-cli/wp --path=#{site_directory} plugin is-active #{new_resource.plugin}" end directory plugin_directory do diff --git a/cookbooks/wordpress/resources/site.rb b/cookbooks/wordpress/resources/site.rb index 5a47a8e0c..cebd7b26f 100644 --- a/cookbooks/wordpress/resources/site.rb +++ b/cookbooks/wordpress/resources/site.rb @@ -24,6 +24,9 @@ default_action :create property :site, :kind_of => String, :name_property => true property :aliases, :kind_of => [String, Array] +property :title, :kind_of => String +property :admin_user, :kind_of => String, :default => "osm_admin" +property :admin_email, :kind_of => String, :default => "admins@openstreetmap.org" property :directory, :kind_of => String property :version, :kind_of => String property :database_name, :kind_of => String, :required => true @@ -139,6 +142,15 @@ action :create do backup false end + # Setup wordpress database and create admin user with random password + execute "wp core install" do + command "/opt/wp-cli/wp --path=#{site_directory} core install --url=#{new_resource.site} --title='#{new_resource.title}' --admin_user=#{new_resource.admin_user} --admin_email=#{new_resource.admin_email} --skip-email" + user node[:wordpress][:user] + group node[:wordpress][:group] + only_if { ::File.exist?("#{site_directory}/wp-config.php") } + not_if "/opt/wp-cli/wp --path=#{site_directory} core is-installed" + end + ssl_certificate new_resource.site do domains [new_resource.site] + Array(new_resource.aliases) end diff --git a/test/integration/blog/serverspec/wordpress_spec.rb b/test/integration/blog/serverspec/wordpress_spec.rb new file mode 100644 index 000000000..c4a8ca14d --- /dev/null +++ b/test/integration/blog/serverspec/wordpress_spec.rb @@ -0,0 +1,13 @@ + +require "serverspec" + +# Required by serverspec +set :backend, :exec + +describe command("/opt/wp-cli/wp --allow-root --path=/srv/blog.openstreetmap.org/wp/ core is-installed") do + its("exit_status") { should eq 0 } +end + +describe command("/opt/wp-cli/wp --allow-root --path=/srv/blog.openstreetmap.org/wp/ plugin is-active wp-fail2ban") do + its("exit_status") { should eq 0 } +end -- 2.39.5