|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/DataDog/test-infra-definitions/common/config" |
| 8 | + "github.com/DataDog/test-infra-definitions/components/command" |
| 9 | + "github.com/DataDog/test-infra-definitions/components/datadog/agentparams" |
| 10 | + remoteComp "github.com/DataDog/test-infra-definitions/components/remote" |
| 11 | + |
| 12 | + "github.com/pulumi/pulumi/sdk/v3/go/pulumi" |
| 13 | +) |
| 14 | + |
| 15 | +type agentMacOSManager struct { |
| 16 | + host *remoteComp.Host |
| 17 | +} |
| 18 | + |
| 19 | +func newMacOSManager(host *remoteComp.Host) agentOSManager { |
| 20 | + return &agentMacOSManager{host: host} |
| 21 | +} |
| 22 | + |
| 23 | +// directInstallCommand expects a locally provided .dmg or .pkg uploaded to the host; it will install it with installer |
| 24 | +func (am *agentMacOSManager) directInstallCommand(env config.Env, packagePath string, _ agentparams.PackageVersion, additionalInstallParameters []string, opts ...pulumi.ResourceOption) (command.Command, error) { |
| 25 | + // Unsupported for now, installing directly from a dmg without the install script requires way too many step that would imply duplicating the install script code in there. |
| 26 | + return nil, fmt.Errorf("installing directly from a dmg without the install script requires way too many step that would imply duplicating the install script code in there") |
| 27 | +} |
| 28 | + |
| 29 | +// getInstallCommand downloads appropriate pkg and installs it |
| 30 | +func (am *agentMacOSManager) getInstallCommand(version agentparams.PackageVersion, apiKey pulumi.StringInput, _ []string) (pulumi.StringOutput, error) { |
| 31 | + // For macOS, use the official install script which supports DD_API_KEY and version envs, |
| 32 | + // mirroring Linux flow but using the macOS path. The script detects OS and uses pkg. |
| 33 | + // If pipeline is specified, we cannot use public script; we assume local package will be provided in that case. |
| 34 | + |
| 35 | + exports := []string{} |
| 36 | + if version.Major != "" { |
| 37 | + exports = append(exports, fmt.Sprintf("DD_AGENT_MAJOR_VERSION=%s", version.Major)) |
| 38 | + } |
| 39 | + if version.Minor != "" { |
| 40 | + exports = append(exports, fmt.Sprintf("DD_AGENT_MINOR_VERSION=%s", version.Minor)) |
| 41 | + } |
| 42 | + |
| 43 | + if version.PipelineID != "" { |
| 44 | + exports = append(exports, fmt.Sprintf("DD_REPO_URL=https://dd-agent-macostesting.s3.amazonaws.com/ci/datadog-agent/pipeline-%s", version.PipelineID)) |
| 45 | + } |
| 46 | + |
| 47 | + env := strings.Join(exports, " ") |
| 48 | + // Retry curl few times |
| 49 | + cmd := fmt.Sprintf(`for i in 1 2 3 4 5; do curl -fsSL https://install.datadoghq.com/scripts/install_mac_os.sh -o install-script.sh && break || sleep $((2**$i)); done && for i in 1 2 3; do DD_API_KEY=%%s %%s %[1]s DD_INSTALL_ONLY=true bash install-script.sh && exit 0 || sleep $((2**$i)); done; exit 1`, env) |
| 50 | + // Only the systemdaemon install is supported on macOS, because single user requires to interact with the pop-up. |
| 51 | + pulumiCmdStr := pulumi.Sprintf(cmd, apiKey, pulumi.Sprintf("DD_SYSTEMDAEMON_INSTALL=true DD_SYSTEMDAEMON_USER_GROUP=%s:staff", am.host.Username)) |
| 52 | + return pulumiCmdStr, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (am *agentMacOSManager) getAgentConfigFolder() string { |
| 56 | + // macOS Agent config default |
| 57 | + return "/opt/datadog-agent/etc" |
| 58 | +} |
| 59 | + |
| 60 | +func (am *agentMacOSManager) restartAgentServices(transform command.Transformer, opts ...pulumi.ResourceOption) (command.Command, error) { |
| 61 | + // On macOS, the launchd service is "com.datadoghq.agent" |
| 62 | + cmdName := am.host.Name() + "-restart-agent" |
| 63 | + var cmdArgs command.RunnerCommandArgs = &command.Args{ |
| 64 | + Sudo: true, |
| 65 | + Create: pulumi.String("launchctl kickstart -k system/com.datadoghq.agent"), |
| 66 | + } |
| 67 | + if transform != nil { |
| 68 | + cmdName, cmdArgs = transform(cmdName, cmdArgs) |
| 69 | + } |
| 70 | + return am.host.OS.Runner().Command(cmdName, cmdArgs, opts...) |
| 71 | +} |
0 commit comments