diff --git a/x-pack/plugins/fleet/server/services/agents/actions.ts b/x-pack/plugins/fleet/server/services/agents/actions.ts index c4f3530892543a..acd9ecb8b11d37 100644 --- a/x-pack/plugins/fleet/server/services/agents/actions.ts +++ b/x-pack/plugins/fleet/server/services/agents/actions.ts @@ -123,16 +123,6 @@ export async function cancelAgentAction(esClient: ElasticsearchClient, actionId: if (!hit._source || !hit._source.agents || !hit._source.action_id) { continue; } - await createAgentAction(esClient, { - id: cancelActionId, - type: 'CANCEL', - agents: hit._source.agents, - data: { - target_id: hit._source.action_id, - }, - created_at: now, - expiration: hit._source.expiration, - }); if (hit._source.type === 'UPGRADE') { await bulkUpdateAgents( esClient, @@ -145,6 +135,16 @@ export async function cancelAgentAction(esClient: ElasticsearchClient, actionId: })) ); } + await createAgentAction(esClient, { + id: cancelActionId, + type: 'CANCEL', + agents: hit._source.agents, + data: { + target_id: hit._source.action_id, + }, + created_at: now, + expiration: hit._source.expiration, + }); } return { diff --git a/x-pack/plugins/fleet/server/services/agents/unenroll.ts b/x-pack/plugins/fleet/server/services/agents/unenroll.ts index d8a5b4b32a101b..990ce903e08aa0 100644 --- a/x-pack/plugins/fleet/server/services/agents/unenroll.ts +++ b/x-pack/plugins/fleet/server/services/agents/unenroll.ts @@ -55,14 +55,14 @@ export async function unenrollAgent( return forceUnenrollAgent(soClient, esClient, agentId); } const now = new Date().toISOString(); + await updateAgent(esClient, agentId, { + unenrollment_started_at: now, + }); await createAgentAction(esClient, { agents: [agentId], created_at: now, type: 'UNENROLL', }); - await updateAgent(esClient, agentId, { - unenrollment_started_at: now, - }); } export async function unenrollAgents( @@ -128,6 +128,17 @@ async function unenrollBatch( }, []); const now = new Date().toISOString(); + + // Update the necessary agents + const updateData = options.revoke + ? { unenrolled_at: now, active: false } + : { unenrollment_started_at: now }; + + await bulkUpdateAgents( + esClient, + agentsToUpdate.map(({ id }) => ({ agentId: id, data: updateData })) + ); + if (options.revoke) { // Get all API keys that need to be invalidated await invalidateAPIKeysForAgents(agentsToUpdate); @@ -140,16 +151,6 @@ async function unenrollBatch( }); } - // Update the necessary agents - const updateData = options.revoke - ? { unenrolled_at: now, active: false } - : { unenrollment_started_at: now }; - - await bulkUpdateAgents( - esClient, - agentsToUpdate.map(({ id }) => ({ agentId: id, data: updateData })) - ); - return { items: errorsToResults(givenAgents, outgoingErrors, undefined, skipSuccess), }; diff --git a/x-pack/plugins/fleet/server/services/agents/upgrade.ts b/x-pack/plugins/fleet/server/services/agents/upgrade.ts index efe363b2b46b85..a2e46211efff29 100644 --- a/x-pack/plugins/fleet/server/services/agents/upgrade.ts +++ b/x-pack/plugins/fleet/server/services/agents/upgrade.ts @@ -58,6 +58,10 @@ export async function sendUpgradeAgentAction({ `Cannot upgrade agent ${agentId} in hosted agent policy ${agentPolicy.id}` ); } + await updateAgent(esClient, agentId, { + upgraded_at: null, + upgrade_started_at: now, + }); await createAgentAction(esClient, { agents: [agentId], @@ -66,10 +70,6 @@ export async function sendUpgradeAgentAction({ ack_data: data, type: 'UPGRADE', }); - await updateAgent(esClient, agentId, { - upgraded_at: null, - upgrade_started_at: now, - }); } export async function sendUpgradeAgentsActions( @@ -192,16 +192,6 @@ async function upgradeBatch( options.upgradeDurationSeconds ); - await createAgentAction(esClient, { - id: options.actionId, - created_at: now, - data, - ack_data: data, - type: 'UPGRADE', - agents: agentsToUpdate.map((agent) => agent.id), - ...rollingUpgradeOptions, - }); - await bulkUpdateAgents( esClient, agentsToUpdate.map((agent) => ({ @@ -213,6 +203,16 @@ async function upgradeBatch( })) ); + await createAgentAction(esClient, { + id: options.actionId, + created_at: now, + data, + ack_data: data, + type: 'UPGRADE', + agents: agentsToUpdate.map((agent) => agent.id), + ...rollingUpgradeOptions, + }); + return { items: errorsToResults( givenAgents,