11import hashlib
22import os
33import re
4+ import shutil
45import subprocess
56import sys
67import tempfile
@@ -64,6 +65,15 @@ def omnibus_run_task(
6465 ctx .run (cmd .format (** args ), env = env , replace_env = True , err_stream = sys .stdout )
6566
6667
68+ def _clear_agent_install_directory (agent_path ):
69+ with os .scandir (agent_path ) as entries :
70+ for entry in entries :
71+ if entry .is_dir (follow_symlinks = False ):
72+ shutil .rmtree (entry .path )
73+ else :
74+ os .unlink (entry .path )
75+
76+
6777def bundle_install_omnibus (ctx , gem_path = None , env = None , max_try = 2 ):
6878 with ctx .cd ("omnibus" ):
6979 # make sure bundle install starts from a clean state
@@ -462,9 +472,7 @@ def build_repackaged_agent(ctx, log_level="info"):
462472 ):
463473 raise Exit ("Operation cancelled" )
464474
465- import shutil
466-
467- shutil .rmtree ("/opt/datadog-agent" )
475+ _clear_agent_install_directory (agent_path )
468476
469477 architecture = ctx .run ("dpkg --print-architecture" , hide = True ).stdout .strip ()
470478
@@ -485,6 +493,10 @@ def build_repackaged_agent(ctx, log_level="info"):
485493
486494 env ['OMNIBUS_REPACKAGE_SOURCE_URL' ] = f"https://apt.datad0g.com/{ latest_package .filename } "
487495 env ['OMNIBUS_REPACKAGE_SOURCE_SHA256' ] = latest_package .sha256
496+ base_dir = _resolve_omnibus_path_override (None , "OMNIBUS_BASE_DIR" )
497+ if base_dir :
498+ env ['OMNIBUS_BASE_DIR' ] = base_dir
499+
488500 # Set up compiler flags (assumes an environment based on our glibc-targeting toolchains)
489501 if architecture == "amd64" :
490502 env .update (
@@ -509,7 +521,7 @@ def build_repackaged_agent(ctx, log_level="info"):
509521 ctx ,
510522 "build" ,
511523 "agent" ,
512- base_dir = None ,
524+ base_dir = base_dir ,
513525 env = env ,
514526 log_level = log_level ,
515527 cache_dir = _resolve_omnibus_path_override (None , "OMNIBUS_CACHE_DIR" ),
0 commit comments