Serge Bazanski | 35e8d79 | 2022-10-11 11:32:30 +0200 | [diff] [blame] | 1 | -- name: NewMachine :one |
| 2 | INSERT INTO machines ( |
| 3 | machine_created_at |
| 4 | ) VALUES ( |
| 5 | now() |
| 6 | ) |
| 7 | RETURNING *; |
| 8 | |
| 9 | -- name: NewSession :one |
| 10 | INSERT INTO sessions ( |
| 11 | session_component_name, session_runtime_info, session_created_at, session_interval_seconds, session_deadline |
| 12 | ) VALUES ( |
| 13 | $1, $2, now(), $3, (now() + $3 * interval '1 second') |
| 14 | ) |
| 15 | RETURNING *; |
| 16 | |
| 17 | -- name: SessionPoke :exec |
| 18 | -- Update a given session with a new deadline. Must be called in the same |
| 19 | -- transaction as SessionCheck to ensure the session is still alive. |
| 20 | UPDATE sessions |
| 21 | SET session_deadline = now() + session_interval_seconds * interval '1 second' |
| 22 | WHERE session_id = $1; |
| 23 | |
| 24 | -- name: SessionCheck :many |
| 25 | -- SessionCheck returns a session by ID if that session is still valid (ie. its |
| 26 | -- deadline hasn't expired). |
| 27 | SELECT * |
| 28 | FROM sessions |
| 29 | WHERE session_id = $1 |
| 30 | AND session_deadline > now(); |
| 31 | |
| 32 | -- name: StartWork :exec |
| 33 | INSERT INTO work ( |
| 34 | machine_id, session_id, process |
| 35 | ) VALUES ( |
| 36 | $1, $2, $3 |
| 37 | ); |
| 38 | |
| 39 | -- name: FinishWork :exec |
| 40 | DELETE FROM work |
| 41 | WHERE machine_id = $1 |
| 42 | AND session_id = $2 |
| 43 | AND process = $3; |
| 44 | |
Serge Bazanski | 35e8d79 | 2022-10-11 11:32:30 +0200 | [diff] [blame] | 45 | -- name: MachineAddProvided :exec |
| 46 | INSERT INTO machine_provided ( |
| 47 | machine_id, provider, provider_id |
| 48 | ) VALUES ( |
| 49 | $1, $2, $3 |
| 50 | ); |
| 51 | |
Serge Bazanski | 68ca370 | 2022-11-02 17:30:44 +0100 | [diff] [blame] | 52 | -- name: MachineSetAgentStarted :exec |
| 53 | INSERT INTO machine_agent_started ( |
| 54 | machine_id, agent_started_at, agent_public_key |
| 55 | ) VALUES ( |
| 56 | $1, $2, $3 |
| 57 | ) ON CONFLICT (machine_id) DO UPDATE SET |
| 58 | agent_started_at = $2, |
| 59 | agent_public_key = $3 |
| 60 | ; |
| 61 | |
| 62 | -- name: MachineSetAgentHeartbeat :exec |
| 63 | INSERT INTO machine_agent_heartbeat ( |
| 64 | machine_id, agent_heartbeat_at |
| 65 | ) VALUES ( |
| 66 | $1, $2 |
| 67 | ) ON CONFLICT (machine_id) DO UPDATE SET |
| 68 | agent_heartbeat_at = $2 |
| 69 | ; |
| 70 | |
Serge Bazanski | 4abeb13 | 2022-10-11 11:32:19 +0200 | [diff] [blame] | 71 | -- name: MachineSetHardwareReport :exec |
| 72 | INSERT INTO machine_hardware_report ( |
| 73 | machine_id, hardware_report_raw |
| 74 | ) VALUES ( |
| 75 | $1, $2 |
| 76 | ) ON CONFLICT (machine_id) DO UPDATE SET |
| 77 | hardware_report_raw = $2 |
| 78 | ; |
| 79 | |
Serge Bazanski | 6c9535b | 2023-01-03 13:17:42 +0100 | [diff] [blame] | 80 | -- name: MachineSetOSInstallationRequest :exec |
| 81 | INSERT INTO machine_os_installation_request ( |
| 82 | machine_id, generation, os_installation_request_raw |
| 83 | ) VALUES ( |
| 84 | $1, $2, $3 |
| 85 | ) ON CONFLICT (machine_id) DO UPDATE SET |
| 86 | generation = $2, |
| 87 | os_installation_request_raw = $3 |
| 88 | ; |
| 89 | |
| 90 | -- name: MachineSetOSInstallationReport :exec |
| 91 | INSERT INTO machine_os_installation_report ( |
| 92 | machine_id, generation |
| 93 | ) VALUES ( |
| 94 | $1, $2 |
| 95 | ) ON CONFLICT (machine_id) DO UPDATE SET |
| 96 | generation = $2 |
| 97 | ; |
| 98 | |
Serge Bazanski | 68ca370 | 2022-11-02 17:30:44 +0100 | [diff] [blame] | 99 | -- name: GetMachinesForAgentStart :many |
| 100 | -- Get machines that need agent installed for the first time. Machine can be |
| 101 | -- assumed to be 'new', with no previous attempts or failures. |
Serge Bazanski | 35e8d79 | 2022-10-11 11:32:30 +0200 | [diff] [blame] | 102 | SELECT |
| 103 | machine_provided.* |
| 104 | FROM machines |
| 105 | INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id |
Serge Bazanski | 68ca370 | 2022-11-02 17:30:44 +0100 | [diff] [blame] | 106 | LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall' |
| 107 | LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 108 | WHERE |
| 109 | machine_agent_started.machine_id IS NULL |
| 110 | -- TODO(q3k): exclude machines which are not expected to run the agent (eg. |
| 111 | -- are already exposed to a user). |
Serge Bazanski | 35e8d79 | 2022-10-11 11:32:30 +0200 | [diff] [blame] | 112 | AND work.machine_id IS NULL |
| 113 | LIMIT $1; |
| 114 | |
Serge Bazanski | 68ca370 | 2022-11-02 17:30:44 +0100 | [diff] [blame] | 115 | -- name: GetMachineForAgentRecovery :many |
| 116 | -- Get machines that need agent installed after something went wrong. Either |
| 117 | -- the agent started but never responded, or the agent stopped responding at |
| 118 | -- some point, or the machine is being reinstalled after failure. Assume some |
| 119 | -- work needs to be performed on the shepherd side to diagnose and recover |
| 120 | -- whatever state the machine truly is in. |
| 121 | SELECT |
| 122 | machine_provided.* |
| 123 | FROM machines |
| 124 | INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id |
| 125 | LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall' |
| 126 | LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 127 | LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id |
| 128 | WHERE |
| 129 | -- Only act on machines where the agent is expected to have been started. |
| 130 | machine_agent_started.machine_id IS NOT NULL |
| 131 | AND ( |
| 132 | -- No heartbeat 30 minutes after starting the agent. |
| 133 | ( |
| 134 | machine_agent_heartbeat.machine_id IS NULL |
| 135 | AND now() > (machine_agent_started.agent_started_at + interval '30 minutes') |
| 136 | ) |
| 137 | -- Heartbeats ceased for 10 minutes. |
| 138 | OR ( |
| 139 | machine_agent_heartbeat.machine_id IS NOT NULL |
| 140 | AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes') |
| 141 | ) |
| 142 | ) |
| 143 | AND work.machine_id IS NULL |
| 144 | LIMIT $1; |
Serge Bazanski | 4abeb13 | 2022-10-11 11:32:19 +0200 | [diff] [blame] | 145 | |
Serge Bazanski | 6c9535b | 2023-01-03 13:17:42 +0100 | [diff] [blame] | 146 | -- name: GetExactMachineForOSInstallation :many |
| 147 | SELECT |
| 148 | machine_os_installation_request.* |
| 149 | FROM machines |
| 150 | LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id |
| 151 | LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id |
| 152 | WHERE |
| 153 | -- We are only interested in one concrete machine. |
| 154 | machines.machine_id = $1 |
| 155 | -- We must have an installation request. |
| 156 | AND machine_os_installation_request.machine_id IS NOT NULL |
| 157 | -- And we either must have no installation report, or the installation |
| 158 | -- report's generation must not match the installation request's generation. |
| 159 | AND ( |
| 160 | machine_os_installation_report.machine_id IS NULL |
| 161 | OR machine_os_installation_report.generation != machine_os_installation_request.generation |
| 162 | ) |
| 163 | LIMIT $2; |
| 164 | |
Serge Bazanski | 4abeb13 | 2022-10-11 11:32:19 +0200 | [diff] [blame] | 165 | -- name: AuthenticateAgentConnection :many |
| 166 | SELECT |
| 167 | machine_agent_started.* |
| 168 | FROM machines |
| 169 | INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id |
| 170 | WHERE |
| 171 | machines.machine_id = $1 |
Serge Bazanski | 6c9535b | 2023-01-03 13:17:42 +0100 | [diff] [blame] | 172 | AND machine_agent_started.agent_public_key = $2; |