Add a --dry-run option to harep, so that users can verify
that the actions taken by harep are the ones they want.

Signed-off-by: Klaus Aehlig <[email protected]>
---
 src/Ganeti/HTools/Program/Harep.hs | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/Ganeti/HTools/Program/Harep.hs 
b/src/Ganeti/HTools/Program/Harep.hs
index 54628b0..8ad7deb 100644
--- a/src/Ganeti/HTools/Program/Harep.hs
+++ b/src/Ganeti/HTools/Program/Harep.hs
@@ -48,6 +48,7 @@ import Data.Maybe
 import Data.Ord
 import System.Time
 import qualified Data.Map as Map
+import qualified Text.JSON as J
 
 import Ganeti.BasicTypes
 import Ganeti.Common
@@ -84,6 +85,7 @@ options = do
     [ luxi
     , oJobDelay
     , oReason
+    , oDryRun
     ]
 
 arguments :: [ArgCompletion]
@@ -288,7 +290,7 @@ commitChange opts client instData = do
   let iname = Instance.name $ arInstance instData
       arData = getArData $ arState instData
       rmTags = tagsToRemove instData
-      execJobsWaitOk' opcodes = do
+      execJobsWaitOk' opcodes = unless (optDryRun opts) $ do
         res <- execJobsWaitOk
                  [map (annotateOpCode (optReason opts) now) opcodes] client
         case res of
@@ -401,6 +403,17 @@ detectBroken nl inst =
      _ -> Nothing  -- Other cases are unimplemented for now: DTDiskless,
                    -- DTFile, DTSharedFile, DTBlock, DTRbd, DTExt.
 
+-- | Submit jobs, unless a dry-run is requested; in this case, just report
+-- the job that would be submitted.
+submitJobs' :: Options -> [[MetaOpCode]] -> L.Client -> IO (Result [JobId])
+submitJobs' opts jobs client =
+  if optDryRun opts
+    then do
+      putStrLn . (++) "jobs: " . J.encode $ map (map metaOpCode) jobs
+      return $ Ok []
+    else
+      submitJobs jobs client
+
 -- | Perform the suggested repair on an instance if its policy allows it.
 doRepair :: Options
          -> L.Client     -- ^ The Luxi client
@@ -467,8 +480,10 @@ doRepair opts client delay instData (rtype, opcodes) =
 
         uuid <- newUUID
         time <- getClockTime
-        jids <- submitJobs [map (annotateOpCode (optReason opts) now) opcodes']
-                           client
+        jids <- submitJobs'
+                  opts
+                  [map (annotateOpCode (optReason opts) now) opcodes']
+                  client
 
         case jids of
           Bad e    -> exitErr e
-- 
2.6.0.rc2.230.g3dd15c0

Reply via email to