]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/OptionalConfiguration.hs
Add a bunch of new options allowing htsn to daemonize.
[dead/htsn.git] / src / OptionalConfiguration.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5
6 -- | An OptionalConfiguration is just like a 'Configuration', except
7 -- all of its fields are optional. The user can set options in two
8 -- places: the command-line, and a configuration file. Obviously if
9 -- a parameter is set in one place, it doesn't need to be set in the
10 -- other. Thus, the latter needs to be optional.
11 --
12
13 module OptionalConfiguration (
14 OptionalConfiguration(..),
15 from_rc,
16 merge_maybes )
17 where
18
19 import qualified Data.Configurator as DC (
20 Worth(Optional),
21 load,
22 lookup )
23 import qualified Data.Configurator.Types as DCT (
24 Configured,
25 Value( String ),
26 convert )
27 import Data.Data ( Data )
28 import Data.Maybe ( fromMaybe )
29 import Data.Monoid ( Monoid(..) )
30 import Data.Typeable ( Typeable )
31 import System.Directory ( getHomeDirectory )
32 import System.FilePath ( (</>) )
33 import System.IO.Error ( catchIOError )
34 import System.Log ( Priority(..) )
35
36 import Logging ( log_error )
37 import TSN.FeedHosts ( FeedHosts(..) )
38
39
40 -- Derive standalone instances of Data and Typeable for Priority. This
41 -- is necessary for OptionalConfiguration (which contains a Maybe
42 -- Priority) to derive Data and Typeable.
43 deriving instance Data Priority
44 deriving instance Typeable Priority
45
46 -- | The same as Configuration, except everything is optional. It's easy to
47 -- merge two of these by simply dropping the Nothings in favor of
48 -- the Justs. The 'feed_hosts' are left un-maybed so that cmdargs
49 -- can parse more than one of them.
50 --
51 data OptionalConfiguration =
52 OptionalConfiguration {
53 daemonize :: Maybe Bool,
54 feed_hosts :: FeedHosts,
55 log_file :: Maybe FilePath,
56 log_level :: Maybe Priority,
57 output_directory :: Maybe FilePath,
58 password :: Maybe String,
59 pidfile :: Maybe FilePath,
60 run_as_group :: Maybe String,
61 run_as_user :: Maybe String,
62 syslog :: Maybe Bool,
63 username :: Maybe String }
64 deriving (Show, Data, Typeable)
65
66
67 -- | Combine two Maybes into one, essentially mashing them
68 -- together. We give precedence to the second argument when both are
69 -- Justs.
70 merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a)
71 merge_maybes Nothing Nothing = Nothing
72 merge_maybes (Just x) Nothing = Just x
73 merge_maybes Nothing (Just x) = Just x
74 merge_maybes (Just _) (Just y) = Just y
75
76
77 -- | The Monoid instance for these lets us "combine" two
78 -- OptionalConfigurations. The "combine" operation that we'd like to
79 -- perform is, essentially, to mash them together. So if we have two
80 -- OptionalConfigurations, each half full, we could combine them
81 -- into one big one.
82 --
83 -- This is used to merge command-line and config-file settings.
84 --
85 instance Monoid OptionalConfiguration where
86 -- | An empty OptionalConfiguration.
87 mempty = OptionalConfiguration
88 Nothing
89 (FeedHosts [])
90 Nothing
91 Nothing
92 Nothing
93 Nothing
94 Nothing
95 Nothing
96 Nothing
97 Nothing
98 Nothing
99
100
101 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
102 cfg1 `mappend` cfg2 =
103 OptionalConfiguration
104 (merge_maybes (daemonize cfg1) (daemonize cfg2))
105 all_feed_hosts
106 (merge_maybes (log_file cfg1) (log_file cfg2))
107 (merge_maybes (log_level cfg1) (log_level cfg2))
108 (merge_maybes (output_directory cfg1) (output_directory cfg2))
109 (merge_maybes (password cfg1) (password cfg2))
110 (merge_maybes (pidfile cfg1) (pidfile cfg2))
111 (merge_maybes (run_as_group cfg1) (run_as_group cfg2))
112 (merge_maybes (run_as_user cfg1) (run_as_user cfg2))
113 (merge_maybes (syslog cfg1) (syslog cfg2))
114 (merge_maybes (username cfg1) (username cfg2))
115 where
116 -- Use only the latter feed_hosts if there are any.
117 all_feed_hosts =
118 feed_hosts $ if (null (get_feed_hosts (feed_hosts cfg2)))
119 then cfg1
120 else cfg2
121
122
123 instance DCT.Configured Priority where
124 -- | This allows us to read a Priority level out of a Configurator
125 -- config file. By default Configurator wouldn't know what to do,
126 -- so we have to tell it that we expect one of the valid Priority
127 -- constructors.
128 convert (DCT.String "INFO") = Just INFO
129 convert (DCT.String "WARNING") = Just WARNING
130 convert (DCT.String "ERROR") = Just ERROR
131 convert _ = Nothing
132
133
134 -- | Obtain an OptionalConfiguration from the file ".htsnrc" in the
135 -- user's home directory.
136 --
137 -- We make an attempt at cross-platform compatibility; we will try
138 -- to find the correct directory even on Windows. But if the call
139 -- to getHomeDirectory fails for whatever reason, we fall back to
140 -- using the environment variable $HOME.
141 --
142 from_rc :: IO OptionalConfiguration
143 from_rc = do
144 -- After some thought, the "global" /etc/htsnrc configuration file
145 -- was left out. Since each config file needs a password, and this
146 -- should be run by a dedicated user anyway, the global file does
147 -- not serve much purpose. It could also be a security risk (visible
148 -- password) if the admin screws up.
149 home <- catchIOError getHomeDirectory (\e -> do
150 log_error (show e)
151 return "$(HOME)")
152 let user_config_path = home </> ".htsnrc"
153 cfg <- DC.load [ DC.Optional user_config_path ]
154 cfg_daemonize <- DC.lookup cfg "daemonize"
155 cfg_feed_hosts <- DC.lookup cfg "feed_hosts"
156 cfg_log_file <- DC.lookup cfg "log_file"
157 cfg_log_level <- DC.lookup cfg "log_level"
158 cfg_output_directory <- DC.lookup cfg "output_directory"
159 cfg_password <- DC.lookup cfg "password"
160 cfg_pidfile <- DC.lookup cfg "pidfile"
161 cfg_run_as_group <- DC.lookup cfg "run_as_group"
162 cfg_run_as_user <- DC.lookup cfg "run_as_user"
163 cfg_syslog <- DC.lookup cfg "syslog"
164 cfg_username <- DC.lookup cfg "username"
165
166 return $ OptionalConfiguration
167 cfg_daemonize
168 (fromMaybe (FeedHosts []) cfg_feed_hosts)
169 cfg_log_file
170 cfg_log_level
171 cfg_output_directory
172 cfg_password
173 cfg_pidfile
174 cfg_run_as_group
175 cfg_run_as_user
176 cfg_syslog
177 cfg_username