Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate Windows .bat scripts #31732

Merged
merged 21 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@
/tasks/sds.py @DataDog/agent-processing-and-routing
/tasks/systray.py @DataDog/windows-agent
/tasks/winbuildscripts/ @DataDog/windows-agent
/tasks/winbuild.py @DataDog/windows-agent
/tasks/windows_resources.py @DataDog/windows-agent
/tasks/components.py @DataDog/agent-shared-components
/tasks/components_templates @DataDog/agent-shared-components
Expand Down
2 changes: 1 addition & 1 deletion .gitlab/package_build/installer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ windows-installer-amd64:
-e USE_S3_CACHING="$USE_S3_CACHING"
-e API_KEY_ORG2=${API_KEY_ORG2}
registry.ddbuild.io/ci/datadog-agent-buildimages/windows_1809_${ARCH}${Env:DATADOG_AGENT_WINBUILDIMAGES_SUFFIX}:${Env:DATADOG_AGENT_WINBUILDIMAGES}
c:\mnt\tasks\winbuildscripts\buildinstaller.bat
powershell -C "c:\mnt\tasks\winbuildscripts\Build-InstallerPackages.ps1 -BuildOutOfSource 1 -InstallDeps 1 -CheckGoVersion 1"
julien-lebot marked this conversation as resolved.
Show resolved Hide resolved
after_script:
- '$_instance_id = (iwr -UseBasicParsing http://169.254.169.254/latest/meta-data/instance-id).content ; Write-Host "Running on instance $($_instance_id)"'
artifacts:
Expand Down
7 changes: 3 additions & 4 deletions .gitlab/package_build/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@
-e S3_OMNIBUS_CACHE_BUCKET="$S3_OMNIBUS_CACHE_BUCKET"
-e USE_S3_CACHING="$USE_S3_CACHING"
-e INTEGRATION_WHEELS_CACHE_BUCKET="$INTEGRATION_WHEELS_CACHE_BUCKET"
-e GO_VERSION_CHECK="true"
-e BUNDLE_MIRROR__RUBYGEMS__ORG=${BUNDLE_MIRROR__RUBYGEMS__ORG}
-e PIP_INDEX_URL=${PIP_INDEX_URL}
-e API_KEY_ORG2=${API_KEY_ORG2}
-e OMNIBUS_GIT_CACHE_DIR=${Env:TEMP}/${CI_PIPELINE_ID}/omnibus-git-cache
-e AGENT_FLAVOR=${AGENT_FLAVOR}
registry.ddbuild.io/ci/datadog-agent-buildimages/windows_1809_${ARCH}${Env:DATADOG_AGENT_WINBUILDIMAGES_SUFFIX}:${Env:DATADOG_AGENT_WINBUILDIMAGES}
c:\mnt\tasks\winbuildscripts\buildwin.bat
powershell -C "c:\mnt\tasks\winbuildscripts\Build-AgentPackages.ps1 -BuildOutOfSource 1 -InstallDeps 1 -CheckGoVersion 1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✏️ suggestion: ‏ Maybe for a follow-up PR but I think we could update the entrypoint to execute a PowerShell script / command instead of having to repeat "powershell -C": https://github.com/DataDog/datadog-agent-buildimages/blob/main/windows/entrypoint.bat#L19

- If ($lastExitCode -ne "0") { throw "Previous command returned $lastExitCode" }
- get-childitem omnibus\pkg
- !reference [.upload_sbom_artifacts_windows]
Expand Down Expand Up @@ -89,7 +88,7 @@ windows_zip_agent_binaries_x64-a7:
needs: ["go_mod_tidy_check", "go_deps"]
variables:
ARCH: "x64"
OMNIBUS_TARGET: agent_binaries
OMNIBUS_TARGET: agent-binaries
before_script:
- set RELEASE_VERSION $RELEASE_VERSION_7
script:
Expand Down Expand Up @@ -122,7 +121,7 @@ windows_zip_agent_binaries_x64-a7:
-e PIP_INDEX_URL=${PIP_INDEX_URL}
-e API_KEY_ORG2=${API_KEY_ORG2}
registry.ddbuild.io/ci/datadog-agent-buildimages/windows_1809_${ARCH}${Env:DATADOG_AGENT_WINBUILDIMAGES_SUFFIX}:${Env:DATADOG_AGENT_WINBUILDIMAGES}
c:\mnt\tasks\winbuildscripts\buildwin.bat
powershell -C "c:\mnt\tasks\winbuildscripts\Build-OmnibusTarget.ps1 -BuildOutOfSource 1 -InstallDeps 1 -CheckGoVersion 1"
- If ($lastExitCode -ne "0") { throw "Previous command returned $lastExitCode" }
- get-childitem omnibus\pkg
- !reference [.upload_sbom_artifacts_windows]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfiles/agent/windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ How to build the Agent docker image
From the root of the repository, run the following command:

