blob: 62e4a401486149e4874e0c09bad0bd575ae16659 [file] [log] [blame]
Serge Bazanskicaa12082023-02-16 14:54:04 +01001-- name: GetProvidedMachines :many
2SELECT
3 machine_provided.*
4FROM machines
5INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
6WHERE machine_provided.provider = $1;
7
Serge Bazanski60a00a92023-01-03 13:45:20 +01008-- name: GetMachinesForAgentStart :many
Serge Bazanski10383132023-02-20 15:39:45 +01009-- Get machines that need agent started for the first time. Machine can be
Serge Bazanski60a00a92023-01-03 13:45:20 +010010-- assumed to be 'new', with no previous attempts or failures.
11SELECT
12 machine_provided.*
13FROM machines
14INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
Serge Bazanski00cf57d2023-04-20 11:19:00 +020015LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery')
16LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdAgentStart'
Serge Bazanski60a00a92023-01-03 13:45:20 +010017LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
Serge Bazanskic59c5152023-05-11 16:15:30 +020018LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
19LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
Serge Bazanski60a00a92023-01-03 13:45:20 +010020WHERE
21 machine_agent_started.machine_id IS NULL
Serge Bazanskic59c5152023-05-11 16:15:30 +020022 -- Do not start on machines that have a fulfilled OS installation request.
23 AND (
24 machine_os_installation_request.machine_id IS NULL
25 OR machine_os_installation_request.generation IS DISTINCT FROM machine_os_installation_report.generation
26 )
Serge Bazanski60a00a92023-01-03 13:45:20 +010027 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010028 AND work_backoff.machine_id IS NULL
Serge Bazanski60a00a92023-01-03 13:45:20 +010029LIMIT $1;
30
31-- name: GetMachineForAgentRecovery :many
Serge Bazanski10383132023-02-20 15:39:45 +010032-- Get machines that need agent restarted after something went wrong. Either
Serge Bazanski60a00a92023-01-03 13:45:20 +010033-- the agent started but never responded, or the agent stopped responding at
Serge Bazanski10383132023-02-20 15:39:45 +010034-- some point, or the machine got rebooted or somehow else lost the agent. Assume
35-- some work needs to be performed on the shepherd side to diagnose and recover
Serge Bazanski60a00a92023-01-03 13:45:20 +010036-- whatever state the machine truly is in.
37SELECT
38 machine_provided.*
39FROM machines
40INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
Serge Bazanski00cf57d2023-04-20 11:19:00 +020041LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery')
42LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdRecovery'
Serge Bazanskic59c5152023-05-11 16:15:30 +020043INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
Serge Bazanski60a00a92023-01-03 13:45:20 +010044LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
Serge Bazanskic59c5152023-05-11 16:15:30 +020045LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
46LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
Serge Bazanski60a00a92023-01-03 13:45:20 +010047WHERE
Serge Bazanskic59c5152023-05-11 16:15:30 +020048 -- Do not recover machines that have a fulfilled OS installation request.
49 (
50 machine_os_installation_request.machine_id IS NULL
51 OR machine_os_installation_request.generation IS DISTINCT FROM machine_os_installation_report.generation
52 )
Serge Bazanski60a00a92023-01-03 13:45:20 +010053 AND (
54 -- No heartbeat 30 minutes after starting the agent.
55 (
56 machine_agent_heartbeat.machine_id IS NULL
57 AND now() > (machine_agent_started.agent_started_at + interval '30 minutes')
58 )
59 -- Heartbeats ceased for 10 minutes.
60 OR (
61 machine_agent_heartbeat.machine_id IS NOT NULL
62 AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes')
63 )
64 )
65 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010066 AND work_backoff.machine_id IS NULL
Serge Bazanski60a00a92023-01-03 13:45:20 +010067LIMIT $1;
68
69-- name: AuthenticateAgentConnection :many
70-- Used by bmdb server to verify incoming connections.
71SELECT
72 machine_agent_started.*
73FROM machines
74INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
75WHERE
76 machines.machine_id = $1
77 AND machine_agent_started.agent_public_key = $2;
78
79-- name: GetExactMachineForOSInstallation :many
80-- Get OS installation request for a given machine ID. Used by the bmdb server
81-- to tell agent whether there's a pending installation request for the machine
82-- it's running on.
83SELECT
84 machine_os_installation_request.*
85FROM machines
86LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
87LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
88WHERE
89 -- We are only interested in one concrete machine.
90 machines.machine_id = $1
91 -- We must have an installation request.
92 AND machine_os_installation_request.machine_id IS NOT NULL
93 -- And we either must have no installation report, or the installation
94 -- report's generation must not match the installation request's generation.
95 AND (
96 machine_os_installation_report.machine_id IS NULL
97 OR machine_os_installation_report.generation != machine_os_installation_request.generation
98 )
Serge Bazanskicaa12082023-02-16 14:54:04 +010099LIMIT $2;