2010-10-07 16:30:52 +02:00
|
|
|
{ nixos ? /etc/nixos/nixos
|
|
|
|
, nixpkgs ? /etc/nixos/nixpkgs
|
|
|
|
, networkExpr
|
|
|
|
, infrastructureExpr
|
2010-10-18 21:47:46 +02:00
|
|
|
, targetProperty ? "hostname"
|
2010-10-07 16:30:52 +02:00
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
pkgs = import nixpkgs {};
|
|
|
|
|
2010-10-18 21:47:46 +02:00
|
|
|
inherit (builtins) attrNames getAttr listToAttrs;
|
|
|
|
inherit (pkgs.lib) concatMapStrings;
|
2010-10-07 16:30:52 +02:00
|
|
|
|
|
|
|
network = import networkExpr;
|
|
|
|
infrastructure = import infrastructureExpr;
|
2010-10-18 21:47:46 +02:00
|
|
|
|
|
|
|
generateRollbackSucceededPhase = network: infrastructure: configs:
|
2010-10-07 16:30:52 +02:00
|
|
|
concatMapStrings (configurationName:
|
|
|
|
let
|
|
|
|
infrastructureElement = getAttr configurationName infrastructure;
|
|
|
|
config = getAttr configurationName configs;
|
|
|
|
in
|
|
|
|
''
|
2010-10-18 21:47:46 +02:00
|
|
|
if [ "$rollback" != "$succeeded" ]
|
|
|
|
then
|
|
|
|
ssh $NIX_SSHOPTS ${getAttr targetProperty infrastructureElement} nix-env -p /nix/var/nix/profiles/system --rollback
|
2010-12-02 18:22:24 +01:00
|
|
|
ssh $NIX_SSHOPTS ${getAttr targetProperty infrastructureElement} /nix/var/nix/profiles/system/bin/switch-to-configuration switch
|
2010-10-18 21:47:46 +02:00
|
|
|
|
|
|
|
rollback=$((rollback + 1))
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
) (attrNames network)
|
|
|
|
;
|
|
|
|
|
|
|
|
generateDistributionPhase = network: infrastructure: configs:
|
|
|
|
concatMapStrings (configurationName:
|
|
|
|
let
|
|
|
|
infrastructureElement = getAttr configurationName infrastructure;
|
|
|
|
config = getAttr configurationName configs;
|
|
|
|
in
|
|
|
|
''
|
|
|
|
echo "=== copy system closure to ${getAttr targetProperty infrastructureElement} ==="
|
|
|
|
nix-copy-closure --to ${getAttr targetProperty infrastructureElement} ${config.system.build.toplevel}
|
2010-10-07 16:30:52 +02:00
|
|
|
''
|
|
|
|
) (attrNames network)
|
|
|
|
;
|
2010-10-18 21:47:46 +02:00
|
|
|
|
|
|
|
generateActivationPhase = network: infrastructure: configs:
|
|
|
|
concatMapStrings (configurationName:
|
|
|
|
let
|
|
|
|
infrastructureElement = getAttr configurationName infrastructure;
|
|
|
|
config = getAttr configurationName configs;
|
|
|
|
in
|
|
|
|
''
|
|
|
|
echo "=== activating system configuration on ${getAttr targetProperty infrastructureElement} ==="
|
|
|
|
ssh $NIX_SSHOPTS ${getAttr targetProperty infrastructureElement} nix-env -p /nix/var/nix/profiles/system --set ${config.system.build.toplevel} ||
|
|
|
|
(ssh $NIX_SSHOPTS ${getAttr targetProperty infrastructureElement} nix-env -p /nix/var/nix/profiles/system --rollback; rollbackSucceeded)
|
|
|
|
|
2010-12-02 18:22:24 +01:00
|
|
|
ssh $NIX_SSHOPTS ${getAttr targetProperty infrastructureElement} /nix/var/nix/profiles/system/bin/switch-to-configuration switch ||
|
2010-10-18 21:47:46 +02:00
|
|
|
( ssh $NIX_SSHOPTS ${getAttr targetProperty infrastructureElement} nix-env -p /nix/var/nix/profiles/system --rollback
|
2010-12-02 18:22:24 +01:00
|
|
|
ssh $NIX_SSHOPTS ${getAttr targetProperty infrastructureElement} /nix/var/nix/profiles/system/bin/switch-to-configuration switch
|
2010-10-18 21:47:46 +02:00
|
|
|
rollbackSucceeded
|
|
|
|
)
|
|
|
|
|
|
|
|
succeeded=$((succeeded + 1))
|
|
|
|
''
|
|
|
|
) (attrNames network)
|
|
|
|
;
|
|
|
|
|
2010-10-07 16:30:52 +02:00
|
|
|
evaluateMachines = network: infrastructure:
|
|
|
|
listToAttrs (map (configurationName:
|
|
|
|
let
|
|
|
|
configuration = getAttr configurationName network;
|
|
|
|
system = (getAttr configurationName infrastructure).system;
|
|
|
|
in
|
|
|
|
{ name = configurationName;
|
|
|
|
value = (import "${nixos}/lib/eval-config.nix" {
|
|
|
|
inherit nixpkgs system;
|
|
|
|
modules = [ configuration ];
|
|
|
|
extraArgs = evaluateMachines network infrastructure;
|
|
|
|
}).config; }
|
|
|
|
) (attrNames (network)))
|
|
|
|
;
|
|
|
|
|
|
|
|
configs = evaluateMachines network infrastructure;
|
|
|
|
in
|
|
|
|
pkgs.stdenv.mkDerivation {
|
|
|
|
name = "deploy-script";
|
2010-12-02 19:09:53 +01:00
|
|
|
|
|
|
|
# This script has a zillion dependencies and is trivial to build, so
|
|
|
|
# we don't want to build it remotely.
|
|
|
|
preferLocalBuild = true;
|
|
|
|
|
2010-10-18 21:47:46 +02:00
|
|
|
buildCommand =
|
|
|
|
''
|
2010-10-07 16:30:52 +02:00
|
|
|
ensureDir $out/bin
|
|
|
|
cat > $out/bin/deploy-systems << "EOF"
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
2010-10-18 21:47:46 +02:00
|
|
|
|
|
|
|
rollbackSucceeded()
|
|
|
|
{
|
|
|
|
rollback=0
|
|
|
|
${generateRollbackSucceededPhase network infrastructure configs}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Distribution phase
|
|
|
|
|
|
|
|
${generateDistributionPhase network infrastructure configs}
|
|
|
|
|
|
|
|
# Activation phase
|
|
|
|
|
|
|
|
succeeded=0
|
|
|
|
|
|
|
|
${generateActivationPhase network infrastructure configs}
|
2010-10-07 16:30:52 +02:00
|
|
|
EOF
|
|
|
|
chmod +x $out/bin/deploy-systems
|
|
|
|
'';
|
|
|
|
}
|