commit 9fdd3d0fe3bd48cc9354a0d9697362b0bd5f036e
Author: Iustin Pop <iustin@google.com>
Date:   Thu Dec 29 17:39:32 2011 +0100

    Switch hspace to read the disk template from the cluster
    
    The default disk template is the first enabled template.
    
    Signed-off-by: Iustin Pop <iustin@google.com>
    Reviewed-by: René Nussbaumer <rn@google.com>

diff --git a/htools/Ganeti/HTools/CLI.hs b/htools/Ganeti/HTools/CLI.hs
index 4e801fd..8fb813f 100644
--- a/htools/Ganeti/HTools/CLI.hs
+++ b/htools/Ganeti/HTools/CLI.hs
@@ -107,7 +107,7 @@ data Options = Options
   { optDataFile    :: Maybe FilePath -- ^ Path to the cluster data file
   , optDiskMoves   :: Bool           -- ^ Allow disk moves
   , optInstMoves   :: Bool           -- ^ Allow instance moves
-  , optDiskTemplate :: DiskTemplate  -- ^ The requested disk template
+  , optDiskTemplate :: Maybe DiskTemplate  -- ^ Override for the disk template
   , optDynuFile    :: Maybe FilePath -- ^ Optional file with dynamic use data
   , optEvacMode    :: Bool           -- ^ Enable evacuation mode
   , optExInst      :: [String]       -- ^ Instances to be excluded
@@ -146,7 +146,7 @@ defaultOptions  = Options
   { optDataFile    = Nothing
   , optDiskMoves   = True
   , optInstMoves   = True
-  , optDiskTemplate = DTDrbd8
+  , optDiskTemplate = Nothing
   , optDynuFile    = Nothing
   , optEvacMode    = False
   , optExInst      = []
@@ -214,8 +214,8 @@ oDiskTemplate :: OptType
 oDiskTemplate = Option "" ["disk-template"]
                 (ReqArg (\ t opts -> do
                            dt <- diskTemplateFromRaw t
-                           return $ opts { optDiskTemplate = dt }) "TEMPLATE")
-                "select the desired disk template"
+                           return $ opts { optDiskTemplate = Just dt })
+                 "TEMPLATE") "select the desired disk template"
 
 oSelInst :: OptType
 oSelInst = Option "" ["select-instances"]
diff --git a/htools/Ganeti/HTools/Program/Hspace.hs b/htools/Ganeti/HTools/Program/Hspace.hs
index f1f281b..b9df468 100644
--- a/htools/Ganeti/HTools/Program/Hspace.hs
+++ b/htools/Ganeti/HTools/Program/Hspace.hs
@@ -358,10 +358,11 @@ runAllocation :: ClusterData                -- ^ Cluster data
               -> Maybe Cluster.AllocResult  -- ^ Optional stop-allocation
               -> Result Cluster.AllocResult -- ^ Allocation result
               -> RSpec                      -- ^ Requested instance spec
+              -> DiskTemplate               -- ^ Requested disk template
               -> SpecType                   -- ^ Allocation type
               -> Options                    -- ^ CLI options
               -> IO (FailStats, Node.List, Int, [(RSpec, Int)])
-runAllocation cdata stop_allocation actual_result spec mode opts = do
+runAllocation cdata stop_allocation actual_result spec dt mode opts = do
   (reasons, new_nl, new_il, new_ixes, _) <-
       case stop_allocation of
         Just result_noalloc -> return result_noalloc
@@ -371,7 +372,7 @@ runAllocation cdata stop_allocation actual_result spec mode opts = do
       descr = name ++ " allocation"
       ldescr = "after " ++ map toLower descr
 
-  printISpec (optMachineReadable opts) spec mode (optDiskTemplate opts)
+  printISpec (optMachineReadable opts) spec mode dt
 
   printAllocationMap (optVerbose opts) descr new_nl new_ixes
 
@@ -399,16 +400,24 @@ main = do
          exitWith $ ExitFailure 1
 
   let verbose = optVerbose opts
-      disk_template = optDiskTemplate opts
-      req_nodes = Instance.requiredNodes disk_template
       machine_r = optMachineReadable opts
 
   orig_cdata@(ClusterData gl fixed_nl il _ ipol) <- loadExternalData opts
   nl <- setNodeStatus opts fixed_nl
 
+  cluster_disk_template <-
+    case iPolicyDiskTemplates ipol of
+      first_templ:_ -> return first_templ
+      _ -> do
+         _ <- hPutStrLn stderr $ "Error: null list of disk templates\
+                               \ received from cluster!"
+         exitWith $ ExitFailure 1
+
   let num_instances = Container.size il
       all_nodes = Container.elems fixed_nl
       cdata = orig_cdata { cdNodes = fixed_nl }
+      disk_template = fromMaybe cluster_disk_template (optDiskTemplate opts)
+      req_nodes = Instance.requiredNodes disk_template
       csf = commonSuffix fixed_nl il
 
   when (not (null csf) && verbose > 1) $
@@ -440,7 +449,7 @@ main = do
     runAllocation cdata stop_allocation
        (Cluster.tieredAlloc nl il alloclimit
         (instFromSpec tspec disk_template) allocnodes [] [])
-       tspec SpecTiered opts
+       tspec disk_template SpecTiered opts
 
   printTiered machine_r spec_map (optMcpu opts) nl trl_nl treason
 
@@ -453,7 +462,7 @@ main = do
       runAllocation cdata stop_allocation
             (Cluster.iterateAlloc nl il alloclimit
              (instFromSpec ispec disk_template) allocnodes [] [])
-            ispec SpecNormal opts
+            ispec disk_template SpecNormal opts
 
   printResults machine_r nl fin_nl num_instances allocs sreason
 
diff --git a/man/hspace.rst b/man/hspace.rst
index a487834..1e7def8 100644
--- a/man/hspace.rst
+++ b/man/hspace.rst
@@ -203,8 +203,9 @@ OPTIONS
 The options that can be passed to the program are as follows:
 
 --disk-template *template*
-  The disk template for the instance; one of the Ganeti disk templates
-  (e.g. plain, drbd, so on) should be passed in.
+  Overrides the disk template for the instance read from the cluster;
+  one of the Ganeti disk templates (e.g. plain, drbd, so on) should be
+  passed in.
 
 --max-cpu=*cpu-ratio*
   The maximum virtual to physical cpu ratio, as a floating point number
