Skip to content

Commit

Permalink
Bring back side-by-side packaging (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhli authored Nov 5, 2024
1 parent 44346bb commit 13c627c
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 18 deletions.
18 changes: 17 additions & 1 deletion packaging/debian/rules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/make -f

export PATH := /tmp/go/bin:$(PATH)
export PATH := /tmp/go/bin:/usr/share/gocode/bin:$(HOME)/.local/bin:$(PATH)
export SHELL := env PATH=$(PATH) /bin/bash

export DH_OPTIONS
Expand All @@ -22,24 +22,40 @@ override_dh_auto_install:
install -p -m 0644 instance_configs.cfg debian/google-guest-agent/usr/share/google-guest-agent
install -d debian/google-guest-agent/lib/systemd/system
install -p -m 0644 gce-workload-cert-refresh.timer debian/google-guest-agent/lib/systemd/system/
if [[ -d google-guest-agent ]]; then\
install -p -m 0644 google-guest-agent/cmd/google_guest_agent/google_guest_agent debian/google-guest-agent/usr/bin/google_guest_agent_manager;\
fi

override_dh_golang:
# We don't use any packaged dependencies, so skip dh_golang step.

override_dh_auto_build:
dh_auto_build -O--buildsystem=golang -- -ldflags="-s -w -X main.version=$(VERSION)" -mod=readonly
if [[ -d google-guest-agent ]]; then\
VERSION=$(VERSION) make -C google-guest-agent cmd/google_guest_agent/google_guest_agent;\
fi

override_dh_installinit:
# We don't ship sysvinit files or need script changes for them.

override_dh_systemd_enable:
install -d debian/google-guest-agent/lib/systemd/system
install -p -m 0644 *.service debian/google-guest-agent/lib/systemd/system/
# Don't include guest agent manager if not building with it.
if [[ ! -d google-guest-agent ]]; then\
rm -f debian/google-guest-agent/lib/systemd/systemd/google-guest-agent-manager.service;\
fi
install -d debian/google-guest-agent/lib/systemd/system-preset
install -p -m 0644 *.preset debian/google-guest-agent/lib/systemd/system-preset/
dh_systemd_enable google-guest-agent.service google-startup-scripts.service google-shutdown-scripts.service gce-workload-cert-refresh.timer
if [[ -d google-guest-agent ]]; then\
dh_systemd_enable google-guest-agent-manager.service;\
fi

override_dh_systemd_start:
# Only perform start/stop actions for the guest agent and cert refresh timer.
dh_systemd_start google-guest-agent.service
dh_systemd_start gce-workload-cert-refresh.timer
if [[ -d google-guest-agent ]]; then\
dh_systemd_start google-guest-agent-manager.service;\
fi
66 changes: 49 additions & 17 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,38 +35,64 @@ $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
}
else {
Set-Service -Name $name `
-DisplayName $display_name `
-Description $description
-Description $service_desc
} else {
Set-Service -Name $service_name `
-DisplayName $service_display_name `
-Description $service_desc
}
}

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
}
}

Set-ServiceConfig
# 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
} else {
if (Get-Service $manager_name -ErrorAction SilentlyContinue) {
Stop-Service $manager_name
& sc.exe delete $manager_name
}
}

$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
Expand Down
7 changes: 7 additions & 0 deletions packaging/googet/agent_uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@

Stop-Service GCEAgent -Verbose
& sc.exe delete GCEAgent

$name = 'GCEAgentManager'
if (Get-Service $name -ErrorAction SilentlyContinue) {
Stop-Service $name -Verbose
& sc.exe delete $name
}

62 changes: 62 additions & 0 deletions packaging/google-guest-agent.spec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
# Don't build debuginfo packages.
%define debug_package %{nil}

# The only use for extra source is to build plugin manager.
%if 0%{?has_extra_source}
%define build_plugin_manager %{has_extra_source}
%endif

Name: google-guest-agent
Epoch: 1
Version: %{_version}
Expand All @@ -23,6 +28,11 @@ Summary: Google Compute Engine guest agent.
License: ASL 2.0
Url: https://cloud.google.com/compute/docs/images/guest-environment
Source0: %{name}_%{version}.orig.tar.gz

%if 0%{?build_plugin_manager}
Source1: %{name}_extra-%{version}.orig.tar.gz
%endif

Requires: google-compute-engine-oslogin >= 1:20231003

BuildArch: %{_arch}
Expand All @@ -36,14 +46,25 @@ Obsoletes: python-google-compute-engine, python3-google-compute-engine
Contains the Google guest agent binary.

%prep

%if 0%{?build_plugin_manager}
%autosetup -a 1
%else
%autosetup
%endif

%build
for bin in google_guest_agent google_metadata_script_runner gce_workload_cert_refresh; do
pushd "$bin"
GOPATH=%{_gopath} CGO_ENABLED=0 %{_go} build -ldflags="-s -w -X main.version=%{_version}" -mod=readonly
popd
done
# Build side-by-side both new agent (plugin manager) and legacy agent.
%if 0%{?build_plugin_manager}
pushd %{name}-extra-%{version}/
VERSION=%{version} make cmd/google_guest_agent/google_guest_agent
popd
%endif

