Replace instance states hardcoded with constants
[ganeti-local] / htools / Ganeti / HTools / Text.hs
index 1f446ac..a52665a 100644 (file)
@@ -7,7 +7,7 @@ files, as produced by gnt-node and gnt-instance list.
 
 {-
 
-Copyright (C) 2009, 2010 Google Inc.
+Copyright (C) 2009, 2010, 2011 Google Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -86,9 +86,10 @@ serializeInstance nl inst =
                     then ""
                     else Container.nameOf nl sidx)
     in
-      printf "%s|%d|%d|%d|%s|%s|%s|%s"
+      printf "%s|%d|%d|%d|%s|%s|%s|%s|%s"
              iname (Instance.mem inst) (Instance.dsk inst)
              (Instance.vcpus inst) (Instance.runSt inst)
+             (if Instance.auto_balance inst then "Y" else "N")
              pnode snode (intercalate "," (Instance.tags inst))
 
 -- | Generate instance file data from instance objects
@@ -109,7 +110,7 @@ serializeCluster (ClusterData gl nl il ctags) =
 loadGroup :: (Monad m) => [String] -> m (String, Group.Group)
 loadGroup [name, gid, apol] = do
   xapol <- apolFromString apol
-  return $ (gid, Group.create name gid xapol)
+  return (gid, Group.create name gid xapol)
 
 loadGroup s = fail $ "Invalid/incomplete group data: '" ++ show s ++ "'"
 
@@ -134,17 +135,23 @@ loadNode _ s = fail $ "Invalid/incomplete node data: '" ++ show s ++ "'"
 -- | Load an instance from a field list.
 loadInst :: (Monad m) =>
             NameAssoc -> [String] -> m (String, Instance.Instance)
-loadInst ktn [name, mem, dsk, vcpus, status, pnode, snode, tags] = do
+loadInst ktn [name, mem, dsk, vcpus, status, auto_bal, pnode, snode, tags] = do
   pidx <- lookupNode ktn name pnode
   sidx <- (if null snode then return Node.noSecondary
            else lookupNode ktn name snode)
   vmem <- tryRead name mem
   vdsk <- tryRead name dsk
   vvcpus <- tryRead name vcpus
+  auto_balance <- case auto_bal of
+                    "Y" -> return True
+                    "N" -> return False
+                    _ -> fail $ "Invalid auto_balance value '" ++ auto_bal ++
+                         "' for instance " ++ name
   when (sidx == pidx) $ fail $ "Instance " ++ name ++
            " has same primary and secondary node - " ++ pnode
   let vtags = sepSplit ',' tags
-      newinst = Instance.create name vmem vdsk vvcpus status vtags pidx sidx
+      newinst = Instance.create name vmem vdsk vvcpus status vtags
+                auto_balance pidx sidx
   return (name, newinst)
 loadInst _ s = fail $ "Invalid/incomplete instance data: '" ++ show s ++ "'"
 
@@ -188,4 +195,4 @@ parseData fdata = do
 -- | Top level function for data loading
 loadData :: String -- ^ Path to the text file
          -> IO (Result ClusterData)
-loadData afile = readData afile >>= return . parseData
+loadData = fmap parseData . readData