Serge Bazanski | caa1208 | 2023-02-16 14:54:04 +0100 | [diff] [blame] | 1 | -- name: GetProvidedMachines :many |
| 2 | SELECT |
| 3 | machine_provided.* |
| 4 | FROM machines |
| 5 | INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id |
| 6 | WHERE machine_provided.provider = $1; |
| 7 | |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 8 | -- name: GetMachinesForAgentStart :many |
Serge Bazanski | 1038313 | 2023-02-20 15:39:45 +0100 | [diff] [blame] | 9 | -- Get machines that need agent started for the first time. Machine can be |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 10 | -- assumed to be 'new', with no previous attempts or failures. |
| 11 | SELECT |
| 12 | machine_provided.* |
| 13 | FROM machines |
| 14 | INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id |
Serge Bazanski | 00cf57d | 2023-04-20 11:19:00 +0200 | [diff] [blame] | 15 | LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery') |
| 16 | LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdAgentStart' |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 17 | LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
Serge Bazanski | c59c515 | 2023-05-11 16:15:30 +0200 | [diff] [blame] | 18 | LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id |
| 19 | LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 20 | WHERE |
| 21 | machine_agent_started.machine_id IS NULL |
Serge Bazanski | c59c515 | 2023-05-11 16:15:30 +0200 | [diff] [blame] | 22 | -- 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 Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 27 | AND work.machine_id IS NULL |
Serge Bazanski | a9580a7 | 2023-01-12 14:44:35 +0100 | [diff] [blame] | 28 | AND work_backoff.machine_id IS NULL |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 29 | LIMIT $1; |
| 30 | |
| 31 | -- name: GetMachineForAgentRecovery :many |
Serge Bazanski | 1038313 | 2023-02-20 15:39:45 +0100 | [diff] [blame] | 32 | -- Get machines that need agent restarted after something went wrong. Either |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 33 | -- the agent started but never responded, or the agent stopped responding at |
Serge Bazanski | 1038313 | 2023-02-20 15:39:45 +0100 | [diff] [blame] | 34 | -- 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 Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 36 | -- whatever state the machine truly is in. |
| 37 | SELECT |
| 38 | machine_provided.* |
| 39 | FROM machines |
| 40 | INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id |
Serge Bazanski | 00cf57d | 2023-04-20 11:19:00 +0200 | [diff] [blame] | 41 | LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery') |
| 42 | LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdRecovery' |
Serge Bazanski | c59c515 | 2023-05-11 16:15:30 +0200 | [diff] [blame] | 43 | INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 44 | LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id |
Serge Bazanski | c59c515 | 2023-05-11 16:15:30 +0200 | [diff] [blame] | 45 | LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id |
| 46 | LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 47 | WHERE |
Serge Bazanski | c59c515 | 2023-05-11 16:15:30 +0200 | [diff] [blame] | 48 | -- 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 Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 53 | 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 Bazanski | a9580a7 | 2023-01-12 14:44:35 +0100 | [diff] [blame] | 66 | AND work_backoff.machine_id IS NULL |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 67 | LIMIT $1; |
| 68 | |
| 69 | -- name: AuthenticateAgentConnection :many |
| 70 | -- Used by bmdb server to verify incoming connections. |
| 71 | SELECT |
| 72 | machine_agent_started.* |
| 73 | FROM machines |
| 74 | INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 75 | WHERE |
| 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. |
| 83 | SELECT |
| 84 | machine_os_installation_request.* |
| 85 | FROM machines |
| 86 | LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id |
| 87 | LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id |
| 88 | WHERE |
| 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 Bazanski | caa1208 | 2023-02-16 14:54:04 +0100 | [diff] [blame] | 99 | LIMIT $2; |