summaryrefslogtreecommitdiff
path: root/src/Propellor/Host.hs
blob: 14d56e20ff7575c1ae364c0cd11e08cf82aba7e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE PackageImports #-}

module Propellor.Host where

import Data.Monoid
import qualified Data.Set as S

import Propellor.Types
import Propellor.Info
import Propellor.Property
import Propellor.PrivData

-- | Starts accumulating the properties of a Host.
--
-- > host "example.com"
-- > 	& someproperty
-- > 	! oldproperty
-- > 	& otherproperty
host :: HostName -> Host
host hn = Host hn [] mempty

-- | Something that can accumulate properties.
class Hostlike h where
	-- | Adds a property.
	--
	-- Can add Properties and RevertableProperties
	(&) :: IsProp p => h -> p -> h

	-- | Like (&), but adds the property as the
	-- first property of the host. Normally, property
	-- order should not matter, but this is useful
	-- when it does.
	(&^) :: IsProp p => h -> p -> h

	getHost :: h -> Host

instance Hostlike Host where
	(Host hn ps is) &  p = Host hn (ps ++ [toProp p]) (is <> getInfo p)
	(Host hn ps is) &^ p = Host hn ([toProp p] ++ ps) (getInfo p <> is)
	getHost h = h

-- | Adds a property in reverted form.
(!) :: Hostlike h => h -> RevertableProperty -> h
h ! p = h & revert p

infixl 1 &^
infixl 1 &
infixl 1 !

-- | When eg, docking a container, some of the Info about the container
-- should propigate out to the Host it's on. This includes DNS info,
-- so that eg, aliases of the container are reflected in the dns for the
-- host where it runs.
--
-- This adjusts the Property that docks a container, to include such info
-- from the container.
propigateInfo :: Hostlike hl => hl -> Property -> (Info -> Info) -> Property
propigateInfo hl p f = combineProperties (propertyDesc p) $
	p' : dnsprops ++ privprops
  where
	p' = p { propertyInfo = f (propertyInfo p) }
	i = hostInfo (getHost hl)
	dnsprops = map addDNS (S.toList $ _dns i)
	privprops = map addPrivDataField (S.toList $ _privDataFields i)