-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
205 lines (183 loc) · 7.19 KB
/
install.ps1
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright 2022 The NATS Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#Requires -RunAsAdministrator
param(
[String]$Secret
)
$ErrorActionPreference = 'Stop'
$CurrentNightlyUrl = "https://get-nats.io/current-nightly"
$PlatformsUrl = "https://get-nats.io/synadia-nats-platforms.json"
$NATSDir = "NATS"
$OSInfo = "windows-amd64"
$Stable = "stable"
$Nightly = "nightly"
$NscTool = "nsc"
$NatsTool = "nats"
$NscExe = "nsc.exe"
$NatsExe = "nats.exe"
$NscZip = "nsc.zip"
$NatsZip = "nats.zip"
$NscEnvOperatorName = "synadia"
if ( $env:NSC_OPERATOR_NAME ) {
$NscEnvOperatorName = $env:NSC_OPERATOR_NAME
}
# ----------------------------------------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------------------------------------
Function Read-NightlyVersion() {
if (!$_currentNightly) {
$temp = [string](Invoke-WebRequest -Uri $CurrentNightlyUrl)
$_currentNightly = $temp.Split("`r?`n")[0]
}
return $_currentNightly
}
Function Format-EndWithBackslash($s) {
if ($s.EndsWith("\")){
return $s
}
return $s + "\"
}
Function Format-DoesntEndWithBackslash($s) {
if ($s.EndsWith("\")){
return $s.Substring(0, $s.Length - 1)
}
return $s
}
Function Select-Folder() {
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
$FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.RootFolder = 'MyComputer'
[void] $FolderBrowserDialog.ShowDialog()
return $FolderBrowserDialog.SelectedPath
}
Function Invoke-Backup($BinDir, $Tool, $ExePath) {
if ( Test-Path $ExePath )
{
Write-Host "Backing up existing $Tool executable..."
for($i = 1; $i -lt 100; $i++) # I give up after 99 tries
{
$nn = "$BinDir$Tool-" + (Get-Date -Format "yyyyMMdd") + "-backup$i.exe"
if (!(Test-Path $nn))
{
Rename-Item -Path $ExePath -NewName $nn
return
}
}
Write-Host "Cannot backup $ExePath, all backups exist."
Exit -2
}
}
# ----------------------------------------------------------------------------------------------------
# Execution
# ----------------------------------------------------------------------------------------------------
# They get to pick the folder given a default
$tempDir = (Format-EndWithBackslash $Env:ProgramFiles) + $NATSDir
$opt0 = New-Object System.Management.Automation.Host.ChoiceDescription "&Default Location","Default Location is $tempDir"
$opt1 = New-Object System.Management.Automation.Host.ChoiceDescription "&Choose Location","Choose your location."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($opt0, $opt1)
$result = $host.ui.PromptForChoice("Installation Location", "Where will the programs be installed? Default Location is $tempDir", $options, 0)
if ($result -eq 1) {
$tempDir = Select-Folder
if (!$tempDir) {
Write-Host "You must pick a directory. Exiting"
Exit -1
}
}
$binDir = Format-EndWithBackslash $tempDir
$binDirNoSlash = Format-DoesntEndWithBackslash $binDir
if ( !(Test-Path $binDirNoSlash) ) {
New-Item $binDirNoSlash -ItemType Directory | Out-Null
}
# some local variables now that I have $binDir
$nscExePath = $binDir + $NscExe
$natsExePath = $binDir + $NatsExe
$nscZipLocal = $binDir + $NscZip
$natsZipLocal = $binDir + $NatsZip
# $channel Have the user pick which type of channel they want, i.e. stable or nightly. Get the channel from the conf
$dataDir = (Format-EndWithBackslash $Env:LOCALAPPDATA) + $NATSDir
$dataFile = "$dataDir\install-channel.txt"
$dflt = 0
if ( Test-Path $dataFile ) {
$chn = (Get-Content -Path $dataFile).Split("`r?`n")[0]
if ($chn -eq $Nightly) {
$dflt = 1
}
}
$opt0 = New-Object System.Management.Automation.Host.ChoiceDescription "&$Stable","Latest Stable Build."
$opt1 = New-Object System.Management.Automation.Host.ChoiceDescription "&$Nightly","Current Nightly Build."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($opt0, $opt1)
$result = $host.ui.PromptForChoice("$Channel Selection", "Which channel do you want to install?", $options, $dflt)
switch ($result) {
0{$channel = $Stable}
1{$channel = $Nightly}
}
Write-Host ""
# Add bin dir to path if not already in path
Write-Host "Ensuring $binDirNoSlash is in the path..."
$Machine = [EnvironmentVariableTarget]::Machine
$Path = [Environment]::GetEnvironmentVariable('Path', $Machine)
if (!(";$Path;".ToLower() -like "*;$binDirNoSlash;*".ToLower())) {
[Environment]::SetEnvironmentVariable('Path', "$Path;$binDirNoSlash", $Machine)
$Env:Path += ";$binDirNoSlash"
}
Write-Host "Downloading platform info..."
$json = (Invoke-WebRequest https://get-nats.io/synadia-nats-platforms.json -ContentType "application/json" -UseBasicParsing) | ConvertFrom-Json
$nscZipUrl = $json.$channel.platforms.$OSInfo.tools.$NscTool.zip_url
$natsZipUrl = $json.$channel.platforms.$OSInfo.tools.$NatsTool.zip_url
if ($channel -eq $Nightly) {
$verNsc = $verNats = Read-NightlyVersion
Write-Host "$NscTool $Nightly version $verNsc"
Write-Host "$NatsTool $Nightly version $verNats"
$nscZipUrl = $nscZipUrl.Replace("%NIGHTLY%", $verNsc)
$natsZipUrl = $natsZipUrl.Replace("%NIGHTLY%", $verNats)
}
else {
$verNsc = $json.$channel.platforms.$OSInfo.tools.$NscTool."version_tag"
$verNats = $json.$channel.platforms.$OSInfo.tools.$NatsTool."version_tag"
Write-Host "$NscTool $Stable version $verNsc"
Write-Host "$NatsTool $Stable version $verNats"
}
# Download the zip files
Write-Host "Downloading archive $nscZipUrl..."
Invoke-WebRequest -Uri $nscZipUrl -OutFile $nscZipLocal -UseBasicParsing
Write-Host "Downloading archive $natsZipUrl..."
Invoke-WebRequest -Uri $natsZipUrl -OutFile $natsZipLocal -UseBasicParsing
# Backup existing versions now that the downloads worked
Invoke-Backup $binDir $NscTool $nscExePath
Invoke-Backup $binDir $NatsTool $natsExePath
# nsc: Unzip, Remove Archive
Write-Host "Installing $NscTool..."
Expand-Archive -Path $nscZipLocal -DestinationPath $binDir
Remove-Item $nscZipLocal
# nats: Unzip, stable:(move exe from folder then remove folder), Remove Archive
Write-Host "Installing $NatsTool..."
Expand-Archive -Path $natsZipLocal -DestinationPath $binDir -Force
if ($channel -eq $Stable) {
$bareVersion = $json.$channel.platforms.$OSInfo.tools.$NatsTool."version_bare"
$natsZipFolderLocal = "$binDir$NatsTool-$bareVersion-$OSInfo"
Move-Item -Path "$natsZipFolderLocal\$NatsExe" -Destination "$binDir$NatsExe"
Remove-Item "$natsZipFolderLocal\*"
Remove-Item $natsZipFolderLocal
}
Remove-Item $natsZipLocal
# write the install-channel.txt file
Write-Host "Remembering the installed channel choice..."
if ( !(Test-Path $dataDir) ) {
New-Item $dataDir -ItemType Directory | Out-Null
}
"$channel" | Out-File -FilePath $dataFile
if ( $Secret ) {
nsc load --profile "nsc://${NscEnvOperatorName}?secret=$Secret"
nats context ls
}
Write-Host "Done!`r`n"