Skip to content

Commit

Permalink
feat: nushell installer + remove command + some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tulilirockz committed May 23, 2024
1 parent 508d28b commit 92d994f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
46 changes: 32 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ dP dP `88888P' Y88888P 88Y888P' `88888P8 8888P Y8P dP dP 8P
```
---

A [Nushell](https://nushell.sh) wrapper over systemd-nspawn and machinectl initially inspired by the the [nspawn](https://github.com/nspawn/nspawn/tree/master) nspawnhub wrapper script.
[![Copr build status](https://copr.fedorainfracloud.org/coprs/tulilirockz/nuspawn/package/nuspawn/status_image/last_build.png)](https://copr.fedorainfracloud.org/coprs/tulilirockz/nuspawn/package/nuspawn/)

A [Nushell](https://nushell.sh) wrapper over systemd-nspawn and machinectl initially inspired by the the [nspawn](https://github.com/nspawn/nspawn/tree/master) nspawnhub wrapper script meant to make usage easier and more integrated with [nspawn.org](https://nspawn.org/) (nspawnhub)

We aim to make this as self-contained with as few dependencies as possible, using just the [nushell](https://nushell.sh) and few binaries like [machinectl](https://www.freedesktop.org/software/systemd/man/latest/machinectl.html), [gpg](https://www.gnupg.org/) and GNU tar (optionally).

Expand All @@ -27,7 +29,7 @@ machinectl start debian-sid-tar
machinectl login debian-sid-tar

# Advanced usage example: Importing a nspawn configuration to the container and verifiying using the nspawnhub gpg key
nuspawn init debian sid --name "mydebbox" --config=./distrobox-like.nspawn
nuspawn init debian sid --name "mydebbox" --config=./distrobox-like.nspawn.ini
```

### Composing machines
Expand Down Expand Up @@ -75,16 +77,6 @@ nuspawn config show debox # Shows every property specified in your configuration
nuspawn config remove debox # Removes any configuration set for `systemd-nspawn@debox.service`
```

### Prune

Deletes every image from your system, including configurations if specified.

```bash
~/opt/tulilirockz/nuspawn/src> nuspawn prune
[nuspawn] THIS COMMAND WILL CLEAR ALL IMAGES IN LOCAL STORAGE, type YES if you agree to delete everything
Do you wish to delete all your local images? [N]:
```
### Inspecting images

You can fetch images locally without adding them to the systemd-nspawn machine directory by using `nuspawn fetch`
Expand All @@ -98,16 +90,42 @@ nuspawn fetch --type=raw debian sid .
systemd-dissect ./debian-sid-raw.raw
```


### Deleting machines

```bash
~/opt/tulilirockz/nuspawn/src> nuspawn remove debox
Do you wish to delete all your local images? [N]:
```

You can also Prune, which will delete every image from your system, including configurations if specified.

