workspaces 0.1.16 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/DESIGN.md +2 -0
- data/README.md +113 -6
- data/lib/workspaces/activity_tracker.rb +107 -0
- data/lib/workspaces/child_env.rb +6 -2
- data/lib/workspaces/cli.rb +46 -10
- data/lib/workspaces/config.rb +29 -0
- data/lib/workspaces/coordinator.rb +165 -10
- data/lib/workspaces/dashboard/app.js +15 -1
- data/lib/workspaces/dashboard/environment.js +196 -54
- data/lib/workspaces/dashboard/styles.css +36 -0
- data/lib/workspaces/dashboard/template.html.erb +15 -2
- data/lib/workspaces/dashboard_view.rb +79 -11
- data/lib/workspaces/environment_overrides.rb +103 -4
- data/lib/workspaces/front_door.rb +140 -15
- data/lib/workspaces/presentation.rb +30 -3
- data/lib/workspaces/recipe.rb +30 -2
- data/lib/workspaces/registry.rb +26 -0
- data/lib/workspaces/sleep_policy.rb +39 -0
- data/lib/workspaces/sleeper.rb +48 -0
- data/lib/workspaces/vault/file_vault.rb +174 -0
- data/lib/workspaces/vault.rb +137 -0
- data/lib/workspaces/version.rb +1 -1
- metadata +7 -2
|
@@ -22,6 +22,12 @@ module Workspaces
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def start(id, force: false, preset: nil, set: {}, unset: [])
|
|
25
|
+
if !force && sleeping?(id)
|
|
26
|
+
return wake(id) unless preset || !set.empty? || !unset.empty?
|
|
27
|
+
|
|
28
|
+
return launch(id, restart: true, force: true, prelaunch: settings_update(id, preset, set, unset))
|
|
29
|
+
end
|
|
30
|
+
|
|
25
31
|
launch(id, restart: false, force: force, prelaunch: settings_update(id, preset, set, unset))
|
|
26
32
|
end
|
|
27
33
|
|
|
@@ -49,7 +55,7 @@ module Workspaces
|
|
|
49
55
|
end
|
|
50
56
|
|
|
51
57
|
def update_environment(id, set:, remove:)
|
|
52
|
-
restart = Backend.new(id).running?
|
|
58
|
+
restart = Backend.new(id).running? || sleeping?(id)
|
|
53
59
|
launch(id, restart: restart, force: true,
|
|
54
60
|
prelaunch: lambda { |_workspace|
|
|
55
61
|
Recipe.new
|
|
@@ -57,8 +63,19 @@ module Workspaces
|
|
|
57
63
|
})
|
|
58
64
|
end
|
|
59
65
|
|
|
66
|
+
# Saves the settings form (rows of name / value / secret) and starts the workspace, restarting it
|
|
67
|
+
# if it is already up. See EnvironmentOverrides#replace_rows.
|
|
68
|
+
def replace_environment_rows(id, rows:)
|
|
69
|
+
restart = Backend.new(id).running? || sleeping?(id)
|
|
70
|
+
launch(id, restart: restart, force: true,
|
|
71
|
+
prelaunch: lambda { |_workspace|
|
|
72
|
+
defaults = Recipe.new.default_editable_env
|
|
73
|
+
EnvironmentOverrides.new(id).replace_rows(rows, defaults: defaults)
|
|
74
|
+
})
|
|
75
|
+
end
|
|
76
|
+
|
|
60
77
|
def replace_editable_environment(id, text:)
|
|
61
|
-
restart = Backend.new(id).running?
|
|
78
|
+
restart = Backend.new(id).running? || sleeping?(id)
|
|
62
79
|
launch(id, restart: restart, force: true,
|
|
63
80
|
prelaunch: lambda { |_workspace|
|
|
64
81
|
defaults = Recipe.new.default_editable_env
|
|
@@ -66,6 +83,105 @@ module Workspaces
|
|
|
66
83
|
})
|
|
67
84
|
end
|
|
68
85
|
|
|
86
|
+
# Stops an idle workspace's server and keeps everything else: worktree, database, dependencies,
|
|
87
|
+
# built assets, settings and build history. Returns true when it slept. Does nothing (false) while
|
|
88
|
+
# the workspace is busy, is kept awake, is not running, or is already asleep.
|
|
89
|
+
def sleep!(id)
|
|
90
|
+
@registry.find(id)
|
|
91
|
+
lock = acquire_lock(id)
|
|
92
|
+
return false unless lock
|
|
93
|
+
|
|
94
|
+
begin
|
|
95
|
+
entry = @state.get(id) || {}
|
|
96
|
+
return false unless sleepable?(id, entry)
|
|
97
|
+
|
|
98
|
+
update(id, 'sleep_state' => 'sleeping', 'slept_at' => now)
|
|
99
|
+
begin
|
|
100
|
+
Backend.new(id).stop!
|
|
101
|
+
rescue StandardError
|
|
102
|
+
update(id, 'sleep_state' => nil, 'slept_at' => nil)
|
|
103
|
+
raise
|
|
104
|
+
end
|
|
105
|
+
true
|
|
106
|
+
ensure
|
|
107
|
+
lock.close
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Starts a sleeping workspace's server again and returns at once; the caller watches for readiness.
|
|
112
|
+
# Only a workspace that is intentionally asleep is woken: one that was stopped by hand, failed,
|
|
113
|
+
# or is already starting is left alone (false). Concurrent callers share one wake because only
|
|
114
|
+
# the one holding the workspace lock gets past the check.
|
|
115
|
+
def wake(id)
|
|
116
|
+
workspace = @registry.find(id)
|
|
117
|
+
lock = acquire_lock(id)
|
|
118
|
+
return false unless lock
|
|
119
|
+
|
|
120
|
+
entry = @state.get(id) || {}
|
|
121
|
+
if entry['sleep_state'] != 'sleeping' || Backend.new(id).running?
|
|
122
|
+
lock.close
|
|
123
|
+
return false
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
restart = preparation_current?(entry)
|
|
127
|
+
publish_starting(id, restart, extra: { 'sleep_state' => 'waking', 'message' => 'Waking workspace' })
|
|
128
|
+
spawn_worker(workspace, restart, lock)
|
|
129
|
+
true
|
|
130
|
+
rescue StandardError
|
|
131
|
+
lock&.close unless lock&.closed?
|
|
132
|
+
raise
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Stops the server by hand. A workspace stopped this way is not asleep, so traffic never wakes it.
|
|
136
|
+
def stop(id)
|
|
137
|
+
@registry.with_lock(id) do
|
|
138
|
+
Backend.new(id).stop!
|
|
139
|
+
@state.set(id, 'status' => 'stopped', 'message' => 'Server stopped', 'sleep_state' => nil, 'slept_at' => nil)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def keep_awake(id, enabled)
|
|
144
|
+
@registry.find(id)
|
|
145
|
+
@state.set(id, 'keep_awake' => !!enabled)
|
|
146
|
+
!!enabled
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def sleeping?(id)
|
|
150
|
+
(@state.get(id) || {})['sleep_state'] == 'sleeping'
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# :sleeping, :waking (a wake is running), or nil.
|
|
154
|
+
def sleep_phase(id)
|
|
155
|
+
case (@state.get(id) || {})['sleep_state']
|
|
156
|
+
when 'sleeping' then :sleeping
|
|
157
|
+
when 'waking' then locked?(id) ? :waking : nil
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def status_of(id)
|
|
162
|
+
entry = @state.get(id) || {}
|
|
163
|
+
resolve_status(id, entry, locked?(id))
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Workspaces that look ready, are not kept awake and are not asleep. A cheap read of the state
|
|
167
|
+
# file; `sleep!` checks everything again under the workspace lock.
|
|
168
|
+
def sleep_candidates
|
|
169
|
+
@state.all.filter_map do |id, entry|
|
|
170
|
+
id if entry['status'] == 'ready' && !entry['keep_awake'] && entry['sleep_state'].nil?
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Run once when a front door starts. A sleep that was cut short is finished, and a wake that was
|
|
175
|
+
# cut short is cleared, so the workspace reads as interrupted rather than as waking forever.
|
|
176
|
+
def reconcile_sleep
|
|
177
|
+
@state.all.each do |id, entry|
|
|
178
|
+
case entry['sleep_state']
|
|
179
|
+
when 'sleeping' then finish_interrupted_sleep(id)
|
|
180
|
+
when 'waking' then @state.set(id, 'sleep_state' => nil) unless locked?(id)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
69
185
|
def wait(id)
|
|
70
186
|
workers_mutex.synchronize { workers[id] }&.value
|
|
71
187
|
snapshot(id)
|
|
@@ -77,7 +193,7 @@ module Workspaces
|
|
|
77
193
|
workspace = @registry.find(id)
|
|
78
194
|
entry = @state.get(id) || {}
|
|
79
195
|
busy = locked?(id)
|
|
80
|
-
status = resolve_status(id, entry
|
|
196
|
+
status = resolve_status(id, entry, busy)
|
|
81
197
|
data = workspace.describe.merge(snapshot_fields(id, entry, busy, status, log, include_log))
|
|
82
198
|
data.merge('presentation' => Presentation.new(data).to_h)
|
|
83
199
|
end
|
|
@@ -87,7 +203,7 @@ module Workspaces
|
|
|
87
203
|
id = workspace.fetch('id')
|
|
88
204
|
entry = @state.get(id) || {}
|
|
89
205
|
busy = locked?(id)
|
|
90
|
-
workspace.merge('status' => resolve_status(id, entry
|
|
206
|
+
workspace.merge('status' => resolve_status(id, entry, busy),
|
|
91
207
|
'running' => !!Backend.new(id).running?, 'active' => busy,
|
|
92
208
|
'started_at' => entry['started_at'])
|
|
93
209
|
end
|
|
@@ -103,17 +219,43 @@ module Workspaces
|
|
|
103
219
|
end
|
|
104
220
|
end
|
|
105
221
|
|
|
106
|
-
def resolve_status(id,
|
|
222
|
+
def resolve_status(id, entry, busy)
|
|
223
|
+
status = entry.fetch('status', 'idle')
|
|
107
224
|
return busy ? status : 'interrupted' if ACTIVE.include?(status)
|
|
108
225
|
return status if status == 'error'
|
|
109
226
|
return 'ready' if Backend.new(id).running?
|
|
227
|
+
return 'sleeping' if entry['sleep_state'] == 'sleeping'
|
|
110
228
|
|
|
111
229
|
status == 'ready' ? 'stopped' : status
|
|
112
230
|
end
|
|
113
231
|
|
|
232
|
+
def sleepable?(id, entry)
|
|
233
|
+
entry['status'] == 'ready' && !entry['keep_awake'] && entry['sleep_state'].nil? && Backend.new(id).running?
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# What was prepared still matches the recipe, so waking only needs the server and readiness steps.
|
|
237
|
+
# A workspace prepared before this was recorded is trusted.
|
|
238
|
+
def preparation_current?(entry)
|
|
239
|
+
recorded = entry['prepared_digest']
|
|
240
|
+
recorded.nil? || recorded == Recipe.new.preparation_digest
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def finish_interrupted_sleep(id)
|
|
244
|
+
lock = acquire_lock(id)
|
|
245
|
+
return unless lock
|
|
246
|
+
|
|
247
|
+
begin
|
|
248
|
+
Backend.new(id).stop! if Backend.new(id).running?
|
|
249
|
+
ensure
|
|
250
|
+
lock.close
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
114
254
|
def snapshot_fields(id, entry, busy, status, log, include_log)
|
|
115
255
|
{
|
|
116
256
|
'workspace_id' => id, 'status' => status, 'active' => busy,
|
|
257
|
+
'sleeping' => status == 'sleeping', 'slept_at' => entry['slept_at'],
|
|
258
|
+
'keep_awake' => !!entry['keep_awake'], 'sleep_after_seconds' => Config.sleep_after_seconds,
|
|
117
259
|
'message' => status == entry['status'] ? entry['message'] : status.capitalize,
|
|
118
260
|
'prepare_started_at' => entry['prepare_started_at'], 'started_at' => entry['started_at'],
|
|
119
261
|
'running' => !!Backend.new(id).running?, 'updated_at' => entry['updated_at'],
|
|
@@ -128,6 +270,8 @@ module Workspaces
|
|
|
128
270
|
'log_tail' => include_log ? log_tail(id, log) : '', 'log_source' => log, 'port' => entry['port'],
|
|
129
271
|
'environment_keys' => environment_keys(id),
|
|
130
272
|
'editable_environment' => editable_environment(id),
|
|
273
|
+
'secret_names' => secret_names(id),
|
|
274
|
+
'vault' => Vault.status,
|
|
131
275
|
'environment_presets' => environment_presets
|
|
132
276
|
}
|
|
133
277
|
end
|
|
@@ -191,18 +335,20 @@ module Workspaces
|
|
|
191
335
|
end
|
|
192
336
|
end
|
|
193
337
|
|
|
194
|
-
def publish_starting(id, restart)
|
|
338
|
+
def publish_starting(id, restart, extra: {})
|
|
195
339
|
recipe = Recipe.new
|
|
196
340
|
steps = recipe.for_operation(restart: restart).map do |step|
|
|
197
341
|
{ 'key' => step['id'], 'label' => step['name'], 'state' => 'pending', 'duration_seconds' => nil }
|
|
198
342
|
end
|
|
199
343
|
# Frozen for the whole run, so the bar does not shift under the reader and the history file
|
|
200
344
|
# is read once per run rather than once per poll.
|
|
201
|
-
update(id,
|
|
345
|
+
update(id, {
|
|
346
|
+
'status' => 'preparing', 'message' => restart ? 'Restarting application' : 'Preparing workspace',
|
|
202
347
|
'steps' => steps, 'estimates' => BuildHistory.estimates(steps.map { |step| step['key'] }),
|
|
203
348
|
'operation' => restart ? 'restart' : 'prepare', 'rerun_keys' => nil, 'failed_phase' => nil,
|
|
204
349
|
'prepare_started_at' => now, 'completed_at' => nil, 'last_error' => nil,
|
|
205
|
-
'total_seconds' => nil
|
|
350
|
+
'total_seconds' => nil, 'sleep_state' => nil, 'slept_at' => nil
|
|
351
|
+
}.merge(extra))
|
|
206
352
|
end
|
|
207
353
|
|
|
208
354
|
def spawn_worker(workspace, restart, lock, plan: nil)
|
|
@@ -241,8 +387,10 @@ module Workspaces
|
|
|
241
387
|
total = elapsed_since(started)
|
|
242
388
|
# A re-run that left no server running (a step before it on a stopped workspace) keeps the
|
|
243
389
|
# recorded port as it was; the workspace then reads as stopped until it is started.
|
|
244
|
-
attributes = { 'status' => 'ready', 'message' => 'Workspace ready', 'completed_at' => now, 'total_seconds' => total
|
|
390
|
+
attributes = { 'status' => 'ready', 'message' => 'Workspace ready', 'completed_at' => now, 'total_seconds' => total,
|
|
391
|
+
'sleep_state' => nil }
|
|
245
392
|
attributes['port'] = port if port
|
|
393
|
+
attributes['prepared_digest'] = Recipe.new.preparation_digest if plan.nil? && !restart
|
|
246
394
|
update(id, attributes)
|
|
247
395
|
record_run(id, 'complete', total)
|
|
248
396
|
end
|
|
@@ -254,7 +402,7 @@ module Workspaces
|
|
|
254
402
|
total = started && elapsed_since(started)
|
|
255
403
|
update(id, 'status' => 'error', 'message' => 'Workspace preparation failed', 'last_error' => detail,
|
|
256
404
|
'failed_phase' => @state.get(id)&.fetch('status', nil), 'completed_at' => now,
|
|
257
|
-
'total_seconds' => total)
|
|
405
|
+
'total_seconds' => total, 'sleep_state' => nil)
|
|
258
406
|
record_run(id, 'failed', total)
|
|
259
407
|
end
|
|
260
408
|
|
|
@@ -340,6 +488,13 @@ module Workspaces
|
|
|
340
488
|
[]
|
|
341
489
|
end
|
|
342
490
|
|
|
491
|
+
# Names only. A secret's value is never part of a snapshot, a status response or a page.
|
|
492
|
+
def secret_names(id)
|
|
493
|
+
EnvironmentOverrides.new(id).secret_names
|
|
494
|
+
rescue EnvironmentOverrides::Invalid
|
|
495
|
+
[]
|
|
496
|
+
end
|
|
497
|
+
|
|
343
498
|
def editable_environment(id)
|
|
344
499
|
EnvironmentOverrides.new(id).editable_values(Recipe.new.default_editable_env)
|
|
345
500
|
rescue Recipe::Invalid, EnvironmentOverrides::Invalid
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
const selectedLog = new URLSearchParams(window.location.search).get('log') === 'backend'
|
|
8
8
|
? 'backend'
|
|
9
9
|
: 'setup';
|
|
10
|
-
const allowedActions = ['start', 'prepare', 'restart', 'resume', 'stop', 'update-pr'];
|
|
10
|
+
const allowedActions = ['start', 'prepare', 'restart', 'resume', 'stop', 'update-pr', 'keep-awake', 'allow-sleep'];
|
|
11
11
|
let busy = root.dataset.busy === 'true';
|
|
12
12
|
let started = Date.parse(root.dataset.started);
|
|
13
13
|
let completed = Date.parse(root.dataset.completed);
|
|
@@ -245,9 +245,23 @@
|
|
|
245
245
|
window.WorkspaceEnvironment.setSubmitLabel(view.environment_submit);
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
+
function sleepNote(view) {
|
|
249
|
+
const sleep = view.sleep;
|
|
250
|
+
if (!sleep) return '';
|
|
251
|
+
if (sleep.keep_awake) return 'Kept awake: this workspace will not sleep when idle.';
|
|
252
|
+
if (view.state !== 'ready') return '';
|
|
253
|
+
const minutes = Math.round(sleep.after_seconds / 60);
|
|
254
|
+
return 'Sleeps after ' + (minutes >= 1 ? minutes + ' min' : Math.round(sleep.after_seconds) + ' s') + ' without preview traffic.';
|
|
255
|
+
}
|
|
256
|
+
|
|
248
257
|
function renderLead(view) {
|
|
249
258
|
node('status').textContent = view.headline || view.title;
|
|
250
259
|
node('summary').textContent = view.summary || '';
|
|
260
|
+
const note = node('sleep-note');
|
|
261
|
+
if (note) {
|
|
262
|
+
note.textContent = sleepNote(view);
|
|
263
|
+
note.hidden = !note.textContent;
|
|
264
|
+
}
|
|
251
265
|
renderProgress(view.progress);
|
|
252
266
|
const title = node('environment-title');
|
|
253
267
|
if (title && view.environment_title) title.textContent = view.environment_title;
|
|
@@ -5,14 +5,17 @@
|
|
|
5
5
|
const status = document.getElementById('environment-status');
|
|
6
6
|
const fieldset = form.querySelector('[data-environment-fieldset]');
|
|
7
7
|
const submit = form.querySelector('[data-env-submit]');
|
|
8
|
-
const
|
|
8
|
+
const rowsContainer = document.getElementById('env-rows');
|
|
9
|
+
const rowTemplate = document.getElementById('env-row-template');
|
|
10
|
+
const addButton = form.querySelector('[data-env-add-row]');
|
|
9
11
|
const presetButtons = Array.from(form.querySelectorAll('[data-environment-preset]'));
|
|
10
12
|
const presetGroup = form.querySelector('[data-preset-controls]');
|
|
11
|
-
const initialDraft = editor ? editor.value : '';
|
|
12
13
|
let lastAnnounced = '';
|
|
13
14
|
let activeBusy = document.getElementById('workspace').dataset.busy === 'true';
|
|
14
15
|
let presets = {};
|
|
15
16
|
let selectedPreset = null;
|
|
17
|
+
let nextIndex = 0;
|
|
18
|
+
let initialSignature = '';
|
|
16
19
|
|
|
17
20
|
try {
|
|
18
21
|
presets = JSON.parse(form.dataset.environmentPresets || '{}');
|
|
@@ -22,17 +25,60 @@
|
|
|
22
25
|
const hasPresets = Object.keys(presets).length > 0;
|
|
23
26
|
if (presetGroup && hasPresets) presetGroup.hidden = false;
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
// A preset is a list of { name, value }. A row's state is what the person typed. Secret rows are
|
|
29
|
+
// never part of any comparison, so a preset can match while secrets are present.
|
|
30
|
+
function rowElements() {
|
|
31
|
+
return rowsContainer ? Array.from(rowsContainer.querySelectorAll('[data-env-row]')) : [];
|
|
29
32
|
}
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
// The value input is missing on a stored secret until Replace is clicked, so it can be null.
|
|
35
|
+
function fieldsOf(row) {
|
|
36
|
+
return {
|
|
37
|
+
name: row.querySelector('.env-row__name'),
|
|
38
|
+
value: row.querySelector('[data-env-value]'),
|
|
39
|
+
secret: row.querySelector('[data-env-secret]'),
|
|
40
|
+
remove: row.querySelector('[data-env-remove-row]'),
|
|
41
|
+
stored: row.querySelector('[data-env-stored]'),
|
|
42
|
+
replace: row.querySelector('[data-env-replace]')
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const valueOf = (fields) => (fields.value ? fields.value.value || '' : '');
|
|
47
|
+
|
|
48
|
+
// Keeps password managers out of every input this script creates: autocomplete is ignored on
|
|
49
|
+
// password fields, so each manager's own opt-out goes with it.
|
|
50
|
+
function noAutofill(input) {
|
|
51
|
+
input.autocomplete = 'off';
|
|
52
|
+
input.setAttribute('autocomplete', 'off');
|
|
53
|
+
input.setAttribute('data-1p-ignore', '');
|
|
54
|
+
input.setAttribute('data-lpignore', 'true');
|
|
55
|
+
input.setAttribute('data-bwignore', '');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function visibleEntries() {
|
|
59
|
+
return rowElements()
|
|
60
|
+
.map((row) => {
|
|
61
|
+
const fields = fieldsOf(row);
|
|
62
|
+
return {
|
|
63
|
+
name: (fields.name.value || '').trim(),
|
|
64
|
+
value: valueOf(fields),
|
|
65
|
+
secret: !!(fields.secret && fields.secret.checked)
|
|
66
|
+
};
|
|
67
|
+
})
|
|
68
|
+
.filter((entry) => !entry.secret && (entry.name || entry.value));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function signatureOf(entries) {
|
|
72
|
+
return JSON.stringify(
|
|
73
|
+
entries
|
|
74
|
+
.map((entry) => [entry.name, entry.value])
|
|
75
|
+
.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0))
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function matchingPresetName() {
|
|
80
|
+
const current = signatureOf(visibleEntries());
|
|
81
|
+
return Object.keys(presets).find((name) => signatureOf(presets[name]) === current) || null;
|
|
36
82
|
}
|
|
37
83
|
|
|
38
84
|
function updatePresetButtons(nextSelected) {
|
|
@@ -69,15 +115,13 @@
|
|
|
69
115
|
if (fieldset) fieldset.disabled = activeBusy;
|
|
70
116
|
if (submit) submit.disabled = activeBusy;
|
|
71
117
|
presetButtons.forEach((button) => {
|
|
72
|
-
button.disabled = activeBusy;
|
|
118
|
+
button.disabled = activeBusy || !hasPresets;
|
|
73
119
|
});
|
|
74
120
|
}
|
|
75
121
|
|
|
76
122
|
function updateStatus(busy) {
|
|
77
123
|
if (busy)
|
|
78
|
-
setStatus(
|
|
79
|
-
'Workspace is busy. Environment updates are temporarily disabled.'
|
|
80
|
-
);
|
|
124
|
+
setStatus('Workspace is busy. Environment updates are temporarily disabled.');
|
|
81
125
|
else if (status.textContent === 'Workspace is busy. Environment updates are temporarily disabled.') {
|
|
82
126
|
status.textContent = '';
|
|
83
127
|
lastAnnounced = '';
|
|
@@ -86,75 +130,173 @@
|
|
|
86
130
|
|
|
87
131
|
function announceUnsaved(message) {
|
|
88
132
|
if (activeBusy) return;
|
|
89
|
-
const nextMessage =
|
|
133
|
+
const nextMessage =
|
|
134
|
+
message || 'Draft updated locally and not saved yet. Use ' + submitLabel() + ' to apply changes.';
|
|
90
135
|
status.textContent = nextMessage;
|
|
91
136
|
lastAnnounced = nextMessage;
|
|
92
137
|
}
|
|
93
138
|
|
|
94
|
-
function replaceDraft(value, message) {
|
|
95
|
-
if (!editor) return;
|
|
96
|
-
editor.value = value || '';
|
|
97
|
-
updatePresetButtons(matchingPresetNameForDraft(editor.value));
|
|
98
|
-
announceUnsaved(message);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
139
|
function clearStatus() {
|
|
102
140
|
if (activeBusy) return;
|
|
103
141
|
status.textContent = '';
|
|
104
142
|
lastAnnounced = '';
|
|
105
143
|
}
|
|
106
144
|
|
|
145
|
+
// The checkbox of a secret that is already stored is disabled (and a hidden field carries it), so a
|
|
146
|
+
// saved secret cannot be turned back into a visible variable. Ticking Secret on a new row switches
|
|
147
|
+
// its value to a password field; the value is write-only from then on.
|
|
148
|
+
function syncRow(row) {
|
|
149
|
+
const fields = fieldsOf(row);
|
|
150
|
+
const secret = !!(fields.secret && fields.secret.checked);
|
|
151
|
+
if (secret) row.setAttribute('data-secret', 'true');
|
|
152
|
+
else row.removeAttribute('data-secret');
|
|
153
|
+
if (!fields.value) return;
|
|
154
|
+
fields.value.type = secret ? 'password' : 'text';
|
|
155
|
+
noAutofill(fields.value);
|
|
156
|
+
if (row.hasAttribute('data-stored')) fields.value.placeholder = 'New value (leave blank to keep)';
|
|
157
|
+
else fields.value.placeholder = secret ? 'Value' : 'value';
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Replace adds the value input to a stored secret. Until then the page has no password field for
|
|
161
|
+
// it at all.
|
|
162
|
+
function revealReplaceInput(row) {
|
|
163
|
+
const fields = fieldsOf(row);
|
|
164
|
+
if (fields.value || !fields.stored) return;
|
|
165
|
+
const input = document.createElement('input');
|
|
166
|
+
input.className = 'env-row__value';
|
|
167
|
+
input.name = (fields.name.name || '').replace(/\[name\]$/, '[value]');
|
|
168
|
+
input.type = 'password';
|
|
169
|
+
input.value = '';
|
|
170
|
+
input.placeholder = 'New value (leave blank to keep)';
|
|
171
|
+
input.setAttribute('aria-label', 'New value');
|
|
172
|
+
input.setAttribute('spellcheck', 'false');
|
|
173
|
+
input.setAttribute('autocapitalize', 'off');
|
|
174
|
+
input.setAttribute('data-env-value', '');
|
|
175
|
+
noAutofill(input);
|
|
176
|
+
fields.stored.parentNode.insertBefore(input, fields.stored);
|
|
177
|
+
fields.stored.parentNode.removeChild(fields.stored);
|
|
178
|
+
input.focus();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function bindRow(row) {
|
|
182
|
+
const fields = fieldsOf(row);
|
|
183
|
+
if (fields.remove) {
|
|
184
|
+
fields.remove.hidden = false;
|
|
185
|
+
fields.remove.addEventListener('click', () => {
|
|
186
|
+
if (activeBusy) return;
|
|
187
|
+
row.parentNode.removeChild(row);
|
|
188
|
+
draftChanged();
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (fields.replace) {
|
|
192
|
+
fields.replace.hidden = false;
|
|
193
|
+
fields.replace.addEventListener('click', () => {
|
|
194
|
+
if (activeBusy) return;
|
|
195
|
+
revealReplaceInput(row);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
['input', 'change'].forEach((type) => {
|
|
199
|
+
row.addEventListener(type, (event) => {
|
|
200
|
+
if (event && event.target && event.target.matches && event.target.matches('[data-env-secret]'))
|
|
201
|
+
syncRow(row);
|
|
202
|
+
draftChanged();
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
syncRow(row);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function addRow(entry) {
|
|
209
|
+
if (!rowTemplate || !rowsContainer) return null;
|
|
210
|
+
const holder = document.createElement('div');
|
|
211
|
+
holder.innerHTML = rowTemplate.innerHTML.split('__INDEX__').join(String(nextIndex++));
|
|
212
|
+
const row = holder.querySelector('[data-env-row]');
|
|
213
|
+
if (!row) return null;
|
|
214
|
+
rowsContainer.append(row);
|
|
215
|
+
const fields = fieldsOf(row);
|
|
216
|
+
if (entry) {
|
|
217
|
+
fields.name.value = entry.name || '';
|
|
218
|
+
if (fields.value) fields.value.value = entry.value || '';
|
|
219
|
+
}
|
|
220
|
+
bindRow(row);
|
|
221
|
+
return row;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function draftChanged() {
|
|
225
|
+
updatePresetButtons(matchingPresetName());
|
|
226
|
+
const unchanged = rowElements().length === 0 ? false : currentSignature() === initialSignature;
|
|
227
|
+
if (unchanged) clearStatus();
|
|
228
|
+
else announceUnsaved();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function currentSignature() {
|
|
232
|
+
return JSON.stringify(
|
|
233
|
+
rowElements().map((row) => {
|
|
234
|
+
const fields = fieldsOf(row);
|
|
235
|
+
const secret = !!(fields.secret && fields.secret.checked);
|
|
236
|
+
// A secret's value is kept out of the signature, but whether one has been typed is not: a
|
|
237
|
+
// pending replacement is an unsaved change.
|
|
238
|
+
return [fields.name.value, secret ? '' : valueOf(fields), secret, secret && valueOf(fields) !== ''];
|
|
239
|
+
})
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Loading a preset replaces the visible variables only. Secrets are never touched by a preset.
|
|
244
|
+
function loadPreset(name) {
|
|
245
|
+
const entries = presets[name] || [];
|
|
246
|
+
rowElements().forEach((row) => {
|
|
247
|
+
const fields = fieldsOf(row);
|
|
248
|
+
const isSecret = !!(fields.secret && fields.secret.checked);
|
|
249
|
+
if (!isSecret) row.parentNode.removeChild(row);
|
|
250
|
+
});
|
|
251
|
+
entries.forEach((entry) => addRow(entry));
|
|
252
|
+
updatePresetButtons(name);
|
|
253
|
+
announceUnsaved('Preset loaded into draft only. Use ' + submitLabel() + ' to apply changes.');
|
|
254
|
+
}
|
|
255
|
+
|
|
107
256
|
form.addEventListener('submit', () => {
|
|
108
257
|
setStatus('Submitting environment settings…');
|
|
109
258
|
});
|
|
110
259
|
|
|
111
|
-
if (editor)
|
|
112
|
-
editor.addEventListener('input', () => {
|
|
113
|
-
if (editor.value === initialDraft) {
|
|
114
|
-
updatePresetButtons(matchingPresetNameForDraft(initialDraft));
|
|
115
|
-
clearStatus();
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
updatePresetButtons(matchingPresetNameForDraft(editor.value));
|
|
119
|
-
announceUnsaved();
|
|
120
|
-
});
|
|
121
|
-
|
|
122
260
|
presetButtons.forEach((button) => {
|
|
123
261
|
button.addEventListener('click', () => {
|
|
124
262
|
if (activeBusy) return;
|
|
125
263
|
const nextValue = button.dataset.environmentPreset;
|
|
126
264
|
if (!nextValue) return;
|
|
127
265
|
if (!Object.prototype.hasOwnProperty.call(presets, nextValue)) return;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
266
|
+
loadPreset(nextValue);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
if (addButton) {
|
|
271
|
+
addButton.hidden = false;
|
|
272
|
+
addButton.addEventListener('click', () => {
|
|
273
|
+
if (activeBusy) return;
|
|
274
|
+
const row = addRow(null);
|
|
275
|
+
if (row) fieldsOf(row).name.focus();
|
|
132
276
|
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Rows the server rendered already carry numbered names; new rows continue after the highest.
|
|
280
|
+
rowElements().forEach((row) => {
|
|
281
|
+
const match = /env\[(\d+)\]/.exec(fieldsOf(row).name.name || '');
|
|
282
|
+
if (match) nextIndex = Math.max(nextIndex, Number(match[1]) + 1);
|
|
283
|
+
bindRow(row);
|
|
133
284
|
});
|
|
285
|
+
initialSignature = currentSignature();
|
|
134
286
|
|
|
135
287
|
window.WorkspaceEnvironment = {
|
|
136
288
|
toggleBusy,
|
|
137
289
|
updateStatus,
|
|
138
290
|
setStatus,
|
|
139
291
|
setSubmitLabel,
|
|
292
|
+
// The rows are server-rendered and the person may be editing them, so a poll never rewrites
|
|
293
|
+
// them; it only keeps the selected preset in step when nothing has been touched.
|
|
140
294
|
syncFromSnapshot(snapshot) {
|
|
141
|
-
if (!snapshot || presetButtons.length === 0
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const expected = Object.keys(rendered)
|
|
145
|
-
.sort()
|
|
146
|
-
.map((name) => name + '=' + rendered[name])
|
|
147
|
-
.join('\n');
|
|
148
|
-
if (editor === document.activeElement) return;
|
|
149
|
-
if (editor.value !== expected) return;
|
|
150
|
-
updatePresetButtons(matchingPresetNameForDraft(expected));
|
|
295
|
+
if (!snapshot || presetButtons.length === 0) return;
|
|
296
|
+
if (rowsContainer && rowsContainer.contains(document.activeElement)) return;
|
|
297
|
+
updatePresetButtons(matchingPresetName());
|
|
151
298
|
}
|
|
152
299
|
};
|
|
153
|
-
updatePresetButtons(
|
|
154
|
-
if (!hasPresets) {
|
|
155
|
-
presetButtons.forEach((button) => {
|
|
156
|
-
button.disabled = true;
|
|
157
|
-
});
|
|
158
|
-
}
|
|
300
|
+
updatePresetButtons(matchingPresetName());
|
|
159
301
|
toggleBusy(activeBusy);
|
|
160
302
|
})();
|
|
@@ -266,6 +266,10 @@ h1 {
|
|
|
266
266
|
background: var(--success);
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
+
.status-pill[data-status='sleeping'] .status-dot {
|
|
270
|
+
background: var(--muted);
|
|
271
|
+
}
|
|
272
|
+
|
|
269
273
|
.status-pill[data-status='error'] .status-dot,
|
|
270
274
|
.status-pill[data-status='interrupted'] .status-dot,
|
|
271
275
|
.status-pill[data-status='stopped'] .status-dot {
|
|
@@ -382,6 +386,38 @@ h1 {
|
|
|
382
386
|
overflow: auto;
|
|
383
387
|
}
|
|
384
388
|
|
|
389
|
+
.env-rows { display: grid; gap: var(--space-2); }
|
|
390
|
+
|
|
391
|
+
.env-row {
|
|
392
|
+
display: grid;
|
|
393
|
+
grid-template-columns: minmax(8rem, 1fr) minmax(8rem, 2fr) auto auto;
|
|
394
|
+
gap: var(--space-2);
|
|
395
|
+
align-items: center;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
.env-row input[type='text'],
|
|
399
|
+
.env-row input[type='password'],
|
|
400
|
+
.env-row .env-row__name {
|
|
401
|
+
min-inline-size: 0;
|
|
402
|
+
padding: var(--space-2) var(--space-3);
|
|
403
|
+
color: var(--text);
|
|
404
|
+
background: var(--bg);
|
|
405
|
+
border: 1px solid var(--border);
|
|
406
|
+
border-radius: var(--space-1);
|
|
407
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
.env-row[data-secret] .env-row__value { border-style: dashed; }
|
|
411
|
+
.env-row__stored { display: inline-flex; align-items: center; gap: var(--space-2); font-size: 13px; }
|
|
412
|
+
.env-row__secret { display: inline-flex; align-items: center; gap: var(--space-1); font-size: 13px; white-space: nowrap; }
|
|
413
|
+
.env-row-actions { display: flex; gap: var(--space-2); }
|
|
414
|
+
.env-vault-note { color: var(--warning); }
|
|
415
|
+
|
|
416
|
+
@media (max-width: 560px) {
|
|
417
|
+
.env-row { grid-template-columns: 1fr 1fr; }
|
|
418
|
+
.env-row .env-row__name, .env-row .env-row__value { grid-column: 1 / -1; }
|
|
419
|
+
}
|
|
420
|
+
|
|
385
421
|
.env-actions {
|
|
386
422
|
display: flex;
|
|
387
423
|
flex-wrap: wrap;
|