Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated CLI installation script #69

Merged
merged 7 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 0 additions & 193 deletions cli/installers/install-drasi-cli-github.sh

This file was deleted.

150 changes: 150 additions & 0 deletions cli/installers/install-drasi-cli.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
param (
[string]$Version,
[string]$DrasiRoot = "$env:LOCALAPPDATA\drasi"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Makefile install puts it in $env:ProgramFiles\drasi

)

Write-Output ""
$ErrorActionPreference = 'stop'

$DrasiRoot = $DrasiRoot -replace ' ', '` '

$DrasiCliFileName = "drasi.exe"
$DrasiCliFilePath = "${DrasiRoot}\${DrasiCliFileName}"
$OS = "windows"
$Arch = "x64"
$GitHubOrg = "drasi-project"
$GitHubRepo = "drasi-platform"
$GitHubReleaseJsonUrl = "https://api.github.com/repos/${GitHubOrg}/${GitHubRepo}/releases"


function GetVersionInfo {
param (
[string]$Version,
$Releases
)
# Filter windows binary and download archive
if (!$Version) {
$release = $Releases | Where-Object { $_.tag_name -notlike "*rc*" } | Select-Object -First 1
}
else {
$release = $Releases | Where-Object { $_.tag_name -eq "v$Version" } | Select-Object -First 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this always return the latest release?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think the github api lists releases with the most recent release at the top. This will fetch the first (most recent) release

}

return $release
}


function GetWindowsAsset {
param (
$Release
)
$windowsAsset = $Release | Select-Object -ExpandProperty assets | Where-Object { $_.name -Like "*${OS}-${Arch}.exe" }
if (!$windowsAsset) {
throw "Cannot find the Windows drasi CLI binary"
}
[hashtable]$return = @{}
$return.url = $windowsAsset.url
$return.name = $windowsAsset.name

return $return
}


if ($Env:GITHUB_USER) {
$basicAuth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($Env:GITHUB_USER + ":" + $Env:GITHUB_TOKEN));
$githubHeader = @{"Authorization" = "Basic $basicAuth" }
ruokun-niu marked this conversation as resolved.
Show resolved Hide resolved
}
else {
$githubHeader = @{}
}

if ((Get-ExecutionPolicy) -gt 'RemoteSigned' -or (Get-ExecutionPolicy) -eq 'ByPass') {
Write-Output "PowerShell requires an execution policy of 'RemoteSigned'."
Write-Output "To make this change please run:"
Write-Output "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"

Check warning

Code scanning / devskim

The Set-ExecutionPolicy cmdlet has been used to dynamically change the permissions available to PowerShell. Warning

Do not set a custom execution policy.
break
}

# Change security protocol to support TLS 1.2 / 1.1 / 1.0 - old powershell uses TLS 1.0 as a default protocol
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"

Check notice

Code scanning / devskim

Generic: Hard-coded SSL/TLS Protocol Note

Generic: Hard-coded SSL/TLS Protocol

Check notice

Code scanning / devskim

Generic: Hard-coded SSL/TLS Protocol Note

Generic: Hard-coded SSL/TLS Protocol

if (Test-Path $DrasiCliFilePath -PathType Leaf) {
Write-Output "Previous Drasi CLI detected"
Write-Output "Reinstalling Drasi CLI..."
}
else {
Write-Output "Installing Drasi CLI..."
}

if (-Not (Test-Path $DrasiRoot -PathType Container)) {
Write-Output "Creating $DrasiRoot directory..."
New-Item -ErrorAction Ignore -Path $DrasiRoot -ItemType "directory" | Out-Null
if (!(Test-Path $DrasiRoot -PathType Container)) {
throw "Cannot create $DrasiRoot"
}
}

$releases = Invoke-RestMethod -Uri $GitHubReleaseJsonUrl -Headers $githubHeader -Method Get
if ($releases.Count -eq 0) {
throw "No releases from github.com/${GitHubOrg}/${GitHubRepo}"
}

$release = GetVersionInfo -Version $Version -Releases $releases
if (!$release) {
throw "Cannot find the specified drasi CLI binary version"
}

$asset = GetWindowsAsset -Release $release
$assetName = $asset.name
$exeFileUrl = $asset.url
$exeFilePath = "${DrasiRoot}\${assetName}"

try {
Write-Output "Downloading $exeFileUrl"
githubHeader.Accept = "application/octet-stream"
$oldProgressPreference = $ProgressPreference
$ProgressPreference = "SilentlyContinue" # Do not show progress bar
Invoke-WebRequest -Headers $githubHeader -Uri $exeFileUrl -OutFile $exeFilePath
}
catch [Net.WebException] {
throw "ERROR: The specified release version: $Version does not exist."
}
finally {
$ProgressPreference = $oldProgressPreference;
}

if (!(Test-Path $exeFilePath -PathType Leaf)) {
throw "Failed to download drasi CLI binary - $exeFilePath"
}

# Remove old drasi CLI if exists
if (Test-Path $DrasiCliFilePath -PathType Leaf) {
Remove-Item -Recurse -Force $DrasiCliFilePath
}

# Rename the downloaded drasi CLI binary
Rename-Item -Path $exeFilePath -NewName $DrasiCliFilePath -Force

if (!(Test-Path $DrasiCliFilePath -PathType Leaf)) {
throw "Failed to download drasi CLI binary - $exeFilePath"
}

# Version string
Write-Output "drasi CLI version: $(&$DrasiCliFilePath version -o json | ConvertFrom-JSON | Select-Object -ExpandProperty version)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this works drasi?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right. Maybe we can just output the release tag/version that was used in the download



$UserPathEnvironmentVar = (Get-Item -Path HKCU:\Environment).GetValue(
'PATH', # the registry-value name
$null, # the default value to return if no such value exists.
'DoNotExpandEnvironmentNames' # the option that suppresses expansion
)

if (-Not ($UserPathEnvironmentVar -like '*drasi*')) {
Write-Output "Adding $DrasiRoot to User Path..."
# [Environment]::SetEnvironmentVariable sets the value kind as REG_SZ, use the function below to set a value of kind REG_EXPAND_SZ
Set-ItemProperty HKCU:\Environment "PATH" "$UserPathEnvironmentVar;$DrasiRoot" -Type ExpandString
# Also add the path to the current session
$env:PATH += ";$DrasiRoot"
}

Write-Output "drasi CLI has been successfully installed"
Loading
Loading