%install
install -d "%{buildroot}/%{_docdir}/%{name}"
Expand All @@ -55,6 +76,12 @@ install -p -m 0755 google_metadata_script_runner/google_metadata_script_runner %
install -p -m 0755 gce_workload_cert_refresh/gce_workload_cert_refresh %{buildroot}%{_bindir}/gce_workload_cert_refresh
install -d %{buildroot}/usr/share/google-guest-agent
install -p -m 0644 instance_configs.cfg %{buildroot}/usr/share/google-guest-agent/instance_configs.cfg

# Compat agent, it will become google_guest_agent after the full package transition.
%if 0%{?build_plugin_manager}
install -p -m 0755 %{name}-extra-%{version}/cmd/google_guest_agent/google_guest_agent %{buildroot}%{_bindir}/google_guest_agent_manager
%endif

%if 0%{?el6}
install -d %{buildroot}/etc/init
install -p -m 0644 %{name}.conf %{buildroot}/etc/init/
Expand All @@ -64,6 +91,11 @@ install -p -m 0644 google-shutdown-scripts.conf %{buildroot}/etc/init/
install -d %{buildroot}%{_unitdir}
install -d %{buildroot}%{_presetdir}
install -p -m 0644 %{name}.service %{buildroot}%{_unitdir}

%if 0%{?build_plugin_manager}
install -p -m 0644 google-guest-agent-manager.service %{buildroot}%{_unitdir}
%endif

install -p -m 0644 google-startup-scripts.service %{buildroot}%{_unitdir}
install -p -m 0644 google-shutdown-scripts.service %{buildroot}%{_unitdir}
install -p -m 0644 gce-workload-cert-refresh.service %{buildroot}%{_unitdir}
Expand All @@ -76,6 +108,11 @@ install -p -m 0644 90-%{name}.preset %{buildroot}%{_presetdir}/90-%{name}.preset
%defattr(-,root,root,-)
/usr/share/google-guest-agent/instance_configs.cfg
%{_bindir}/google_guest_agent

%if 0%{?build_plugin_manager}
%{_bindir}/google_guest_agent_manager
%endif

%{_bindir}/google_metadata_script_runner
%{_bindir}/gce_workload_cert_refresh
%if 0%{?el6}
Expand All @@ -84,6 +121,11 @@ install -p -m 0644 90-%{name}.preset %{buildroot}%{_presetdir}/90-%{name}.preset
/etc/init/google-shutdown-scripts.conf
%else
%{_unitdir}/%{name}.service

%if 0%{?build_plugin_manager}
%{_unitdir}/google-guest-agent-manager.service
%endif

%{_unitdir}/google-startup-scripts.service
%{_unitdir}/google-shutdown-scripts.service
%{_unitdir}/gce-workload-cert-refresh.service
Expand All @@ -108,27 +150,47 @@ if [ $1 -eq 1 ]; then
systemctl enable google-shutdown-scripts.service >/dev/null 2>&1 || :
systemctl enable gce-workload-cert-refresh.timer >/dev/null 2>&1 || :

%if 0%{?build_plugin_manager}
systemctl enable google-guest-agent-manager.service >/dev/null 2>&1 || :
%endif

if [ -d /run/systemd/system ]; then
systemctl daemon-reload >/dev/null 2>&1 || :
systemctl start google-guest-agent.service >/dev/null 2>&1 || :
systemctl start gce-workload-cert-refresh.timer >/dev/null 2>&1 || :
%if 0%{?build_plugin_manager}
systemctl start google-guest-agent-manager.service >/dev/null 2>&1 || :
%endif
fi


else
# Package upgrade
if [ -d /run/systemd/system ]; then
systemctl daemon-reload >/dev/null 2>&1 || :
systemctl try-restart google-guest-agent.service >/dev/null 2>&1 || :
%if 0%{?build_plugin_manager}
systemctl enable google-guest-agent-manager.service >/dev/null 2>&1 || :
systemctl restart google-guest-agent-manager.service >/dev/null 2>&1 || :
%endif
fi
fi

%preun
if [ $1 -eq 0 ]; then
# Package removal, not upgrade
%if 0%{?build_plugin_manager}
systemctl --no-reload disable google-guest-agent-manager.service >/dev/null 2>&1 || :
%endif
systemctl --no-reload disable google-guest-agent.service >/dev/null 2>&1 || :
systemctl --no-reload disable google-startup-scripts.service >/dev/null 2>&1 || :
systemctl --no-reload disable google-shutdown-scripts.service >/dev/null 2>&1 || :
systemctl --no-reload disable gce-workload-cert-refresh.timer >/dev/null 2>&1 || :
if [ -d /run/systemd/system ]; then
systemctl stop google-guest-agent.service >/dev/null 2>&1 || :
%if 0%{?build_plugin_manager}
systemctl stop google-guest-agent-manager.service >/dev/null 2>&1 || :
%endif
fi
fi

Expand Down

0 comments on commit 13c627c

Please sign in to comment.