From c8de068333bf8518f339625b1751cc15cf714f9b Mon Sep 17 00:00:00 2001
From: Vincent Untz <vuntz@suse.com>
Date: Tue, 31 Mar 2015 12:12:00 +0200
Subject: [PATCH 1/5] ceph: Check if configuration file is empty before
 checking for existence

---
 chef/cookbooks/glance/recipes/ceph.rb | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/chef/cookbooks/glance/recipes/ceph.rb b/chef/cookbooks/glance/recipes/ceph.rb
index bd9902e..8918a30 100644
--- a/chef/cookbooks/glance/recipes/ceph.rb
+++ b/chef/cookbooks/glance/recipes/ceph.rb
@@ -25,10 +25,10 @@
 if ceph_servers.length > 0
   include_recipe "ceph::keyring"
 else
-  # If Ceph confifuration file is present,
-  # external Ceph cluster will be used,
+  # If Ceph confifuration file is present, external Ceph cluster will be used,
   # we have to install ceph client packages
-  return unless File.exists?(ceph_conf)
+  return if (ceph_conf.empty? || !File.exists?(ceph_conf))
+
   if node[:platform] == "suse"
     package "ceph-common"
   end

From 671acbc3a8153440dded84c861723a2b6323f178 Mon Sep 17 00:00:00 2001
From: Vincent Untz <vuntz@suse.com>
Date: Fri, 3 Apr 2015 10:22:19 +0200
Subject: [PATCH 2/5] ceph: Fix initial run of recipe

"ceph -s" cannot work on the first run:

 - in the external cluster case: ceph is not installed, so install it in
   the compile phase
 - in the crowbar cluster case: ceph.conf is missing. There's no way
   around it, so instead just assume that the ceph cluster will work
   fine (so move "ceph -s" to external cluster only). If it's not, then
   we have other issues as part of crowbar anyway.
---
 chef/cookbooks/glance/recipes/ceph.rb | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/chef/cookbooks/glance/recipes/ceph.rb b/chef/cookbooks/glance/recipes/ceph.rb
index 8918a30..a84161a 100644
--- a/chef/cookbooks/glance/recipes/ceph.rb
+++ b/chef/cookbooks/glance/recipes/ceph.rb
@@ -30,7 +30,10 @@
   return if (ceph_conf.empty? || !File.exists?(ceph_conf))
 
   if node[:platform] == "suse"
-    package "ceph-common"
+    # install package in compile phase because we will run "ceph -s"
+    package "ceph-common" do
+      action :nothing
+    end.run_action(:install)
   end
 
   if File.exists?(admin_keyring)
@@ -39,16 +42,16 @@
     Chef::Log.info("Using external ceph cluster for glance, with no automatic setup.")
     return
   end
-end
 
-# If ceph.conf and admin keyring will be available
-# we have to check ceph cluster status
-cmd = ["ceph", "-k", admin_keyring, "-c", ceph_conf, "-s"]
-check_ceph = Mixlib::ShellOut.new(cmd)
+  # If ceph.conf and admin keyring will be available
+  # we have to check ceph cluster status
+  cmd = ["ceph", "-k", admin_keyring, "-c", ceph_conf, "-s"]
+  check_ceph = Mixlib::ShellOut.new(cmd)
 
-unless check_ceph.run_command.stdout.match("(HEALTH_OK|HEALTH_WARN)")
-  Chef::Log.info("Ceph cluster is not healthy, skipping the ceph setup for glance")
-  return
+  unless check_ceph.run_command.stdout.match("(HEALTH_OK|HEALTH_WARN)")
+    Chef::Log.info("Ceph cluster is not healthy, skipping the ceph setup for glance")
+    return
+  end
 end
 
 ceph_user = node[:glance][:rbd][:store_user]

From 3359c5060360a869ac1b1ff51a9ad2acdd1057b4 Mon Sep 17 00:00:00 2001
From: Vincent Untz <vuntz@suse.com>
Date: Fri, 3 Apr 2015 10:26:38 +0200
Subject: [PATCH 3/5] ceph: Check if admin keyring is empty

