-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Include phase reporting in log file for ssh-ng builds #9280
Merged
thufschmitt
merged 1 commit into
NixOS:master
from
r-vdp:rvdp/fix_remote_logging_phase_reporting
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
{ config, lib, hostPkgs, ... }: | ||
|
||
let | ||
pkgs = config.nodes.client.nixpkgs.pkgs; | ||
|
||
# Trivial Nix expression to build remotely. | ||
expr = config: nr: pkgs.writeText "expr.nix" | ||
'' | ||
let utils = builtins.storePath ${config.system.build.extraUtils}; in | ||
derivation { | ||
name = "hello-${toString nr}"; | ||
system = "i686-linux"; | ||
PATH = "''${utils}/bin"; | ||
builder = "''${utils}/bin/sh"; | ||
args = [ "-c" "${ | ||
lib.concatStringsSep "; " [ | ||
''if [[ -n $NIX_LOG_FD ]]'' | ||
''then echo '@nix {\"action\":\"setPhase\",\"phase\":\"buildPhase\"}' >&''$NIX_LOG_FD'' | ||
"fi" | ||
"echo Hello" | ||
"mkdir $out" | ||
"cat /proc/sys/kernel/hostname > $out/host" | ||
] | ||
}" ]; | ||
outputs = [ "out" ]; | ||
} | ||
''; | ||
in | ||
|
||
{ | ||
name = "remote-builds-ssh-ng"; | ||
|
||
nodes = | ||
{ builder = | ||
{ config, pkgs, ... }: | ||
{ services.openssh.enable = true; | ||
virtualisation.writableStore = true; | ||
nix.settings.sandbox = true; | ||
nix.settings.substituters = lib.mkForce [ ]; | ||
}; | ||
|
||
client = | ||
{ config, lib, pkgs, ... }: | ||
{ nix.settings.max-jobs = 0; # force remote building | ||
nix.distributedBuilds = true; | ||
nix.buildMachines = | ||
[ { hostName = "builder"; | ||
sshUser = "root"; | ||
sshKey = "/root/.ssh/id_ed25519"; | ||
system = "i686-linux"; | ||
maxJobs = 1; | ||
protocol = "ssh-ng"; | ||
} | ||
]; | ||
virtualisation.writableStore = true; | ||
virtualisation.additionalPaths = [ config.system.build.extraUtils ]; | ||
nix.settings.substituters = lib.mkForce [ ]; | ||
programs.ssh.extraConfig = "ConnectTimeout 30"; | ||
}; | ||
}; | ||
|
||
testScript = { nodes }: '' | ||
# fmt: off | ||
import subprocess | ||
|
||
start_all() | ||
|
||
# Create an SSH key on the client. | ||
subprocess.run([ | ||
"${hostPkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", "" | ||
], capture_output=True, check=True) | ||
client.succeed("mkdir -p -m 700 /root/.ssh") | ||
client.copy_from_host("key", "/root/.ssh/id_ed25519") | ||
client.succeed("chmod 600 /root/.ssh/id_ed25519") | ||
|
||
# Install the SSH key on the builder. | ||
client.wait_for_unit("network.target") | ||
builder.succeed("mkdir -p -m 700 /root/.ssh") | ||
builder.copy_from_host("key.pub", "/root/.ssh/authorized_keys") | ||
builder.wait_for_unit("sshd") | ||
client.succeed(f"ssh -o StrictHostKeyChecking=no {builder.name} 'echo hello world'") | ||
|
||
# Perform a build | ||
out = client.succeed("nix-build ${expr nodes.client.config 1} 2> build-output") | ||
|
||
# Verify that the build was done on the builder | ||
builder.succeed(f"test -e {out.strip()}") | ||
|
||
# Print the build log, prefix the log lines to avoid nix intercepting lines starting with @nix | ||
buildOutput = client.succeed("sed -e 's/^/build-output:/' build-output") | ||
print(buildOutput) | ||
|
||
# Make sure that we get the expected build output | ||
client.succeed("grep -qF Hello build-output") | ||
|
||
# We don't want phase reporting in the build output | ||
client.fail("grep -qF '@nix' build-output") | ||
|
||
# Get the log file | ||
client.succeed(f"nix-store --read-log {out.strip()} > log-output") | ||
# Prefix the log lines to avoid nix intercepting lines starting with @nix | ||
logOutput = client.succeed("sed -e 's/^/log-file:/' log-output") | ||
print(logOutput) | ||
|
||
# Check that we get phase reporting in the log file | ||
client.succeed("grep -q '@nix {\"action\":\"setPhase\",\"phase\":\"buildPhase\"}' log-output") | ||
''; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't
work as well? Since it seems like we're just printing the JSON message that we just parsed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah no, it's not the same format. We want to print the JSON message as it was emitted by the build script, which looks like
@nix { "action": "setPhase", "phase": "unpackPhase" }
, but what we receive inDerivationGoal::handleChildOutput
looks like@nix {"action":"result","fields":["unpackPhase"],"id":810782451302409,"type":104}
.That's why we parse the internal JSON message and then write it out in the same format as the build script.