1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use clap::{arg, ArgAction, Args, Parser, Subcommand};
/// Orign CLI.
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
/// The subcommands supported by the CLI.
#[derive(Subcommand)]
pub enum Commands {
/// Create resources.
Create {
#[command(subcommand)]
command: CreateCommands,
},
/// Get resources.
Get {
#[command(subcommand)]
command: GetCommands,
},
/// Delete resources.
Delete {
#[command(subcommand)]
command: DeleteCommands,
},
/// Sync data
Sync {
#[command(subcommand)]
command: SyncCommands,
},
/// Select a checkpoint.
Select {
#[command(subcommand)]
command: SelectCommands,
},
/// Serve the API.
Serve {
/// The address to bind to.
#[arg(long, default_value = "127.0.0.1")]
host: String,
/// The port to bind to.
#[arg(long, default_value_t = 3000)]
port: u16,
/// Disable internal auth server
#[arg(long, default_value_t = true)]
internal_auth: bool,
/// The port to bind the internal auth server to.
#[arg(long, default_value_t = 8080)]
auth_port: u16,
},
/// Proxy services.
Proxy {
#[command(subcommand)]
command: ProxyCommands,
},
/// Run the daemon.
Daemon {
/// The address to bind to.
#[arg(long, default_value = "127.0.0.1")]
host: String,
/// The port to bind to.
#[arg(short, long, default_value_t = 3000)]
port: u16,
/// Run in the background (detached) if true.
#[arg(short, long, default_value_t = false)]
background: bool,
},
/// Fetch logs for a container.
Logs {
/// Container name.
name: String,
/// Container namespace.
#[arg(long, short)]
namespace: Option<String>,
/// Follow the logs
#[arg(long, short, default_value_t = false)]
follow: bool,
},
/// Send a message to a processor.
Send {
#[command(subcommand)]
command: SendCommands,
},
/// Login to a Nebulous API server.
Login {
/// Address of the API server
#[arg(default_value = "http://127.0.0.1")]
url: String,
/// Address of the Auth server
#[arg(long, default_value = None)]
auth: Option<String>,
/// Address of the Hub
#[arg(long, default_value = None)]
hub: Option<String>,
},
/// Auth commands.
Auth {
#[command(subcommand)]
command: AuthCommands,
},
/// Execute a command inside a container.
Exec(ExecArgs),
/// Show configuration information.
Show {
#[command(subcommand)]
command: ShowCommands,
},
/// Set configuration options.
Set {
#[command(subcommand)]
command: SetCommands,
},
}
/// Select a checkpoint.
#[derive(Subcommand)]
pub enum SelectCommands {
/// Select a checkpoint from a base directory using a given criterion.
Checkpoint {
/// Path to the base directory holding checkpoints (e.g., "checkpoint-1", "checkpoint-2").
#[arg(long, default_value = ".")]
base_dir: String,
/// The selection criterion: "latest" or "best".
#[arg(long, default_value = "best")]
criteria: String,
},
}
// The struct that captures all CLI fields for the Exec command.
#[derive(Args)]
pub struct ExecArgs {
/// Container's name
pub name: String,
/// Container's namespace
#[arg(long, short)]
pub namespace: String,
/// The command (and args) to run in the container
#[arg(long, short)]
pub command: String,
/// Whether to pass `-i` (interactive)
#[arg(short = 'i', long, default_value_t = false)]
pub interactive: bool,
/// Whether to pass `-t` (tty)
#[arg(short = 't', long, default_value_t = false)]
pub tty: bool,
}
/// Secret creation parameters
#[derive(Args)]
pub struct SecretCommands {
/// Secret name
pub name: String,
/// Secret namespace
#[arg(long)]
pub namespace: Option<String>,
/// The secret value. (If none given, you must provide a file instead)
#[arg(long)]
pub value: Option<String>,
/// Time (in seconds from epoch or similar) for the secret to expire
#[arg(long)]
pub expires_at: Option<i32>,
/// Read the secret value from a file.
#[arg(short = 'f', long)]
pub file: Option<String>,
}
#[derive(Subcommand)]
pub enum ProxyCommands {
/// Proxy local shell commands over HTTP.
Shell {
/// The host to bind to.
#[arg(long, default_value = "0.0.0.0")]
host: String,
/// The port to bind to.
#[arg(short, long, default_value_t = 8080)]
port: u16,
},
}
/// Subcommands for syncing
#[derive(Subcommand)]
pub enum SyncCommands {
/// Sync a volume.
#[command(aliases = ["volume", "vol"])]
Volumes {
/// Path to the YAML configuration file.
#[arg(short, long)]
config: String,
/// Interval in seconds to sync.
#[arg(short, long, default_value_t = 60)]
interval_seconds: u64,
/// Create the config file if it doesn't exist.
#[arg(long, default_value_t = false)]
create_if_missing: bool,
/// Run in the background.
#[arg(short, long, default_value_t = false)]
watch: bool,
/// Run in the background.
#[arg(short, long, default_value_t = false)]
background: bool,
/// Block until the one time sync paths are complete.
#[arg(long, default_value_t = false)]
block_once: bool,
/// Sync from the NEBU_SYNC_CONFIG environment variable.
#[arg(long, default_value_t = false)]
config_from_env: bool,
},
/// Ensure all syncs are complete.
Wait {
/// Path to the YAML configuration file.
#[arg(short, long)]
config: String,
/// Interval in seconds to sync.
#[arg(short, long, default_value_t = 2)]
interval_seconds: u64,
},
}
/// Create resources.
#[derive(Subcommand)]
pub enum CreateCommands {
/// Create a container.
#[command(aliases = ["container", "co"])]
Containers {
#[command(flatten)]
command: ContainerCommands,
},
/// Create a secret.
#[command(aliases = ["secret", "sec"])]
Secrets {
#[command(flatten)]
command: SecretCommands,
},
}
/// Container creation parameters
#[derive(Args)]
pub struct ContainerCommands {
/// Container name
#[arg(long)]
pub name: Option<String>,
/// Container namespace
#[arg(long)]
pub namespace: Option<String>,
/// Platform to run the container on
#[arg(long)]
pub platform: Option<String>,
/// Container image
#[arg(long)]
pub image: Option<String>,
/// Command to run in the container
#[arg(long)]
pub cmd: Option<String>,
/// Environment variables in KEY=VALUE format
#[arg(long, value_parser = parse_key_val, action = ArgAction::Append)]
pub env: Option<Vec<(String, String)>>,
/// Labels in KEY=VALUE format
#[arg(long, value_parser = parse_key_val, action = ArgAction::Append)]
pub label: Option<Vec<(String, String)>>,
/// Accelerators to use
#[arg(long, action = ArgAction::Append)]
pub accelerators: Option<Vec<String>>,
/// Source path for volume mount
#[arg(long)]
pub volume_source: Option<String>,
/// Destination path for volume mount
#[arg(long)]
pub volume_destination: Option<String>,
/// Enable bidirectional sync for volume (default: true)
#[arg(long, default_value = "RCLONE_SYNC")]
pub volume_type: Option<String>,
/// Enable continuous sync for volume (default: true)
#[arg(long, default_value_t = true)]
pub volume_continuous: bool,
/// Enable resync for volume (default: false)
#[arg(long, default_value_t = false)]
pub volume_resync: bool,
/// Cache directory for volume (default: /nebu/cache)
#[arg(long, default_value = "/nebu/cache")]
pub volume_cache_dir: String,
/// File input
#[arg(short = 'f', long)]
pub file: Option<String>,
/// Meters of the container
#[arg(long)]
pub meter_cost: Option<f64>,
/// Meter cost plus
#[arg(long)]
pub meter_cost_plus: Option<f64>,
/// Meter unit of the container
#[arg(long)]
pub meter_metric: Option<String>,
/// Meter currency of the container
#[arg(long)]
pub meter_currency: Option<String>,
/// Meter unit of the container
#[arg(long)]
pub meter_unit: Option<String>,
/// Restart policy of the container
#[arg(long)]
pub restart: Option<String>,
/// Queue to run the container in
#[arg(long)]
pub queue: Option<String>,
/// Timeout for the container
#[arg(long)]
pub timeout: Option<String>,
/// Minimum CPU
#[arg(long)]
pub min_cpu: Option<f64>,
/// Minimum memory
#[arg(long)]
pub min_memory: Option<f64>,
/// Maximum CPU
#[arg(long)]
pub max_cpu: Option<f64>,
/// Maximum memory
#[arg(long)]
pub max_memory: Option<f64>,
/// Proxy port
#[arg(long)]
pub proxy_port: Option<i16>,
}
/// Parse a key-value pair in the format of KEY=VALUE
fn parse_key_val(s: &str) -> Result<(String, String), String> {
let pos = s
.find('=')
.ok_or_else(|| format!("Invalid KEY=VALUE: no `=` found in `{s}`"))?;
Ok((s[..pos].to_string(), s[pos + 1..].to_string()))
}
/// Get resources.
#[derive(Subcommand)]
pub enum GetCommands {
/// Get accelerators.
#[command(aliases = ["accelerator", "acc"])]
Accelerators {
/// Platform to get accelerators for.
#[arg(long)]
platform: Option<String>,
},
/// Get containers.
#[command(aliases = ["container", "co"])]
Containers {
/// Platform to get containers for.
id: Option<String>,
},
/// Get processors.
#[command(aliases = ["processor", "proc"])]
Processors {
/// Optional processor name.
name: Option<String>,
/// Optional processor namespace.
#[arg(long)]
namespace: Option<String>,
},
/// Get platforms.
#[command(aliases = ["platform", "plat"])]
Platforms,
/// Get secrets.
#[command(aliases = ["secret", "sec"])]
Secrets {
/// Optional secret ID.
id: Option<String>,
},
}
/// Delete resources.
#[derive(Subcommand)]
pub enum DeleteCommands {
/// Delete a container.
#[command(aliases = ["container", "co"])]
Containers {
/// Specific container name to delete. Required unless -A/--all is used.
name: Option<String>,
/// Namespace to delete from.
#[arg(long, short)]
namespace: Option<String>,
/// Delete all containers in the specified namespace.
#[arg(short = 'A', long, default_value_t = false)]
all: bool,
},
/// Delete a processor
#[command(aliases = ["processor", "proc"])]
Processors {
name: String,
/// Namespace to delete from.
#[arg(long, short)]
namespace: Option<String>,
},
}
#[derive(Args)]
pub struct DeleteProcessorCommands {
/// Processor name
pub name: String,
/// Processor namespace
#[arg(short = 'n', long)]
pub namespace: String,
}
/// Send resources.
#[derive(Subcommand)]
pub enum SendCommands {
/// Send a message to a stream.
#[command(aliases = ["message", "msg"])]
Messages {
#[command(flatten)]
command: SendMessageCommands,
},
}
/// Parameters for sending a message to a stream
#[derive(Args)]
pub struct SendMessageCommands {
/// Processor name
pub name: String,
/// Processor namespace
#[arg(long, short)]
pub namespace: Option<String>,
/// File input containing message content (reads from stdin if not provided)
#[arg(short = 'f', long)]
pub file: Option<String>,
/// Wait for the message to be processed and return the result
#[arg(long, short, default_value_t = false)]
pub wait: bool,
}
/// Subcommands for the "work" command
#[derive(Subcommand)]
pub enum WorkCommands {}
/// Subcommands for the "auth" command
#[derive(Subcommand)]
pub enum AuthCommands {
/// Manage API keys for authentication with the Nebulous API server.
#[command(aliases = ["api-key"])]
ApiKeys {
#[command(subcommand)]
action: ApiKeyActions,
},
// TODO: Add auth for tailnet
}
#[derive(Subcommand)]
pub enum ApiKeyActions {
/// List API keys.
List,
/// Get API key details.
Get {
/// The ID of the API key to get.
id: String,
},
/// Generate a new API key.
Generate,
/// Revoke an API key.
Revoke {
/// The ID of the API key to delete.
id: String,
},
}
/// Subcommands for showing configuration information.
#[derive(Subcommand)]
pub enum ShowCommands {
/// Show the current global configuration.
Config,
}
/// Subcommands for setting configuration options.
#[derive(Subcommand)]
pub enum SetCommands {
/// Set the current server context.
Context {
/// Name of the server to set as current
name: String,
},
}