Skip to content

Commit

Permalink
Add support for including agent manager in guest-agent package (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaitanyaKulkarni28 authored Oct 18, 2024
1 parent 00636a7 commit b57b780
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
60 changes: 44 additions & 16 deletions packaging/googet/agent_install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,41 +35,63 @@ $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)) {
$initial_config | Set-Content -Path $config -Encoding ASCII
}

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
}
}
6 changes: 6 additions & 0 deletions packaging/googet/agent_uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions packaging/googet/google-compute-engine-windows.goospec
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"description": "Google Compute Engine Windows agent",
"source": "https://github.com/GoogleCloudPlatform/guest-agent/",
"files": {
"GCEWindowsAgentManager.exe": "<ProgramFiles>/Google/Compute Engine/agent/GCEWindowsAgentManager.exe",
"GCEWindowsAgent.exe": "<ProgramFiles>/Google/Compute Engine/agent/GCEWindowsAgent.exe",
"GCEAuthorizedKeysCommand.exe": "<ProgramFiles>/Google/Compute Engine/agent/GCEAuthorizedKeysCommand.exe",
"THIRD_PARTY_LICENSES": "<ProgramFiles>/Google/Compute Engine/THIRD_PARTY_LICENSES/",
Expand Down Expand Up @@ -36,6 +37,7 @@
"sources": [{
"include": [
"GCEWindowsAgent.exe",
"GCEWindowsAgentManager.exe",
"GCEAuthorizedKeysCommand.exe",
"packaging/googet/agent_install.ps1",
"packaging/googet/agent_uninstall.ps1",
Expand Down
18 changes: 18 additions & 0 deletions packaging/googet/windows_agent_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b57b780

Please sign in to comment.