```bash
~/opt/tulilirockz/nuspawn/src> nuspawn prune
[nuspawn] THIS COMMAND WILL CLEAR ALL IMAGES IN LOCAL STORAGE, type YES if you agree to delete everything
Do you wish to delete all your local images? [N]:
```
## Installing
Most of the packaging is still yet to be done, tracking issue at #3
### Fedora/OpenSUSE/RHEL (RPM)
Available in [my COPR @ `tulilirockz/nuspawn`](https://copr.fedorainfracloud.org/tulilirockz)!
Available in [my COPR @ `tulilirockz/nuspawn`](https://copr.fedorainfracloud.org/tulilirockz/nuspawn)!
### Debian/Ubuntu (DEB)
TODO!
### Arch Linux (PKGBUILD)
TODO!
### Alpine Linux / Chimera / PostmarketOS (APK)
TODO! (maybe), since like... they dont exactly have systemd in them, right?
### NixOS/Nix (Nix)
#### Nix Profile
Expand Down Expand Up @@ -144,7 +162,7 @@ You should be able to install this project by using the `install.nu` script on y
curl -fsSL "https://raw.githubusercontent.com/tulilirockz/nuspawn/main/install.nu" | nu
```
It is NOT recommended to do that, though.
It is NOT recommended to do that, though!
## Known Issues
Expand Down
11 changes: 11 additions & 0 deletions install.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env nu
let temp_folder = (mktemp -d)
git clone https://github.com/tulilirockz/nuspawn.git $temp_folder
try {
sudo sed -i "s~./lib~/usr/libexec/nuspawn~g" $"($temp_folder)/src/nuspawn"
sudo mkdir "/usr/libexec/nuspawn"
sudo cp ...(glob $"($temp_folder)/src/lib/*") /usr/libexec/nuspawn
sudo cp $"($temp_folder)/src/nuspawn" /usr/bin/nuspawn
} catch {
print "Failed installing due to permission error"
}
2 changes: 1 addition & 1 deletion src/lib/config.nu
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use logger.nu *

const CONFIG_EXTENSION = "nspawn"

def get_config_path [config_root: string, machine_name: string, extension: string = $CONFIG_EXTENSION] {
export def get_config_path [config_root: string, machine_name: string, extension: string = $CONFIG_EXTENSION] {
return $"($config_root)/($machine_name).($extension)"
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/list.nu
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export def "main list" [
try {
ls -l $target | select name readonly type created
} catch {
logger error $"Failed listing images, your user should have permissions over the ($target) folder. Try running as root"
logger error $"Failure listing machines due to permission issues"
return
}
}
1 change: 1 addition & 0 deletions src/lib/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export use fetch.nu *
export use prune.nu *
export use config.nu *
export use compose.nu *
export use remove.nu *
10 changes: 7 additions & 3 deletions src/lib/prune.nu
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use logger.nu *
export def "main prune" [
--storage-root = $MACHINE_STORAGE_PATH # Place where all images are located (WARNING: will delete everything there!)
--config-root = $MACHINE_CONFIG_PATH # Place where all images are located (WARNING: will delete everything there!)
--clean = true # Clean hidden files with machinectl
--no-warning # Do not warn that this will delete everything from local storage
] -> null {
if not $no_warning {
logger warning "THIS COMMAND WILL CLEAR ALL IMAGES IN LOCAL STORAGE, type YES if you agree to delete everything"
try {
ls -a $storage_root
ls -a $config_root
ls -a $storage_root
ls -a $config_root
} catch {
logger error "Failure displaying files to be deleted due to permission errors"
return
Expand All @@ -28,7 +29,10 @@ export def "main prune" [
try {
# I dislike this pattern as much as the next person, but I really dont know how to make this work properly
# TODO: Make this better somehow
print ...(glob $"($storage_root)/*") ...(glob $"($config_root)/*")
rm -rifv ...(glob $"($storage_root)/*") ...(glob $"($config_root)/*")
if $clean {
machinectl clean
}
} catch {
logger error "Failure deleting local storage images due to permission errors"
return
Expand Down
39 changes: 39 additions & 0 deletions src/lib/remove.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use meta.nu [MACHINE_STORAGE_PATH, MACHINE_CONFIG_PATH]
use config.nu [get_config_path]
use logger.nu *

# Delete an nspawn image or machine
export def "main remove" [
--storage-root = $MACHINE_STORAGE_PATH # Path for machine storage
--config-root = $MACHINE_CONFIG_PATH # Path for nspawn configurations
--yes (-y) # Do not warn when deleting machine
--full (-f) = true # Delete configuration for the machine too
--type (-t): string = "tar" # Type of the machine to be deleted
machine_name: string # Which machine will be deleted
] -> null {
if not $yes {
try {
ls -la $"($storage_root)/($machine_name)*"
} catch {
logger error "Failure displaying files to be deleted due to permission errors"
return
}
let yesno = (input $"(ansi blue_bold)Do you wish to delete the selected image? [y/N]: (ansi reset)")

match $yesno {
YES|yes|Yes|Y|y => { }
_ => { return }
}
}

logger warning $"Deleting image and configurations"
try {
rm -fivr ...(glob $"($storage_root)/(machine_name)*")
if $full {
rm -fivr ...(glob $"($config_root)/($machine_name)*")
}
} catch {
logger error "Failure deleting local storage image due to permission errors"
return
}
}

0 comments on commit 92d994f

Please sign in to comment.