-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
370 lines (294 loc) · 11 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env pwsh
[cmdletbinding()]
Param(
# installation path
[string] $InstallPath = "~/bin",
# User home folder, where the go-c8y-cli-addons will be installed
[string] $UserHome = "~",
# go-c8y-cli version to install
[string] $Version = "latest",
# Path to profile which should be edited to add the custom location to it
[string] $ProfilePath = $PROFILE,
# Skip the version check even if c8y is already installed
[switch] $SkipVersionCheck
)
<#
.EXAMPLE
./install.ps1
Install go-c8y-cli in the default location. Only install if the current version does not match the latest
.EXAMPLE
./install.ps1 -SkipVersionCheck
Install go-c8y-cli in the default location and ignore any version checking (even if you already have the latest version, it will still be downloaded again)
.EXAMPLE
./install.ps1 -InstallPath /opt/tools/cumulocity -UserHome /opt/tools/cumulocity -SkipVersionCheck
Install go-c8y-cli and addons to a shared location accessible by multiple users
.EXAMPLE
./install.ps1 -InstallPath C:\tools\cumulocity -UserHome C:\tools\cumulocity -SkipVersionCheck
Force re-installation of go-c8y-cli and addons to a shared location. go-c8y-cli will be installed again even the binary already exists.
.EXAMPLE
powershell -executionPolicy bypass C:\Users\myuser\.go-c8y-cli\install.ps1 -InstallPath C:\tools\cumulocity -UserHome C:\tools\cumulocity -SkipVersionCheck -ProfilePath $PROFILE.AllUsersAllHosts
Install/re-install go-c8y-cli and edit the global powershell profile for all users. This needs to be run as admin (and use a full path to the install.ps1 script)
#>
$UserHome = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($UserHome)
# Expand install path (but it might not yet exist)
$InstallPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($InstallPath)
$OWNER = "reubenmiller"
$REPO = "go-c8y-cli"
$AddonRepo = "go-c8y-cli-addons"
Function Invoke-CheckoutAddons {
[cmdletbinding()]
Param()
$Destination = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$UserHome/.go-c8y-cli")
if (Test-Path $Destination) {
Write-Verbose "Addon repository has already been cloned to $Destination"
return
}
if (Get-Command git -ErrorAction SilentlyContinue) {
Write-Host "Cloning $AddonRepo repository"
git clone "https://github.com/$OWNER/${AddonRepo}.git" $Destination
} else {
# TODO: Manually download addons
# Invoke-WebRequest "https://github.com/reubenmiller/go-c8y-cli-addons/archive/refs/heads/main.zip"
Write-Warning "Could not find git. Please install git if you want to be able to install the addons"
}
}
Function Get-CPUArchitecture() {
if ([Environment]::Is64BitOperatingSystem) {
"amd64"
} else {
"386"
}
}
Function Get-OSVersion {
if ($IsMacOS) {
"macOS"
} elseif ($IsLinux) {
"linux"
} else {
"windows"
}
}
Function Get-CurrentVersion {
if (Get-Command "c8y" -ErrorAction SilentlyContinue) {
$current_version = & c8y version --select version --output csv 2> $null
if ([string]::IsNullOrEmpty($current_version)) {
return
}
"v" + "$current_version".Trim()
}
}
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
Function Invoke-DownloadC8yBinary {
[cmdletbinding()]
Param(
[string] $BaseURL,
[string] $Version,
[string] $Tag
)
$os = Get-OSVersion
$arch = Get-CPUArchitecture
if ($InstallPath -and -Not (Test-Path $InstallPath)) {
$null = New-Item -ItemType Directory -Path $InstallPath
}
$binaryName = "c8y"
$Version = $Version -replace "^v", ""
$package = "c8y_${Version}_${os}_${arch}"
$archive = "${package}.tar.gz"
if ($os -eq "windows") {
$archive = "${package}.zip"
$binaryName = "c8y.exe"
}
$tmp = [system.io.path]::GetTempPath()
$DownloadedFile = Join-Path -path $tmp -ChildPath $archive
Invoke-DownloadAsset -Tag $Tag -FileName $archive -OutFile $DownloadedFile
if ($DownloadedFile -match ".zip") {
$tmp = New-TemporaryDirectory
Microsoft.PowerShell.Archive\Expand-Archive -Path $DownloadedFile -DestinationPath "$tmp/$package"
Write-Host "Installing c8y to $InstallPath"
Copy-Item "$tmp/$package/bin/c8y*" -Destination "$InstallPath/"
} else {
if (Get-Command "tar" -ErrorAction SilentlyContinue) {
tar zxf "$tmp/$archive" -C "$tmp"
Write-Host "Installing c8y to $InstallPath"
Copy-Item "$tmp/$package/bin/c8y*" -Destination "$InstallPath/"
} else {
Write-Error "Could not find tar and it is required to extract archive"
}
}
Remove-Item -Path "$tmp/$package" -Recurse -Force
if ($IsMacOS -or $IsLinux) {
chmod a+x $InstallPath/$BinaryName
}
}
Function Invoke-DownloadAsset {
[cmdletbinding()]
Param(
[string] $Tag,
[string] $FileName,
[string] $OutFile
)
$options = @{
Uri = "https://api.github.com/repos/$OWNER/$REPO/releases/tags/$tag"
Headers = @{
Accept = "application/vnd.github.v3+json"
}
}
if (![string]::IsNullOrWhiteSpace($env:CURL_AUTH_HEADER)) {
$options.Headers["Authorization"] = "Bearer $GITHUB_TOKEN"
}
$release_info = Invoke-RestMethod @options
$asset = $release_info.assets `
| Where-Object { $_.name -eq $FileName } `
| Select-Object -First 1
if ($null -eq $asset) {
Write-Warning "Could not find download artifact"
return
}
$options = @{
# Uri = "https://api.github.com/repos/$OWNER/$REPO/releases/assets/$assetId"
Uri = $asset.browser_download_url
Headers = @{
Accept = "application/octet-stream"
}
OutFile = $OutFile
}
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest @options
} catch {
} finally {
$ProgressPreference = 'Continue'
}
}
Function install-c8ybinary () {
[cmdletbinding()]
Param(
[string] $Version,
[switch] $SkipVersionCheck
)
$ReleaseInfo = Get-LatestTag
if ($Version -eq "latest") {
$Version = $ReleaseInfo.Tag
}
$Tag = $ReleaseInfo.Tag
$ReleaseBaseURL = "https://github.com/$OWNER/$REPO/releases/download/$Version"
if ($ReleaseInfo.BinaryBaseUrl) {
$ReleaseBaseURL = $ReleaseInfo.BinaryBaseUrl
}
if ($Version -ne $Tag) {
Write-Host "Latest version: $Version (tag=$Tag)"
} else {
Write-Host "Latest version: $Tag"
}
$CurrentVersion = Get-CurrentVersion
if (-Not $SkipVersionCheck -and $CurrentVersion -eq $Version) {
Write-Host "c8y is already up to date: $Version" -ForegroundColor Green
return
}
if ($CurrentVersion) {
Write-Host "Updating from $CurrentVersion to $Version"
} else {
Write-Host "Installing $Version"
}
Invoke-DownloadC8yBinary -BaseURL $ReleaseBaseURL -Version $Version -Tag $Tag
if ($env:PATH -notlike "*${InstallPath}*") {
if ($IsLinux -or $IsMacOS) {
$env:PATH = $InstallPath + ":" + $env:PATH
} else {
$env:PATH = $InstallPath + ";" + $env:PATH
}
# $WarningMessage = "`n"
# $PathStatement = if ($IsMacOS -or $IsLinux) {
# "export PATH=${InstallPath}:`$env:PATH"
# } else {
# "`$env:PATH = `"${InstallPath};`$env:PATH`""
# }
# $WarningMessage += "`nThe PATH variable (`$PATH) is missing the install directory: $InstallPath`n`nPlease add it using`n`n $PathStatement`n`n"
# Write-Warning $WarningMessage
}
if (-Not (Get-Command "c8y" -ErrorAction SilentlyContinue)) {
if ($IsLinux -or $IsMacOS) {
$env:PATH = $InstallPath + ":" + $env:PATH
} else {
$env:PATH = $InstallPath + ";" + $env:PATH
}
}
# show new version
& $InstallPath/c8y version
}
Function Get-LatestTag () {
[cmdletbinding()]
Param()
$options = @{
Uri = "https://api.github.com/repos/$OWNER/$REPO/releases"
Headers = @{
Accept = "application/vnd.github.v3+json"
}
}
if (![string]::IsNullOrWhiteSpace($env:CURL_AUTH_HEADER)) {
$options.Headers["Authorization"] = "Bearer $GITHUB_TOKEN"
}
$resp = Invoke-RestMethod @options
$TagName = $resp[0].tag_name
$BinaryName = Split-Path $resp[0].assets[0].browser_download_url -Leaf
$BinaryBaseUrl = $resp[0].assets[0].browser_download_url -replace "\/[^\/]+$", ""
New-Object pscustomobject -Property @{
Tag = $TagName
BinaryName = $BinaryName
BinaryBaseUrl = $BinaryBaseUrl
}
}
Function Add-ToProfile {
[cmdletbinding()]
Param()
$ProfileDir = Split-Path $ProfilePath -Parent
if ($ProfileDir -and -Not (Test-Path $ProfileDir)) {
Write-Verbose "Creating powershell profile directory"
$null = New-Item -Path $ProfileDir -ItemType Directory
}
if (-Not (Test-Path $ProfilePath)) {
"" | Out-File -FilePath $ProfilePath
}
$PathStatement = if ($IsMacOS -or $IsLinux) {
"`$env:PATH = `"${InstallPath}:`$env:PATH`""
} else {
"`$env:PATH = `"${InstallPath};`$env:PATH`""
}
$CustomSessionHome = Join-Path $UserHome -ChildPath ".cumulocity"
if (Test-Path $CustomSessionHome) {
if (-Not (Select-String -Path $ProfilePath -Pattern "C8Y_SESSION_HOME" -Quiet)) {
Write-Verbose "Adding detected .cumulocity session home to profile"
Add-Content -Path $ProfilePath -Value "`$env:C8Y_SESSION_HOME = '$CustomSessionHome'`n"
# Also set it now so it is available now
$env:C8Y_SESSION_HOME = "$CustomSessionHome"
}
}
$ImportSnippet = @(
$PathStatement,
". `"$UserHome/.go-c8y-cli/shell/c8y.plugin.ps1`""
)
if (-Not (Select-String -Path $ProfilePath -SimpleMatch -Pattern "env:C8Y_HOME" -Quiet)) {
$CustomSettings = @(
"`$env:C8Y_HOME = `"$UserHome/.go-c8y-cli`"",
"`$env:C8Y_SESSION_HOME = `"$UserHome/.cumulocity`""
)
Write-Host "Adding custom settings to profile: $ProfilePath"
Add-Content -Path $ProfilePath -Value (($CustomSettings -join "`n") + "`n")
}
if (-Not (Select-String -Path $ProfilePath -SimpleMatch -Pattern "$UserHome/.go-c8y-cli/shell/c8y.plugin.ps1" -Quiet)) {
Write-Host "Adding imports to profile: $ProfilePath"
Add-Content -Path $ProfilePath -Value (($ImportSnippet -join "`n") + "`n")
}
# Importing script (for immediate usage)
. "$UserHome/.go-c8y-cli/shell/c8y.plugin.ps1"
}
#
# Main
#
Invoke-CheckoutAddons
install-c8ybinary -Version $Version -SkipVersionCheck:$SkipVersionCheck
Add-ToProfile