Implement readJSON functions for DC Types
[ganeti-local] / src / Ganeti / DataCollectors / CLI.hs
1 {-| Implementation of DataCollectors CLI functions.
2
3 This module holds the common command-line related functions for the
4 collector binaries.
5
6 -}
7
8 {-
9
10 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 02110-1301, USA.
26
27 -}
28
29 module Ganeti.DataCollectors.CLI
30   ( Options(..)
31   , OptType
32   , defaultOptions
33   -- * The options
34   , oShowHelp
35   , oShowVer
36   , oShowComp
37   , oDrbdPairing
38   , oDrbdStatus
39   , oNode
40   , oConfdAddr
41   , oConfdPort
42   , oInputFile
43   , oInstances
44   , genericOptions
45   ) where
46
47 import System.Console.GetOpt
48
49 import Ganeti.BasicTypes
50 import Ganeti.Common as Common
51 import Ganeti.Utils
52
53
54 -- * Data types
55
56 -- | Command line options structure.
57 data Options = Options
58   { optShowHelp    :: Bool           -- ^ Just show the help
59   , optShowComp    :: Bool           -- ^ Just show the completion info
60   , optShowVer     :: Bool           -- ^ Just show the program version
61   , optDrbdStatus  :: Maybe FilePath -- ^ Path to the file containing DRBD
62                                      -- status information
63   , optDrbdPairing :: Maybe FilePath -- ^ Path to the file containing pairings
64                                      -- between instances and DRBD minors
65   , optNode        :: Maybe String   -- ^ Info are requested for this node
66   , optConfdAddr   :: Maybe String   -- ^ IP address of the Confd server
67   , optConfdPort   :: Maybe Int      -- ^ The port of the Confd server to
68                                      -- connect to
69   , optInputFile   :: Maybe FilePath -- ^ Path to the file containing the
70                                      -- information to be parsed
71   , optInstances   :: Maybe FilePath -- ^ Path to the file contained a
72                                      -- serialized list of instances as in:
73                                      -- ([Primary], [Secondary])
74   } deriving Show
75
76 -- | Default values for the command line options.
77 defaultOptions :: Options
78 defaultOptions  = Options
79   { optShowHelp    = False
80   , optShowComp    = False
81   , optShowVer     = False
82   , optDrbdStatus  = Nothing
83   , optDrbdPairing = Nothing
84   , optNode        = Nothing
85   , optConfdAddr   = Nothing
86   , optConfdPort   = Nothing
87   , optInputFile   = Nothing
88   , optInstances   = Nothing
89   }
90
91 -- | Abbreviation for the option type.
92 type OptType = GenericOptType Options
93
94 instance StandardOptions Options where
95   helpRequested = optShowHelp
96   verRequested  = optShowVer
97   compRequested = optShowComp
98   requestHelp o = o { optShowHelp = True }
99   requestVer  o = o { optShowVer  = True }
100   requestComp o = o { optShowComp = True }
101
102 -- * Command line options
103 oDrbdPairing :: OptType
104 oDrbdPairing =
105   ( Option "p" ["drbd-pairing"]
106       (ReqArg (\ f o -> Ok o { optDrbdPairing = Just f}) "FILE")
107       "the FILE containing pairings between instances and DRBD minors",
108     OptComplFile)
109
110 oDrbdStatus :: OptType
111 oDrbdStatus =
112   ( Option "s" ["drbd-status"]
113       (ReqArg (\ f o -> Ok o { optDrbdStatus = Just f }) "FILE")
114       "the DRBD status FILE",
115     OptComplFile)
116
117 oNode :: OptType
118 oNode =
119   ( Option "n" ["node"]
120       (ReqArg (\ n o -> Ok o { optNode = Just n }) "NODE")
121       "the FQDN of the NODE about which information is requested",
122     OptComplFile)
123
124 oConfdAddr :: OptType
125 oConfdAddr =
126   ( Option "a" ["address"]
127       (ReqArg (\ a o -> Ok o { optConfdAddr = Just a }) "IP_ADDR")
128       "the IP address of the Confd server to connect to",
129     OptComplFile)
130
131 oConfdPort :: OptType
132 oConfdPort =
133   (Option "p" ["port"]
134     (reqWithConversion (tryRead "reading port")
135       (\port opts -> Ok opts { optConfdPort = Just port }) "PORT")
136     "Network port of the Confd server to connect to",
137     OptComplInteger)
138
139 oInputFile :: OptType
140 oInputFile =
141   ( Option "f" ["file"]
142       (ReqArg (\ f o -> Ok o { optInputFile = Just f }) "FILE")
143       "the input FILE",
144     OptComplFile)
145
146 oInstances :: OptType
147 oInstances =
148   ( Option "i" ["instances"]
149       (ReqArg (\ f o -> Ok o { optInstances = Just f}) "FILE")
150       "the FILE containing serialized instances",
151     OptComplFile)
152
153 -- | Generic options.
154 genericOptions :: [GenericOptType Options]
155 genericOptions =  [ oShowVer
156                   , oShowHelp
157                   , oShowComp
158                   ]