blob: 49622d824dc1d9843aeec5ca76ba69cf0f34e3e0 [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
Tim Windelschmidt0e749612023-08-07 17:42:59 +000030 AND machine_provided.provider = $2
Serge Bazanski60a00a92023-01-03 13:45:20 +010031LIMIT $1;
32
33-- name: GetMachineForAgentRecovery :many
Serge Bazanski10383132023-02-20 15:39:45 +010034-- Get machines that need agent restarted after something went wrong. Either
Serge Bazanski60a00a92023-01-03 13:45:20 +010035-- the agent started but never responded, or the agent stopped responding at
Serge Bazanski10383132023-02-20 15:39:45 +010036-- some point, or the machine got rebooted or somehow else lost the agent. Assume
37-- some work needs to be performed on the shepherd side to diagnose and recover
Serge Bazanski60a00a92023-01-03 13:45:20 +010038-- whatever state the machine truly is in.
Serge Bazanski6f599512023-04-26 19:08:19 +020039-- ONCHANGE(queries_stats.sql): constraints must be kept in sync with StatsMachinesForAgentRecovery.
Serge Bazanski60a00a92023-01-03 13:45:20 +010040SELECT
41 machine_provided.*
42FROM machines
43INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
Serge Bazanski00cf57d2023-04-20 11:19:00 +020044LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery')
45LEFT 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 +020046INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
Serge Bazanski60a00a92023-01-03 13:45:20 +010047LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
Serge Bazanskic59c5152023-05-11 16:15:30 +020048LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
Tim Windelschmidt53087302023-06-27 16:36:31 +020049LEFT 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 +010050WHERE
Serge Bazanskic59c5152023-05-11 16:15:30 +020051 -- Do not recover machines that have a fulfilled OS installation request.
52 (
53 machine_os_installation_request.machine_id IS NULL
54 OR machine_os_installation_request.generation IS DISTINCT FROM machine_os_installation_report.generation
55 )
Serge Bazanski60a00a92023-01-03 13:45:20 +010056 AND (
57 -- No heartbeat 30 minutes after starting the agent.
58 (
59 machine_agent_heartbeat.machine_id IS NULL
60 AND now() > (machine_agent_started.agent_started_at + interval '30 minutes')
61 )
62 -- Heartbeats ceased for 10 minutes.
63 OR (
64 machine_agent_heartbeat.machine_id IS NOT NULL
65 AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes')
66 )
67 )
68 AND work.machine_id IS NULL
Serge Bazanskia9580a72023-01-12 14:44:35 +010069 AND work_backoff.machine_id IS NULL
Tim Windelschmidt0e749612023-08-07 17:42:59 +000070 AND machine_provided.provider = $2
Serge Bazanski60a00a92023-01-03 13:45:20 +010071LIMIT $1;
72
73-- name: AuthenticateAgentConnection :many
74-- Used by bmdb server to verify incoming connections.
75SELECT
76 machine_agent_started.*
77FROM machines
78INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
79WHERE
80 machines.machine_id = $1
81 AND machine_agent_started.agent_public_key = $2;
82
83-- name: GetExactMachineForOSInstallation :many
84-- Get OS installation request for a given machine ID. Used by the bmdb server
85-- to tell agent whether there's a pending installation request for the machine
86-- it's running on.
87SELECT
88 machine_os_installation_request.*
89FROM machines
90LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
91LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
92WHERE
93 -- We are only interested in one concrete machine.
94 machines.machine_id = $1
95 -- We must have an installation request.
96 AND machine_os_installation_request.machine_id IS NOT NULL
97 -- And we either must have no installation report, or the installation
98 -- report's generation must not match the installation request's generation.
99 AND (
100 machine_os_installation_report.machine_id IS NULL
101 OR machine_os_installation_report.generation != machine_os_installation_request.generation
102 )
Serge Bazanskicaa12082023-02-16 14:54:04 +0100103LIMIT $2;