Skip to content

Commit

Permalink
Improve package and update handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fvanroie committed May 20, 2018
1 parent 326e755 commit 0c933b4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
24 changes: 14 additions & 10 deletions Public/Core/Firmware/Get-OPNsenseUpdateStatus.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
Function Get-OPNsenseUpdateStatus {
[CmdletBinding()]
Param(
[String]$Message = 'Busy',
[String]$Title = 'Busy',
[String]$Status = 'This can take a while',
[double]$Seconds = 0.8
)

Expand All @@ -38,8 +39,8 @@ Function Get-OPNsenseUpdateStatus {
$msg = 'One moment please'

$ProgressSplat = @{
'Activity' = $message
'Status' = 'This can take a while'
'Activity' = $Title
'Status' = $Status
'CurrentOperation' = 'One moment please'
}

Expand All @@ -49,13 +50,16 @@ Function Get-OPNsenseUpdateStatus {

Do {
try {
# TO DO : Check why sometimes Invoke-RestMethod takes 110 seconds to complete
Write-Verbose 'Getting Firmware Upgrade Status ...'
$result = Invoke-OPNsenseCommand core firmware upgradestatus -Verbose:$false
$retries += 0
}
catch {
Write-Verbose ('Firmware Upgrade Status : {0}' -f $result.status)
$retries = 0
} catch {
$retries += 1
if ($retries -gt 4) {
break
Write-Warning "Retries : $reties"
break
}
}

Expand All @@ -64,7 +68,7 @@ Function Get-OPNsenseUpdateStatus {
$log = $result.log.substring($start)
$lines = $log.Split("`n")

if ($elipsis -in '',' '){
if ($elipsis -in '', ' ') {
$elipsis += ' '
} else {
$elipsis = $elipsis.trim() + '.'
Expand All @@ -83,12 +87,12 @@ Function Get-OPNsenseUpdateStatus {
}

# Check for progress marker [xx/yy] Completed...
if ($line -match '\[([0-9]+)/([0-9]+)\] (.*)\.+\. ( done)?') {
if ($line -match '\[([0-9]+)/([0-9]+)\] (.*)\.+\.\. ?( done)?') {
# Save matches because the next check overwrites it
$m = $matches

# Check for progress marker is: [xx/yy] Extracting or Deleting: ... (skip these lines as they are duplicates)
if ($line -match '\[([0-9]+)/([0-9]+)\] (Extracting|Deleting) .*: \.\.') {
if ($line -match '\[([0-9]+)/([0-9]+)\] (Extracting|Deleting) .*: \.\.\.') {
continue # without updating Progress
}

Expand Down
2 changes: 1 addition & 1 deletion Public/Core/Firmware/Invoke-OPNsenseAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Function Invoke-OPNsenseAudit {
$result = Invoke-OPNsenseCommand core firmware audit -Form audit -Verbose:$VerbosePreference

if ($result.status -eq 'ok') {
$log = Get-OPNsenseUpdateStatus -Message "Running Audit in OPNsense:" -Verbose:$VerbosePreference
$log = Get-OPNsenseUpdateStatus -Title "Running Audit in OPNsense:" -Verbose:$VerbosePreference

# Raw Output
if ([bool]::Parse($Raw)) { Return $log }
Expand Down
2 changes: 1 addition & 1 deletion Public/Core/Firmware/Update-OPNsenseFirmware.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Function Update-OPNsenseFirmware {
if ($pscmdlet.ShouldProcess($MyInvocation.MyCommand.Module.PrivateData['OPNsenseApi'])) {
$result = Invoke-OPNsenseCommand core firmware upgrade -Form 'upgrade' -Verbose:$VerbosePreference
if ($result.status -eq 'ok') {
return Get-OPNsenseUpdateStatus -Message "Updating OPNsense:" -Verbose:$VerbosePreference
return Get-OPNsenseUpdateStatus -Title "Update OPNsense" -Verbose:$VerbosePreference
}
return $result
}
Expand Down
12 changes: 7 additions & 5 deletions Public/Core/Packages/Install-OPNsensePackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,29 @@ Function Install-OPNsensePackage {
$results = @()
}
PROCESS {
$thispkg = $pkg | Where-Object { $_.Name -eq $Name }
foreach ($pkgname in $Name) {
$thispkg = $pkg | Where-Object { $_.Name -eq $pkgname }
If ($thispkg.installed -eq 1) {
If (-Not [bool]::Parse($Force)) {
Write-Warning ($thispkg.Name + " is already installed. Use -Force to reinstall the package.")
$status = $null
} else {
$status = Invoke-OPNsenseCommand core firmware "reinstall/$Name" -Form reinstall -addProperty @{ name = $Name.tolower()}
$status = Invoke-OPNsenseCommand core firmware "reinstall/$pkgname" -Form reinstall -addProperty @{ name = $pkgname.tolower()}
}
} else {
$status = Invoke-OPNsenseCommand core firmware "install/$Name" -Form install -addProperty @{ name = $Name.tolower()}
$status = Invoke-OPNsenseCommand core firmware "install/$pkgname" -Form install -addProperty @{ name = $pkgname.tolower()}
}

# Check installation progress
if ($status) {
if ($status.status -eq 'ok') {
$result = Get-OPNsenseUpdateStatus
$result | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Name.tolower()
$result = Get-OPNsenseUpdateStatus -Title "Install OPNsense Package" -Status $pkgname
$result | Add-Member -MemberType NoteProperty -Name 'Name' -Value $pkgname.tolower()
$results += $result
}
}
}
}
END {
return $results
}
Expand Down
10 changes: 6 additions & 4 deletions Public/Core/Packages/Uninstall-OPNsensePackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ Function Uninstall-OPNsensePackage {
$results = @()
}
PROCESS {
$thispkg = $pkg | Where-Object { $_.Name -eq $Name }
foreach ($pkgname in $Name) {
$thispkg = $pkg | Where-Object { $_.Name -eq $pkgname }
If ($thispkg.installed -eq 0) {
Write-Warning ($thispkg.Name + " is not installed and cannot be removed.")
$status = $null
} else {
$status = Invoke-OPNsenseCommand core firmware "remove/$Name" -Form remove -addProperty @{ name = $Name.tolower()}
$status = Invoke-OPNsenseCommand core firmware "remove/$pkgname" -Form remove -addProperty @{ name = $pkgname.tolower()}
if ($status.status -eq 'ok') {
$result = Get-OPNsenseUpdateStatus
$result | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Name.tolower()
$result = Get-OPNsenseUpdateStatus -Title "Uninstall OPNsense Package" -Status $pkgname
$result | Add-Member -MemberType NoteProperty -Name 'Name' -Value $pkgname.tolower()
$results += $result
}
}
}
}
END {
return $results
}
Expand Down
2 changes: 1 addition & 1 deletion Public/Items/Switch-OPNsenseItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
SOFTWARE.
#>

Function Disable-OPNsenseItem {
Function Switch-OPNsenseItem {
# .EXTERNALHELP ../PS_OPNsense.psd1-Help.xml
[CmdletBinding(
SupportsShouldProcess = $true,
Expand Down

0 comments on commit 0c933b4

Please sign in to comment.