Create a script for initiating actions

This commit is contained in:
Manuel Thalmann 2024-11-01 15:32:43 +01:00
parent b2c85d71c5
commit 029ad68e20
4 changed files with 147 additions and 117 deletions

View file

@ -1,55 +1,10 @@
#!/bin/env fish #!/bin/env fish
set -l dir (status dirname) set -l dir (status dirname)
set -l cmdline (cat /proc/$fish_pid/cmdline | string split0) source "$dir/../../lib/action.fish"
source "$dir/../../lib/settings.fish"
source "$dir/../../lib/hooks.fish"
if [ (id -u) -eq 0 ] function installAction -V dir
source "$dir/../../lib/config.fish" source "$dir/../../lib/hooks.fish"
source "$dir/../../lib/nix.fish"
set -l sudoConfig "/etc/sudoers.d/PortValhalla"
rm ~/.bash_profile
if ! git status -C (status dirname) &> /dev/null
git config --system --add safe.directory (realpath "$(status dirname)/../../..")
end
if [ -z "$TMUX" ]
if [ -z "$CONFIG_NAME" ]
selectProfile config
and set -x CONFIG_NAME "$config"
end
and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.'
installNixPkgs
and tmux new-session $cmdline
else
set -l name (getOSConfig setupUser.name)
begin
echo "Creating setup user"
and useradd \
--comment "PortValhalla Setup User" \
--system \
--no-user-group \
--groups nix-users \
--create-home \
--uid (getOSConfig setupUser.id --json) \
"$name"
end
and begin
echo "$name ALL=(ALL:ALL) NOPASSWD: ALL"
end >"$sudoConfig"
and sudo --preserve-env --set-home --user "$name" $cmdline
rm "$sudoConfig"
userdel -rf "$name"
uninstallNixPkgs
read -P "finished. press enter"
end
else
if not type -q getDeploymentScript if not type -q getDeploymentScript
function getDeploymentScript function getDeploymentScript
echo "No deployment script specified! No software will be installed." 1>&2 echo "No deployment script specified! No software will be installed." 1>&2
@ -100,3 +55,5 @@ else
and sleep 5 and sleep 5
and systemctl reboot -i and systemctl reboot -i
end end
runSetupUserAction installAction

View file

@ -1,19 +1,15 @@
#!/bin/env fish #!/bin/env fish
function runSetup function runSetup
set -l dir (status dirname) set -l dir (status dirname)
source "$dir/../../lib/settings.fish" source "$dir/../../lib/action.fish"
source "$dir/../../lib/hooks.fish"
source "$dir/../../lib/nix.fish"
if [ -z "$CONFIG_NAME" ]
selectProfile config
set -x CONFIG_NAME "$config"
end
set -l mountDir (getOSConfig partition.rootDir)
set -l projectRoot (realpath "$dir/../../..") set -l projectRoot (realpath "$dir/../../..")
set -l projectName (basename "$projectRoot") set -l projectName (basename "$projectRoot")
set -l PROJECT_CLONE_ROOT "/opt/$(basename "$projectName")" set -l PROJECT_CLONE_ROOT "/opt/$(basename "$projectName")"
function setupAction -V projectRoot -V PROJECT_CLONE_ROOT
source "$dir/../../lib/hooks.fish"
source "$dir/../../lib/settings.fish"
set -l mountDir (getOSConfig partition.rootDir)
set -l script (mktemp) set -l script (mktemp)
chmod +x "$script" chmod +x "$script"
@ -43,22 +39,6 @@ function runSetup
"$argv" "$argv"
end end
echo "Partitioning drives..."
and getOSConfig partition.script >"$script"
and "$script"
and rm "$script"
# Copy `nixpkgs` channel
and echo "Preparing nix..."
and begin
mkdir -p (dirname "$mountDir/$nixPkgsDir")
cp -r "$nixPkgsDir" "$mountDir/$nixPkgsDir"
end
and echo "Installing dependencies..."
and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.'
and echo "Cloning project..." and echo "Cloning project..."
and source "$dir/../../lib/copy-repo.fish" "$mountDir$PROJECT_CLONE_ROOT" and source "$dir/../../lib/copy-repo.fish" "$mountDir$PROJECT_CLONE_ROOT"
runChroot "$mountDir" git config --system --add safe.directory "$PROJECT_CLONE_ROOT" runChroot "$mountDir" git config --system --add safe.directory "$PROJECT_CLONE_ROOT"
@ -76,10 +56,30 @@ function runSetup
(string escape $script)) (string escape $script))
end | runChroot "$mountDir" tee /root/.bash_profile >/dev/null end | runChroot "$mountDir" tee /root/.bash_profile >/dev/null
end
and echo "Setup finished!" function prepareNix
source "$dir/../../lib/nix.fish"
# Copy `nixpkgs` channel
mkdir -p (dirname "$mountDir/$nixPkgsDir")
cp -r "$nixPkgsDir" "$mountDir/$nixPkgsDir"
end
function prepareAction
echo "Partitioning drives..."
and getOSConfig partition.script >"$script"
and "$script"
and rm "$script"
end
function postAction
echo "Setup finished!"
and echo "This machine will reboot in 5 seconds..." and echo "This machine will reboot in 5 seconds..."
and echo "Press CTRL-C to abort..." and echo "Press CTRL-C to abort..."
and sleep 5 and sleep 5
and systemctl reboot and systemctl reboot
end
runAction setupAction
end end

