blob: 4e32cffb82363d1073714b800f01404f8666f498 [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
45-- Example tag processing queries follow.
46
47-- name: MachineAddProvided :exec
48INSERT INTO machine_provided (
49 machine_id, provider, provider_id
50) VALUES (
51 $1, $2, $3
52);
53
Serge Bazanski68ca3702022-11-02 17:30:44 +010054-- name: MachineSetAgentStarted :exec
55INSERT INTO machine_agent_started (
56 machine_id, agent_started_at, agent_public_key
57) VALUES (
58 $1, $2, $3
59) ON CONFLICT (machine_id) DO UPDATE SET
60 agent_started_at = $2,
61 agent_public_key = $3
62;
63
64-- name: MachineSetAgentHeartbeat :exec
65INSERT INTO machine_agent_heartbeat (
66 machine_id, agent_heartbeat_at
67) VALUES (
68 $1, $2
69) ON CONFLICT (machine_id) DO UPDATE SET
70 agent_heartbeat_at = $2
71;
72
Serge Bazanski4abeb132022-10-11 11:32:19 +020073-- name: MachineSetHardwareReport :exec
74INSERT INTO machine_hardware_report (
75 machine_id, hardware_report_raw
76) VALUES (
77 $1, $2
78) ON CONFLICT (machine_id) DO UPDATE SET
79 hardware_report_raw = $2
80;
81
Serge Bazanski68ca3702022-11-02 17:30:44 +010082-- name: GetMachinesForAgentStart :many
83-- Get machines that need agent installed for the first time. Machine can be
84-- assumed to be 'new', with no previous attempts or failures.
Serge Bazanski35e8d792022-10-11 11:32:30 +020085SELECT
86 machine_provided.*
87FROM machines
88INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
Serge Bazanski68ca3702022-11-02 17:30:44 +010089LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall'
90LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
91WHERE
92 machine_agent_started.machine_id IS NULL
93 -- TODO(q3k): exclude machines which are not expected to run the agent (eg.
94 -- are already exposed to a user).
Serge Bazanski35e8d792022-10-11 11:32:30 +020095 AND work.machine_id IS NULL
96LIMIT $1;
97
Serge Bazanski68ca3702022-11-02 17:30:44 +010098-- name: GetMachineForAgentRecovery :many
99-- Get machines that need agent installed after something went wrong. Either
100-- the agent started but never responded, or the agent stopped responding at
101-- some point, or the machine is being reinstalled after failure. Assume some
102-- work needs to be performed on the shepherd side to diagnose and recover
103-- whatever state the machine truly is in.
104SELECT
105 machine_provided.*
106FROM machines
107INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
108LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process = 'ShepherdInstall'
109LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
110LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
111WHERE
112 -- Only act on machines where the agent is expected to have been started.
113 machine_agent_started.machine_id IS NOT NULL
114 AND (
115 -- No heartbeat 30 minutes after starting the agent.
116 (
117 machine_agent_heartbeat.machine_id IS NULL
118 AND now() > (machine_agent_started.agent_started_at + interval '30 minutes')
119 )
120 -- Heartbeats ceased for 10 minutes.
121 OR (
122 machine_agent_heartbeat.machine_id IS NOT NULL
123 AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes')
124 )
125 )
126 AND work.machine_id IS NULL
127LIMIT $1;
Serge Bazanski4abeb132022-10-11 11:32:19 +0200128
129-- name: AuthenticateAgentConnection :many
130SELECT
131 machine_agent_started.*
132FROM machines
133INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
134WHERE
135 machines.machine_id = $1
136 AND machine_agent_started.agent_public_key = $2;