]> git.openstreetmap.org Git - chef.git/commitdiff
wordpress: use wp-cli for initial DB install and activating plugins
authorGrant Slater <github@firefishy.com>
Tue, 27 Jun 2023 23:21:39 +0000 (00:21 +0100)
committerGrant <github@firefishy.com>
Wed, 28 Jun 2023 13:28:45 +0000 (14:28 +0100)
cookbooks/wordpress/resources/plugin.rb
cookbooks/wordpress/resources/site.rb
test/integration/blog/serverspec/wordpress_spec.rb [new file with mode: 0644]

index 03aadec28d40a2ecc587b3f0889fb2fcc5437686..fee9fcbca940e8b9c579d36b46a73a9d04b98410 100644 (file)
@@ -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
index 5a47a8e0ca6976c722be2aa74ea6a760be87e255..cebd7b26f2894ccc315b21fb8bc0f510b3ba108a 100644 (file)
@@ -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 (file)
index 0000000..c4a8ca1
--- /dev/null
@@ -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