73
scripts/lib/action.fish Normal file
View file

@ -0,0 +1,73 @@
set -l dir (status dirname)
function runAction -V dir
source "$dir/hooks.fish"
source "$dir/nix.fish"
source "$dir/settings.fish"
echo "Preparing nix..."
and runHook prepareNix || installNixPkgs
if [ -z "$CONFIG_NAME" ]
selectProfile config
set -x CONFIG_NAME "$config"
end
and runHook prepareAction || true
and echo "Installing dependencies..."
and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.'
echo "Flagging repository as safe..."
if ! git status -C (status dirname) &> /dev/null
git config --system --add safe.directory (realpath "$(status dirname)/../..")
end
and runHook preAction || true
$argv
and runHook postAction || true
end
function runSetupUserAction -V dir
source "$dir/nix.fish"
if [ (id -u) -eq 0 ]
set -l cmdline (cat /proc/$fish_pid/cmdline | string split0)
if [ -z "$TMUX" ]
tmux new-session $cmdline
else
function setupUserAction -V dir -V sudoConfig -V cmdline
source "$dir/settings.fish"
set -l sudoConfig "/etc/sudoers.d/PortValhalla"
set -l name (getOSConfig setupUser.name)
begin
echo "Creating setup user"
and useradd \
--comment "PortValhalla Setup User" \
--system \
--no-user-group \
--groups nix-users \
--create-home \
--uid (getOSConfig setupUser.id --json) \
"$name"
end
and begin
echo "$name ALL=(ALL:ALL) NOPASSWD: ALL"
end >"$sudoConfig"
and sudo --preserve-env --set-home --user "$name" $cmdline
rm "$sudoConfig"
userdel -rf "$name"
end
runAction setupUserAction $argv
uninstallNixPkgs
read -P "setup user action finished. press enter"
end
else
$argv
end
end

View file

@ -1,4 +1,4 @@
set projectName port-valhalla set projectName port-valhalla
set nixPkgsVersion nixos-24.05 set nixPkgsVersion nixos-24.05
set cacheRoot ~/".cache/$projectName" set valhallaCache ~/".cache/$projectName"
set nixPkgsCache "$cacheRoot/nixpkgs/$nixPkgsVersion" set nixPkgsCache "$valhallaCache/nixpkgs/$nixPkgsVersion"