root / src / Ganeti / Ssconf.hs @ 6ab6b19a
History | View | Annotate | Download (4.8 kB)
1 | aa3adf35 | Iustin Pop | {-# LANGUAGE TemplateHaskell #-} |
---|---|---|---|
2 | aa3adf35 | Iustin Pop | |
3 | aa3adf35 | Iustin Pop | {-| Implementation of the Ganeti Ssconf interface. |
4 | aa3adf35 | Iustin Pop | |
5 | aa3adf35 | Iustin Pop | -} |
6 | aa3adf35 | Iustin Pop | |
7 | aa3adf35 | Iustin Pop | {- |
8 | aa3adf35 | Iustin Pop | |
9 | aa3adf35 | Iustin Pop | Copyright (C) 2012 Google Inc. |
10 | aa3adf35 | Iustin Pop | |
11 | aa3adf35 | Iustin Pop | This program is free software; you can redistribute it and/or modify |
12 | aa3adf35 | Iustin Pop | it under the terms of the GNU General Public License as published by |
13 | aa3adf35 | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
14 | aa3adf35 | Iustin Pop | (at your option) any later version. |
15 | aa3adf35 | Iustin Pop | |
16 | aa3adf35 | Iustin Pop | This program is distributed in the hope that it will be useful, but |
17 | aa3adf35 | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | aa3adf35 | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | aa3adf35 | Iustin Pop | General Public License for more details. |
20 | aa3adf35 | Iustin Pop | |
21 | aa3adf35 | Iustin Pop | You should have received a copy of the GNU General Public License |
22 | aa3adf35 | Iustin Pop | along with this program; if not, write to the Free Software |
23 | aa3adf35 | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
24 | aa3adf35 | Iustin Pop | 02110-1301, USA. |
25 | aa3adf35 | Iustin Pop | |
26 | aa3adf35 | Iustin Pop | -} |
27 | aa3adf35 | Iustin Pop | |
28 | aa3adf35 | Iustin Pop | module Ganeti.Ssconf |
29 | aa3adf35 | Iustin Pop | ( SSKey(..) |
30 | aa3adf35 | Iustin Pop | , sSKeyToRaw |
31 | aa3adf35 | Iustin Pop | , sSKeyFromRaw |
32 | aa3adf35 | Iustin Pop | , getPrimaryIPFamily |
33 | d8e9131b | Michele Tartara | , getMasterCandidatesIps |
34 | c5b4a186 | Iustin Pop | , keyToFilename |
35 | c5b4a186 | Iustin Pop | , sSFilePrefix |
36 | aa3adf35 | Iustin Pop | ) where |
37 | aa3adf35 | Iustin Pop | |
38 | aa3adf35 | Iustin Pop | import Ganeti.THH |
39 | aa3adf35 | Iustin Pop | |
40 | 79ac58fa | Iustin Pop | import Control.Exception |
41 | aa3adf35 | Iustin Pop | import Control.Monad (liftM) |
42 | aa3adf35 | Iustin Pop | import Data.Maybe (fromMaybe) |
43 | aa3adf35 | Iustin Pop | import qualified Network.Socket as Socket |
44 | aa3adf35 | Iustin Pop | import System.FilePath ((</>)) |
45 | 79ac58fa | Iustin Pop | import System.IO.Error (isDoesNotExistError) |
46 | aa3adf35 | Iustin Pop | |
47 | aa3adf35 | Iustin Pop | import qualified Ganeti.Constants as C |
48 | 9eeb0aa5 | Michael Hanselmann | import qualified Ganeti.Path as Path |
49 | aa3adf35 | Iustin Pop | import Ganeti.BasicTypes |
50 | 26d62e4c | Iustin Pop | import Ganeti.Utils |
51 | aa3adf35 | Iustin Pop | |
52 | aa3adf35 | Iustin Pop | -- | Maximum ssconf file size we support. |
53 | aa3adf35 | Iustin Pop | maxFileSize :: Int |
54 | aa3adf35 | Iustin Pop | maxFileSize = 131072 |
55 | aa3adf35 | Iustin Pop | |
56 | c5b4a186 | Iustin Pop | -- | ssconf file prefix, re-exported from Constants. |
57 | c5b4a186 | Iustin Pop | sSFilePrefix :: FilePath |
58 | c5b4a186 | Iustin Pop | sSFilePrefix = C.ssconfFileprefix |
59 | c5b4a186 | Iustin Pop | |
60 | aa3adf35 | Iustin Pop | $(declareSADT "SSKey" |
61 | aa3adf35 | Iustin Pop | [ ("SSClusterName", 'C.ssClusterName) |
62 | aa3adf35 | Iustin Pop | , ("SSClusterTags", 'C.ssClusterTags) |
63 | aa3adf35 | Iustin Pop | , ("SSFileStorageDir", 'C.ssFileStorageDir) |
64 | aa3adf35 | Iustin Pop | , ("SSSharedFileStorageDir", 'C.ssSharedFileStorageDir) |
65 | aa3adf35 | Iustin Pop | , ("SSMasterCandidates", 'C.ssMasterCandidates) |
66 | aa3adf35 | Iustin Pop | , ("SSMasterCandidatesIps", 'C.ssMasterCandidatesIps) |
67 | aa3adf35 | Iustin Pop | , ("SSMasterIp", 'C.ssMasterIp) |
68 | aa3adf35 | Iustin Pop | , ("SSMasterNetdev", 'C.ssMasterNetdev) |
69 | aa3adf35 | Iustin Pop | , ("SSMasterNetmask", 'C.ssMasterNetmask) |
70 | aa3adf35 | Iustin Pop | , ("SSMasterNode", 'C.ssMasterNode) |
71 | aa3adf35 | Iustin Pop | , ("SSNodeList", 'C.ssNodeList) |
72 | aa3adf35 | Iustin Pop | , ("SSNodePrimaryIps", 'C.ssNodePrimaryIps) |
73 | aa3adf35 | Iustin Pop | , ("SSNodeSecondaryIps", 'C.ssNodeSecondaryIps) |
74 | aa3adf35 | Iustin Pop | , ("SSOfflineNodes", 'C.ssOfflineNodes) |
75 | aa3adf35 | Iustin Pop | , ("SSOnlineNodes", 'C.ssOnlineNodes) |
76 | aa3adf35 | Iustin Pop | , ("SSPrimaryIpFamily", 'C.ssPrimaryIpFamily) |
77 | aa3adf35 | Iustin Pop | , ("SSInstanceList", 'C.ssInstanceList) |
78 | aa3adf35 | Iustin Pop | , ("SSReleaseVersion", 'C.ssReleaseVersion) |
79 | aa3adf35 | Iustin Pop | , ("SSHypervisorList", 'C.ssHypervisorList) |
80 | aa3adf35 | Iustin Pop | , ("SSMaintainNodeHealth", 'C.ssMaintainNodeHealth) |
81 | aa3adf35 | Iustin Pop | , ("SSUidPool", 'C.ssUidPool) |
82 | aa3adf35 | Iustin Pop | , ("SSNodegroups", 'C.ssNodegroups) |
83 | aa3adf35 | Iustin Pop | ]) |
84 | aa3adf35 | Iustin Pop | |
85 | aa3adf35 | Iustin Pop | -- | Convert a ssconf key into a (full) file path. |
86 | 37904802 | Iustin Pop | keyToFilename :: FilePath -- ^ Config path root |
87 | 37904802 | Iustin Pop | -> SSKey -- ^ Ssconf key |
88 | 37904802 | Iustin Pop | -> FilePath -- ^ Full file name |
89 | 67e4fcf4 | Iustin Pop | keyToFilename cfgpath key = |
90 | 37904802 | Iustin Pop | cfgpath </> sSFilePrefix ++ sSKeyToRaw key |
91 | aa3adf35 | Iustin Pop | |
92 | aa3adf35 | Iustin Pop | -- | Runs an IO action while transforming any error into 'Bad' |
93 | aa3adf35 | Iustin Pop | -- values. It also accepts an optional value to use in case the error |
94 | aa3adf35 | Iustin Pop | -- is just does not exist. |
95 | aa3adf35 | Iustin Pop | catchIOErrors :: Maybe a -- ^ Optional default |
96 | aa3adf35 | Iustin Pop | -> IO a -- ^ Action to run |
97 | aa3adf35 | Iustin Pop | -> IO (Result a) |
98 | aa3adf35 | Iustin Pop | catchIOErrors def action = |
99 | 1251817b | Iustin Pop | Control.Exception.catch |
100 | 1251817b | Iustin Pop | (do |
101 | aa3adf35 | Iustin Pop | result <- action |
102 | aa3adf35 | Iustin Pop | return (Ok result) |
103 | aa3adf35 | Iustin Pop | ) (\err -> let bad_result = Bad (show err) |
104 | aa3adf35 | Iustin Pop | in return $ if isDoesNotExistError err |
105 | aa3adf35 | Iustin Pop | then maybe bad_result Ok def |
106 | aa3adf35 | Iustin Pop | else bad_result) |
107 | aa3adf35 | Iustin Pop | |
108 | aa3adf35 | Iustin Pop | -- | Read an ssconf file. |
109 | aa3adf35 | Iustin Pop | readSSConfFile :: Maybe FilePath -- ^ Optional config path override |
110 | aa3adf35 | Iustin Pop | -> Maybe String -- ^ Optional default value |
111 | aa3adf35 | Iustin Pop | -> SSKey -- ^ Desired ssconf key |
112 | aa3adf35 | Iustin Pop | -> IO (Result String) |
113 | aa3adf35 | Iustin Pop | readSSConfFile optpath def key = do |
114 | 29a30533 | Iustin Pop | dpath <- Path.dataDir |
115 | 37904802 | Iustin Pop | result <- catchIOErrors def . readFile . |
116 | 29a30533 | Iustin Pop | keyToFilename (fromMaybe dpath optpath) $ key |
117 | aa3adf35 | Iustin Pop | return (liftM (take maxFileSize) result) |
118 | aa3adf35 | Iustin Pop | |
119 | aa3adf35 | Iustin Pop | -- | Parses a string containing an IP family |
120 | aa3adf35 | Iustin Pop | parseIPFamily :: Int -> Result Socket.Family |
121 | aa3adf35 | Iustin Pop | parseIPFamily fam | fam == C.ip4Family = Ok Socket.AF_INET |
122 | aa3adf35 | Iustin Pop | | fam == C.ip6Family = Ok Socket.AF_INET6 |
123 | aa3adf35 | Iustin Pop | | otherwise = Bad $ "Unknown af_family value: " ++ show fam |
124 | aa3adf35 | Iustin Pop | |
125 | aa3adf35 | Iustin Pop | -- | Read the primary IP family. |
126 | aa3adf35 | Iustin Pop | getPrimaryIPFamily :: Maybe FilePath -> IO (Result Socket.Family) |
127 | aa3adf35 | Iustin Pop | getPrimaryIPFamily optpath = do |
128 | aa3adf35 | Iustin Pop | result <- readSSConfFile optpath (Just (show C.ip4Family)) SSPrimaryIpFamily |
129 | 256e28c4 | Iustin Pop | return (liftM rStripSpace result >>= |
130 | aa3adf35 | Iustin Pop | tryRead "Parsing af_family" >>= parseIPFamily) |
131 | d8e9131b | Michele Tartara | |
132 | d8e9131b | Michele Tartara | -- | Read the list of IP addresses of the master candidates of the cluster. |
133 | d8e9131b | Michele Tartara | getMasterCandidatesIps :: Maybe FilePath -> IO (Result [String]) |
134 | d8e9131b | Michele Tartara | getMasterCandidatesIps optPath = do |
135 | d8e9131b | Michele Tartara | result <- readSSConfFile optPath Nothing SSMasterCandidatesIps |
136 | d8e9131b | Michele Tartara | return $ liftM lines result |