|
1 | 1 | module mcl.commands.machine; |
2 | 2 |
|
3 | 3 | import std; |
| 4 | + |
| 5 | +import argparse; |
| 6 | + |
4 | 7 | import mcl.utils.log : prompt; |
5 | 8 | import mcl.utils.process : execute; |
6 | 9 | import mcl.utils.nix : nix, toNix, Literal, mkDefault; |
@@ -173,22 +176,22 @@ string[] getGroups() |
173 | 176 | return groups; |
174 | 177 | } |
175 | 178 |
|
176 | | -User createUser() { |
| 179 | +User createUser(create_machine_args args) { |
177 | 180 |
|
178 | | - auto createUser = params.createUser || prompt!bool("Create new user"); |
| 181 | + auto createUser = args.createUser || prompt!bool("Create new user"); |
179 | 182 | if (!createUser) |
180 | 183 | { |
181 | 184 | string[] existingUsers = getExistingUsers(); |
182 | | - string userName = params.userName != "" ? params.userName : prompt!string("Select an existing username", existingUsers); |
| 185 | + string userName = args.userName != "" ? args.userName : prompt!string("Select an existing username", existingUsers); |
183 | 186 | return getUser(userName); |
184 | 187 | } |
185 | 188 | else |
186 | 189 | { |
187 | 190 | User user; |
188 | | - user.userName = params.userName != "" ? params.userName : prompt!string("Enter the new username"); |
189 | | - user.userInfo.description = params.description != "" ? params.description : prompt!string("Enter the user's description/full name"); |
190 | | - user.userInfo.isNormalUser = params.isNormalUser || prompt!bool("Is this a normal or root user"); |
191 | | - user.userInfo.extraGroups = (params.extraGroups != "" ? params.extraGroups : prompt!string("Enter the user's extra groups (comma delimited)", getGroups())).split(",").map!(strip).array; |
| 191 | + user.userName = args.userName != "" ? args.userName : prompt!string("Enter the new username"); |
| 192 | + user.userInfo.description = args.description != "" ? args.description : prompt!string("Enter the user's description/full name"); |
| 193 | + user.userInfo.isNormalUser = args.isNormalUser || prompt!bool("Is this a normal or root user"); |
| 194 | + user.userInfo.extraGroups = (args.extraGroups != "" ? args.extraGroups : prompt!string("Enter the user's extra groups (comma delimited)", getGroups())).split(",").map!(strip).array; |
192 | 195 | createUserDir(user); |
193 | 196 | return user; |
194 | 197 | } |
@@ -220,8 +223,8 @@ struct MachineConfiguration |
220 | 223 | MachineUserInfo users; |
221 | 224 | } |
222 | 225 |
|
223 | | -void createMachine(MachineType machineType, string machineName, User user) { |
224 | | - auto infoJSON = execute(["ssh", params.sshPath, "sudo nix --experimental-features \\'nix-command flakes\\' --refresh --accept-flake-config run github:metacraft-labs/nixos-modules/#mcl host_info"],false, false); |
| 226 | +void createMachine(create_machine_args args, MachineType machineType, string machineName, User user) { |
| 227 | + auto infoJSON = execute(["ssh", args.sshPath, "sudo nix --experimental-features \\'nix-command flakes\\' --refresh --accept-flake-config run github:metacraft-labs/nixos-modules/#mcl host_info"],false, false); |
225 | 228 | auto infoJSONParsed = infoJSON.parseJSON; |
226 | 229 | Info info = infoJSONParsed.fromJSON!Info; |
227 | 230 |
|
@@ -273,7 +276,7 @@ void createMachine(MachineType machineType, string machineName, User user) { |
273 | 276 | // Disks |
274 | 277 | hardwareConfiguration.disko.DISKO.makeZfsPartitions.swapSizeGB = (info.hardwareInfo.memoryInfo.totalGB.to!double*1.5).to!int; |
275 | 278 | auto nvmeDevices = info.hardwareInfo.storageInfo.devices.filter!(a => a.dev.indexOf("nvme") != -1 || a.model.indexOf("SSD") != -1).array.map!(a => a.model.replace(" ", "_") ~ "_" ~ a.serial).array; |
276 | | - string[] disks = (nvmeDevices.length == 1 ? nvmeDevices[0] : (params.disks != "" ? params.disks : prompt!string("Enter the disks to use (comma delimited)", nvmeDevices))).split(",").map!(strip).array.map!(a => "/dev/disk/by-id/nvme-" ~ a).array; |
| 279 | + string[] disks = (nvmeDevices.length == 1 ? nvmeDevices[0] : (args.disks != "" ? args.disks : prompt!string("Enter the disks to use (comma delimited)", nvmeDevices))).split(",").map!(strip).array.map!(a => "/dev/disk/by-id/nvme-" ~ a).array; |
277 | 280 | hardwareConfiguration.disko.DISKO.makeZfsPartitions.disks = disks; |
278 | 281 |
|
279 | 282 | hardwareConfiguration = hardwareConfiguration.uniqArrays; |
@@ -365,43 +368,58 @@ struct HardwareConfiguration { |
365 | 368 | Services services; |
366 | 369 | } |
367 | 370 |
|
368 | | -void createMachineConfiguration() |
| 371 | +int createMachineConfiguration(create_machine_args args) |
369 | 372 | { |
370 | 373 | checkifNixosMachineConfigRepo(); |
371 | | - auto machineType = cast(int)params.machineType != 0 ? params.machineType : prompt!MachineType("Machine type"); |
372 | | - auto machineName = params.machineName != "" ? params.machineName : prompt!string("Enter the name of the machine"); |
| 374 | + auto machineType = cast(int)args.machineType != 0 ? args.machineType : prompt!MachineType("Machine type"); |
| 375 | + auto machineName = args.machineName != "" ? args.machineName : prompt!string("Enter the name of the machine"); |
373 | 376 | User user; |
374 | | - user = createUser(); |
375 | | - machineType.createMachine( machineName, user); |
| 377 | + user = createUser(args); |
| 378 | + args.createMachine(machineType, machineName, user); |
| 379 | + return 0; |
376 | 380 | } |
377 | 381 |
|
378 | | -Params params; |
379 | 382 |
|
380 | | -export void machine(string[] args) |
| 383 | +export int machine(machine_args args) |
381 | 384 | { |
382 | | - params = parseEnv!Params; |
383 | | - switch (args.front) |
384 | | - { |
385 | | - case "create": |
386 | | - createMachineConfiguration(); |
387 | | - break; |
388 | | - default: |
389 | | - assert(0, "Unknown machine action: " ~ args.front); |
390 | | - } |
| 385 | + return args.cmd.match!( |
| 386 | + (create_machine_args a) => createMachineConfiguration(a), |
| 387 | + (unknown_command_args a) => unknown_command(a) |
| 388 | + ); |
391 | 389 | } |
392 | | -struct Params |
393 | | -{ |
| 390 | +@(Command("create").Description("Create a new machine")) |
| 391 | +struct create_machine_args { |
| 392 | + |
| 393 | + @(PositionalArgument(0).Placeholder("ssh").Description("SSH path to the machine")) |
394 | 394 | string sshPath; |
395 | | - @optional() bool createUser; |
396 | | - @optional() string userName; |
397 | | - @optional() string machineName; |
398 | | - @optional() string description; |
399 | | - @optional() bool isNormalUser; |
400 | | - @optional() string extraGroups; |
401 | | - @optional() MachineType machineType = cast(MachineType)0; |
402 | | - @optional() string disks; |
403 | | - |
404 | | - void setup() |
405 | | - { |
406 | | - } |
| 395 | + @(NamedArgument(["create-user"]).Placeholder("true/false").Description("Create a new user")) |
| 396 | + bool createUser; |
| 397 | + @(NamedArgument(["user-name"]).Placeholder("username").Description("Username")) |
| 398 | + string userName; |
| 399 | + @(NamedArgument(["machine-name"]).Placeholder("machine-name").Description("Name of the machine")) |
| 400 | + string machineName; |
| 401 | + @(NamedArgument(["description"]).Placeholder("description").Description("Description of the user")) |
| 402 | + string description; |
| 403 | + @(NamedArgument(["is-normal-user"]).Placeholder("true/false").Description("Is this a normal user")) |
| 404 | + bool isNormalUser; |
| 405 | + @(NamedArgument(["extra-groups"]).Placeholder("group1,group2").Description("Extra groups for the user")) |
| 406 | + string extraGroups; |
| 407 | + @(NamedArgument(["machine-type"]).Placeholder("desktop/server/container").Description("Type of machine")) |
| 408 | + MachineType machineType = cast(MachineType)0; |
| 409 | + @(NamedArgument(["disks"]).Placeholder("CT2000P3PSSD8_2402E88C1519,...").Description("Disks to use")) |
| 410 | + string disks; |
| 411 | +} |
| 412 | +@(Command(" ").Description(" ")) |
| 413 | +struct unknown_command_args {} |
| 414 | +int unknown_command(unknown_command_args unused) |
| 415 | +{ |
| 416 | + stderr.writeln("Unknown machine command. Use --help for a list of available commands."); |
| 417 | + return 1; |
| 418 | +} |
| 419 | + |
| 420 | +@(Command("machine").Description("Manage machines")) |
| 421 | +struct machine_args |
| 422 | +{ |
| 423 | + |
| 424 | + @SubCommands SumType!(create_machine_args,Default!unknown_command_args) cmd; |
407 | 425 | } |
0 commit comments