module Propellor.Property.Journald where

import Propellor.Base
import qualified Propellor.Property.Systemd as Systemd
import Utility.DataUnits

-- | Configures journald, restarting it so the changes take effect.
configured :: Systemd.Option -> String -> Property Linux
configured :: Option -> Option -> Property Linux
configured Option
option Option
value =
	Option -> Option -> Option -> Property Linux
Systemd.configured Option
"/etc/systemd/journald.conf" Option
option Option
value
		Property Linux
-> Property Linux -> CombinedType (Property Linux) (Property Linux)
forall x y. Combines x y => x -> y -> CombinedType x y
`onChange` Option -> Property Linux
Systemd.restarted Option
"systemd-journald"

-- The string is parsed to get a data size.
-- Examples: "100 megabytes" or "0.5tb"
type DataSize = String

configuredSize :: Systemd.Option -> DataSize -> Property Linux
configuredSize :: Option -> Option -> Property Linux
configuredSize Option
option Option
s = case [Unit] -> Option -> Maybe ByteSize
readSize [Unit]
dataUnits Option
s of
	Just ByteSize
sz -> Option -> Option -> Property Linux
configured Option
option (ByteSize -> Option
systemdSizeUnits ByteSize
sz)
	Maybe ByteSize
Nothing -> Option -> Propellor Result -> Property Linux
forall k (metatypes :: k).
SingI metatypes =>
Option -> Propellor Result -> Property (MetaTypes metatypes)
property (Option
"unable to parse " Option -> Option -> Option
forall a. [a] -> [a] -> [a]
++ Option
option Option -> Option -> Option
forall a. [a] -> [a] -> [a]
++ Option
" data size " Option -> Option -> Option
forall a. [a] -> [a] -> [a]
++ Option
s) (Propellor Result -> Property Linux)
-> Propellor Result -> Property Linux
forall a b. (a -> b) -> a -> b
$
		Result -> Propellor Result
forall (m :: * -> *) a. Monad m => a -> m a
return Result
FailedChange

systemMaxUse :: DataSize -> Property Linux
systemMaxUse :: Option -> Property Linux
systemMaxUse = Option -> Option -> Property Linux
configuredSize Option
"SystemMaxUse"

runtimeMaxUse :: DataSize -> Property Linux
runtimeMaxUse :: Option -> Property Linux
runtimeMaxUse = Option -> Option -> Property Linux
configuredSize Option
"RuntimeMaxUse"

systemKeepFree :: DataSize -> Property Linux
systemKeepFree :: Option -> Property Linux
systemKeepFree = Option -> Option -> Property Linux
configuredSize Option
"SystemKeepFree"

runtimeKeepFree :: DataSize -> Property Linux
runtimeKeepFree :: Option -> Property Linux
runtimeKeepFree = Option -> Option -> Property Linux
configuredSize Option
"RuntimeKeepFree"

systemMaxFileSize :: DataSize -> Property Linux
systemMaxFileSize :: Option -> Property Linux
systemMaxFileSize = Option -> Option -> Property Linux
configuredSize Option
"SystemMaxFileSize"

runtimeMaxFileSize :: DataSize -> Property Linux
runtimeMaxFileSize :: Option -> Property Linux
runtimeMaxFileSize = Option -> Option -> Property Linux
configuredSize Option
"RuntimeMaxFileSize"

-- Generates size units as used in journald.conf.
systemdSizeUnits :: Integer -> String
systemdSizeUnits :: ByteSize -> Option
systemdSizeUnits ByteSize
sz = (Char -> Bool) -> Option -> Option
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
' ') ([Unit] -> Bool -> ByteSize -> Option
roughSize [Unit]
cfgfileunits Bool
True ByteSize
sz)
  where
	cfgfileunits :: [Unit]
	cfgfileunits :: [Unit]
cfgfileunits =
	        [ ByteSize -> Option -> Option -> Unit
Unit (ByteSize -> ByteSize
p ByteSize
6) Option
"E" Option
"exabyte"
		, ByteSize -> Option -> Option -> Unit
Unit (ByteSize -> ByteSize
p ByteSize
5) Option
"P" Option
"petabyte"
		, ByteSize -> Option -> Option -> Unit
Unit (ByteSize -> ByteSize
p ByteSize
4) Option
"T" Option
"terabyte"
		, ByteSize -> Option -> Option -> Unit
Unit (ByteSize -> ByteSize
p ByteSize
3) Option
"G" Option
"gigabyte"
		, ByteSize -> Option -> Option -> Unit
Unit (ByteSize -> ByteSize
p ByteSize
2) Option
"M" Option
"megabyte"
		, ByteSize -> Option -> Option -> Unit
Unit (ByteSize -> ByteSize
p ByteSize
1) Option
"K" Option
"kilobyte"
		]
        p :: Integer -> Integer
        p :: ByteSize -> ByteSize
p ByteSize
n = ByteSize
1024ByteSize -> ByteSize -> ByteSize
forall a b. (Num a, Integral b) => a -> b -> a
^ByteSize
n