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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8a0a1c93490624ea0d8bb2fe77ddb8e7d1ad9bf53fe7700b6eec7245cc2e031a
|
|
4
|
+
data.tar.gz: 561ec874bbbe8d1c537066b612065c23db82caea34ca8d19cc894b66245be24d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7c9e26c91a93c37d8ea2391a0dd1c6ed3a163ba64c37deb2d73cae6d4688c33a7d5f9573ff98a8510789820d06e3f9af36dd78e86ba66b80a13435b11526ffd4
|
|
7
|
+
data.tar.gz: d0deac511f060614b66a41e793af6a4e3c7eb64e2150681dccd65c4910f0fffb5b74d213db865f38a3cb748adfc712bcfdf0a268328d4d9aea4d3375e23f2070
|
data/DESIGN.md
CHANGED
|
@@ -61,6 +61,8 @@ Restrained tooling UI: dark elevated panels, compact spacing rhythm, clear statu
|
|
|
61
61
|
- `unknown`: a minimal view with technical details open.
|
|
62
62
|
- **Disclosure behavior**: the server renders the initial open state and the polling script re-applies it from `presentation.open`, but only when the state changes. A section the user opened or closed is left alone on later polls. Sections that are not meaningful in a state are hidden, never shown disabled.
|
|
63
63
|
- **Per-step re-run controls**: inside each step's accordion, small subtle **Re-run this step** and **Re-run from here** forms aimed at `/workspaces/ID/resume?only=KEY` and `?from=KEY`. The server decides which appear (`presentation.resume.step_modes`), so the page never offers something that would be refused; they are hidden while a run is in progress and are disabled with the other controls while busy.
|
|
64
|
+
- **Sleeping**: a workspace asleep is its own state (`sleeping`), distinct from stopped and failed. The lead reads "Your workspace is asleep", says files and data are kept and that opening the preview wakes it, and offers one **Wake workspace** button. A ready workspace shows one muted line, "Sleeps after N min without preview traffic", or "Kept awake" when exempt. **Keep awake** / **Allow sleep** are subtle workspace controls. A waking workspace is simply busy.
|
|
65
|
+
- **Environment rows**: the settings form is a list of rows, each a name input, a value input and a **Secret** checkbox, with **Add variable** and a per-row **Remove** (both revealed by script; without it the server-rendered rows still save). Rows post as `env[N][name|value|secret]`. A secret row's value is a password field that is never pre-filled; a stored secret shows its name, the placeholder "Stored. Enter a new value to replace it", and a locked checked checkbox (a hidden field carries it), so it can only be replaced or removed, not exposed. Ticking Secret on a new row switches its value to a password field. Presets replace only the visible rows and are matched against visible rows alone. A note under the rows shows why secrets are unavailable when the vault is not usable, and a line explains that secrets are write-only.
|
|
64
66
|
- **Action buttons**: start, prepare/rebuild, restart server, stop server, retry failed step, and refresh
|
|
65
67
|
- **Open workspace**: success-styled link available when the server is ready
|
|
66
68
|
- **Lifecycle controls**: hidden while busy (see state machine); helper text distinguishes rebuilding from server-only restart
|
data/README.md
CHANGED
|
@@ -114,6 +114,58 @@ usually takes, count the running step by how much of its usual time has passed,
|
|
|
114
114
|
times its usual time is flagged as taking longer than usual. With no history yet, steps weigh
|
|
115
115
|
the same and no time estimate is shown. Delete the file to start the estimates over.
|
|
116
116
|
|
|
117
|
+
## Sleeping idle workspaces
|
|
118
|
+
|
|
119
|
+
A ready workspace that sees no preview traffic for a while is put to sleep: its server (the
|
|
120
|
+
managed process group, and nothing shared such as PostgreSQL or Redis) is stopped, and the
|
|
121
|
+
front door keeps answering on the same preview URL. The worktree, database, dependencies, built
|
|
122
|
+
assets, environment settings and build history are untouched. The first request to a sleeping
|
|
123
|
+
preview wakes it, waits for readiness and then forwards that request.
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
WORKSPACES_SLEEP_AFTER_MINUTES=30 # default; 15 also works; 0 switches sleeping off
|
|
127
|
+
WORKSPACES_WAKE_TIMEOUT_SECONDS=90 # how long the first request waits for a wake
|
|
128
|
+
WORKSPACES_SLEEP_SWEEP_SECONDS=30 # how often idle workspaces are looked for
|
|
129
|
+
bundle exec workspaces keep-awake <workspace-id> # never sleep this workspace
|
|
130
|
+
bundle exec workspaces keep-awake <workspace-id> --off # allow sleep again
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The dashboard shows when a workspace sleeps, says "asleep" with a **Wake workspace** button, and has
|
|
134
|
+
**Keep awake** / **Allow sleep** under Workspace controls. The setting is stored with the workspace.
|
|
135
|
+
|
|
136
|
+
What counts as activity:
|
|
137
|
+
|
|
138
|
+
- Preview requests do. Dashboard and status polling never reach the preview and do not count.
|
|
139
|
+
- `/health` and `/up` never count and never wake a workspace. List more background polling under
|
|
140
|
+
`sleep.ignore_paths` (globs on the request path) in `.workspaces.yml`:
|
|
141
|
+
|
|
142
|
+
```yaml
|
|
143
|
+
sleep:
|
|
144
|
+
ignore_paths:
|
|
145
|
+
- /api/heartbeat
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
- A request in flight holds the workspace awake until its response body is finished. Streamed
|
|
149
|
+
responses hold it until the body is closed. Streaming and upgraded connections are not newly
|
|
150
|
+
supported by the proxy; this only means the ones it already serves are never cut off.
|
|
151
|
+
- Work the proxy cannot see (background jobs, scheduled tasks) is not activity. A workspace that
|
|
152
|
+
runs such work needs `keep-awake`, or `WORKSPACES_SLEEP_AFTER_MINUTES=0`.
|
|
153
|
+
|
|
154
|
+
How waking behaves:
|
|
155
|
+
|
|
156
|
+
- Only an intentionally sleeping workspace wakes. A workspace stopped by hand, failed, or still
|
|
157
|
+
preparing is never started by traffic; a GET still lands on the dashboard as before.
|
|
158
|
+
- Simultaneous first requests share one wake and start one server. A request is sent upstream
|
|
159
|
+
once, never replayed, and keeps its method and body.
|
|
160
|
+
- A wake runs only the server and readiness steps. If a step before the server changed in
|
|
161
|
+
`.workspaces.yml` since the workspace was prepared, it runs the full preparation instead.
|
|
162
|
+
- A wake that fails or runs past the timeout answers `503` with `Retry-After`, a link to the
|
|
163
|
+
dashboard, and the failure in the workspace's status and logs. It is not retried in a loop.
|
|
164
|
+
- Sleeping and waking use the same per-workspace lock as start, stop, restart and rebuild, so
|
|
165
|
+
they cannot overlap. After a front-door restart a sleeping workspace stays asleep, a sleep cut
|
|
166
|
+
short is finished, and a wake cut short shows as interrupted.
|
|
167
|
+
- Idle clocks live in the front door's memory, so a restart gives every workspace a fresh idle period.
|
|
168
|
+
|
|
117
169
|
## Serve the dashboard and previews
|
|
118
170
|
|
|
119
171
|
Run `serve` in a separate terminal using the **same project root and `WORKSPACES_HOME`**
|
|
@@ -190,6 +242,61 @@ temporarily stashed and restored (including staged state). On a restore conflict
|
|
|
190
242
|
the stash is retained for manual recovery. Updating the checkout does not run
|
|
191
243
|
setup or restart the already-running backend; use Rebuild & restart afterward.
|
|
192
244
|
|
|
245
|
+
### Environment variables and secrets
|
|
246
|
+
|
|
247
|
+
A workspace's settings are rows of **name** and **value**, with a **Secret**
|
|
248
|
+
checkbox on each. Add and remove rows as needed; **Save & restart** applies them.
|
|
249
|
+
|
|
250
|
+
A *secret* is an environment variable that is write-only. Its value goes to a
|
|
251
|
+
vault, is handed to the workspace's setup steps and server as an ordinary
|
|
252
|
+
environment variable, and is never shown again: not in the dashboard, the status
|
|
253
|
+
API, the logs (any output that contains it is filtered) or error messages. A saved
|
|
254
|
+
secret appears as its name with an empty, password-style field that says a value is
|
|
255
|
+
stored. Leave that field blank to keep the stored value, type a new one to replace
|
|
256
|
+
it, or remove the row to delete the secret. A stored secret cannot be turned back
|
|
257
|
+
into a visible variable. Names that look like secrets (`*_TOKEN`, `*_SECRET`,
|
|
258
|
+
`*_PASSWORD`, `*_API_KEY`, ...) cannot be saved as visible variables; tick **Secret**.
|
|
259
|
+
Presets only ever change visible variables.
|
|
260
|
+
|
|
261
|
+
Which vault is used is set where `serve` runs, with `WORKSPACES_VAULT` (default
|
|
262
|
+
`file`). The built-in `file` vault keeps one AES-256-GCM encrypted file per workspace
|
|
263
|
+
in `$WORKSPACES_HOME/vault/`, with a key derived per workspace from a master key you
|
|
264
|
+
provide:
|
|
265
|
+
|
|
266
|
+
```sh
|
|
267
|
+
export WORKSPACES_MASTER_KEY="$(openssl rand -base64 32)" # keep this; it cannot be recovered
|
|
268
|
+
bin/workspaces serve --secure
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Without `WORKSPACES_MASTER_KEY` the settings page says secrets are turned off, and
|
|
272
|
+
everything else works as before. With the wrong key a run fails with a plain reason
|
|
273
|
+
rather than starting without its secrets, and saving is refused so stored secrets are
|
|
274
|
+
never overwritten. The master key is read only by the front door: it is removed from
|
|
275
|
+
the environment of every step, server and `exec` command.
|
|
276
|
+
|
|
277
|
+
Removing a workspace (`remove`, or the dashboard's Remove button) deletes its secrets
|
|
278
|
+
too. The file vault is always asked to delete its own files, even if `WORKSPACES_VAULT`
|
|
279
|
+
has since been changed. A secret that cannot be deleted is reported rather than hidden:
|
|
280
|
+
`remove` exits non-zero and lists it under `secrets_left`, and the dashboard says the
|
|
281
|
+
workspace was removed but its secrets were not. Secrets live outside the workspace's
|
|
282
|
+
checkout, in `$WORKSPACES_HOME/vault/<workspace-id>.enc`, with no other record of the
|
|
283
|
+
link, so a workspace deleted some other way (by hand, or `git worktree remove`) leaves
|
|
284
|
+
its file behind. Clean those up with:
|
|
285
|
+
|
|
286
|
+
```sh
|
|
287
|
+
bin/workspaces purge-secrets --dry-run # list secrets whose workspace no longer exists
|
|
288
|
+
bin/workspaces purge-secrets # delete them
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
It refuses to run if the workspaces directory cannot be read, so an unmounted volume is
|
|
292
|
+
never mistaken for "every workspace is gone".
|
|
293
|
+
|
|
294
|
+
Another store can be added behind the same small interface (`available?`/`problem`,
|
|
295
|
+
`read(scope)`, `write(scope, entries)`, `purge(scope)`, and optionally `scopes` to
|
|
296
|
+
list what it holds so `purge-secrets` can find orphans): subclass
|
|
297
|
+
`Workspaces::Vault::Provider` and call `Workspaces::Vault.register('name', YourClass)`
|
|
298
|
+
from a file loaded with `serve --require`, then set `WORKSPACES_VAULT=name`.
|
|
299
|
+
|
|
193
300
|
### Editable environment presets
|
|
194
301
|
|
|
195
302
|
Recipes can define editable environment defaults and named presets in
|
|
@@ -206,20 +313,20 @@ environment_presets:
|
|
|
206
313
|
DEMO_MODE: "false"
|
|
207
314
|
```
|
|
208
315
|
|
|
209
|
-
Each preset maps editable variable names to values. In the workspace
|
|
210
|
-
choosing a preset
|
|
211
|
-
|
|
212
|
-
**Save & restart** to apply the draft. Presets are not applied automatically,
|
|
316
|
+
Each preset maps editable variable names to values. In the workspace settings,
|
|
317
|
+
choosing a preset replaces the visible variable rows rather than merging with
|
|
318
|
+
them (secret rows are left alone). Selection does not save or restart the
|
|
319
|
+
workspace; choose **Save & restart** to apply the draft. Presets are not applied automatically,
|
|
213
320
|
and defining them requires no JavaScript edits. Preset keys must be declared in
|
|
214
321
|
`default_editable_env`; recipes reject undeclared keys and secret-like values.
|
|
215
|
-
Hidden environment values are never exposed in the
|
|
322
|
+
Hidden environment values are never exposed in the settings or preset choices.
|
|
216
323
|
|
|
217
324
|
Use `create --branch main --preset Demo` to save a preset for a new, still-idle
|
|
218
325
|
workspace. `start ID --preset Demo` applies it before preparing a stopped
|
|
219
326
|
workspace; `restart ID --preset 'Demo off'` switches a running workspace and
|
|
220
327
|
restarts its server. `prepare ID --preset Demo` switches and rebuilds. An
|
|
221
328
|
unknown name is rejected without changing saved settings. Selecting a preset
|
|
222
|
-
replaces the editable settings (including custom
|
|
329
|
+
replaces the editable settings (including custom rows) but preserves
|
|
223
330
|
hidden legacy overrides. A running workspace requires `restart --preset` or
|
|
224
331
|
`prepare --preset`, not `start --preset`.
|
|
225
332
|
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Workspaces
|
|
4
|
+
# Remembers when each workspace last saw preview traffic and how many requests are in flight.
|
|
5
|
+
# Only the front door that proxies the traffic can know this, so it lives in memory.
|
|
6
|
+
#
|
|
7
|
+
# A request holds a Lease from the moment it is accepted until its response is finished, and a
|
|
8
|
+
# workspace with any lease out is never put to sleep. The decision to sleep and new requests meet
|
|
9
|
+
# at one mutex: once a workspace is chosen for sleep, `acquire` refuses until the sleep is over,
|
|
10
|
+
# so a request can never slip in between "nothing in flight" and "server stopped".
|
|
11
|
+
class ActivityTracker
|
|
12
|
+
class Lease
|
|
13
|
+
def initialize(tracker, id, counted)
|
|
14
|
+
@tracker = tracker
|
|
15
|
+
@id = id
|
|
16
|
+
@counted = counted
|
|
17
|
+
@released = false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def release
|
|
21
|
+
@tracker.send(:release, self, @id, @counted)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def released?
|
|
25
|
+
@released
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def mark_released!
|
|
29
|
+
was = @released
|
|
30
|
+
@released = true
|
|
31
|
+
!was
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def initialize(clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
36
|
+
@clock = clock
|
|
37
|
+
@mutex = Mutex.new
|
|
38
|
+
@last_activity = {}
|
|
39
|
+
@in_flight = Hash.new(0)
|
|
40
|
+
@sleeping = {}
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# `counted: false` is for traffic that must not keep a workspace awake (configured background
|
|
44
|
+
# polling): it still holds a lease, so the server is not stopped under it.
|
|
45
|
+
# Returns nil while the workspace is being put to sleep.
|
|
46
|
+
def acquire(id, counted: true)
|
|
47
|
+
@mutex.synchronize do
|
|
48
|
+
return nil if @sleeping[id]
|
|
49
|
+
|
|
50
|
+
@in_flight[id] += 1
|
|
51
|
+
@last_activity[id] = @clock.call if counted
|
|
52
|
+
Lease.new(self, id, counted)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def touch(id)
|
|
57
|
+
@mutex.synchronize { @last_activity[id] = @clock.call }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Seconds since the last counted activity. A workspace seen for the first time starts its idle
|
|
61
|
+
# clock now, which is also how the clocks restart after the front door does.
|
|
62
|
+
def idle_for(id)
|
|
63
|
+
@mutex.synchronize { @clock.call - (@last_activity[id] ||= @clock.call) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def in_flight(id)
|
|
67
|
+
@mutex.synchronize { @in_flight[id] }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Runs the block only if the workspace has been idle for `threshold` seconds with nothing in
|
|
71
|
+
# flight, and keeps new requests out until it returns. Returns the block's value, or false.
|
|
72
|
+
def sleep_if_idle(id, threshold)
|
|
73
|
+
@mutex.synchronize do
|
|
74
|
+
return false if @sleeping[id] || @in_flight[id].positive?
|
|
75
|
+
return false if @clock.call - (@last_activity[id] ||= @clock.call) < threshold
|
|
76
|
+
|
|
77
|
+
@sleeping[id] = true
|
|
78
|
+
end
|
|
79
|
+
begin
|
|
80
|
+
yield
|
|
81
|
+
ensure
|
|
82
|
+
@mutex.synchronize { @sleeping.delete(id) }
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def forget(id)
|
|
87
|
+
@mutex.synchronize do
|
|
88
|
+
@last_activity.delete(id)
|
|
89
|
+
@in_flight.delete(id)
|
|
90
|
+
@sleeping.delete(id)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
def release(lease, id, counted)
|
|
97
|
+
@mutex.synchronize do
|
|
98
|
+
return false unless lease.mark_released!
|
|
99
|
+
|
|
100
|
+
@in_flight[id] -= 1
|
|
101
|
+
@in_flight.delete(id) if @in_flight[id] <= 0
|
|
102
|
+
@last_activity[id] = @clock.call if counted
|
|
103
|
+
true
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
data/lib/workspaces/child_env.rb
CHANGED
|
@@ -2,6 +2,10 @@ module Workspaces
|
|
|
2
2
|
module ChildEnv
|
|
3
3
|
module_function
|
|
4
4
|
|
|
5
|
+
# Held by the front door alone. Anything a step, the server or `exec` starts must never see the
|
|
6
|
+
# key that opens the secret vault, however it was set.
|
|
7
|
+
WITHHELD = %w(WORKSPACES_MASTER_KEY).freeze
|
|
8
|
+
|
|
5
9
|
def for_workspace(workspace_path:, extra_env: {}, parent_env: ENV.to_h)
|
|
6
10
|
env = parent_env.to_h.transform_keys(&:to_s).dup
|
|
7
11
|
if defined?(::Bundler)
|
|
@@ -10,7 +14,7 @@ module Workspaces
|
|
|
10
14
|
clean.key?(name) ? env[name] = clean[name] : env.delete(name)
|
|
11
15
|
end
|
|
12
16
|
end
|
|
13
|
-
env.delete_if { |key, _| bundler_key?(key) }
|
|
17
|
+
env.delete_if { |key, _| bundler_key?(key) || WITHHELD.include?(key) }
|
|
14
18
|
|
|
15
19
|
gemfile = File.join(workspace_path.to_s, 'Gemfile')
|
|
16
20
|
env['BUNDLE_GEMFILE'] = gemfile
|
|
@@ -18,7 +22,7 @@ module Workspaces
|
|
|
18
22
|
|
|
19
23
|
extra_env.to_h.each do |name, value|
|
|
20
24
|
key = name.to_s
|
|
21
|
-
next if bundler_key?(key)
|
|
25
|
+
next if bundler_key?(key) || WITHHELD.include?(key)
|
|
22
26
|
|
|
23
27
|
if value.nil?
|
|
24
28
|
env.delete(key)
|
data/lib/workspaces/cli.rb
CHANGED
|
@@ -8,13 +8,16 @@ require_relative 'front_door'
|
|
|
8
8
|
require_relative 'proxy_middleware'
|
|
9
9
|
require_relative 'tls'
|
|
10
10
|
require_relative 'child_env'
|
|
11
|
+
require_relative 'activity_tracker'
|
|
12
|
+
require_relative 'sleeper'
|
|
13
|
+
require_relative 'duration_format'
|
|
11
14
|
|
|
12
15
|
module Workspaces
|
|
13
16
|
class Cli
|
|
14
17
|
COMMANDS = {
|
|
15
18
|
'serve' => :serve, 'create' => :create, 'list' => :list, 'show' => :show,
|
|
16
19
|
'start' => :lifecycle, 'prepare' => :lifecycle, 'restart' => :lifecycle,
|
|
17
|
-
'stop' => :stop, 'remove' => :remove, 'resume' => :resume, 'help' => :help
|
|
20
|
+
'stop' => :stop, 'remove' => :remove, 'purge-secrets' => :purge_secrets, 'resume' => :resume, 'keep-awake' => :keep_awake, 'help' => :help
|
|
18
21
|
}.freeze
|
|
19
22
|
def self.run(argv)
|
|
20
23
|
new.run(argv.dup)
|
|
@@ -85,6 +88,8 @@ module Workspaces
|
|
|
85
88
|
parser.on('--from REF') { |value| options[:from] = value }
|
|
86
89
|
parser.on('--json') { options[:json] = true }
|
|
87
90
|
parser.on('--force') { options[:force] = true }
|
|
91
|
+
parser.on('--dry-run') { options[:dry_run] = true }
|
|
92
|
+
parser.on('--off') { options[:off] = true }
|
|
88
93
|
end
|
|
89
94
|
|
|
90
95
|
def parse_tls_options(parser, options)
|
|
@@ -110,7 +115,20 @@ module Workspaces
|
|
|
110
115
|
puts "Dashboard: #{Config.public_origin}/workspaces"
|
|
111
116
|
puts "Managed directory: #{Config.worktrees_dir}"
|
|
112
117
|
print_tls_exports(tls)
|
|
113
|
-
|
|
118
|
+
tracker = ActivityTracker.new
|
|
119
|
+
sleeper = Sleeper.new(coordinator: coordinator, tracker: tracker).start
|
|
120
|
+
puts sleep_banner
|
|
121
|
+
begin
|
|
122
|
+
Rackup::Handler::Puma.run(FrontDoor.new(coordinator: coordinator, registry: registry, tracker: tracker),
|
|
123
|
+
**puma_options(tls))
|
|
124
|
+
ensure
|
|
125
|
+
sleeper.stop
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def sleep_banner
|
|
130
|
+
seconds = Config.sleep_after_seconds
|
|
131
|
+
seconds ? "Idle workspaces sleep after #{DurationFormat.call(seconds)} without preview traffic" : 'Idle sleep is off'
|
|
114
132
|
end
|
|
115
133
|
|
|
116
134
|
def print_tls_exports(tls)
|
|
@@ -177,16 +195,34 @@ module Workspaces
|
|
|
177
195
|
end
|
|
178
196
|
|
|
179
197
|
def stop(id, options)
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
198
|
+
coordinator.stop(id)
|
|
199
|
+
emit(coordinator.snapshot(id), options)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# Exempts a workspace from sleeping (or, with --off, lets it sleep again).
|
|
203
|
+
def keep_awake(id, options)
|
|
204
|
+
raise ArgumentError, 'keep-awake needs a workspace ID' unless id
|
|
205
|
+
|
|
206
|
+
coordinator.keep_awake(id, !options[:off])
|
|
184
207
|
emit(coordinator.snapshot(id), options)
|
|
185
208
|
end
|
|
186
209
|
|
|
187
210
|
def remove(id, options)
|
|
188
|
-
registry.remove(id, force: options[:force])
|
|
189
|
-
|
|
211
|
+
leftover = registry.remove(id, force: options[:force])
|
|
212
|
+
leftover = [] unless leftover.is_a?(Array)
|
|
213
|
+
emit({ 'id' => id, 'removed' => true, 'branch_preserved' => true, 'secrets_left' => leftover }, options)
|
|
214
|
+
return if leftover.empty?
|
|
215
|
+
|
|
216
|
+
warn "The workspace was removed, but its secrets could not be: #{leftover.join('; ')}. " \
|
|
217
|
+
'Fix that, then run `workspaces purge-secrets`.'
|
|
218
|
+
raise ArgumentError, 'Secrets were left behind'
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Deletes stored secrets whose workspace no longer exists. `--dry-run` lists them without deleting.
|
|
222
|
+
def purge_secrets(_id, options)
|
|
223
|
+
result = registry.sweep_orphaned_secrets(dry_run: options[:dry_run])
|
|
224
|
+
emit(result.merge('dry_run' => !!options[:dry_run]), options)
|
|
225
|
+
raise ArgumentError, "Could not remove: #{result['problems'].join('; ')}" unless result['problems'].empty?
|
|
190
226
|
end
|
|
191
227
|
|
|
192
228
|
def execute(argv)
|
|
@@ -220,8 +256,8 @@ module Workspaces
|
|
|
220
256
|
def usage
|
|
221
257
|
'Usage: bin/workspaces serve [--require FILE] | create (--branch REF | --pr NUMBER | --new-branch NAME [--from REF]) [--preset NAME] [--set NAME=VALUE] [--unset NAME] [--json] | ' \
|
|
222
258
|
'list [--pr NUMBER] [--json] | show/start/prepare/restart ID [--preset NAME] [--set NAME=VALUE] [--unset NAME] [--json] | ' \
|
|
223
|
-
'resume ID [--from STEP | --only STEP] [--json] | stop ID | ' \
|
|
224
|
-
'exec ID -- COMMAND... | remove ID [--force]'
|
|
259
|
+
'resume ID [--from STEP | --only STEP] [--json] | stop ID | keep-awake ID [--off] | ' \
|
|
260
|
+
'exec ID -- COMMAND... | remove ID [--force] | purge-secrets [--dry-run]'
|
|
225
261
|
end
|
|
226
262
|
|
|
227
263
|
def help(*)
|
data/lib/workspaces/config.rb
CHANGED
|
@@ -17,6 +17,35 @@ module Workspaces
|
|
|
17
17
|
DEFAULT_SUBDOMAIN_PREFIX = 'ws-'
|
|
18
18
|
DEFAULT_FRONT_DOOR_PORT = 4747
|
|
19
19
|
BACKEND_PORT_RANGE = (30_000..30_999)
|
|
20
|
+
DEFAULT_SLEEP_AFTER_MINUTES = 30
|
|
21
|
+
DEFAULT_WAKE_TIMEOUT_SECONDS = 90
|
|
22
|
+
DEFAULT_SLEEP_SWEEP_SECONDS = 30
|
|
23
|
+
|
|
24
|
+
# Seconds a ready workspace may go without preview traffic before it is put to sleep, or nil when
|
|
25
|
+
# sleeping is switched off (WORKSPACES_SLEEP_AFTER_MINUTES=0). Fractions of a minute are allowed.
|
|
26
|
+
def self.sleep_after_seconds
|
|
27
|
+
minutes = positive_number('WORKSPACES_SLEEP_AFTER_MINUTES', DEFAULT_SLEEP_AFTER_MINUTES, allow_zero: true)
|
|
28
|
+
minutes.zero? ? nil : minutes * 60
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# How long the first request to a sleeping preview waits for the server to be ready.
|
|
32
|
+
def self.wake_timeout_seconds
|
|
33
|
+
positive_number('WORKSPACES_WAKE_TIMEOUT_SECONDS', DEFAULT_WAKE_TIMEOUT_SECONDS)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.sleep_sweep_seconds
|
|
37
|
+
positive_number('WORKSPACES_SLEEP_SWEEP_SECONDS', DEFAULT_SLEEP_SWEEP_SECONDS)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.positive_number(name, default, allow_zero: false)
|
|
41
|
+
value = Float(ENV.fetch(name, default.to_s))
|
|
42
|
+
return value if value.finite? && (value.positive? || (allow_zero && value.zero?))
|
|
43
|
+
|
|
44
|
+
raise ArgumentError, "#{name} must be a#{allow_zero ? ' non-negative' : ' positive'} number"
|
|
45
|
+
rescue ArgumentError, TypeError
|
|
46
|
+
raise ArgumentError, "#{name} must be a#{allow_zero ? ' non-negative' : ' positive'} number"
|
|
47
|
+
end
|
|
48
|
+
private_class_method :positive_number
|
|
20
49
|
|
|
21
50
|
def self.front_door_port
|
|
22
51
|
Integer(ENV.fetch('WORKSPACES_PORT', DEFAULT_FRONT_DOOR_PORT))
|