@@ -2,7 +2,10 @@ package agent
2
2
3
3
import (
4
4
"fmt"
5
+ "io/fs"
6
+ "os"
5
7
"path"
8
+ "strings"
6
9
7
10
"github.com/DataDog/datadog-agent/pkg/util/option"
8
11
"github.com/DataDog/test-infra-definitions/common/config"
@@ -12,6 +15,7 @@ import (
12
15
"github.com/DataDog/test-infra-definitions/components/command"
13
16
"github.com/DataDog/test-infra-definitions/components/datadog/agentparams"
14
17
perms "github.com/DataDog/test-infra-definitions/components/datadog/agentparams/filepermissions"
18
+ tifos "github.com/DataDog/test-infra-definitions/components/os"
15
19
remoteComp "github.com/DataDog/test-infra-definitions/components/remote"
16
20
17
21
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
@@ -69,10 +73,10 @@ func NewHostAgent(e config.Env, host *remoteComp.Host, options ...agentparams.Op
69
73
return hostInstallComp , nil
70
74
}
71
75
72
- func (h * HostAgent ) installAgent (env config.Env , params * agentparams.Params , baseOpts ... pulumi.ResourceOption ) error {
76
+ func (h * HostAgent ) installScriptInstallation (env config.Env , params * agentparams.Params , baseOpts ... pulumi.ResourceOption ) (command. Command , error ) {
73
77
installCmdStr , err := h .manager .getInstallCommand (params .Version , params .AdditionalInstallParameters )
74
78
if err != nil {
75
- return err
79
+ return nil , err
76
80
}
77
81
78
82
installCmd , err := h .Host .OS .Runner ().Command (
@@ -81,7 +85,77 @@ func (h *HostAgent) installAgent(env config.Env, params *agentparams.Params, bas
81
85
Create : pulumi .Sprintf (installCmdStr , env .AgentAPIKey ()),
82
86
}, baseOpts ... )
83
87
if err != nil {
84
- return err
88
+ return nil , err
89
+ }
90
+ return installCmd , nil
91
+ }
92
+
93
+ func (h * HostAgent ) directInstallInstallation (env config.Env , params * agentparams.Params , baseOpts ... pulumi.ResourceOption ) (command.Command , error ) {
94
+ var wantedExt string
95
+ switch h .Host .OS .Descriptor ().Flavor {
96
+ case tifos .AmazonLinux , tifos .CentOS , tifos .RedHat , tifos .AmazonLinuxECS , tifos .Fedora , tifos .Suse , tifos .RockyLinux :
97
+ wantedExt = ".rpm"
98
+ case tifos .Debian , tifos .Ubuntu :
99
+ wantedExt = ".deb"
100
+ case tifos .WindowsServer :
101
+ wantedExt = ".msi"
102
+ case tifos .MacosOS , tifos .Unknown :
103
+ fallthrough
104
+ default :
105
+ return nil , fmt .Errorf ("unsupported flavor for local packages installation: %s" , h .Host .OS .Descriptor ().Flavor )
106
+ }
107
+
108
+ pathInfo , err := os .Stat (params .Version .LocalPath )
109
+ if err != nil {
110
+ return nil , err
111
+ }
112
+ packagePath := params .Version .LocalPath
113
+ matches := []string {}
114
+ if pathInfo .IsDir () {
115
+ matches , err = fs .Glob (os .DirFS (params .Version .LocalPath ), fmt .Sprintf ("*%s" , wantedExt ))
116
+ if err != nil {
117
+ return nil , err
118
+ }
119
+ if len (matches ) == 0 {
120
+ return nil , fmt .Errorf ("no package found in %s with extension %s" , params .Version .LocalPath , wantedExt )
121
+ }
122
+ if len (matches ) > 1 {
123
+ env .Ctx ().Log .Warn (fmt .Sprintf ("Found multiple packages to install, using the first one: %s" , matches [0 ]), nil )
124
+ }
125
+ packagePath = path .Join (packagePath , matches [0 ])
126
+ } else {
127
+ if strings .HasSuffix (params .Version .LocalPath , wantedExt ) {
128
+ matches = append (matches , path .Base (params .Version .LocalPath ))
129
+ } else {
130
+ return nil , fmt .Errorf ("local package %s does not have the expected extension %s" , params .Version .LocalPath , wantedExt )
131
+ }
132
+ }
133
+ packageToInstall := matches [0 ]
134
+ env .Ctx ().Log .Info (fmt .Sprintf ("Found local package to install %s" , packageToInstall ), nil )
135
+ uploadCmd , err := h .Host .OS .FileManager ().CopyFile ("copy-agent-package" , pulumi .String (packagePath ), pulumi .String ("./" ), baseOpts ... )
136
+ if err != nil {
137
+ return nil , err
138
+ }
139
+
140
+ installCmd , err := h .manager .directInstallCommand (env , packageToInstall , params .Version , []string {}, utils .MergeOptions (baseOpts , utils .PulumiDependsOn (uploadCmd ))... )
141
+ if err != nil {
142
+ return nil , err
143
+ }
144
+ return installCmd , nil
145
+ }
146
+ func (h * HostAgent ) installAgent (env config.Env , params * agentparams.Params , baseOpts ... pulumi.ResourceOption ) error {
147
+ var installCmd pulumi.Resource
148
+ var err error
149
+ if params .Version .LocalPath != "" {
150
+ installCmd , err = h .directInstallInstallation (env , params , baseOpts ... )
151
+ if err != nil {
152
+ return err
153
+ }
154
+ } else {
155
+ installCmd , err = h .installScriptInstallation (env , params , baseOpts ... )
156
+ if err != nil {
157
+ return err
158
+ }
85
159
}
86
160
87
161
afterInstallOpts := utils .MergeOptions (baseOpts , utils .PulumiDependsOn (installCmd ))
0 commit comments