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