sillydev 1.0.0 → 1.2.0
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/README.md +56 -6
- data/lib/sillydev/cli.rb +510 -3
- data/lib/sillydev/client.rb +35 -8
- data/lib/sillydev/console.rb +17 -0
- data/lib/sillydev/errors.rb +24 -4
- data/lib/sillydev/mcp_server.rb +255 -10
- data/lib/sillydev/monitoring/client.rb +23 -3
- data/lib/sillydev/monitoring/runtime.rb +5 -5
- data/lib/sillydev/monitoring.rb +11 -1
- data/lib/sillydev/resources.rb +365 -27
- data/lib/sillydev/version.rb +1 -1
- metadata +2 -2
data/lib/sillydev/cli.rb
CHANGED
|
@@ -13,15 +13,33 @@ module SillyDev
|
|
|
13
13
|
# sillydev logs 1a7ce997
|
|
14
14
|
# sillydev console 1a7ce997
|
|
15
15
|
# sillydev deploy 1a7ce997
|
|
16
|
+
# sillydev rollback 1a7ce997
|
|
16
17
|
# sillydev files ls 1a7ce997 /src
|
|
17
18
|
# sillydev backups create 1a7ce997
|
|
19
|
+
# sillydev sign-in-methods
|
|
20
|
+
# sillydev status subscribe --preset major
|
|
21
|
+
# sillydev status discord add https://discord.com/api/webhooks/...
|
|
22
|
+
# sillydev databases list
|
|
23
|
+
# sillydev databases create "Bot cache" --engine valkey
|
|
24
|
+
# sillydev keys list 12 "user:*"
|
|
25
|
+
# sillydev keys get 12 user:1
|
|
26
|
+
# sillydev folders list
|
|
27
|
+
# sillydev bulk restart <uuid> <uuid>
|
|
28
|
+
# sillydev alerts show 1a7ce997
|
|
29
|
+
# sillydev passkeys
|
|
30
|
+
# sillydev sessions
|
|
18
31
|
# sillydev mcp
|
|
19
32
|
#
|
|
20
33
|
# The API key comes from --api-key or the SILLYDEV_API_KEY environment variable. Add --json to
|
|
21
34
|
# any command for machine-readable output.
|
|
22
35
|
class CLI
|
|
23
36
|
POWER_SIGNALS = %w[start stop restart kill].freeze
|
|
37
|
+
ENGINES = %w[postgres mariadb valkey].freeze
|
|
38
|
+
ALERT_EVENTS = %w[crash crash_loop oom disk].freeze
|
|
24
39
|
TRANSPORTS = (["auto"] + %w[websocket stream poll relay]).freeze
|
|
40
|
+
PRESETS = %w[everything new_resolved major].freeze
|
|
41
|
+
BULK_ACTIONS = %w[start stop restart kill backup].freeze
|
|
42
|
+
FOLDER_COLOURS = %w[slate red orange amber green teal blue violet pink].freeze
|
|
25
43
|
PRINTED = ["console output", "install output", "daemon message"].freeze
|
|
26
44
|
|
|
27
45
|
USAGE = <<~TEXT
|
|
@@ -38,14 +56,70 @@ module SillyDev
|
|
|
38
56
|
logs <server> [--transport WAY] print the server's recent console output
|
|
39
57
|
console <server> [--transport WAY] follow the console live; lines typed are sent as
|
|
40
58
|
commands (Ctrl+C to stop)
|
|
41
|
-
deploy <server>
|
|
59
|
+
deploy <server> [--resume] deploy the linked Git repository now; --resume
|
|
60
|
+
also turns auto deploy back on after a rollback
|
|
61
|
+
rollback <server> [deployment] deploy the previous (or that deployment's) commit
|
|
62
|
+
again; auto deploy pauses
|
|
63
|
+
deploy-log <server> [deployment] print what the newest (or that) deployment's build
|
|
64
|
+
and release command printed
|
|
42
65
|
files ls <server> [directory] list a directory
|
|
43
66
|
files cat <server> <file> print a file
|
|
44
67
|
backups list <server> list a server's backups
|
|
45
68
|
backups create <server> [--name NAME] [--locked] [--ignore PATH ...]
|
|
46
69
|
start a backup
|
|
70
|
+
status subscription show the status page email and Discord subscriptions
|
|
71
|
+
status subscribe [--preset PRESET] email the account about status page incidents
|
|
72
|
+
status unsubscribe stop those emails
|
|
73
|
+
status discord add <url> [--label NAME] [--preset PRESET]
|
|
74
|
+
send status page incidents to a Discord webhook
|
|
75
|
+
status discord remove <id> stop sending them to a webhook
|
|
76
|
+
databases list list your databases and Redis stores
|
|
77
|
+
databases create <label> [--engine postgres|mariadb|valkey] [--version-id N]
|
|
78
|
+
[--eviction-policy P] create a database or Redis store
|
|
79
|
+
keys list <database> [pattern] [--db N] list a Redis store's keys
|
|
80
|
+
keys get <database> <key> [--db N] print a key's value
|
|
81
|
+
keys set <database> <key> <value> [--field F] [--ttl S] [--db N]
|
|
82
|
+
set a string key or a hash field
|
|
83
|
+
keys del <database> <key...> [--db N] delete keys
|
|
84
|
+
alerts show <server> show which crash, memory and disk alerts are on
|
|
85
|
+
alerts set <server> [--on EVENT ...] [--off EVENT ...] [--email on|off]
|
|
86
|
+
[--disk-percent N] [--webhook URL | --remove-webhook] [--rotate-secret | --show-secret]
|
|
87
|
+
change which alerts are sent and where
|
|
88
|
+
alerts test <server> send a test alert
|
|
89
|
+
alerts deliveries <server> list the webhook's last deliveries
|
|
90
|
+
passkeys list your account's passkeys (no key material)
|
|
91
|
+
sessions list the browsers signed in to your account
|
|
92
|
+
backups destination <server> show the bucket backups are copied to
|
|
93
|
+
backups destination-set <server> --bucket B --access-key K [--secret-key S | --secret-key-file F]
|
|
94
|
+
[--endpoint URL | --amazon-s3] [--region R] [--prefix P] [--path-style] [--pause]
|
|
95
|
+
set up or change it (the secret can also be in
|
|
96
|
+
$SILLYDEV_BACKUP_SECRET_KEY, out of your shell history)
|
|
97
|
+
backups destination-test <server> write, read and delete a test file in the bucket
|
|
98
|
+
backups destination-remove <server> forget the bucket (its copies are kept)
|
|
99
|
+
backups retry-copy <server> <backup> try copying a backup to the bucket again
|
|
100
|
+
folders list list dashboard folders and what is in them
|
|
101
|
+
folders create <name> [--colour COLOUR] make a folder
|
|
102
|
+
folders update <id> [--name NAME] [--colour COLOUR]
|
|
103
|
+
rename or recolour a folder
|
|
104
|
+
folders delete <id> delete a folder (its servers are kept)
|
|
105
|
+
folders move <id|none> <uuid>... put servers in a folder, or in none
|
|
106
|
+
bulk <action> <uuid>... start|stop|restart|kill|backup up to 25 servers
|
|
107
|
+
db backups list <database> list a database's backups (its numeric id)
|
|
108
|
+
db backups create <database> back a database up now
|
|
109
|
+
db backups download <database> <backup> [--output FILE]
|
|
110
|
+
save a backup as a .sql.gz file
|
|
111
|
+
db backups restore <database> <backup> --yes
|
|
112
|
+
replace the database with the backup (undoable)
|
|
113
|
+
db backups restore <database> <backup> --new LABEL --version-id ID
|
|
114
|
+
restore the backup into a new database
|
|
115
|
+
sign-in-methods list the Discord, Google and GitHub accounts you
|
|
116
|
+
sign in with
|
|
47
117
|
mcp run the MCP server on stdio
|
|
48
118
|
|
|
119
|
+
PRESET is everything, new_resolved or major. The status commands need a key with the status.write
|
|
120
|
+
scope to change anything.
|
|
121
|
+
|
|
122
|
+
EVENT is crash, crash_loop, oom or disk.
|
|
49
123
|
WAY is auto (the default: tries websocket, stream, poll and then relay), websocket, stream,
|
|
50
124
|
poll or relay. <server> is the 8-character identifier or the UUID.
|
|
51
125
|
|
|
@@ -66,7 +140,7 @@ module SillyDev
|
|
|
66
140
|
|
|
67
141
|
# Runs one command and returns the exit status.
|
|
68
142
|
def run(argv)
|
|
69
|
-
options = { json: false, transport: "auto", ignore: [] }
|
|
143
|
+
options = { json: false, transport: "auto", ignore: [], engine: "postgres", on: [], off: [] }
|
|
70
144
|
parser = build_parser(options)
|
|
71
145
|
args = catch(:done) { parser.parse(argv) }
|
|
72
146
|
return 0 if args.nil? # --help or --version
|
|
@@ -76,6 +150,29 @@ module SillyDev
|
|
|
76
150
|
@err.puts(parser.help)
|
|
77
151
|
return 2
|
|
78
152
|
end
|
|
153
|
+
if options[:api_key_flag]
|
|
154
|
+
@err.puts("sillydev: warning: --api-key puts the key in your shell history and in ps; use SILLYDEV_API_KEY or --api-key-file.")
|
|
155
|
+
end
|
|
156
|
+
if options[:api_key_file]
|
|
157
|
+
begin
|
|
158
|
+
options[:api_key] = File.read(options[:api_key_file]).strip
|
|
159
|
+
rescue SystemCallError => e
|
|
160
|
+
@err.puts("sillydev: could not read #{options[:api_key_file]}: #{e.message}")
|
|
161
|
+
return 2
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
if options[:secret_key_flag]
|
|
165
|
+
@err.puts("sillydev: warning: --secret-key puts the key in your shell history and in ps; " \
|
|
166
|
+
"use SILLYDEV_BACKUP_SECRET_KEY or --secret-key-file.")
|
|
167
|
+
end
|
|
168
|
+
if options[:secret_key_file]
|
|
169
|
+
begin
|
|
170
|
+
options[:secret_key] = File.read(options[:secret_key_file]).strip
|
|
171
|
+
rescue SystemCallError => e
|
|
172
|
+
@err.puts("sillydev: could not read #{options[:secret_key_file]}: #{e.message}")
|
|
173
|
+
return 2
|
|
174
|
+
end
|
|
175
|
+
end
|
|
79
176
|
return mcp(options) if command == "mcp"
|
|
80
177
|
|
|
81
178
|
client = @client_factory.call(api_key: options[:api_key], base_url: options[:base_url])
|
|
@@ -110,7 +207,11 @@ module SillyDev
|
|
|
110
207
|
def build_parser(options)
|
|
111
208
|
OptionParser.new do |o|
|
|
112
209
|
o.banner = USAGE
|
|
113
|
-
o.on("--api-key KEY", "API key (default: $SILLYDEV_API_KEY)")
|
|
210
|
+
o.on("--api-key KEY", "API key (default: $SILLYDEV_API_KEY; a key on the command line shows in ps)") do |v|
|
|
211
|
+
options[:api_key] = v
|
|
212
|
+
options[:api_key_flag] = true
|
|
213
|
+
end
|
|
214
|
+
o.on("--api-key-file FILE", "Read the API key from FILE") { |v| options[:api_key_file] = v }
|
|
114
215
|
o.on("--base-url URL", "API base URL (default: $SILLYDEV_BASE_URL or the public API)") { |v| options[:base_url] = v }
|
|
115
216
|
o.on("--json", "print JSON") { options[:json] = true }
|
|
116
217
|
o.on("--search TEXT", "servers list: match the name, identifier, UUID or an ip:port") { |v| options[:search] = v }
|
|
@@ -118,7 +219,48 @@ module SillyDev
|
|
|
118
219
|
o.on("--transport WAY", TRANSPORTS, "logs, console: how to connect") { |v| options[:transport] = v }
|
|
119
220
|
o.on("--name NAME", "backups create: the backup's name") { |v| options[:name] = v }
|
|
120
221
|
o.on("--locked", "backups create: lock it so it cannot be deleted") { options[:locked] = true }
|
|
222
|
+
o.on("--resume", "deploy: also turn auto deploy back on after a rollback") { options[:resume] = true }
|
|
121
223
|
o.on("--ignore PATH", "backups create: leave out a path (repeatable)") { |v| options[:ignore] << v }
|
|
224
|
+
o.on("--label NAME", "status discord add: a name for the webhook") { |v| options[:label] = v }
|
|
225
|
+
o.on("--preset PRESET", PRESETS, "status subscribe, status discord add: everything, new_resolved or major") do |v|
|
|
226
|
+
options[:preset] = v
|
|
227
|
+
end
|
|
228
|
+
o.on("--engine ENGINE", ENGINES, "databases create: postgres, mariadb or valkey") { |v| options[:engine] = v }
|
|
229
|
+
o.on("--version-id N", Integer, "databases create, db backups restore --new: a version id") { |v| options[:version_id] = v }
|
|
230
|
+
o.on("--eviction-policy P", "databases create: what a Redis store does when full") { |v| options[:eviction_policy] = v }
|
|
231
|
+
o.on("--field F", "keys set: set this field of a hash") { |v| options[:field] = v }
|
|
232
|
+
o.on("--ttl S", Integer, "keys set: expire after S seconds") { |v| options[:ttl] = v }
|
|
233
|
+
o.on("--db N", Integer, "keys: logical database 0-15") { |v| options[:db] = v }
|
|
234
|
+
o.on("--on EVENT", ALERT_EVENTS, "alerts set: turn an alert on (repeatable)") { |v| options[:on] << v }
|
|
235
|
+
o.on("--off EVENT", ALERT_EVENTS, "alerts set: turn an alert off (repeatable)") { |v| options[:off] << v }
|
|
236
|
+
o.on("--email STATE", %w[on off], "alerts set: send alerts by email") { |v| options[:email] = v == "on" }
|
|
237
|
+
o.on("--disk-percent N", Integer, "alerts set: disk alert threshold, 50 to 99 (paid servers)") { |v| options[:disk_percent] = v }
|
|
238
|
+
o.on("--webhook URL", "alerts set: a Discord or custom https webhook") { |v| options[:webhook] = v }
|
|
239
|
+
o.on("--remove-webhook", "alerts set: stop sending alerts to the webhook") { options[:remove_webhook] = true }
|
|
240
|
+
o.on("--rotate-secret", "alerts set: give a custom webhook a new signing secret (printed)") { options[:rotate_secret] = true }
|
|
241
|
+
o.on("--show-secret", "alerts show|set: also print the webhook's signing secret") { options[:show_secret] = true }
|
|
242
|
+
o.on("--output FILE", "db backups download: where to save it (default <backup>.sql.gz)") { |v| options[:output] = v }
|
|
243
|
+
o.on("--yes", "db backups restore: confirm replacing the database (undoable for a few days)") { options[:yes] = true }
|
|
244
|
+
o.on("--new LABEL", "db backups restore: restore into a new database with this name") { |v| options[:new_label] = v }
|
|
245
|
+
o.on("--bucket NAME", "backups destination-set: the bucket") { |v| options[:bucket] = v }
|
|
246
|
+
o.on("--access-key KEY", "backups destination-set: the bucket's access key") { |v| options[:access_key] = v }
|
|
247
|
+
o.on("--secret-key KEY", "backups destination-set: the secret key (shows in ps and your shell history; " \
|
|
248
|
+
"prefer --secret-key-file or $SILLYDEV_BACKUP_SECRET_KEY)") do |v|
|
|
249
|
+
options[:secret_key] = v
|
|
250
|
+
options[:secret_key_flag] = true
|
|
251
|
+
end
|
|
252
|
+
o.on("--secret-key-file FILE", "backups destination-set: read the secret key from FILE") do |v|
|
|
253
|
+
options[:secret_key_file] = v
|
|
254
|
+
end
|
|
255
|
+
o.on("--endpoint URL", "backups destination-set: an https endpoint; leave out for Amazon S3") { |v| options[:endpoint] = v }
|
|
256
|
+
o.on("--amazon-s3", "backups destination-set: forget the endpoint and use Amazon S3 again") { options[:amazon_s3] = true }
|
|
257
|
+
o.on("--region NAME", "backups destination-set: the bucket's region") { |v| options[:region] = v }
|
|
258
|
+
o.on("--prefix PATH", "backups destination-set: a folder in the bucket") { |v| options[:prefix] = v }
|
|
259
|
+
o.on("--path-style", "backups destination-set: path-style addresses") { options[:path_style] = true }
|
|
260
|
+
o.on("--pause", "backups destination-set: stop copying without forgetting the bucket") { options[:pause] = true }
|
|
261
|
+
o.on("--colour COLOUR", FOLDER_COLOURS, "folders create, update: slate, red, orange, amber, green, teal, blue, violet, pink") do |v|
|
|
262
|
+
options[:colour] = v
|
|
263
|
+
end
|
|
122
264
|
o.on("-v", "--version", "print the version") do
|
|
123
265
|
@out.puts("sillydev #{VERSION}")
|
|
124
266
|
throw :done
|
|
@@ -131,6 +273,33 @@ module SillyDev
|
|
|
131
273
|
end
|
|
132
274
|
end
|
|
133
275
|
|
|
276
|
+
def previous_deployment(panel, server)
|
|
277
|
+
previous = panel.git.get(server)["previous"]
|
|
278
|
+
raise Error, "there is no earlier deployment to roll back to" unless previous
|
|
279
|
+
|
|
280
|
+
previous["uuid"]
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def newest_deployment(panel, server)
|
|
284
|
+
deployments = panel.git.get(server)["deployments"] || []
|
|
285
|
+
raise Error, "this server has no deployments yet" if deployments.empty?
|
|
286
|
+
|
|
287
|
+
deployments.first["uuid"]
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def show_resume(data)
|
|
291
|
+
@out.puts("Auto deploy resumed")
|
|
292
|
+
queued = data["deployment"]
|
|
293
|
+
@out.puts("Deployment #{queued['uuid']} #{queued['status']} (#{queued['branch']})") if queued
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def show_deployment_log(data)
|
|
297
|
+
failed = data["failed_step"] ? " (failed at #{data['failed_step']})" : ""
|
|
298
|
+
@out.puts("Deployment #{data['uuid']} #{data['status']}#{failed}")
|
|
299
|
+
@out.puts(data["error"]) if data["error"]
|
|
300
|
+
@out.write(data["output"].to_s)
|
|
301
|
+
end
|
|
302
|
+
|
|
134
303
|
def need(args, index, name)
|
|
135
304
|
args[index] or raise UsageError, "missing <#{name}>"
|
|
136
305
|
end
|
|
@@ -145,6 +314,17 @@ module SillyDev
|
|
|
145
314
|
in ["servers", "get"]
|
|
146
315
|
data = panel.servers.get(need(args, 1, "server"))
|
|
147
316
|
[data, -> { @out.puts(JSON.pretty_generate(data)) }]
|
|
317
|
+
in ["status", "subscription"]
|
|
318
|
+
data = panel.account.get_status_subscription
|
|
319
|
+
[data, -> { show_subscription(data) }]
|
|
320
|
+
in ["status", "subscribe"]
|
|
321
|
+
data = panel.account.update_status_subscription(subscribed: true, preset: options[:preset])
|
|
322
|
+
[data, -> { @out.puts("Status page emails are on (#{data.dig('email', 'preset')})") }]
|
|
323
|
+
in ["status", "unsubscribe"]
|
|
324
|
+
data = panel.account.update_status_subscription(subscribed: false)
|
|
325
|
+
[data, -> { @out.puts("Status page emails are off") }]
|
|
326
|
+
in ["status", "discord"]
|
|
327
|
+
status_discord(panel, args, options)
|
|
148
328
|
in ["status", _]
|
|
149
329
|
data = panel.servers.resources(need(args, 0, "server"))
|
|
150
330
|
[data, -> { show_status(data) }]
|
|
@@ -165,9 +345,26 @@ module SillyDev
|
|
|
165
345
|
in ["logs", _]
|
|
166
346
|
lines = panel.console.history(need(args, 0, "server"), transport: options[:transport])
|
|
167
347
|
[lines, -> { lines.each { |line| @out.puts(line) } }]
|
|
348
|
+
in ["deploy", _] if options[:resume]
|
|
349
|
+
data = panel.git.resume(need(args, 0, "server"), deploy: true)
|
|
350
|
+
[data, -> { show_resume(data) }]
|
|
168
351
|
in ["deploy", _]
|
|
169
352
|
data = panel.git.deploy(need(args, 0, "server"))
|
|
170
353
|
[data, -> { @out.puts("Deployment #{data['uuid']} #{data['status']} (#{data['branch']})") }]
|
|
354
|
+
in ["rollback", _]
|
|
355
|
+
server = need(args, 0, "server")
|
|
356
|
+
deployment = args[1] || previous_deployment(panel, server)
|
|
357
|
+
data = panel.git.redeploy(server, deployment)
|
|
358
|
+
sha = data["target_commit"].to_s[0, 7]
|
|
359
|
+
[data, lambda {
|
|
360
|
+
@out.puts("Deployment #{data['uuid']} #{data['status']}: #{sha} again; " \
|
|
361
|
+
"auto deploy is paused (sillydev deploy --resume)")
|
|
362
|
+
}]
|
|
363
|
+
in ["deploy-log", _]
|
|
364
|
+
server = need(args, 0, "server")
|
|
365
|
+
deployment = args[1] || newest_deployment(panel, server)
|
|
366
|
+
data = panel.git.deployment(server, deployment)
|
|
367
|
+
[data, -> { show_deployment_log(data) }]
|
|
171
368
|
in ["files", "ls"]
|
|
172
369
|
data = panel.files.list(need(args, 1, "server"), args[2] || "/")
|
|
173
370
|
[data, -> { show_files(data) }]
|
|
@@ -178,15 +375,249 @@ module SillyDev
|
|
|
178
375
|
in ["backups", "list"]
|
|
179
376
|
data = panel.backups.list(need(args, 1, "server")).to_a
|
|
180
377
|
[data, -> { show_backups(data) }]
|
|
378
|
+
in ["sign-in-methods", _]
|
|
379
|
+
data = panel.account.sign_in_methods
|
|
380
|
+
[data, -> { show_sign_in_methods(data) }]
|
|
381
|
+
in ["folders", _]
|
|
382
|
+
data = dispatch_folders(panel, args, options)
|
|
383
|
+
[data, -> { show_folders(data) }]
|
|
384
|
+
in ["bulk", _]
|
|
385
|
+
action = args.first
|
|
386
|
+
raise UsageError, "bulk action must be one of #{BULK_ACTIONS.join(', ')}" unless BULK_ACTIONS.include?(action)
|
|
387
|
+
|
|
388
|
+
servers = args.drop(1)
|
|
389
|
+
raise UsageError, "missing <uuid>" if servers.empty?
|
|
390
|
+
|
|
391
|
+
data = panel.folders.bulk(action, servers)
|
|
392
|
+
[data, -> { show_bulk(data) }]
|
|
181
393
|
in ["backups", "create"]
|
|
182
394
|
data = panel.backups.create(need(args, 1, "server"), name: options[:name], is_locked: options[:locked],
|
|
183
395
|
ignored: options[:ignore].empty? ? nil : options[:ignore])
|
|
184
396
|
[data, -> { @out.puts("Backup #{data['uuid']} started") }]
|
|
397
|
+
in ["databases", "list"]
|
|
398
|
+
data = panel.databases.list["databases"] || []
|
|
399
|
+
[data, -> { show_databases(data) }]
|
|
400
|
+
in ["databases", "create"]
|
|
401
|
+
label = need(args, 1, "label")
|
|
402
|
+
data = panel.databases.create(label: label, version_id: options[:version_id] || pick_version(panel, options[:engine]),
|
|
403
|
+
eviction_policy: options[:eviction_policy])
|
|
404
|
+
[data, lambda {
|
|
405
|
+
@out.puts("Created #{data['label']} (##{data['id']}) at #{data['host']}:#{data['port']}, user #{data['username']}")
|
|
406
|
+
}]
|
|
407
|
+
in ["keys", "list"]
|
|
408
|
+
data = scan_keys(panel, database_id(args), args[2] || "*", options[:db])
|
|
409
|
+
[data, -> { show_keys(data) }]
|
|
410
|
+
in ["keys", "get"]
|
|
411
|
+
data = panel.databases.get_key(database_id(args), need(args, 2, "key"), db: options[:db])
|
|
412
|
+
[data, -> { @out.puts(data["type"] == "string" ? data["value"].to_s : JSON.pretty_generate(data["items"] || [])) }]
|
|
413
|
+
in ["keys", "set"]
|
|
414
|
+
key = need(args, 2, "key")
|
|
415
|
+
panel.databases.set_key(database_id(args), key, need(args, 3, "value"),
|
|
416
|
+
field: options[:field], ttl_seconds: options[:ttl], db: options[:db])
|
|
417
|
+
[{ ok: true, key: key }, -> { @out.puts("Set #{key}") }]
|
|
418
|
+
in ["keys", "del"]
|
|
419
|
+
need(args, 2, "key")
|
|
420
|
+
data = panel.databases.delete_keys(database_id(args), args[2..], db: options[:db])
|
|
421
|
+
[data, -> { @out.puts("Deleted #{data['deleted'] || 0}") }]
|
|
422
|
+
in ["alerts", _]
|
|
423
|
+
dispatch_alerts(panel, args, options)
|
|
424
|
+
in ["sessions", nil]
|
|
425
|
+
data = panel.account.sessions
|
|
426
|
+
[data, -> { show_sessions(data) }]
|
|
427
|
+
in ["passkeys", nil]
|
|
428
|
+
data = panel.account.passkeys
|
|
429
|
+
[data, -> { show_passkeys(data) }]
|
|
430
|
+
in ["backups", "destination"]
|
|
431
|
+
data = panel.backups.destination(need(args, 1, "server"))
|
|
432
|
+
[data, -> { show_destination(data) }]
|
|
433
|
+
in ["backups", "destination-set"]
|
|
434
|
+
data = panel.backups.set_destination(
|
|
435
|
+
need(args, 1, "server"),
|
|
436
|
+
bucket: options[:bucket], access_key: options[:access_key],
|
|
437
|
+
secret_key: options[:secret_key] || ENV.fetch("SILLYDEV_BACKUP_SECRET_KEY", nil),
|
|
438
|
+
endpoint: options[:endpoint], region: options[:region], prefix: options[:prefix],
|
|
439
|
+
path_style: options[:path_style], enabled: options[:pause] ? false : nil,
|
|
440
|
+
clear_endpoint: options[:amazon_s3] ? true : false
|
|
441
|
+
)
|
|
442
|
+
[data, -> { @out.puts("Saved.") }]
|
|
443
|
+
in ["backups", "retry-copy"]
|
|
444
|
+
data = panel.backups.retry_copy(need(args, 1, "server"), need(args, 2, "backup"))
|
|
445
|
+
[data, -> { @out.puts("Queued. The copy starts shortly.") }]
|
|
446
|
+
in ["backups", "destination-test"]
|
|
447
|
+
data = panel.backups.test_destination(need(args, 1, "server"))
|
|
448
|
+
[data, -> { @out.puts("The bucket works.") }]
|
|
449
|
+
in ["backups", "destination-remove"]
|
|
450
|
+
panel.backups.delete_destination(need(args, 1, "server"))
|
|
451
|
+
[{ "removed" => true }, -> { @out.puts("Removed. Copies already in the bucket are kept.") }]
|
|
452
|
+
in ["db", "backups"]
|
|
453
|
+
db_backups(panel, args.drop(1), options)
|
|
185
454
|
else
|
|
186
455
|
raise UsageError, "unknown command: #{[command, args.first].compact.join(' ')}"
|
|
187
456
|
end
|
|
188
457
|
end
|
|
189
458
|
|
|
459
|
+
def status_discord(panel, args, options)
|
|
460
|
+
case args[1]
|
|
461
|
+
when "add"
|
|
462
|
+
data = panel.account.add_status_discord_webhook(url: need(args, 2, "url"), label: options[:label],
|
|
463
|
+
preset: options[:preset])
|
|
464
|
+
[data, -> { @out.puts("Discord webhook added") }]
|
|
465
|
+
when "remove"
|
|
466
|
+
id = need(args, 2, "id")
|
|
467
|
+
panel.account.remove_status_discord_webhook(id)
|
|
468
|
+
[{ ok: true, id: id }, -> { @out.puts("Discord webhook #{id} removed") }]
|
|
469
|
+
else
|
|
470
|
+
raise UsageError, "unknown command: status discord #{args[1]}".strip
|
|
471
|
+
end
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
def show_subscription(data)
|
|
475
|
+
email = data["email"] || {}
|
|
476
|
+
@out.puts("email: #{email['subscribed'] ? "on (#{email['preset']})" : 'off'}")
|
|
477
|
+
hooks = Array(data["discord"])
|
|
478
|
+
return @out.puts("discord: none") if hooks.empty?
|
|
479
|
+
|
|
480
|
+
table([%w[ID LABEL WEBHOOK PRESET STATE]] + hooks.map do |h|
|
|
481
|
+
[h["id"], h["label"], h["webhook"], h["preset"], h["disabled"] ? "off (#{h['disabled_reason']})" : "on"].map(&:to_s)
|
|
482
|
+
end)
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
def database_id(args)
|
|
486
|
+
id = need(args, 1, "database")
|
|
487
|
+
raise UsageError, "<database> must be a number" unless id.match?(/\A\d+\z/)
|
|
488
|
+
|
|
489
|
+
id.to_i
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
def pick_version(panel, engine)
|
|
493
|
+
versions = (panel.databases.list["versions"] || []).select { |v| v["engine"] == engine && v["status"] == "active" }
|
|
494
|
+
raise UsageError, "no #{engine} version is on offer" if versions.empty?
|
|
495
|
+
|
|
496
|
+
(versions.find { |v| v["label"] == "recommended" } || versions.first)["id"]
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
def scan_keys(panel, database, pattern, db)
|
|
500
|
+
found = []
|
|
501
|
+
cursor = "0"
|
|
502
|
+
loop do
|
|
503
|
+
page = panel.databases.list_keys(database, pattern: pattern, cursor: cursor, count: 500, db: db)
|
|
504
|
+
found.concat(page["keys"] || [])
|
|
505
|
+
cursor = page["cursor"].to_s
|
|
506
|
+
break if cursor == "0" || cursor.empty? || found.length >= 1000
|
|
507
|
+
end
|
|
508
|
+
found
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
def show_keys(data)
|
|
512
|
+
table([%w[TYPE TTL KEY]] + data.map do |k|
|
|
513
|
+
ttl = k["ttl_ms"].to_i
|
|
514
|
+
[k["type"].to_s, ttl.negative? ? "-" : "#{ttl / 1000}s", k["key"].to_s]
|
|
515
|
+
end)
|
|
516
|
+
end
|
|
517
|
+
|
|
518
|
+
def show_databases(data)
|
|
519
|
+
table([%w[ID NAME TYPE ADDRESS USED]] + data.map do |d|
|
|
520
|
+
[d["id"].to_s, d["label"].to_s, (d["version"] || {})["name"].to_s, "#{d['host']}:#{d['port']}", size(d["size_bytes"])]
|
|
521
|
+
end)
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
def dispatch_alerts(panel, args, options)
|
|
525
|
+
server = need(args, 1, "server")
|
|
526
|
+
case args.first
|
|
527
|
+
when "show"
|
|
528
|
+
data = hide_alert_secret(panel.settings.alerts(server), options[:show_secret])
|
|
529
|
+
[data, -> { show_alerts(data) }]
|
|
530
|
+
when "set"
|
|
531
|
+
raise UsageError, "use --webhook or --remove-webhook, not both" if options[:webhook] && options[:remove_webhook]
|
|
532
|
+
|
|
533
|
+
events = options[:on].to_h { |e| [e, true] }.merge(options[:off].to_h { |e| [e, false] })
|
|
534
|
+
data = panel.settings.update_alerts(server, email: options[:email], events: events.empty? ? nil : events,
|
|
535
|
+
disk_percent: options[:disk_percent], webhook_url: options[:webhook],
|
|
536
|
+
remove_webhook: options[:remove_webhook] || false,
|
|
537
|
+
rotate_secret: options[:rotate_secret])
|
|
538
|
+
data = hide_alert_secret(data, options[:show_secret] || options[:rotate_secret])
|
|
539
|
+
[data, -> { show_alerts(data) }]
|
|
540
|
+
when "test"
|
|
541
|
+
panel.settings.send_test_alert(server)
|
|
542
|
+
[{ ok: true }, -> { @out.puts("Test alert sent") }]
|
|
543
|
+
when "deliveries"
|
|
544
|
+
data = panel.settings.alert_deliveries(server)
|
|
545
|
+
[data, lambda {
|
|
546
|
+
table([%w[WHEN EVENT HOST STATUS HTTP ERROR]] + data.map do |d|
|
|
547
|
+
[d["created_at"], d["event"], d["host"], d["status"], d["http_status"] || "-", d["error"] || "-"].map(&:to_s)
|
|
548
|
+
end)
|
|
549
|
+
}]
|
|
550
|
+
else
|
|
551
|
+
raise UsageError, "unknown command: alerts #{args.first}"
|
|
552
|
+
end
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
# Leaves the webhook's signing secret out of the output (text and --json) unless asked for.
|
|
556
|
+
def hide_alert_secret(data, show)
|
|
557
|
+
webhook = data["webhook"]
|
|
558
|
+
return data if show || !webhook.is_a?(Hash) || !webhook.key?("secret")
|
|
559
|
+
|
|
560
|
+
data.merge("webhook" => webhook.except("secret"))
|
|
561
|
+
end
|
|
562
|
+
|
|
563
|
+
def show_alerts(data)
|
|
564
|
+
events = data["events"] || {}
|
|
565
|
+
webhook = data["webhook"] || {}
|
|
566
|
+
on = ALERT_EVENTS.select { |e| events[e] }
|
|
567
|
+
@out.puts("alerts: #{on.empty? ? 'none' : on.join(', ')}")
|
|
568
|
+
@out.puts("email: #{data['email'] ? 'on' : 'off'}")
|
|
569
|
+
@out.puts("disk at: #{data['disk_percent']}%")
|
|
570
|
+
if webhook["set"]
|
|
571
|
+
@out.puts("webhook: #{webhook['type']} #{webhook['masked']}")
|
|
572
|
+
@out.puts("secret: #{webhook['secret']}") if webhook["secret"]
|
|
573
|
+
else
|
|
574
|
+
@out.puts("webhook: none")
|
|
575
|
+
end
|
|
576
|
+
end
|
|
577
|
+
|
|
578
|
+
def db_backups(panel, args, options)
|
|
579
|
+
action = args.first
|
|
580
|
+
database = need(args, 1, "database")
|
|
581
|
+
case action
|
|
582
|
+
when "list"
|
|
583
|
+
data = panel.databases.backups(database)
|
|
584
|
+
[data, -> { show_database_backups(data) }]
|
|
585
|
+
when "create"
|
|
586
|
+
data = panel.databases.back_up(database)
|
|
587
|
+
[data, -> { @out.puts("Backup #{data['id']} made (#{size(data['size_bytes'])})") }]
|
|
588
|
+
when "download"
|
|
589
|
+
backup = need(args, 2, "backup")
|
|
590
|
+
body = panel.databases.download_backup(database, backup)
|
|
591
|
+
path = options[:output] || "#{backup}.sql.gz"
|
|
592
|
+
File.binwrite(path, body)
|
|
593
|
+
[{ "file" => path, "bytes" => body.bytesize }, -> { @out.puts("Saved #{path} (#{size(body.bytesize)})") }]
|
|
594
|
+
when "restore"
|
|
595
|
+
backup = need(args, 2, "backup")
|
|
596
|
+
if options[:new_label]
|
|
597
|
+
raise UsageError, "--new needs --version-id ID" if options[:version_id].nil?
|
|
598
|
+
|
|
599
|
+
data = panel.databases.restore_backup(database, backup, new_label: options[:new_label],
|
|
600
|
+
version_id: options[:version_id])
|
|
601
|
+
elsif options[:yes]
|
|
602
|
+
data = panel.databases.restore_backup(database, backup)
|
|
603
|
+
else
|
|
604
|
+
raise UsageError, "restoring replaces everything in the database (it can be undone for a few days); " \
|
|
605
|
+
"add --yes, or --new LABEL --version-id ID"
|
|
606
|
+
end
|
|
607
|
+
[data, -> { @out.puts("Restoring #{backup} into #{data['label']} (database #{data['id']})") }]
|
|
608
|
+
else
|
|
609
|
+
raise UsageError, "unknown command: db backups #{action}".rstrip
|
|
610
|
+
end
|
|
611
|
+
end
|
|
612
|
+
|
|
613
|
+
def show_database_backups(data)
|
|
614
|
+
rows = [["ID", "KIND", "SIZE", "CREATED", ""]]
|
|
615
|
+
(data["backups"] || []).each do |b|
|
|
616
|
+
rows << [b["id"].to_s, b["kind"].to_s, size(b["size_bytes"]), b["created_at"].to_s, b["pinned"] ? "pinned" : ""]
|
|
617
|
+
end
|
|
618
|
+
table(rows)
|
|
619
|
+
end
|
|
620
|
+
|
|
190
621
|
def table(rows)
|
|
191
622
|
return if rows.empty?
|
|
192
623
|
|
|
@@ -222,6 +653,50 @@ module SillyDev
|
|
|
222
653
|
end)
|
|
223
654
|
end
|
|
224
655
|
|
|
656
|
+
def show_destination(data)
|
|
657
|
+
dest = data["destination"]
|
|
658
|
+
if !data["available"]
|
|
659
|
+
@out.puts("Backups to your own storage are not available for this server.")
|
|
660
|
+
elsif dest.nil?
|
|
661
|
+
@out.puts("No bucket is set up.")
|
|
662
|
+
else
|
|
663
|
+
@out.puts("#{dest['bucket']}/#{dest['prefix']} (#{dest['enabled'] ? 'on' : 'off'})")
|
|
664
|
+
@out.puts("Last error: #{dest['last_error']}") if dest["last_error"]
|
|
665
|
+
end
|
|
666
|
+
end
|
|
667
|
+
|
|
668
|
+
def dispatch_folders(panel, args, options)
|
|
669
|
+
case args.first
|
|
670
|
+
when "list" then panel.folders.list
|
|
671
|
+
when "create" then panel.folders.create(name: need(args, 1, "name"), colour: options[:colour] || "slate")
|
|
672
|
+
when "update" then panel.folders.update(Integer(need(args, 1, "id")), name: options[:name], colour: options[:colour])
|
|
673
|
+
when "delete" then panel.folders.delete(Integer(need(args, 1, "id")))
|
|
674
|
+
when "move"
|
|
675
|
+
id = need(args, 1, "id|none")
|
|
676
|
+
servers = args.drop(2)
|
|
677
|
+
raise UsageError, "missing <uuid>" if servers.empty?
|
|
678
|
+
|
|
679
|
+
panel.folders.assign(id == "none" ? nil : Integer(id), servers)
|
|
680
|
+
else
|
|
681
|
+
raise UsageError, "usage: sillydev folders list|create|update|delete|move"
|
|
682
|
+
end
|
|
683
|
+
end
|
|
684
|
+
|
|
685
|
+
def show_folders(data)
|
|
686
|
+
assignments = data.dig("meta", "assignments") || {}
|
|
687
|
+
rows = [%w[ID NAME COLOUR SERVERS]]
|
|
688
|
+
(data["data"] || []).each do |f|
|
|
689
|
+
rows << [f["id"].to_s, f["name"].to_s, f["colour"].to_s, assignments.values.count(f["id"]).to_s]
|
|
690
|
+
end
|
|
691
|
+
table(rows)
|
|
692
|
+
end
|
|
693
|
+
|
|
694
|
+
def show_bulk(data)
|
|
695
|
+
rows = [%w[SERVER RESULT REASON]]
|
|
696
|
+
(data["data"] || []).each { |r| rows << [(r["name"] || r["uuid"]).to_s, r["status"].to_s, r["reason"].to_s] }
|
|
697
|
+
table(rows)
|
|
698
|
+
end
|
|
699
|
+
|
|
225
700
|
def show_backups(data)
|
|
226
701
|
rows = [%w[UUID NAME SIZE STATUS CREATED]]
|
|
227
702
|
data.each do |b|
|
|
@@ -235,6 +710,38 @@ module SillyDev
|
|
|
235
710
|
table(rows)
|
|
236
711
|
end
|
|
237
712
|
|
|
713
|
+
def show_sessions(data)
|
|
714
|
+
rows = [["ID", "BROWSER", "SYSTEM", "IP ADDRESS", "LAST ACTIVE"]]
|
|
715
|
+
data.each do |s|
|
|
716
|
+
rows << [s["id"].to_s, s["browser"] || "-", s["platform"] || "-", s["ip_address"] || "-",
|
|
717
|
+
s["last_active_at"] || "-"]
|
|
718
|
+
end
|
|
719
|
+
table(rows)
|
|
720
|
+
end
|
|
721
|
+
|
|
722
|
+
def show_sign_in_methods(data)
|
|
723
|
+
@out.puts("password: #{data['has_password'] ? 'set' : 'not set'}")
|
|
724
|
+
methods = data["methods"] || []
|
|
725
|
+
if methods.empty?
|
|
726
|
+
@out.puts("No sign-in methods connected.")
|
|
727
|
+
return
|
|
728
|
+
end
|
|
729
|
+
rows = [["PROVIDER", "USERNAME", "CONNECTED", "LAST USED"]]
|
|
730
|
+
methods.each do |m|
|
|
731
|
+
rows << [m["provider_name"].to_s, m["username"].to_s, m["connected_at"].to_s, (m["last_used_at"] || "-").to_s]
|
|
732
|
+
end
|
|
733
|
+
table(rows)
|
|
734
|
+
end
|
|
735
|
+
|
|
736
|
+
def show_passkeys(data)
|
|
737
|
+
rows = [["NAME", "DEVICE", "SYNCED", "ADDED", "LAST USED"]]
|
|
738
|
+
data.each do |p|
|
|
739
|
+
rows << [p["name"].to_s, p["device"] || "-", p["synced"] ? "yes" : "no", p["created_at"] || "-",
|
|
740
|
+
p["last_used_at"] || "never"]
|
|
741
|
+
end
|
|
742
|
+
table(rows)
|
|
743
|
+
end
|
|
744
|
+
|
|
238
745
|
# Follows the console until Ctrl+C (or the stop latch) or the console ends; lines from stdin
|
|
239
746
|
# become commands.
|
|
240
747
|
def follow(client, server, options)
|