blob: 8f40aacd2fecb5d2d016819ee98fc33a97180301 [file] [log] [blame]
Serge Bazanski35e8d792022-10-11 11:32:30 +02001-- name: NewMachine :one
2INSERT INTO machines (
3 machine_created_at
4) VALUES (
5 now()
6)
7RETURNING *;
8
9-- name: NewSession :one
10INSERT 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)
15RETURNING *;
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.
20UPDATE sessions
21SET session_deadline = now() + session_interval_seconds * interval '1 second'
22WHERE 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).
27SELECT *
28FROM sessions
29WHERE session_id = $1
30AND session_deadline > now();
31
32-- name: StartWork :exec
33INSERT INTO work (
34 machine_id, session_id, process
35) VALUES (
36 $1, $2, $3
37);
38
39-- name: FinishWork :exec
40DELETE FROM work
41WHERE machine_id = $1
42 AND session_id = $2
43 AND process = $3;
44
Serge Bazanski35e8d792022-10-11 11:32:30 +020045-- name: MachineAddProvided :exec
46INSERT INTO machine_provided (
47 machine_id, provider, provider_id
48) VALUES (
49 $1, $2, $3
50);
51
Serge Bazanski68ca3702022-11-02 17:30:44 +010052-- name: MachineSetAgentStarted :exec
53INSERT 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
63INSERT 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 Bazanski4abeb132022-10-11 11:32:19 +020071-- name: MachineSetHardwareReport :exec
72INSERT 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 Bazanski6c9535b2023-01-03 13:17:42 +010080-- name: MachineSetOSInstallationRequest :exec
81INSERT 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
91INSERT 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 Bazanski68ca3702022-11-02 17:30:44 +010099-- 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 Bazanski35e8d792022-10-11 11:32:30 +0200102SELECT
103 machine_provided.*
104FROM machines
105INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
Serge Bazanski68ca3702022-11-02 17:30:44 +0100106LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall'
107LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
108WHERE
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 Bazanski35e8d792022-10-11 11:32:30 +0200112 AND work.machine_id IS NULL
113LIMIT $1;
114
Serge Bazanski68ca3702022-11-02 17:30:44 +0100115-- 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.
121SELECT
122 machine_provided.*
123FROM machines
124INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
125LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall'
126LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
127LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
128WHERE
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
144LIMIT $1;
Serge Bazanski4abeb132022-10-11 11:32:19 +0200145
Serge Bazanski6c9535b2023-01-03 13:17:42 +0100146-- name: GetExactMachineForOSInstallation :many
147SELECT
148 machine_os_installation_request.*
149FROM machines
150LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
151LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
152WHERE
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 )
163LIMIT $2;
164
Serge Bazanski4abeb132022-10-11 11:32:19 +0200165-- name: AuthenticateAgentConnection :many
166SELECT
167 machine_agent_started.*
168FROM machines
169INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
170WHERE
171 machines.machine_id = $1
Serge Bazanski6c9535b2023-01-03 13:17:42 +0100172 AND machine_agent_started.agent_public_key = $2;