1 # Licensed to the .NET Foundation under one or more agreements.
2 # The .NET Foundation licenses this file to you under the MIT license.
3 # See the LICENSE file in the project root for more information.
4 
5 #Requires -RunAsAdministrator
6 
7 Param (
8     [switch] $uninstall=$false
9 )
10 
11 # Import configuration.
12 . .\setup_common.ps1
13 
TestMachineStatus($role)14 Function TestMachineStatus($role)
15 {
16     try
17     {
18         $status = Invoke-Command -ComputerName $role.MachineName -ArgumentList $role.Name -ErrorAction Stop { [Environment]::GetEnvironmentVariable($args[0], "Machine") }
19 
20         $role.Reachable = $true
21         if ($status -eq "Installed")
22         {
23             $role.Installed = $true
24         }
25         else
26         {
27             if (-not [string]::IsNullOrWhiteSpace($status))
28             {
29                 Write-Warning "Role $($role.Name) found to have status of: $status."
30                 Write-Warning "The script will try to resume the installation. To manually resume installation, manually run the $($role.Script) on $($role.MachineName) as Administrator."
31             }
32         }
33     }
34     catch [System.Exception]
35     {
36         $role.Reachable = $false
37     }
38 }
39 
CheckRolesnull40 Function CheckRoles
41 {
42     Write-Host "Verifying server applications:"
43     $iisApplications = GetIISCodePath
44     if (-not (Test-Path (Join-Path $iisApplications "index.html")))
45     {
46         throw "Cannot find index.html within the $iisApplications path. Make sure that you have built and copied the Server code before running this script."
47     }
48 
49     Write-Host "Verifying roles:"
50     foreach ($role in ($script:Roles + $script:PreRebootRoles))
51     {
52         Write-Host -ForegroundColor DarkGray "`t" $role.Name
53         if (-not (Test-Path $role.Script))
54         {
55             throw "Cannot find installation script for $($role.Name): $($role.Script)"
56         }
57 
58         if ([string]::IsNullOrWhiteSpace($role.MachineName))
59         {
60             throw "Please edit config.ps1 and set a valid value name for $($role.Name)_Machine."
61         }
62 
63         TestMachineStatus $role
64     }
65 
66     Write-Host "OK."
67 }
68 
EnsurePreRebootForCurrentMachinenull69 Function EnsurePreRebootForCurrentMachine
70 {
71     $machineInfo = Get-WmiObject win32_computersystem
72     $currentRole = GetPreRebootRoleForMachine($Env:COMPUTERNAME)
73 
74     if (($machineInfo.PartOfDomain -eq $true) -and `
75         ($machineInfo.Domain -eq $script:domainName) -and `
76         ($currentRole.Installed -eq $true))
77     {
78         return $true
79     }
80     elseif (($machineInfo.PartOfDomain -eq $true) -and `
81             ($machineInfo.Domain -ne $script:domainName))
82     {
83         Write-Error "The current machine is already joiend to the $($machineInfo.Domain) domain."
84         Write-Error "Either change config.ps1 to use the correct domain information, select a different machine or remove the machine from the current domain."
85         throw "Cannot use the current machine: already joined to a domain."
86     }
87 
88     & (".\" + $currentRole.Script)
89 }
90 
CreateDestinationPath($s)91 Function CreateDestinationPath($s)
92 {
93     return Invoke-Command -Session $s `
94     {
95         $destPath = Join-Path $env:SystemDrive "COREFX_NET_Scripts"
96         mkdir $destPath -ErrorAction SilentlyContinue | Out-Null
97         return $destPath
98     }
99 }
100 
CopyScripts($s, $remotePath)101 Function CopyScripts($s, $remotePath)
102 {
103     Copy-Item -Recurse -Force -Path ".\*" -Destination $remotePath -ToSession $s -ErrorAction Stop
104 }
105 
InstallRoles()106 Function InstallRoles
107 {
108     Write-Host -ForegroundColor Cyan "Remotely installing all roles."
109 
110     foreach ($role in $script:Roles)
111     {
112         Write-Host -ForegroundColor DarkCyan "Installing role [$($role.Name)]: $($role.MachineName)"
113 
114         Write-Host -ForegroundColor DarkGray "`tConnecting"
115         $s = New-PSSession -ComputerName $role.MachineName
116 
117         Write-Host -ForegroundColor DarkGray "`tCopying scripts"
118         # Copy scripts
119         $remotePath = CreateDestinationPath $s
120         CopyScripts $s $remotePath
121 
122         Write-Host -ForegroundColor DarkGray "`tInstalling"
123         # Run remote scripts
124         Invoke-Command -Session $s -ArgumentList $remotePath, $role.Script `
125         {
126             $path = $args[0]
127             $script = $args[1]
128 
129             cd $path
130             & (".\" + $script)
131         }
132 
133         Write-Host -ForegroundColor DarkCyan "Role [$($role.Name)]: $($role.MachineName) installation complete."
134         Write-Host
135         Write-Host
136     }
137 }
138 
Install()139 Function Install
140 {
141     Write-Host -ForegroundColor Cyan "Install/Update CoreFX Networking multi-machine prerequisites"
142     Write-Host
143     CheckRoles
144 
145     EnsurePreRebootForCurrentMachine
146 
147     if (($script:Roles | where {$_.Reachable -ne $true}).Count -ne 0)
148     {
149         Write-Warning "Not all roles are reachable from this host."
150         Write-Host "- If all machines are joined to the Domain, make sure you are logged on as $($script:domainNetbios)\Administrator and not as a local Administrator."
151         Write-Host "- If not all machines are joined to the Domain: Log-on to the following machines, copy all scripts and run .\setup.ps1 as Administrator:"
152         $script:Roles | where {$_.Reachable -ne $true}
153         Write-Host -ForegroundColor Cyan "Rerun this command after all machines have been started and joined to the $($script:domainNetBios) domain."
154         return
155     }
156 
157     InstallRoles
158 
159     Write-Host -ForegroundColor Cyan "Role installation complete."
160 }
161 
UnistallMachines()162 Function UnistallMachines
163 {
164     Write-Host -ForegroundColor Cyan "Remotely uninstalling roles."
165 
166     foreach ($role in $script:Roles)
167     {
168         Write-Host -ForegroundColor DarkCyan "Uninstalling role [$($role.Name)]: $($role.MachineName)"
169 
170         Write-Host -ForegroundColor DarkGray "`tConnecting"
171         $s = New-PSSession -ComputerName $role.MachineName
172 
173         Write-Host -ForegroundColor DarkGray "`tCopying scripts"
174         # Copy scripts
175         $remotePath = CreateDestinationPath $s
176         CopyScripts $s $remotePath
177 
178         $preRebootRole = GetPreRebootRoleForMachine $role.MachineName
179 
180         Write-Host -ForegroundColor DarkGray "`tUninstalling"
181         # Run remote scripts
182         Invoke-Command -Session $s -ArgumentList $remotePath, $role.Script, $preRebootRole.Script `
183         {
184             $path = $args[0]
185             $script = $args[1]
186             $preRebootScript = $args[2]
187 
188             cd $path
189             & (".\" + $script) -uninstall
190             & (".\" + $preRebootScript) -uninstall
191         }
192 
193         Write-Host -ForegroundColor DarkCyan "Role [$($role.Name)]: $($role.MachineName) uninstall complete."
194         Write-Host
195         Write-Host
196     }
197 }
198 
Uninstall()199 Function Uninstall
200 {
201     Write-Host -ForegroundColor Cyan "Uninstall CoreFX Networking multi-machine prerequisites"
202     Write-Host
203     CheckRoles
204     Write-Host
205 
206     Write-Warning "Some of the installed components may have existed before or got changes outside of this setup script."
207     Write-Warning "The scripts will attempt to remove all components regardless of these changes."
208     $continue = Read-Host "Do you want to continue? [Y/N]"
209 
210     if ($continue.ToUpper() -ne "Y")
211     {
212         Write-Warning "Aborted by user."
213         return
214     }
215 
216     UnistallMachines
217 }
218 
219 if ($uninstall)
220 {
221     Uninstall
222 }
223 else
224 {
225     Install
226 }
227