Skip to content

Commit

Permalink
changed order of update agents and actions
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Sep 30, 2022
1 parent 22830e4 commit 0a3adc6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
20 changes: 10 additions & 10 deletions x-pack/plugins/fleet/server/services/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
27 changes: 14 additions & 13 deletions x-pack/plugins/fleet/server/services/agents/unenroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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),
};
Expand Down
28 changes: 14 additions & 14 deletions x-pack/plugins/fleet/server/services/agents/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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(
Expand Down Expand Up @@ -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) => ({
Expand All @@ -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,
Expand Down

0 comments on commit 0a3adc6

Please sign in to comment.