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

Fix upcoming activities for ABM-deleted hosts #25530

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 changes/22353-abm-hosts-upcoming-activities
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Hosts that are restored from ABM no longer have old activities in their feed
16 changes: 11 additions & 5 deletions server/datastore/mysql/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,21 @@ func (ds *Datastore) ListHostUpcomingActivities(ctx context.Context, hostID uint
LEFT OUTER JOIN
host_software_installs hsi ON hsi.execution_id = hsr.execution_id
WHERE hsr.host_id = :host_id AND
hsr.host_deleted_at IS NULL AND
exit_code IS NULL AND
hsi.execution_id IS NULL AND
(sync_request = 0 OR hsr.created_at >= DATE_SUB(NOW(), INTERVAL :max_wait_time SECOND))`,
`SELECT
COUNT(*) c
FROM host_software_installs hsi
WHERE hsi.host_id = :host_id AND hsi.software_installer_id IS NOT NULL AND
hsi.host_deleted_at IS NULL AND
hsi.status = :software_status_install_pending`,
`SELECT
COUNT(*) c
FROM host_software_installs hsi
WHERE hsi.host_id = :host_id AND hsi.software_installer_id IS NOT NULL AND
hsi.host_deleted_at IS NULL AND
hsi.status = :software_status_uninstall_pending`,
`
SELECT
Expand Down Expand Up @@ -334,6 +337,7 @@ func (ds *Datastore) ListHostUpcomingActivities(ctx context.Context, hostID uint
setup_experience_scripts ses ON ses.id = hsr.setup_experience_script_id
WHERE
hsr.host_id = :host_id AND
hsr.host_deleted_at IS NULL AND
hsr.exit_code IS NULL AND
(
hsr.sync_request = 0 OR
Expand Down Expand Up @@ -377,6 +381,7 @@ func (ds *Datastore) ListHostUpcomingActivities(ctx context.Context, hostID uint
host_display_names hdn ON hdn.host_id = hsi.host_id
WHERE
hsi.host_id = :host_id AND
hsi.host_deleted_at IS NULL AND
hsi.status = :software_status_install_pending
`,
// list pending software uninstalls
Expand Down Expand Up @@ -413,6 +418,7 @@ func (ds *Datastore) ListHostUpcomingActivities(ctx context.Context, hostID uint
host_display_names hdn ON hdn.host_id = hsi.host_id
WHERE
hsi.host_id = :host_id AND
hsi.host_deleted_at IS NULL AND
hsi.status = :software_status_uninstall_pending
`,
// list pending VPP installs
Expand All @@ -439,15 +445,15 @@ SELECT
) AS details
FROM
host_vpp_software_installs hvsi
INNER JOIN
INNER JOIN
nano_view_queue nvq ON nvq.command_uuid = hvsi.command_uuid
LEFT OUTER JOIN
LEFT OUTER JOIN
users u ON hvsi.user_id = u.id
LEFT OUTER JOIN
LEFT OUTER JOIN
host_display_names hdn ON hdn.host_id = hvsi.host_id
LEFT OUTER JOIN
LEFT OUTER JOIN
vpp_apps vpa ON hvsi.adam_id = vpa.adam_id AND hvsi.platform = vpa.platform
LEFT OUTER JOIN
LEFT OUTER JOIN
software_titles st ON st.id = vpa.title_id
WHERE
nvq.status IS NULL
Expand Down
3 changes: 2 additions & 1 deletion server/datastore/mysql/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func (ds *Datastore) ListPendingHostScriptExecutions(ctx context.Context, hostID
host_script_results
WHERE
host_id = ? AND
host_deleted_at IS NULL AND
%s
%s
ORDER BY
Expand Down Expand Up @@ -504,7 +505,7 @@ func (ds *Datastore) deletePendingHostScriptExecutionsForPolicy(ctx context.Cont
deleteStmt := fmt.Sprintf(`
DELETE FROM
host_script_results
WHERE
WHERE
policy_id = ? AND
script_id IN (
SELECT id FROM scripts WHERE scripts.global_or_team_id = ?
Expand Down
24 changes: 12 additions & 12 deletions server/datastore/mysql/software.go
Original file line number Diff line number Diff line change
Expand Up @@ -2259,7 +2259,7 @@ INNER JOIN software_cve scve ON scve.software_id = s.id
-- get the status of the latest attempt; 27-1 is the length of the timestamp
SUBSTRING(MAX(CONCAT(created_at, COALESCE(status, ''))), 27) AS hsi_status
FROM host_software_installs
WHERE host_id = :host_id AND removed = 0
WHERE host_id = :host_id AND removed = 0 AND host_deleted_at IS NULL
GROUP BY host_id, software_installer_id
UNION
-- get latest install attempt
Expand All @@ -2271,7 +2271,7 @@ INNER JOIN software_cve scve ON scve.software_id = s.id
NULL as hsi_uninstalled_at, NULL as hsi_uninstall_execution_id,
NULL as hsi_status
FROM host_software_installs
WHERE host_id = :host_id AND removed = 0 AND uninstall = 0
WHERE host_id = :host_id AND removed = 0 AND uninstall = 0 AND host_deleted_at IS NULL
GROUP BY host_id, software_installer_id
UNION
-- get latest uninstall attempt
Expand All @@ -2283,7 +2283,7 @@ INNER JOIN software_cve scve ON scve.software_id = s.id
SUBSTRING(MAX(CONCAT(created_at, execution_id)), 27) AS hsi_uninstall_execution_id,
NULL as hsi_status
FROM host_software_installs
WHERE host_id = :host_id AND removed = 0 AND uninstall = 1
WHERE host_id = :host_id AND removed = 0 AND uninstall = 1 AND host_deleted_at IS NULL
GROUP BY host_id, software_installer_id
) as hsi_group
GROUP BY hsi_group.host_id, hsi_group.software_installer_id
Expand Down Expand Up @@ -2311,7 +2311,7 @@ INNER JOIN software_cve scve ON scve.software_id = s.id
-- on host (via installer or VPP app). If only available for install is
-- requested, then the software installed on host clause is empty.
( %s hsi.host_id IS NOT NULL OR hvsi.host_id IS NOT NULL )
AND
AND
-- label membership check
(
-- do the label membership check only for software installers
Expand Down Expand Up @@ -2346,9 +2346,9 @@ INNER JOIN software_cve scve ON scve.software_id = s.id

UNION

-- exclude any, ignore software that depends on labels created
-- _after_ the label_updated_at timestamp of the host (because
-- we don't have results for that label yet, the host may or may
-- exclude any, ignore software that depends on labels created
-- _after_ the label_updated_at timestamp of the host (because
-- we don't have results for that label yet, the host may or may
-- not be a member).
SELECT
COUNT(*) AS count_installer_labels,
Expand All @@ -2358,7 +2358,7 @@ INNER JOIN software_cve scve ON scve.software_id = s.id
software_installer_labels sil
LEFT OUTER JOIN labels lbl
ON lbl.id = sil.label_id
LEFT OUTER JOIN label_membership lm
LEFT OUTER JOIN label_membership lm
ON lm.label_id = sil.label_id AND lm.host_id = :host_id
WHERE
sil.software_installer_id = si.id
Expand Down Expand Up @@ -2473,9 +2473,9 @@ INNER JOIN software_cve scve ON scve.software_id = s.id

UNION

-- exclude any, ignore software that depends on labels created
-- _after_ the label_updated_at timestamp of the host (because
-- we don't have results for that label yet, the host may or may
-- exclude any, ignore software that depends on labels created
-- _after_ the label_updated_at timestamp of the host (because
-- we don't have results for that label yet, the host may or may
-- not be a member).
SELECT
COUNT(*) AS count_installer_labels,
Expand All @@ -2485,7 +2485,7 @@ INNER JOIN software_cve scve ON scve.software_id = s.id
software_installer_labels sil
LEFT OUTER JOIN labels lbl
ON lbl.id = sil.label_id
LEFT OUTER JOIN label_membership lm
LEFT OUTER JOIN label_membership lm
ON lm.label_id = sil.label_id AND lm.host_id = :host_id
WHERE
sil.software_installer_id = si.id
Expand Down
24 changes: 13 additions & 11 deletions server/datastore/mysql/software_installers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (ds *Datastore) ListPendingSoftwareInstalls(ctx context.Context, hostID uin
host_software_installs
WHERE
host_id = ?
AND
host_deleted_at IS NULL
AND
status = ?
ORDER BY
Expand Down Expand Up @@ -1020,7 +1022,7 @@ func (ds *Datastore) GetHostLastInstallData(ctx context.Context, hostID, install
MAX(id)
FROM host_software_installs
WHERE
software_installer_id = :installer_id AND host_id = :host_id
software_installer_id = :installer_id AND host_id = :host_id AND host_deleted_at IS NULL
GROUP BY
host_id, software_installer_id)
`
Expand Down Expand Up @@ -1709,24 +1711,24 @@ func (ds *Datastore) IsSoftwareInstallerLabelScoped(ctx context.Context, install

UNION

-- exclude any, ignore software that depends on labels created
-- _after_ the label_updated_at timestamp of the host (because
-- we don't have results for that label yet, the host may or may
-- exclude any, ignore software that depends on labels created
-- _after_ the label_updated_at timestamp of the host (because
-- we don't have results for that label yet, the host may or may
-- not be a member).
SELECT
COUNT(*) AS count_installer_labels,
COUNT(lm.label_id) AS count_host_labels,
SUM(CASE
WHEN
lbl.created_at IS NOT NULL AND (SELECT label_updated_at FROM hosts WHERE id = :host_id) >= lbl.created_at THEN 1
ELSE
0
SUM(CASE
WHEN
lbl.created_at IS NOT NULL AND (SELECT label_updated_at FROM hosts WHERE id = :host_id) >= lbl.created_at THEN 1
ELSE
0
END) as count_host_updated_after_labels
FROM
software_installer_labels sil
LEFT OUTER JOIN labels lbl
ON lbl.id = sil.label_id
LEFT OUTER JOIN label_membership lm
LEFT OUTER JOIN label_membership lm
ON lm.label_id = sil.label_id AND lm.host_id = :host_id
WHERE
sil.software_installer_id = :installer_id
Expand Down Expand Up @@ -1756,7 +1758,7 @@ func (ds *Datastore) IsSoftwareInstallerLabelScoped(ctx context.Context, install
return res, nil
}

const labelScopedFilter = `
const labelScopedFilter = `
SELECT
1
FROM (
Expand Down
Loading