Serge Bazanski | 60a00a9 | 2023-01-03 13:45:20 +0100 | [diff] [blame^] | 1 | -- name: GetMachinesForAgentStart :many |
| 2 | -- Get machines that need agent installed for the first time. Machine can be |
| 3 | -- assumed to be 'new', with no previous attempts or failures. |
| 4 | SELECT |
| 5 | machine_provided.* |
| 6 | FROM machines |
| 7 | INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id |
| 8 | LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall' |
| 9 | LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 10 | WHERE |
| 11 | machine_agent_started.machine_id IS NULL |
| 12 | -- TODO(q3k): exclude machines which are not expected to run the agent (eg. |
| 13 | -- are already exposed to a user). |
| 14 | AND work.machine_id IS NULL |
| 15 | LIMIT $1; |
| 16 | |
| 17 | -- name: GetMachineForAgentRecovery :many |
| 18 | -- Get machines that need agent installed after something went wrong. Either |
| 19 | -- the agent started but never responded, or the agent stopped responding at |
| 20 | -- some point, or the machine is being reinstalled after failure. Assume some |
| 21 | -- work needs to be performed on the shepherd side to diagnose and recover |
| 22 | -- whatever state the machine truly is in. |
| 23 | SELECT |
| 24 | machine_provided.* |
| 25 | FROM machines |
| 26 | INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id |
| 27 | LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall' |
| 28 | LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 29 | LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id |
| 30 | WHERE |
| 31 | -- Only act on machines where the agent is expected to have been started. |
| 32 | machine_agent_started.machine_id IS NOT NULL |
| 33 | AND ( |
| 34 | -- No heartbeat 30 minutes after starting the agent. |
| 35 | ( |
| 36 | machine_agent_heartbeat.machine_id IS NULL |
| 37 | AND now() > (machine_agent_started.agent_started_at + interval '30 minutes') |
| 38 | ) |
| 39 | -- Heartbeats ceased for 10 minutes. |
| 40 | OR ( |
| 41 | machine_agent_heartbeat.machine_id IS NOT NULL |
| 42 | AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes') |
| 43 | ) |
| 44 | ) |
| 45 | AND work.machine_id IS NULL |
| 46 | LIMIT $1; |
| 47 | |
| 48 | -- name: AuthenticateAgentConnection :many |
| 49 | -- Used by bmdb server to verify incoming connections. |
| 50 | SELECT |
| 51 | machine_agent_started.* |
| 52 | FROM machines |
| 53 | INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 54 | WHERE |
| 55 | machines.machine_id = $1 |
| 56 | AND machine_agent_started.agent_public_key = $2; |
| 57 | |
| 58 | -- name: GetExactMachineForOSInstallation :many |
| 59 | -- Get OS installation request for a given machine ID. Used by the bmdb server |
| 60 | -- to tell agent whether there's a pending installation request for the machine |
| 61 | -- it's running on. |
| 62 | SELECT |
| 63 | machine_os_installation_request.* |
| 64 | FROM machines |
| 65 | LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id |
| 66 | LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id |
| 67 | WHERE |
| 68 | -- We are only interested in one concrete machine. |
| 69 | machines.machine_id = $1 |
| 70 | -- We must have an installation request. |
| 71 | AND machine_os_installation_request.machine_id IS NOT NULL |
| 72 | -- And we either must have no installation report, or the installation |
| 73 | -- report's generation must not match the installation request's generation. |
| 74 | AND ( |
| 75 | machine_os_installation_report.machine_id IS NULL |
| 76 | OR machine_os_installation_report.generation != machine_os_installation_request.generation |
| 77 | ) |
| 78 | LIMIT $2; |
| 79 | |