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 | 1038313 | 2023-02-20 15:39:45 +0100 | [diff] [blame^] | 15 | LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdAccess' |
| 16 | LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdAccess' |
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 |
| 18 | WHERE |
| 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 Bazanski | a9580a7 | 2023-01-12 14:44:35 +0100 | [diff] [blame] | 23 | AND work_backoff.machine_id IS NULL |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 24 | LIMIT $1; |
| 25 | |
| 26 | -- name: GetMachineForAgentRecovery :many |
Serge Bazanski | 1038313 | 2023-02-20 15:39:45 +0100 | [diff] [blame^] | 27 | -- Get machines that need agent restarted after something went wrong. Either |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 28 | -- the agent started but never responded, or the agent stopped responding at |
Serge Bazanski | 1038313 | 2023-02-20 15:39:45 +0100 | [diff] [blame^] | 29 | -- 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 Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 31 | -- whatever state the machine truly is in. |
| 32 | SELECT |
| 33 | machine_provided.* |
| 34 | FROM machines |
| 35 | INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id |
Serge Bazanski | 1038313 | 2023-02-20 15:39:45 +0100 | [diff] [blame^] | 36 | LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdAccess' |
| 37 | LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdAccess' |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 38 | LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 39 | LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id |
| 40 | WHERE |
| 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 Bazanski | a9580a7 | 2023-01-12 14:44:35 +0100 | [diff] [blame] | 56 | AND work_backoff.machine_id IS NULL |
Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame] | 57 | LIMIT $1; |
| 58 | |
| 59 | -- name: AuthenticateAgentConnection :many |
| 60 | -- Used by bmdb server to verify incoming connections. |
| 61 | SELECT |
| 62 | machine_agent_started.* |
| 63 | FROM machines |
| 64 | INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 65 | WHERE |
| 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. |
| 73 | SELECT |
| 74 | machine_os_installation_request.* |
| 75 | FROM machines |
| 76 | LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id |
| 77 | LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id |
| 78 | WHERE |
| 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 Bazanski | caa1208 | 2023-02-16 14:54:04 +0100 | [diff] [blame] | 89 | LIMIT $2; |