blob: b7b60fee8492f930616423bd048df9df42b37665 [file] [log] [blame]
Serge Bazanski60a00a92023-01-03 13:45:20 +01001-- name: GetMachinesForAgentStart :many
2-- Get machines that need agent installed for the first time. Machine can be
3-- assumed to be 'new', with no previous attempts or failures.
4SELECT
5 machine_provided.*
6FROM machines
7INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
8LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall'
Serge Bazanskia9580a72023-01-12 14:44:35 +01009LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdInstall'
Serge Bazanski60a00a92023-01-03 13:45:20 +010010LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
11WHERE
12 machine_agent_started.machine_id IS NULL
13 -- TODO(q3k): exclude machines which are not expected to run the agent (eg.
14 -- are already exposed to a user).
15 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010016 AND work_backoff.machine_id IS NULL
Serge Bazanski60a00a92023-01-03 13:45:20 +010017LIMIT $1;
18
19-- name: GetMachineForAgentRecovery :many
20-- Get machines that need agent installed after something went wrong. Either
21-- the agent started but never responded, or the agent stopped responding at
22-- some point, or the machine is being reinstalled after failure. Assume some
23-- work needs to be performed on the shepherd side to diagnose and recover
24-- whatever state the machine truly is in.
25SELECT
26 machine_provided.*
27FROM machines
28INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
29LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall'
Serge Bazanskia9580a72023-01-12 14:44:35 +010030LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdInstall'
Serge Bazanski60a00a92023-01-03 13:45:20 +010031LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
32LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
33WHERE
34 -- Only act on machines where the agent is expected to have been started.
35 machine_agent_started.machine_id IS NOT NULL
36 AND (
37 -- No heartbeat 30 minutes after starting the agent.
38 (
39 machine_agent_heartbeat.machine_id IS NULL
40 AND now() > (machine_agent_started.agent_started_at + interval '30 minutes')
41 )
42 -- Heartbeats ceased for 10 minutes.
43 OR (
44 machine_agent_heartbeat.machine_id IS NOT NULL
45 AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes')
46 )
47 )
48 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010049 AND work_backoff.machine_id IS NULL
Serge Bazanski60a00a92023-01-03 13:45:20 +010050LIMIT $1;
51
52-- name: AuthenticateAgentConnection :many
53-- Used by bmdb server to verify incoming connections.
54SELECT
55 machine_agent_started.*
56FROM machines
57INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
58WHERE
59 machines.machine_id = $1
60 AND machine_agent_started.agent_public_key = $2;
61
62-- name: GetExactMachineForOSInstallation :many
63-- Get OS installation request for a given machine ID. Used by the bmdb server
64-- to tell agent whether there's a pending installation request for the machine
65-- it's running on.
66SELECT
67 machine_os_installation_request.*
68FROM machines
69LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
70LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
71WHERE
72 -- We are only interested in one concrete machine.
73 machines.machine_id = $1
74 -- We must have an installation request.
75 AND machine_os_installation_request.machine_id IS NOT NULL
76 -- And we either must have no installation report, or the installation
77 -- report's generation must not match the installation request's generation.
78 AND (
79 machine_os_installation_report.machine_id IS NULL
80 OR machine_os_installation_report.generation != machine_os_installation_request.generation
81 )
Serge Bazanskia9580a72023-01-12 14:44:35 +010082LIMIT $2;