bins: remove xin-status in favor of xin

This commit is contained in:
Aaron Bieber 2024-06-25 14:43:40 -06:00
parent 8363089de1
commit a708943107
No known key found for this signature in database
5 changed files with 126 additions and 31 deletions

View File

@ -20,9 +20,6 @@ let
checkRestart =
pkgs.writeScriptBin "check-restart"
(import ./check-restart.nix { inherit (pkgs) perl; });
xinStatus =
pkgs.writeScriptBin "xin-status"
(import ./xin-status.nix { inherit (pkgs) perl perlPackages; });
sfetch = pkgs.writeScriptBin "sfetch" (import ./sfetch.nix {
inherit gosignify;
inherit (pkgs) curl;
@ -40,7 +37,6 @@ in
ix
sfetch
xclip
xinStatus
] ++ (if config.services.postgresql.enable then
[ upgrade-pg ]
else [ ]);

View File

@ -1,27 +0,0 @@
{ perl
, perlPackages
, ...
}: ''
#!${perl}/bin/perl
use strict;
use warnings;
use MIME::Base64;
use lib "${perlPackages.JSON}/${perl.libPrefix}/${perl.version}/";
use JSON qw{ decode_json encode_json };
my $info = decode_json(`nixos-version --json`);
$info->{needs_restart} = system('check-restart >/dev/null') == 0 ? JSON::false : JSON::true;
my $sys_diff = `nix store diff-closures /run/booted-system /run/current-system`;
$sys_diff =~ s/\e\[[0-9;]*m(?:\e\[K)?//g;
$info->{system_diff} = encode_base64($sys_diff);
$info->{uname_a} = `uname -a`;
$info->{uptime} = `uptime`;
chomp $info->{uname_a};
chomp $info->{uptime};
print encode_json $info;
''

22
bins/xin/Makefile.PL Normal file
View File

@ -0,0 +1,22 @@
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'xin',
VERSION_FROM => 'xin.pl',
EXE_FILES => [qw(xin.pl)],
LICENSE => 'MIT',
MIN_PERL_VERSION => '5.0038',
META_MERGE => {
'meta-spec' => { version => 2 },
resources => {
repository => {
type => 'git',
url => 'https://github.com/qbit/xin.git',
web => 'https://github.com/qbit/xin',
},
},
},
PREREQ_PM => {
'JSON' => '0',
},
);

16
bins/xin/default.nix Normal file
View File

@ -0,0 +1,16 @@
{ pkgs, ... }:
with pkgs;
perlPackages.buildPerlPackage {
pname = "xin";
version = "v0.0.1";
src = ./.;
buildInputs = with pkgs; [ perlPackages.JSON ];
outputs = [ "out" "dev" ];
installPhase = ''
mkdir -p $out/bin
install xin.pl $out/bin/xin
'';
}

88
bins/xin/xin.pl Executable file
View File

@ -0,0 +1,88 @@
#!/usr/bin/env perl
use strict;
use warnings;
use v5.38;
use feature "switch";
use MIME::Base64;
use JSON qw{ decode_json encode_json };
my $VERSION = 'v0.0.1';
my @command_list;
my @ssh_cmd;
if (defined $ENV{SSH_ORIGINAL_COMMAND}) {
@ssh_cmd = split(" ", $ENV{SSH_ORIGINAL_COMMAND});
shift @ssh_cmd;
}
if (@ARGV > @ssh_cmd) {
@command_list = @ARGV;
} else {
@command_list = @ssh_cmd;
}
my $command = shift @command_list || "status";
sub is_root {
my $cmd = shift;
if ($<) {
say "$cmd: must be run as root";
exit 1;
}
}
if ( $command =~ /^status$/ ) {
my $info = decode_json(`nixos-version --json`);
$info->{needs_restart} =
system('check-restart >/dev/null') == 0 ? JSON::false : JSON::true;
my $sys_diff =
`nix store diff-closures /run/booted-system /run/current-system`;
$sys_diff =~ s/\e\[[0-9;]*m(?:\e\[K)?//g;
$info->{system_diff} = encode_base64($sys_diff);
$info->{uname_a} = `uname -a`;
$info->{uptime} = `uptime`;
$info->{ssh_command} = $ENV{SSH_ORIGINAL_COMMAND} || "NO COMMAND PASSED";
chomp $info->{uname_a};
chomp $info->{uptime};
print encode_json $info;
}
elsif ( $command =~ /^update$/ ) {
is_root("update");
exit system(qq{nixos-rebuild switch --flake github:qbit/xin --refresh});
}
elsif ( $command =~ /^ci$/ ) {
if ( -e "/etc/systemd/system/xin-ci.service"
&& -e "/etc/systemd/system/xin-ci-update.service" )
{
my $subcmd = shift @command_list || "status";
if ( $subcmd =~ /^start$/ ) {
exit system(qq{systemctl start xin-ci.service});
}
elsif ( $subcmd =~ /^update$/ ) {
exit system(qq{systemctl start xin-ci-update.service});
}
else {
say "unknown subcommand: $subcmd";
exit 1;
}
}
else {
say "not running on ci machine";
exit 1;
}
}
elsif ( $command =~ /^reboot$/ ) {
is_root("reboot");
exit system("reboot");
}
else {
say "unknown command: $command";
exit 1;
}