Better to do that before checking for the file existence, as we'll want
to support this case.
---
 chef/cookbooks/glance/recipes/ceph.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/chef/cookbooks/glance/recipes/ceph.rb b/chef/cookbooks/glance/recipes/ceph.rb
index a84161a..878a159 100644
--- a/chef/cookbooks/glance/recipes/ceph.rb
+++ b/chef/cookbooks/glance/recipes/ceph.rb
@@ -36,7 +36,7 @@
     end.run_action(:install)
   end
 
-  if File.exists?(admin_keyring)
+  if !admin_keyring.empty? && File.exists?(admin_keyring)
     Chef::Log.info("Using external ceph cluster for glance, with automatic setup.")
   else
     Chef::Log.info("Using external ceph cluster for glance, with no automatic setup.")

From a80b4319bbfe4deb465df862c3a3a3574a5e99b8 Mon Sep 17 00:00:00 2001
From: Vincent Untz <vuntz@suse.com>
Date: Fri, 3 Apr 2015 10:30:44 +0200
Subject: [PATCH 4/5] ceph: Ensure right config file/keyring file are used in
 crowbar case

We do not want to use the attributes in that case, since what the user
might enter there doesn't matter.
---
 chef/cookbooks/glance/recipes/ceph.rb | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/chef/cookbooks/glance/recipes/ceph.rb b/chef/cookbooks/glance/recipes/ceph.rb
index 878a159..44d1735 100644
--- a/chef/cookbooks/glance/recipes/ceph.rb
+++ b/chef/cookbooks/glance/recipes/ceph.rb
@@ -17,14 +17,17 @@
 # limitations under the License.
 #
 
-ceph_conf = node[:glance][:rbd][:store_ceph_conf]
-admin_keyring = node[:glance][:rbd][:store_admin_keyring]
-
 ceph_env_filter = " AND ceph_config_environment:ceph-config-default"
 ceph_servers = search(:node, "roles:ceph-osd#{ceph_env_filter}") || []
 if ceph_servers.length > 0
   include_recipe "ceph::keyring"
+
+  ceph_conf = "/etc/ceph/ceph.conf"
+  admin_keyring = "/etc/ceph/ceph.client.admin.keyring"
 else
+  ceph_conf = node[:glance][:rbd][:store_ceph_conf]
+  admin_keyring = node[:glance][:rbd][:store_admin_keyring]
+
   # If Ceph confifuration file is present, external Ceph cluster will be used,
   # we have to install ceph client packages
   return if (ceph_conf.empty? || !File.exists?(ceph_conf))

From d84c3a6801bb95736d3f9ad294a9b6872f304434 Mon Sep 17 00:00:00 2001
From: Vincent Untz <vuntz@suse.com>
Date: Fri, 3 Apr 2015 11:04:52 +0200
Subject: [PATCH 5/5] ceph: Ensure that right ceph config file is used in
 glance-api.conf

---
 chef/cookbooks/glance/recipes/ceph.rb | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/chef/cookbooks/glance/recipes/ceph.rb b/chef/cookbooks/glance/recipes/ceph.rb
index 44d1735..815533f 100644
--- a/chef/cookbooks/glance/recipes/ceph.rb
+++ b/chef/cookbooks/glance/recipes/ceph.rb
@@ -24,6 +24,9 @@
 
   ceph_conf = "/etc/ceph/ceph.conf"
   admin_keyring = "/etc/ceph/ceph.client.admin.keyring"
+  # Ensure that the ceph config file that will be written in the glance config
+  # file is the right one
+  node.default[:glance][:rbd][:store_ceph_conf] = ceph_conf
 else
   ceph_conf = node[:glance][:rbd][:store_ceph_conf]
   admin_keyring = node[:glance][:rbd][:store_admin_keyring]
