blob: 07ebf777768333120417ac4368e49aea59654a40 [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
18WHERE
19 machine_agent_started.machine_id IS NULL
20 -- TODO(q3k): exclude machines which are not expected to run the agent (eg.
21 -- are already exposed to a user).
22 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010023 AND work_backoff.machine_id IS NULL
Serge Bazanski60a00a92023-01-03 13:45:20 +010024LIMIT $1;
25
26-- name: GetMachineForAgentRecovery :many
Serge Bazanski10383132023-02-20 15:39:45 +010027-- Get machines that need agent restarted after something went wrong. Either
Serge Bazanski60a00a92023-01-03 13:45:20 +010028-- the agent started but never responded, or the agent stopped responding at
Serge Bazanski10383132023-02-20 15:39:45 +010029-- some point, or the machine got rebooted or somehow else lost the agent. Assume
30-- some work needs to be performed on the shepherd side to diagnose and recover
Serge Bazanski60a00a92023-01-03 13:45:20 +010031-- whatever state the machine truly is in.
32SELECT
33 machine_provided.*
34FROM machines
35INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
Serge Bazanski00cf57d2023-04-20 11:19:00 +020036LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery')
37LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdRecovery'
Serge Bazanski60a00a92023-01-03 13:45:20 +010038LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
39LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
40WHERE
41 -- Only act on machines where the agent is expected to have been started.
42 machine_agent_started.machine_id IS NOT NULL
43 AND (
44 -- No heartbeat 30 minutes after starting the agent.
45 (
46 machine_agent_heartbeat.machine_id IS NULL
47 AND now() > (machine_agent_started.agent_started_at + interval '30 minutes')
48 )
49 -- Heartbeats ceased for 10 minutes.
50 OR (
51 machine_agent_heartbeat.machine_id IS NOT NULL
52 AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes')
53 )
54 )
55 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010056 AND work_backoff.machine_id IS NULL
Serge Bazanski60a00a92023-01-03 13:45:20 +010057LIMIT $1;
58
59-- name: AuthenticateAgentConnection :many
60-- Used by bmdb server to verify incoming connections.
61SELECT
62 machine_agent_started.*
63FROM machines
64INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
65WHERE
66 machines.machine_id = $1
67 AND machine_agent_started.agent_public_key = $2;
68
69-- name: GetExactMachineForOSInstallation :many
70-- Get OS installation request for a given machine ID. Used by the bmdb server
71-- to tell agent whether there's a pending installation request for the machine
72-- it's running on.
73SELECT
74 machine_os_installation_request.*
75FROM machines
76LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
77LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
78WHERE
79 -- We are only interested in one concrete machine.
80 machines.machine_id = $1
81 -- We must have an installation request.
82 AND machine_os_installation_request.machine_id IS NOT NULL
83 -- And we either must have no installation report, or the installation
84 -- report's generation must not match the installation request's generation.
85 AND (
86 machine_os_installation_report.machine_id IS NULL
87 OR machine_os_installation_report.generation != machine_os_installation_request.generation
88 )
Serge Bazanskicaa12082023-02-16 14:54:04 +010089LIMIT $2;