krops: udpate shell to make it more powerful

This commit is contained in:
Ingolf Wagner 2020-03-06 14:10:55 +08:00
parent 95da0e6bb2
commit 79524242dc
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B

102
shell.nix
View file

@ -124,33 +124,48 @@ let
hostPattern = name: hostPattern = name:
if name == "porani" then "${name}.insecure" else "${name}.private"; if name == "porani" then "${name}.insecure" else "${name}.private";
deployment = populateCommands: name: deployment = { secrets, content }:
name:
{ host ? (hostPattern name), target ? "/var/src/", user ? "root" { host ? (hostPattern name), target ? "/var/src/", user ? "root"
, commandPrefix ? "deploy", enableSwitch ? true }: , commandPrefix ? "deploy", enableSwitch ? true, enableSecrets ? true }:
with ops; with ops;
jobs "${commandPrefix}-${name}" "${user}@${host}${target}" let
(populateCommands ++ (if enableSwitch then [ switch ] else [ ])); commandName = if enableSecrets then
"${commandPrefix}-${name}-with-secrets"
else
"${commandPrefix}-${name}-without-secrets";
populateCommands = with lib; flatten [
content
(optionals enableSecrets secrets)
(optionals enableSwitch [ switch ])
];
in jobs commandName "${user}@${host}${target}" populateCommands;
serverDeployment = name: serverDeployment = name:
with ops; with ops;
deployment [ deployment {
(populate source.nixPkgs) content = [
(populate source.modules) (populate source.nixPkgs)
(populate (source.secrets name)) (populate source.modules)
(populate (source.system name)) (populate (source.system name))
] name; ];
secrets = [ (populate (source.secrets name)) ];
} name;
desktopDeployment = name: desktopDeployment = name:
with ops; with ops;
deployment [ deployment {
(populate source.nixPkgs) content = [
(populate source.modules) (populate source.nixPkgs)
(populate (source.secrets name)) (populate source.modules)
(populate (source.system name)) (populate (source.system name))
(populate source.desktopSecrets) ];
] name; secrets =
[ (populate (source.secrets name)) (populate source.desktopSecrets) ];
} name;
cleanupNix = name: cleanupNix = name:
{ ... }:
let let
target = { target = {
host = hostPattern name; host = hostPattern name;
@ -165,40 +180,45 @@ let
nix-collect-garbage -d nix-collect-garbage -d
''; '';
# generate tasks # helper function to make stuff more readable
runForAll = serverList: command: arguments:
let f = args: (map (name: command name (arguments // args)) serverList);
in (f { enableSecrets = false; }) ++ (f { enableSecrets = true; });
servers = with lib; # generate tasks
servers = serverList:
with lib;
let let
serverList = [ "workhorse" "sputnik" "porani" ]; doForAll = runForAll serverList;
deployments = flip map serverList (name: serverDeployment name { }); deployments = doForAll serverDeployment { };
cleanup = flip map serverList (name: cleanupNix name); cleanup = doForAll cleanupNix { };
install = flip map serverList (name: install = doForAll serverDeployment {
serverDeployment name { commandPrefix = "install";
commandPrefix = "install"; host = installHost;
host = installHost; target = "/mnt/var/src";
target = "/mnt/var/src"; enableSwitch = false;
enableSwitch = false; };
});
in deployments ++ cleanup ++ install; in deployments ++ cleanup ++ install;
desktops = with lib; desktops = desktopList:
with lib;
let let
desktopList = [ "pepe" "workout" "sterni" "mobi" ]; doForAll = runForAll desktopList;
deployments = flip map desktopList (name: desktopDeployment name { }); deployments = doForAll desktopDeployment { };
cleanup = flip map desktopList (name: cleanupNix name); cleanup = doForAll cleanupNix { };
install = flip map desktopList (name: install = doForAll desktopDeployment {
desktopDeployment name { commandPrefix = "install";
commandPrefix = "install"; host = installHost;
host = installHost; target = "/mnt/var/src";
target = "/mnt/var/src"; enableSwitch = false;
enableSwitch = false; };
});
in deployments ++ cleanup ++ install; in deployments ++ cleanup ++ install;
in pkgs.mkShell { in pkgs.mkShell {
buildInputs = with pkgs; buildInputs = with pkgs;
servers ++ desktops ++ [ (servers [ "workhorse" "sputnik" "porani" ])
++ (desktops [ "pepe" "workout" "sterni" "mobi" ]) ++ [
(pkgs.writers.writeBashBin "reformat" '' (pkgs.writers.writeBashBin "reformat" ''
find ${ find ${
toString ./. toString ./.