summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/DebianMirror.hs
blob: ad15f9a2835b5d8958cfb359a6baf94c300e2315 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
-- | Maintainer: FĂ©lix Sipma <felix+propellor@gueux.org>

module Propellor.Property.DebianMirror
	( DebianPriority (..)
	, showPriority
	, mirror
	, RsyncExtra (..)
	, Method (..)
	, DebianMirror
	, debianMirrorHostName
	, debianMirrorSuites
	, debianMirrorArchitectures
	, debianMirrorSections
	, debianMirrorSourceBool
	, debianMirrorPriorities
	, debianMirrorMethod
	, debianMirrorKeyring
	, debianMirrorRsyncExtra
	, mkDebianMirror
	) where

import Propellor.Base
import qualified Propellor.Property.File as File
import qualified Propellor.Property.Apt as Apt
import qualified Propellor.Property.Cron as Cron
import qualified Propellor.Property.User as User

import Data.List


data DebianPriority = Essential | Required | Important | Standard | Optional | Extra
	deriving (Show, Eq)

showPriority :: DebianPriority -> String
showPriority Essential = "essential"
showPriority Required  = "required"
showPriority Important = "important"
showPriority Standard  = "standard"
showPriority Optional  = "optional"
showPriority Extra     = "extra"

data RsyncExtra = Doc | Indices | Tools | Trace
	deriving (Show, Eq)

showRsyncExtra :: RsyncExtra -> String
showRsyncExtra Doc = "doc"
showRsyncExtra Indices = "indices"
showRsyncExtra Tools = "tools"
showRsyncExtra Trace = "trace"

data Method = Ftp | Http | Https | Rsync | MirrorFile

showMethod :: Method -> String
showMethod Ftp = "ftp"
showMethod Http = "http"
showMethod Https = "https"
showMethod Rsync = "rsync"
showMethod MirrorFile = "file"

-- | To get a new DebianMirror and set options, use:
--
-- > mkDebianMirror mymirrordir mycrontimes
-- > 	. debianMirrorHostName "otherhostname"
-- > 	. debianMirrorSourceBool True

data DebianMirror = DebianMirror
	{ _debianMirrorHostName :: HostName
	, _debianMirrorDir :: FilePath
	, _debianMirrorSuites :: [DebianSuite]
	, _debianMirrorArchitectures :: [Architecture]
	, _debianMirrorSections :: [Apt.Section]
	, _debianMirrorSourceBool :: Bool
	, _debianMirrorPriorities :: [DebianPriority]
	, _debianMirrorMethod :: Method
	, _debianMirrorKeyring :: FilePath
	, _debianMirrorRsyncExtra :: [RsyncExtra]
	, _debianMirrorCronTimes :: Cron.Times
	}

mkDebianMirror :: FilePath -> Cron.Times -> DebianMirror
mkDebianMirror dir crontimes = DebianMirror
	{ _debianMirrorHostName = "deb.debian.org"
	, _debianMirrorDir = dir
	, _debianMirrorSuites = []
	, _debianMirrorArchitectures = []
	, _debianMirrorSections = []
	, _debianMirrorSourceBool = False
	, _debianMirrorPriorities = []
	, _debianMirrorMethod = Http
	, _debianMirrorKeyring = "/usr/share/keyrings/debian-archive-keyring.gpg"
	, _debianMirrorRsyncExtra = [Trace]
	, _debianMirrorCronTimes = crontimes
	}

debianMirrorHostName :: HostName -> DebianMirror -> DebianMirror
debianMirrorHostName hn m = m { _debianMirrorHostName = hn }

debianMirrorSuites :: [DebianSuite] -> DebianMirror -> DebianMirror
debianMirrorSuites s m = m { _debianMirrorSuites = s }

debianMirrorArchitectures :: [Architecture] -> DebianMirror -> DebianMirror
debianMirrorArchitectures a m = m { _debianMirrorArchitectures = a }

debianMirrorSections :: [Apt.Section] -> DebianMirror -> DebianMirror
debianMirrorSections s m = m { _debianMirrorSections = s }

debianMirrorSourceBool :: Bool -> DebianMirror -> DebianMirror
debianMirrorSourceBool s m = m { _debianMirrorSourceBool = s }

debianMirrorPriorities :: [DebianPriority] -> DebianMirror -> DebianMirror
debianMirrorPriorities p m = m { _debianMirrorPriorities = p }

debianMirrorMethod :: Method -> DebianMirror -> DebianMirror
debianMirrorMethod me m = m { _debianMirrorMethod = me }

debianMirrorKeyring :: FilePath -> DebianMirror -> DebianMirror
debianMirrorKeyring k m = m { _debianMirrorKeyring = k }

debianMirrorRsyncExtra :: [RsyncExtra] -> DebianMirror -> DebianMirror
debianMirrorRsyncExtra r m = m { _debianMirrorRsyncExtra = r }

mirror :: DebianMirror -> Property DebianLike
mirror mirror' = propertyList ("Debian mirror " ++ dir) $ props
	& Apt.installed ["debmirror"]
	& User.accountFor (User "debmirror")
	& File.dirExists dir
	& File.ownerGroup dir (User "debmirror") (Group "debmirror")
	& check (not . and <$> mapM suitemirrored suites)
		(cmdProperty "debmirror" args)
			`describe` "debmirror setup"
	& Cron.niceJob ("debmirror_" ++ dir) (_debianMirrorCronTimes mirror') (User "debmirror") "/"
		(unwords ("/usr/bin/debmirror" : args))
  where
	dir = _debianMirrorDir mirror'
	suites = _debianMirrorSuites mirror'
	suitemirrored suite = doesDirectoryExist $ dir </> "dists" </> Apt.showSuite suite
	architecturearg = intercalate ","
	suitearg = intercalate "," $ map Apt.showSuite suites
	priorityRegex pp = "(" ++ intercalate "|" (map showPriority pp) ++ ")"
	rsyncextraarg [] = "none"
	rsyncextraarg res = intercalate "," $ map showRsyncExtra res
	args =
		[ "--dist" , suitearg
		, "--arch", architecturearg $ map architectureToDebianArchString (_debianMirrorArchitectures mirror')
		, "--section", intercalate "," $ _debianMirrorSections mirror'
		, "--limit-priority", "\"" ++ priorityRegex (_debianMirrorPriorities mirror') ++ "\""
		]
		++
		(if _debianMirrorSourceBool mirror' then [] else ["--nosource"])
		++
		[ "--host", _debianMirrorHostName mirror'
		, "--method", showMethod $ _debianMirrorMethod mirror'
		, "--rsync-extra", rsyncextraarg $ _debianMirrorRsyncExtra mirror'
		, "--keyring", _debianMirrorKeyring mirror'
		, dir
		]