blob: 8400d4e72b4b054fbf7b5fc3648269dfc2cd6e59 [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.
Serge Bazanski6f599512023-04-26 19:08:19 +020011-- ONCHANGE(queries_stats.sql): constraints must be kept in sync with StatsMachinesForAgentStart.
Serge Bazanski60a00a92023-01-03 13:45:20 +010012SELECT
13 machine_provided.*
14FROM machines
15INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
Serge Bazanski00cf57d2023-04-20 11:19:00 +020016LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery')
17LEFT 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 +010018LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
Serge Bazanskic59c5152023-05-11 16:15:30 +020019LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
20LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
Serge Bazanski60a00a92023-01-03 13:45:20 +010021WHERE
22 machine_agent_started.machine_id IS NULL
Serge Bazanskic59c5152023-05-11 16:15:30 +020023 -- Do not start on machines that have a fulfilled OS installation request.
24 AND (
25 machine_os_installation_request.machine_id IS NULL
26 OR machine_os_installation_request.generation IS DISTINCT FROM machine_os_installation_report.generation
27 )
Serge Bazanski60a00a92023-01-03 13:45:20 +010028 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010029 AND work_backoff.machine_id IS NULL
Serge Bazanski60a00a92023-01-03 13:45:20 +010030LIMIT $1;
31
32-- name: GetMachineForAgentRecovery :many
Serge Bazanski10383132023-02-20 15:39:45 +010033-- Get machines that need agent restarted after something went wrong. Either
Serge Bazanski60a00a92023-01-03 13:45:20 +010034-- the agent started but never responded, or the agent stopped responding at
Serge Bazanski10383132023-02-20 15:39:45 +010035-- some point, or the machine got rebooted or somehow else lost the agent. Assume
36-- some work needs to be performed on the shepherd side to diagnose and recover
Serge Bazanski60a00a92023-01-03 13:45:20 +010037-- whatever state the machine truly is in.
Serge Bazanski6f599512023-04-26 19:08:19 +020038-- ONCHANGE(queries_stats.sql): constraints must be kept in sync with StatsMachinesForAgentRecovery.
Serge Bazanski60a00a92023-01-03 13:45:20 +010039SELECT
40 machine_provided.*
41FROM machines
42INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
Serge Bazanski00cf57d2023-04-20 11:19:00 +020043LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery')
44LEFT 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 +020045INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
Serge Bazanski60a00a92023-01-03 13:45:20 +010046LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
Serge Bazanskic59c5152023-05-11 16:15:30 +020047LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
Tim Windelschmidt53087302023-06-27 16:36:31 +020048LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id AND machine_os_installation_report.os_installation_result = 'Success'
Serge Bazanski60a00a92023-01-03 13:45:20 +010049WHERE
Serge Bazanskic59c5152023-05-11 16:15:30 +020050 -- Do not recover machines that have a fulfilled OS installation request.
51 (
52 machine_os_installation_request.machine_id IS NULL
53 OR machine_os_installation_request.generation IS DISTINCT FROM machine_os_installation_report.generation
54 )
Serge Bazanski60a00a92023-01-03 13:45:20 +010055 AND (
56 -- No heartbeat 30 minutes after starting the agent.
57 (
58 machine_agent_heartbeat.machine_id IS NULL
59 AND now() > (machine_agent_started.agent_started_at + interval '30 minutes')
60 )
61 -- Heartbeats ceased for 10 minutes.
62 OR (
63 machine_agent_heartbeat.machine_id IS NOT NULL
64 AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes')
65 )
66 )
67 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010068 AND work_backoff.machine_id IS NULL
Serge Bazanski60a00a92023-01-03 13:45:20 +010069LIMIT $1;
70
71-- name: AuthenticateAgentConnection :many
72-- Used by bmdb server to verify incoming connections.
73SELECT
74 machine_agent_started.*
75FROM machines
76INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
77WHERE
78 machines.machine_id = $1
79 AND machine_agent_started.agent_public_key = $2;
80
81-- name: GetExactMachineForOSInstallation :many
82-- Get OS installation request for a given machine ID. Used by the bmdb server
83-- to tell agent whether there's a pending installation request for the machine
84-- it's running on.
85SELECT
86 machine_os_installation_request.*
87FROM machines
88LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
89LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
90WHERE
91 -- We are only interested in one concrete machine.
92 machines.machine_id = $1
93 -- We must have an installation request.
94 AND machine_os_installation_request.machine_id IS NOT NULL
95 -- And we either must have no installation report, or the installation
96 -- report's generation must not match the installation request's generation.
97 AND (
98 machine_os_installation_report.machine_id IS NULL
99 OR machine_os_installation_report.generation != machine_os_installation_request.generation
100 )
Serge Bazanskicaa12082023-02-16 14:54:04 +0100101LIMIT $2;