@@ -1532,6 +1532,38 @@ function Restart-LoggingAgent {
1532
1532
Start-Service StackdriverLogging
1533
1533
}
1534
1534
1535
+ # Check whether the logging agent is installed by whether it's registered as service
1536
+ function IsLoggingAgentInstalled {
1537
+ $stackdriver_status = (Get-Service StackdriverLogging - ErrorAction Ignore).Status
1538
+ return -not [string ]::IsNullOrEmpty($stackdriver_status )
1539
+ }
1540
+
1541
+ # Clean up the logging agent's registry key and root folder if they exist from a prior installation.
1542
+ # Try to uninstall it first, if it failed, remove the registry key at least,
1543
+ # as the registry key will block the silent installation later on.
1544
+ function Cleanup-LoggingAgent {
1545
+ # For 64 bits app, the registry path is 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
1546
+ # for 32 bits app, it's 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
1547
+ # StackdriverLogging is installed as 32 bits app
1548
+ $x32_app_reg = ' HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
1549
+ $uninstall_string = (Get-ChildItem $x32_app_reg | Get-ItemProperty | Where-Object {$_.DisplayName -match " Stackdriver" }).UninstallString
1550
+ if (-not [string ]::IsNullOrEmpty($uninstall_string )) {
1551
+ try {
1552
+ Start-Process - FilePath " $uninstall_string " - ArgumentList " /S" - Wait
1553
+ } catch {
1554
+ Log- Output " Exception happens during uninstall logging agent, so remove the registry key at least"
1555
+ Remove-Item - Path " $x32_app_reg \GoogleStackdriverLoggingAgent\"
1556
+ }
1557
+ }
1558
+
1559
+ # If we chose reboot after uninstallation, the root folder would be clean.
1560
+ # But since we couldn't reboot, so some files & folders would be left there,
1561
+ # which could block the re-installation later on, so clean it up
1562
+ if (Test-Path $STACKDRIVER_ROOT ){
1563
+ Remove-Item - Force - Recurse $STACKDRIVER_ROOT
1564
+ }
1565
+ }
1566
+
1535
1567
# Installs the Stackdriver logging agent according to
1536
1568
# https://cloud.google.com/logging/docs/agent/installation.
1537
1569
# TODO(yujuhong): Update to a newer Stackdriver agent once it is released to
@@ -1548,17 +1580,18 @@ function Install-LoggingAgent {
1548
1580
(" $STACKDRIVER_ROOT \LoggingAgent\Main\pos\winevtlog.pos\worker0\" +
1549
1581
" storage.json" )
1550
1582
1551
- if (Test-Path $STACKDRIVER_ROOT ) {
1583
+ if (IsLoggingAgentInstalled ) {
1552
1584
# Note: we should reinstall the Stackdriver agent if $REDO_STEPS is true
1553
1585
# here, but we don't know how to run the installer without it prompting
1554
1586
# when Stackdriver is already installed. We dumped the strings in the
1555
1587
# installer binary and searched for flags to do this but found nothing. Oh
1556
1588
# well.
1557
- Log- Output (" Skip: $STACKDRIVER_ROOT is already present, assuming that " +
1558
- " Stackdriver logging agent is already installed" )
1559
- Restart-LoggingAgent
1589
+ Log- Output (" Skip: Stackdriver logging agent is already installed" )
1560
1590
return
1561
1591
}
1592
+
1593
+ # After a crash, the StackdriverLogging service could be missing, but its files will still be present
1594
+ Cleanup- LoggingAgent
1562
1595
1563
1596
$url = (" https://storage.googleapis.com/gke-release/winnode/stackdriver/" +
1564
1597
" StackdriverLogging-${STACKDRIVER_VERSION} .exe" )
0 commit comments