```
docker run --rm -it -v "${pwd}:c:\mnt" -e OMNIBUS_TARGET=main -e MAJOR_VERSION=7 -e RELEASE_VERSION=nightly -e PY_RUNTIMES=3 datadog/agent-buildimages-windows_x64:1809 c:\mnt\tasks\winbuildscripts\buildwin.bat
docker run --rm -it -v "${pwd}:c:\mnt" -e OMNIBUS_TARGET=main -e MAJOR_VERSION=7 -e RELEASE_VERSION=nightly-a7 -e PY_RUNTIMES=3 datadog/agent-buildimages-windows_x64:1809 powershell -C "c:\mnt\tasks\winbuildscripts\Build-AgentPackages.ps1 -BuildOutOfSource 1 -InstallDeps 1 -CheckGoVersion 1"
```

The build artifacts will be in `omnibus\pkg`.
Expand Down
6 changes: 3 additions & 3 deletions docs/dev/agent_omnibus.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Start a Powershell prompt and navigate to your local clone of the `datadog-agent
Run the following command:

```powershell
docker run -v "$(Get-Location):c:\mnt" -e OMNIBUS_TARGET=main -e RELEASE_VERSION=nightly -e MAJOR_VERSION=7 -e TARGET_ARCH=x64 datadog/agent-buildimages-windows_x64:1809 c:\mnt\tasks\winbuildscripts\buildwin.bat
docker run -v "$(Get-Location):c:\mnt" -e OMNIBUS_TARGET=main -e RELEASE_VERSION=nightly-a7 -e MAJOR_VERSION=7 -e TARGET_ARCH=x64 datadog/agent-buildimages-windows_x64:1809 powershell -C "c:\mnt\tasks\winbuildscripts\Build-AgentPackages.ps1 -BuildOutOfSource 1 -InstallDeps 1 -CheckGoVersion 1"
```

Downloading the Docker image may take some time in the first run.
Expand All @@ -100,7 +100,7 @@ Alternatively here's a small Powershell script to facilitate using the docker im
param (
[int]$MAJOR_VERSION=7,
$TARGET_ARCH="x64",
$RELEASE_VERSION="nightly",
$RELEASE_VERSION="nightly-a7",
[bool]$RM_CONTAINER=$true,
[bool]$DEBUG=$false
)
Expand All @@ -113,7 +113,7 @@ $opts = "-e OMNIBUS_TARGET=main -e RELEASE_VERSION=$RELEASE_VERSION -e MAJOR_VER
if ($DEBUG) {
$opts += " -e DEBUG_CUSTOMACTION=yes "
}
$cmd += " -m 8192M -v ""$(Get-Location):c:\mnt"" $opts datadog/agent-buildimages-windows_x64:1809 c:\mnt\tasks\winbuildscripts\buildwin.bat"
$cmd += " -m 8192M -v ""$(Get-Location):c:\mnt"" $opts datadog/agent-buildimages-windows_x64:1809 powershell -C ""c:\mnt\tasks\winbuildscripts\Build-AgentPackages.ps1 -BuildOutOfSource 1 -InstallDeps 1 -CheckGoVersion 1"""
Write-Host $cmd
Invoke-Expression -Command $cmd
```
Expand Down
2 changes: 2 additions & 0 deletions tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
trace_agent,
vim,
vscode,
winbuild,
worktree,
)
from tasks.build_tags import audit_tag_impact, print_default_build_tags
Expand Down Expand Up @@ -212,6 +213,7 @@
ns.add_collection(collector)
ns.add_collection(invoke_unit_tests)
ns.add_collection(debug)
ns.add_collection(winbuild)
ns.add_collection(worktree)
ns.configure(
{
Expand Down
70 changes: 70 additions & 0 deletions tasks/winbuild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os
import shutil

from invoke.tasks import task
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?


from tasks.flavor import AgentFlavor
from tasks.libs.common.utils import get_version
from tasks.msi import build as agent_msi_build
from tasks.msi import build_installer
from tasks.omnibus import build as omnibus_build

# Output directory for package files
OUTPUT_PATH = os.path.join(os.getcwd(), "omnibus", "pkg")
# Omnibus stores files here, e.g. C:\opt\datadog-agent, C:\opt\dataog-installer
OPT_SOURCE_DIR = os.path.join('C:\\', 'opt')


@task
def agent_package(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:suggestion: I think it was not advised to call tasks from another task (but it was maybe a namespace problem, I'm not 100% sure). What do you think of calling explicitly these tasks from the powershell script?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean having the PowerShell script call the individual functions like:

inv omnibus.omnibus-build --flavor ....
inv msi.agent-msi-build --release-version ...
& "./tasks/winbuildscripts/Generate-OCIPackage.ps1 -package datadog-agent"

?

I think that would be cool but how would developers do all the above in a single step? I think that was one of the goals of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was not advised to call tasks from another task (but it was maybe a namespace problem, I'm not 100% sure)

I haven't been able to find a source for that. The only oddity I can see is just a plain Python thing. Many of our invoke tasks and helperfunctions define defaults in the function definition, if the defaults change in an inner function then any out outer calls will be using the wrong default. I typically prefer to use None as the default so outers can defer to the inner call for default values.

def inner(release_version=None):
    if release_version is None:
        release_version = 'nightly-a7'
def outer(release_version=None):
    inner(release_version=release_version)

What do you think of calling explicitly these tasks from the powershell script?

hmmm, perhaps. I think it's up to if people would rather modify PowerShell to add additional build steps, or if they'd rather modify Python. I like having the PowerShell script handle the env related things that can't be done in Python and have Python handle the actual "build".

I think that would be cool but how would developers do all the above in a single step?

User would run Build-AgentPacakge.ps1. Though if your env is already properly configured you could also just run the invoke task.

ctx,
flavor=AgentFlavor.base.name,
release_version="nightly-a7",
skip_deps=False,
):
# Build agent
omnibus_build(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ question
Should you handle errors on intermediate steps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think any errors will be raised as exceptions and trigger exit, so I don't need to check explicitly here

ctx,
flavor=flavor,
release_version=release_version,
skip_deps=skip_deps,
)

# Package Agent into MSI
agent_msi_build(ctx, release_version=release_version)

# Package MSI into OCI
ctx.run('powershell -C "./tasks/winbuildscripts/Generate-OCIPackage.ps1 -package datadog-agent"')


@task
def installer_package(
ctx,
release_version="nightly-a7",
skip_deps=False,
):
# Build installer
omnibus_build(
ctx,
release_version=release_version,
skip_deps=skip_deps,
target_project="installer",
)

# Package Insaller into MSI
build_installer(ctx)
clarkb7 marked this conversation as resolved.
Show resolved Hide resolved

# Package MSI into OCI
ctx.run('powershell -C "./tasks/winbuildscripts/Generate-OCIPackage.ps1 -package datadog-installer"')

# Copy installer.exe to the output dir so it can be deployed as the bootstrapper
agent_version = get_version(
ctx,
include_git=True,
url_safe=True,
include_pipeline_id=True,
)
shutil.copy2(
os.path.join(OPT_SOURCE_DIR, "datadog-installer\\datadog-installer.exe"),
os.path.join(OUTPUT_PATH, f"datadog-installer-{agent_version}-1-x86_64.exe"),
)
83 changes: 83 additions & 0 deletions tasks/winbuildscripts/Build-AgentPackages.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<#
.SYNOPSIS
Builds the Datadog Agent packages for Windows. Builds everything with omnibus and packages the output into MSI, ZIP, and OCI.

.DESCRIPTION
This script builds the Datadog Agent packages for Windows, with options to configure the build environment.

.PARAMETER ReleaseVersion
Specifies the release version of the build. Default is the value of the environment variable RELEASE_VERSION.

.PARAMETER Flavor
Specifies the flavor of the agent. Default is the value of the environment variable AGENT_FLAVOR.

.PARAMETER BuildOutOfSource
Specifies whether to build out of source. Default is $false.

Use this option in the CI to keep the job directory clean and avoid conflicts/stale data.
Use this option in Hyper-V based containers to improve build performance.

.PARAMETER InstallDeps
Specifies whether to install dependencies (python requirements, go deps, etc.). Default is $true.

.PARAMETER CheckGoVersion
Specifies whether to check the Go version. If not provided, it defaults to the value of the environment variable GO_VERSION_CHECK or $true if the environment variable is not set.

.EXAMPLE
.\Build-AgentPackages.ps1 -InstallDeps $false

.EXAMPLE
.\Build-AgentPackages.ps1 -BuildOutOfSource $true -InstallDeps $true -Flavor "fips" -CheckGoVersion $true

.NOTES
This script should be run from the root of the repository.

#>
param(
[bool] $BuildOutOfSource = $false,
[nullable[bool]] $CheckGoVersion,
[bool] $InstallDeps = $true,
[string] $ReleaseVersion = $env:RELEASE_VERSION,
[string] $Flavor = $env:AGENT_FLAVOR
)

. "$PSScriptRoot\common.ps1"

Invoke-BuildScript `
-BuildOutOfSource $BuildOutOfSource `
-InstallDeps $InstallDeps `
-CheckGoVersion $CheckGoVersion `
-Command {
$inv_args = @(
"--skip-deps"
)
if ($ReleaseVersion) {
$inv_args += "--release-version"
$inv_args += $ReleaseVersion
$env:RELEASE_VERSION=$ReleaseVersion
}

if ($Flavor) {
$inv_args += "--flavor"
$inv_args += $Flavor
$env:AGENT_FLAVOR=$Flavor
}

Write-Host "inv -e winbuild.agent-package $inv_args"
inv -e winbuild.agent-package @inv_args
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to build the agent package"
exit 1
}

# Show the contents of the output package directories for debugging purposes
Get-ChildItem -Path C:\omnibus-ruby\pkg\
Get-ChildItem -Path "C:\opt\datadog-agent\bin\agent\"
Get-ChildItem -Path ".\omnibus\pkg\"

if ($BuildOutOfSource) {
# Copy the resulting package to the mnt directory
mkdir C:\mnt\omnibus\pkg -Force -ErrorAction Stop | Out-Null
Copy-Item -Path ".\omnibus\pkg\*" -Destination "C:\mnt\omnibus\pkg" -Force -ErrorAction Stop
}
}
72 changes: 72 additions & 0 deletions tasks/winbuildscripts/Build-InstallerPackages.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<#
.SYNOPSIS
Builds the Datadog Installer packages for Windows. Builds everything with omnibus and packages the output into MSI, ZIP, and OCI.

.DESCRIPTION
This script builds the Datadog Installer packages for Windows, with options to configure the build environment.

.PARAMETER BuildOutOfSource
Specifies whether to build out of source. Default is $false.

Use this option in the CI to keep the job directory clean and avoid conflicts/stale data.
Use this option in Hyper-V based containers to improve build performance.

.PARAMETER InstallDeps
Specifies whether to install dependencies (python requirements, go deps, etc.). Default is $true.

.PARAMETER ReleaseVersion
Specifies the release version of the build. Default is the value of the environment variable RELEASE_VERSION.

.PARAMETER CheckGoVersion
Specifies whether to check the Go version. If not provided, it defaults to the value of the environment variable GO_VERSION_CHECK or $true if the environment variable is not set.

.EXAMPLE
.\Build-InstallerPackages.ps1 -InstallDeps $false

.EXAMPLE
.\Build-InstallerPackages.ps1 -BuildOutOfSource $true -InstallDeps $true -CheckGoVersion $true

.NOTES
This script should be run from the root of the repository.

#>
param(
[bool] $BuildOutOfSource = $false,
[nullable[bool]] $CheckGoVersion,
[bool] $InstallDeps = $true,
[string] $ReleaseVersion = $env:RELEASE_VERSION
)

. "$PSScriptRoot\common.ps1"

Invoke-BuildScript `
-BuildOutOfSource $BuildOutOfSource `
-InstallDeps $InstallDeps `
-CheckGoVersion $CheckGoVersion `
-Command {
$inv_args = @(
"--skip-deps"
)
if ($ReleaseVersion) {
$inv_args += "--release-version"
$inv_args += $ReleaseVersion
}

Write-Host "inv -e winbuild.installer-package $inv_args"
inv -e winbuild.installer-package @inv_args
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to build the agent package"
exit 1
}

# Show the contents of the output package directories for debugging purposes
Get-ChildItem -Path C:\omnibus-ruby\pkg\
Get-ChildItem -Path C:\opt\datadog-installer
Get-ChildItem -Path ".\omnibus\pkg\"

if ($BuildOutOfSource) {
# Copy the resulting package to the mnt directory
mkdir C:\mnt\omnibus\pkg -Force -ErrorAction Stop | Out-Null
Copy-Item -Path ".\omnibus\pkg\*" -Destination "C:\mnt\omnibus\pkg" -Force -ErrorAction Stop
}
}
Loading
Loading