forked from fsprojects/FAKE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIISHelper.fs
126 lines (106 loc) · 4.77 KB
/
IISHelper.fs
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
[<AutoOpen>]
module Fake.IISHelper
open Microsoft.Web.Administration
open Fake.PermissionsHelper
open Fake.ProcessHelper
let private bindApplicationPool (appPool : ApplicationPool) (app : Application) =
app.ApplicationPoolName <- appPool.Name
let private doWithManager (f : ServerManager->unit) (mgr : ServerManager option) =
match mgr with
| Some m -> f m
| None ->
let m = new ServerManager()
f m
m.CommitChanges()
let SetPhysicalPath (virtualPath : string) physicalPath (siteName : string) (manager : ServerManager option) =
doWithManager (fun m ->
let site = m.Sites.[siteName]
let app = site.Applications.[virtualPath]
let virtDir = app.VirtualDirectories.[virtualPath]
virtDir.PhysicalPath <- physicalPath
) manager
let RemoveBindingFromSite bindingInformation bindingProtocol (siteName : string) (manager : ServerManager option) =
doWithManager (fun m ->
let site = m.Sites.[siteName]
match site.Bindings |> Seq.tryFind( fun b -> b.Protocol = bindingProtocol && b.BindingInformation = bindingInformation) with
| Some b -> site.Bindings.Remove b
| None -> ()
) manager
let AddBindingToSite (bindingInformation : string) (bindingProtocol : string) (siteName : string) (manager : ServerManager option) =
doWithManager (fun m ->
let site = m.Sites.[siteName]
match site.Bindings |> Seq.exists( fun b -> b.Protocol = bindingProtocol && b.BindingInformation = bindingInformation) with
| false -> site.Bindings.Add(bindingInformation, bindingProtocol) |> ignore
| true -> ()
) manager
let Site (name : string) protocol binding (physicalPath : string) appPool (mgr : ServerManager) =
let mutable site = mgr.Sites.[name]
match (site) with
| null -> site <- mgr.Sites.Add(name, protocol, binding, physicalPath)
| _ ->
SetPhysicalPath "/" physicalPath name (Some mgr)
AddBindingToSite binding protocol name (Some mgr)
site.ApplicationDefaults.ApplicationPoolName <- appPool
site
let Application (virtualPath : string) (physicalPath : string) (site : Site) (mgr : ServerManager) =
let app = site.Applications.[virtualPath]
match (app) with
| null -> site.Applications.Add(virtualPath, physicalPath)
| _ -> app.VirtualDirectories.[0].PhysicalPath <- physicalPath; app
let commit (mgr : ServerManager) = mgr.CommitChanges()
type ApplicationPoolConfig(name : string, ?runtime:string, ?allow32on64:bool, ?identity : ProcessModelIdentityType) = class
member this.name = name
member this.runtime = defaultArg runtime "v4.0"
member this.allow32on64 = defaultArg allow32on64 false
member this.identity = defaultArg identity ProcessModelIdentityType.ApplicationPoolIdentity
end
let private MergeAppPoolProperties (appPool:ApplicationPool)(config:ApplicationPoolConfig) =
appPool.Enable32BitAppOnWin64 <- config.allow32on64
appPool.ManagedRuntimeVersion <- config.runtime
appPool.ProcessModel.IdentityType <- config.identity
appPool
let ApplicationPool (config: ApplicationPoolConfig) (mgr : ServerManager) =
let appPool = mgr.ApplicationPools.[config.name]
match (appPool) with
| null ->
let pool = mgr.ApplicationPools.Add(config.name)
MergeAppPoolProperties pool config
| _ ->
MergeAppPoolProperties appPool config
let IIS (site : ServerManager -> Site)
(appPool : ServerManager -> ApplicationPool)
(app : (Site -> ServerManager -> Application) option) =
use mgr = new ServerManager()
requiresAdmin (fun _ ->
match app with
| Some(app) -> bindApplicationPool (appPool mgr) (app (site mgr) mgr)
| None -> bindApplicationPool (appPool mgr) (site mgr).Applications.[0]
commit mgr
)
let AppCmd (command : string) =
System.Console.WriteLine("Applying {0} via appcmd.exe", command)
if 0 <> ExecProcess (fun info ->
info.FileName <- @"c:\windows\system32\inetsrv\appcmd.exe"
info.Arguments <- command) (System.TimeSpan.FromSeconds(30.))
then failwithf "AppCmd.exe %s failed." command
()
let UnlockSection (configPath : string) =
requiresAdmin (fun _ -> AppCmd (sprintf "unlock config -section:%s" configPath))
let deleteSite (name : string) =
use mgr = new ServerManager()
let site = mgr.Sites.[name]
if site <> null then
site.Delete()
commit mgr
let deleteApp (name : string) (site : Site) =
use mgr = new ServerManager()
let app = site.Applications.[name]
if app <> null then
app.Delete()
commit mgr
let deleteApplicationPool (name : string) =
use mgr = new ServerManager()
let appPool = mgr.ApplicationPools.[name]
if appPool <> null then
appPool.Delete()
commit mgr