1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
4 * This program is free software
; you can redistribute it
and/or
5 * modify it under the terms
of the GNU General Public License
6 * as published by the Free Software Foundation
; either version
2
7 * of the License
, or (at your option
) any later version
.
9 * This program is distributed
in the hope that it will be useful
,
10 * but WITHOUT ANY WARRANTY
; without even the implied warranty
of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
. See the
12 * GNU General Public License for more details
.
14 * You should have received a copy
of the GNU General Public License
15 * along
with this program
; if not
, write to the Free Software
16 * Foundation
, Inc
., 51 Franklin Street
, Fifth Floor
, Boston
, MA
02110-1301, USA
.
19 (* Domain
-related primitive actions
*)
21 structure Domain
:> DOMAIN
= struct
23 structure SM
= DataStructures
.StringMap
25 val nodes
= map #
2 Config
.nodeIps
26 val nodeMap
= foldl (fn ((node
, ip
), mp
) => SM
.insert (mp
, node
, ip
))
27 SM
.empty Config
.nodeIps
30 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
31 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
32 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
35 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
38 size s
> 0 andalso size s
< 20
39 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
42 size s
> 0 andalso size s
< 100
43 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
45 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
47 val _
= Env
.type_one
"ip"
51 val _
= Env
.type_one
"host"
55 val _
= Env
.type_one
"domain"
59 val _
= Env
.type_one
"node"
65 val dl
= ErrorMsg
.dummyLoc
67 val nsD
= (EString Config
.defaultNs
, dl
)
68 val serialD
= (EVar
"serialAuto", dl
)
69 val refD
= (EInt Config
.defaultRefresh
, dl
)
70 val retD
= (EInt Config
.defaultRetry
, dl
)
71 val expD
= (EInt Config
.defaultExpiry
, dl
)
72 val minD
= (EInt Config
.defaultMinimum
, dl
)
74 val soaD
= multiApp ((EVar
"soa", dl
),
76 [nsD
, serialD
, refD
, retD
, expD
, minD
])
78 val masterD
= (EApp ((EVar
"internalMaster", dl
),
79 (EString Config
.defaultNode
, dl
)),
82 val _
= Main
.registerDefault ("DNS",
83 (TBase
"dnsKind", dl
),
84 (multiApp ((EVar
"useDns", dl
),
86 [soaD
, masterD
, (EList
[], dl
)])))
88 val _
= Main
.registerDefault ("TTL",
90 (EInt Config
.Bind
.defaultTTL
, dl
))
92 type soa
= {ns
: string,
99 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
100 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
103 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
104 ((EVar
"soa", _
), ns
), _
),
110 (case (Env
.string ns
, serial sl
, Env
.int rf
,
111 Env
.int ret
, Env
.int exp
, Env
.int min
) of
112 (SOME ns
, SOME sl
, SOME rf
,
113 SOME ret
, SOME exp
, SOME min
) =>
124 ExternalMaster
of string
125 | InternalMaster
of string
127 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (Env
.string e
)
128 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
132 UseDns
of {soa
: soa
,
134 slaves
: string list
}
137 val dnsKind
= fn (EApp ((EApp ((EApp
138 ((EVar
"useDns", _
), sa
), _
),
141 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
142 (SOME sa
, SOME mstr
, SOME slaves
) =>
143 SOME (UseDns
{soa
= sa
,
149 val befores
= ref (fn (_
: string) => ())
150 val afters
= ref (fn (_
: string) => ())
152 fun registerBefore f
=
156 befores
:= (fn x
=> (old x
; f x
))
159 fun registerAfter f
=
163 afters
:= (fn x
=> (old x
; f x
))
167 val currentPath
= ref (fn (_
: string) => "")
171 fun currentDomain () = !current
173 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
174 TextIO.openOut (!currentPath node ^ name
))
178 val toks
= String.fields (fn ch
=> ch
= #
".") domain
180 val elems
= foldr (fn (piece
, elems
) =>
182 val elems
= piece
:: elems
186 val path
= String.concatWith
"/"
187 (Config
.resultRoot
:: node
:: rev elems
)
188 val tmpPath
= String.concatWith
"/"
189 (Config
.tmpDir
:: node
:: rev elems
)
191 (if Posix
.FileSys
.ST
.isDir
192 (Posix
.FileSys
.stat path
) then
195 (OS
.FileSys
.remove path
;
196 OS
.FileSys
.mkDir path
))
197 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
199 (if Posix
.FileSys
.ST
.isDir
200 (Posix
.FileSys
.stat tmpPath
) then
203 (OS
.FileSys
.remove tmpPath
;
204 OS
.FileSys
.mkDir tmpPath
))
205 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
212 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
215 datatype file_action
' =
216 Add
' of {src
: string, dst
: string}
218 | Modify
' of {src
: string, dst
: string}
220 fun findDiffs (site
, dom
, acts
) =
223 val realPath
= gp (Config
.resultRoot
, site
)
224 val tmpPath
= gp (Config
.tmpDir
, site
)
226 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
228 val dir
= Posix
.FileSys
.opendir realPath
231 case Posix
.FileSys
.readdir dir
of
232 NONE
=> (Posix
.FileSys
.closedir dir
;
236 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
238 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
241 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
243 else if Posix
.FileSys
.access (tmp
, []) then
244 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
247 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
249 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
252 val acts
= loopReal acts
254 val dir
= Posix
.FileSys
.opendir tmpPath
257 case Posix
.FileSys
.readdir dir
of
258 NONE
=> (Posix
.FileSys
.closedir dir
;
262 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
264 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
267 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
269 else if Posix
.FileSys
.access (real, []) then
272 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
275 val acts
= loopTmp acts
280 fun findAllDiffs () =
282 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
283 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
285 fun exploreSites diffs
=
286 case Posix
.FileSys
.readdir dir
of
290 fun explore (dname
, diffs
) =
292 val dir
= Posix
.FileSys
.opendir dname
295 case Posix
.FileSys
.readdir dir
of
299 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
302 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
304 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
305 val dom
= List.drop (dom
, len
)
306 val dom
= String.concatWith
"." (rev dom
)
308 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
312 findDiffs (site
, dom
, diffs
))
319 before Posix
.FileSys
.closedir dir
322 explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
327 before Posix
.FileSys
.closedir dir
330 val masterNode
: string option ref
= ref NONE
331 fun dnsMaster () = !masterNode
333 val _
= Env
.containerV_one
"domain"
334 ("domain", Env
.string)
337 val kind
= Env
.env
dnsKind (evs
, "DNS")
338 val ttl
= Env
.env Env
.int (evs
, "TTL")
340 val path
= getPath dom
342 val () = (current
:= dom
;
343 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
345 fun saveSoa (kind
, soa
: soa
) node
=
347 val outf
= domainFile
{node
= node
, name
= "soa"}
349 TextIO.output (outf
, kind
);
350 TextIO.output (outf
, "\n");
351 TextIO.output (outf
, Int.toString ttl
);
352 TextIO.output (outf
, "\n");
353 TextIO.output (outf
, #ns soa
);
354 TextIO.output (outf
, "\n");
357 | SOME n
=> TextIO.output (outf
, Int.toString n
);
358 TextIO.output (outf
, "\n");
359 TextIO.output (outf
, Int.toString (#ref soa
));
360 TextIO.output (outf
, "\n");
361 TextIO.output (outf
, Int.toString (#ret soa
));
362 TextIO.output (outf
, "\n");
363 TextIO.output (outf
, Int.toString (#exp soa
));
364 TextIO.output (outf
, "\n");
365 TextIO.output (outf
, Int.toString (#min soa
));
366 TextIO.output (outf
, "\n");
370 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
372 val outf
= domainFile
{node
= node
, name
= "named.conf"}
374 TextIO.output (outf
, "\nzone \"");
375 TextIO.output (outf
, dom
);
376 TextIO.output (outf
, "\" IN {\n\ttype ");
377 TextIO.output (outf
, kind
);
378 TextIO.output (outf
, ";\n\tfile \"");
379 TextIO.output (outf
, Config
.Bind
.zonePath
);
380 TextIO.output (outf
, "/");
381 TextIO.output (outf
, dom
);
382 TextIO.output (outf
, ".zone\";\n");
384 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
385 | _
=> (TextIO.output (outf
, "\tmasters { ");
386 TextIO.output (outf
, masterIp
);
387 TextIO.output (outf
, " };\n"));
388 TextIO.output (outf
, "};\n");
393 NoDns
=> masterNode
:= NONE
398 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
399 | ExternalMaster ip
=> ip
401 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
402 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
404 InternalMaster node
=>
405 (masterNode
:= SOME node
;
406 saveSoa ("master", #soa dns
) node
;
407 saveNamed ("master", #soa dns
, masterIp
) node
)
408 | _
=> masterNode
:= NONE
;
412 fn () => !afters (!current
))
414 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
415 fn cl
=> "Temp file cleanup failed: " ^ cl
));
416 OS
.FileSys
.mkDir Config
.tmpDir
;
417 app (fn node
=> OS
.FileSys
.mkDir
418 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
421 app (fn node
=> OS
.FileSys
.mkDir
422 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
424 handle OS
.SysErr _
=> ())
427 val () = Env
.registerPost (fn () =>
429 val diffs
= findAllDiffs ()
431 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
432 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
433 fn cl
=> "Copy failed: " ^ cl
);
439 |
(site
, dom
, dir
, Delete
' dst
) =>
440 (OS
.FileSys
.remove dst
441 handle OS
.SysErr _
=>
442 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
444 {action
= Slave
.Delete
,
448 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
449 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
450 fn cl
=> "Copy failed: " ^ cl
);
452 {action
= Slave
.Modify
,
457 if !ErrorMsg
.anyErrors
then
460 Slave
.handleChanges (map #
2 diffs
);
461 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
462 fn cl
=> "Temp file cleanup failed: " ^ cl
))