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

[windows] Fix idempotence when reinstalling same pinned version #269

Merged
merged 2 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
include_tasks: pkg-windows.yml
when: ansible_os_family == "Windows"

- name: Linux Install Tasks (Agent 5)
- name: Linux Configuration Tasks (Agent 5)
include_tasks: agent5-linux.yml
when: datadog_agent_major_version|int == 5 and ansible_os_family != "Windows"

- name: Linux Install Tasks
- name: Linux Configuration Tasks
include_tasks: agent-linux.yml
when: datadog_agent_major_version|int > 5 and ansible_os_family != "Windows"

- name: Agent6 Install Tasks (Windows)
- name: Windows Configuration Tasks
include_tasks: agent-win.yml
when: datadog_agent_major_version|int > 5 and ansible_os_family == "Windows"

Expand Down
43 changes: 38 additions & 5 deletions tasks/pkg-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,81 @@
msg: "The Datadog ansible role does not currently support Agent 5"
when: datadog_agent_major_version|int == 5

# This is a best-effort solution to avoid redownloading the msi when
# It won't work with rc / beta builds, and won't work if the version to install
# is not pinned (as we don't know what version latest will be before downloading
# the package).
- name: Check currently installed Agent version
win_shell: |
$product_name = "Datadog Agent"
Copy link
Contributor

Choose a reason for hiding this comment

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

On Chef we use agent status for this: https://github.com/DataDog/chef-datadog/blob/483f88a43569b1efd00e7d5c0cb1e479eb09b1a2/libraries/recipe_helpers.rb#L92

I'm not sure which approach is better, but I think we shouldn't have two.

Copy link
Contributor Author

@KSerrania KSerrania Mar 9, 2020

Choose a reason for hiding this comment

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

The Chef approach is slightly better because it would work with rc and beta builds.

However, contrary to this method, it does have a caveat: it only works if you know where the Agent binary is located at (Chef expects it to be C:/Program Files/Datadog/Datadog Agent/bin/agent).

One thing to note is that we currently don't offer the possibility to choose the application directory for Windows install; we do have a datadog_agent_binary_path_windows parameter, but it's not advertised in the README (and thus I suppose it's an internal parameter) and only used for the integration command, so we could use this option here to use agent status (that way people can still configure it for their use case if needed).

Copy link
Contributor

Choose a reason for hiding this comment

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

If we have a variable for the Agent path, then I think using that to call the binary is reasonable (assuming we won't change the output of agent status).

I'm not sure this is necessarily better than your approach, though. I would still sync with @kbogtob, who wrote the Chef code I linked before, to discuss pros and cons of both approaches.

$query = "Select Name,IdentifyingNumber,InstallDate,InstallLocation,ProductID,Version FROM Win32_Product where Name like '$product_name%'"
$installs = Get-WmiObject -query $query

if (!$installs -or ($installs.Count -eq 0) -or ($installs.Count -gt 1)) {
Write-Host ""
} else {
$ddmaj, $ddmin, $ddpatch, $ddbuild = $installs.Version.split(".")
Write-Host "$($ddmaj).$($ddmin).$($ddpatch)"
}
register: version_check
changed_when: false # By default win_shell returns changed, but we know this script doesn't change anything.

- name: Initialize skip install flag to false
set_fact:
datadog_skip_windows_install: "False"

- name: Set skip install flag if version already installed
set_fact:
datadog_skip_windows_install: "{{ version_check.stdout | trim == datadog_agent_windows_version }}"
when: datadog_agent_windows_version is defined

- name: Download windows datadog agent 614 fix script
win_get_url:
url: "{{ datadog_windows_614_fix_script_url }}"
dest: '%TEMP%\fix_6_14.ps1'
when: not datadog_skip_windows_install

- name: Run 6.14.0/1 PowerShell fix
win_shell: |
Set-ExecutionPolicy Bypass -Scope Process -Force
&$env:temp\fix_6_14.ps1
when: not datadog_skip_windows_install

- include_tasks: win_agent_latest.yml
when: datadog_agent_windows_version is not defined
when: (not datadog_skip_windows_install) and (datadog_agent_windows_version is not defined)

- include_tasks: win_agent_version.yml
when: datadog_agent_windows_version is defined
when: (not datadog_skip_windows_install) and (datadog_agent_windows_version is defined)

- name: show URL var
debug:
var: dd_download_url
when: not datadog_skip_windows_install

- include_tasks: pkg-windows-opts.yml
when: not datadog_skip_windows_install

- name: pre-Delete temporary msi
win_file:
path: '%TEMP%\ddagent.msi'
state: absent
when: not datadog_skip_windows_install

- name: Download windows datadog agent
win_get_url:
url: "{{ dd_download_url }}"
dest: '%TEMP%\ddagent.msi'
register: download_msi_result

when: not datadog_skip_windows_install

- name: Install downloaded agent
win_package:
path: "{{ download_msi_result.dest }}"
arguments: "{{ win_install_args }}"
when: not ansible_check_mode
when: (not datadog_skip_windows_install) and (not ansible_check_mode)

- name: Delete temporary msi
win_file:
path: "{{ download_msi_result.dest }}"
state: absent
when: download_msi_result.status_code == 200
when: (not datadog_skip_windows_install) and (download_msi_result.status_code == 200)