diff --git a/packaging/googet/agent_install.ps1 b/packaging/googet/agent_install.ps1 index 5909a2a5..b4938a2f 100644 --- a/packaging/googet/agent_install.ps1 +++ b/packaging/googet/agent_install.ps1 @@ -16,6 +16,12 @@ $name = 'GCEAgent' $path = '"C:\Program Files\Google\Compute Engine\agent\GCEWindowsAgent.exe"' $display_name = 'Google Compute Engine Agent' $description = 'Google Compute Engine Agent' + +$manager_name = 'GCEAgentManager' +$manager_path = '"C:\Program Files\Google\Compute Engine\agent\GCEWindowsAgentManager.exe"' +$manager_display_name = 'Google Compute Engine Agent Manager' +$manager_description = 'Google Compute Engine Agent Manager' + $initial_config = @' # GCE Instance Configuration @@ -29,31 +35,49 @@ $initial_config = @' # disable=false '@ -function Set-ServiceConfig { +function Set-ServiceConfig($service_name, $service_binary) { # Restart service after 1s, then 2s. Reset error counter after 60s. - sc.exe failure $name reset= 60 actions= restart/1000/restart/2000 + sc.exe failure $service_name reset= 60 actions= restart/1000/restart/2000 # Set dependency and delayed start - cmd.exe /c "sc.exe config ${name} depend= `"samss`" start= delayed-auto binpath= \`"${path}\`"" + cmd.exe /c "sc.exe config ${service_name} depend= `"samss`" start= delayed-auto binpath= \`"${service_binary}\`"" # Create trigger to start the service on first IP address - sc.exe triggerinfo $name start/networkon + sc.exe triggerinfo $service_name start/networkon } -try { - - if (-not (Get-Service $name -ErrorAction SilentlyContinue)) { - New-Service -Name $name ` - -DisplayName $display_name ` - -BinaryPathName $path ` +function Set-New-Service($service_name, $service_display_name, $service_desc, $service_binary) { + if (-not (Get-Service $service_name -ErrorAction SilentlyContinue)) { + New-Service -Name $service_name ` + -DisplayName $service_display_name ` + -BinaryPathName $service_binary ` -StartupType Automatic ` - -Description $description + -Description $service_desc } else { - Set-Service -Name $name ` - -DisplayName $display_name ` - -Description $description + Set-Service -Name $service_name ` + -DisplayName $service_display_name ` + -Description $service_desc } +} - Set-ServiceConfig +try { + # This is to safeguard from installing agent manager using placeholder file + $install_manager = $false + if (Test-Path ($manager_path -replace '"', "")) { + $contains = Select-String -Path ($manager_path -replace '"', "") -Pattern "This is a placeholder file" + if ($contains -eq $null) { + $install_manager = $true + } + } + + # Guest Agent service + Set-New-Service $name $display_name $description $path + Set-ServiceConfig $name $path + + # Guest Agent Manager service + if ($install_manager) { + Set-New-Service $manager_name $manager_display_name $manager_description $manager_path + Set-ServiceConfig $manager_name $manager_path + } $config = "${env:ProgramFiles}\Google\Compute Engine\instance_configs.cfg" if (-not (Test-Path $config)) { @@ -61,9 +85,13 @@ try { } Restart-Service $name -Verbose + + if ($install_manager) { + Restart-Service $manager_name -Verbose + } } catch { Write-Output $_.InvocationInfo.PositionMessage Write-Output "Install failed: $($_.Exception.Message)" exit 1 -} +} \ No newline at end of file diff --git a/packaging/googet/agent_uninstall.ps1 b/packaging/googet/agent_uninstall.ps1 index 6049e4e3..c45e3816 100644 --- a/packaging/googet/agent_uninstall.ps1 +++ b/packaging/googet/agent_uninstall.ps1 @@ -14,3 +14,9 @@ Stop-Service GCEAgent -Verbose & sc.exe delete GCEAgent + +$name = 'GCEAgentManager' +if (Get-Service $name -ErrorAction SilentlyContinue) { + Stop-Service $name -Verbose + & sc.exe delete $name +} \ No newline at end of file diff --git a/packaging/googet/google-compute-engine-windows.goospec b/packaging/googet/google-compute-engine-windows.goospec index 8d9e54d7..e1f36dcb 100644 --- a/packaging/googet/google-compute-engine-windows.goospec +++ b/packaging/googet/google-compute-engine-windows.goospec @@ -7,6 +7,7 @@ "description": "Google Compute Engine Windows agent", "source": "https://github.com/GoogleCloudPlatform/guest-agent/", "files": { + "GCEWindowsAgentManager.exe": "/Google/Compute Engine/agent/GCEWindowsAgentManager.exe", "GCEWindowsAgent.exe": "/Google/Compute Engine/agent/GCEWindowsAgent.exe", "GCEAuthorizedKeysCommand.exe": "/Google/Compute Engine/agent/GCEAuthorizedKeysCommand.exe", "THIRD_PARTY_LICENSES": "/Google/Compute Engine/THIRD_PARTY_LICENSES/", @@ -36,6 +37,7 @@ "sources": [{ "include": [ "GCEWindowsAgent.exe", + "GCEWindowsAgentManager.exe", "GCEAuthorizedKeysCommand.exe", "packaging/googet/agent_install.ps1", "packaging/googet/agent_uninstall.ps1", diff --git a/packaging/googet/windows_agent_build.sh b/packaging/googet/windows_agent_build.sh index a5e9520a..ec309121 100755 --- a/packaging/googet/windows_agent_build.sh +++ b/packaging/googet/windows_agent_build.sh @@ -22,3 +22,21 @@ version=$1 GOOS=windows /tmp/go/bin/go build -ldflags "-X main.version=$version" -mod=readonly -o GCEWindowsAgent.exe ./google_guest_agent GOOS=windows /tmp/go/bin/go build -ldflags "-X main.version=$version" -mod=readonly -o GCEAuthorizedKeysCommand.exe ./google_authorized_keys + +# Script expects guest-agent and google-guest-agent codebase are placed +# side-by-side within same directory and this script is executed from root of +# guest-agent codebase. +GUEST_AGENT_REPO="../google-guest-agent" + +if [[ ! -f "$GUEST_AGENT_REPO/Makefile" ]]; then + # This is a placeholder file for guest-agent package, google-compute-engine-windows.goospec + # looks for this file during goopack packaging and will fail if not found. + echo "This is a placeholder file so guest agent package build without error. Package will have actual Guest Agent Manager executable instead if both repos are cloned side-by-side." > GCEWindowsAgentManager.exe + exit 0 +fi + +BUILD_DIR=$(pwd) +pushd $GUEST_AGENT_REPO +GOOS=windows VERSION=$version make cmd/google_guest_agent/google_guest_agent +cp cmd/google_guest_agent/google_guest_agent.exe $BUILD_DIR/GCEWindowsAgentManager.exe +popd