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