Skip to content

Commit 0fa25d0

Browse files
committed
asdf
1 parent eef1b08 commit 0fa25d0

File tree

5 files changed

+199
-1
lines changed

5 files changed

+199
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: 'Compare Disko NixOS configs'
2+
3+
on:
4+
# Allow this workflow to be reused by other workflows:
5+
workflow_call:
6+
inputs:
7+
runner:
8+
description: 'JSON-encoded list of runner labels'
9+
default: '["self-hosted"]'
10+
required: false
11+
type: string
12+
secrets:
13+
NIX_GITHUB_TOKEN:
14+
description: GitHub token to add as access-token in nix.conf
15+
required: false
16+
CACHIX_AUTH_TOKEN:
17+
description: 'Cachix auth token'
18+
required: true
19+
workflow_dispatch:
20+
21+
jobs:
22+
post-initial-comment:
23+
runs-on: ${{ fromJSON(inputs.runner) }}
24+
steps:
25+
- name: 'Post initial disko status comment'
26+
uses: marocchino/[email protected]
27+
with:
28+
recreate: true
29+
message: |
30+
Thanks for your Pull Request!
31+
32+
This comment will be updated automatically with the status of disko on each machine.
33+
compare-disko-create:
34+
runs-on: ${{ fromJSON(inputs.runner) }}
35+
name: 'Compare disko _create on machines'
36+
37+
steps:
38+
- uses: actions/checkout@v4
39+
with:
40+
fetch-depth: 0
41+
42+
- name: Install Nix
43+
uses: metacraft-labs/nixos-modules/.github/install-nix@main
44+
with:
45+
nix-github-token: ${{ secrets.BLOCKSENSE_GITHUB_ACCESS_TOKEN }}
46+
cachix-auth-token: ${{ secrets.CACHIX_AUTH_TOKEN }}
47+
cachix-cache: ${{ vars.CACHIX_CACHE }}
48+
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
49+
substituters: ${{ vars.SUBSTITUTERS }}
50+
51+
- name: Compare disko configurations
52+
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl compare_disko create
53+
54+
compare-disko-config:
55+
runs-on: ${{ fromJSON(inputs.runner) }}
56+
name: 'Compare disko _config on machines'
57+
58+
steps:
59+
- uses: actions/checkout@v4
60+
with:
61+
fetch-depth: 0
62+
63+
- name: Install Nix
64+
uses: metacraft-labs/nixos-modules/.github/install-nix@main
65+
with:
66+
nix-github-token: ${{ secrets.BLOCKSENSE_GITHUB_ACCESS_TOKEN }}
67+
cachix-auth-token: ${{ secrets.CACHIX_AUTH_TOKEN }}
68+
cachix-cache: ${{ vars.CACHIX_CACHE }}
69+
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
70+
substituters: ${{ vars.SUBSTITUTERS }}
71+
72+
- name: Compare disko configurations
73+
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl compare_disko config
74+
75+
results:
76+
runs-on: ${{ fromJSON(inputs.runner) }}
77+
needs: [post-initial-comment, compare-disko-create, compare-disko-config]
78+
name: 'Final results'

packages/mcl/src/main.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ alias supportedCommands = imported!`std.traits`.AliasSeq!(
1818
cmds.ci,
1919
cmds.machine,
2020
cmds.config,
21+
cmds.compare_disko,
2122
);
2223

2324
int main(string[] args)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
module mcl.commands.compare_disko;
2+
3+
import mcl.utils.env : optional, parseEnv;
4+
import mcl.utils.nix : nix;
5+
import mcl.utils.path : getTopLevel;
6+
import mcl.utils.process : execute;
7+
8+
import std.typecons : Tuple, tuple;
9+
import std.file : mkdirRecurse, exists, rmdirRecurse;
10+
import std.format : fmt = format;
11+
import std.stdio;
12+
import std.json : JSONValue, parseJSON, JSONOptions;
13+
import std.file : write;
14+
import std.logger : tracef, errorf, infof;
15+
import std.process : environment;
16+
import mcl.utils.log : errorAndExit;
17+
18+
struct Params
19+
{
20+
@optional() string baseBranch;
21+
22+
void setup(){
23+
if (baseBranch == null){
24+
baseBranch = "main";
25+
}
26+
}
27+
}
28+
29+
export void compare_disko(string[] args){
30+
const params = parseEnv!Params;
31+
JSONValue configurations = nix.flake!JSONValue("", ["show"]);
32+
string[] machines;
33+
foreach (string k, JSONValue v; configurations["nixosConfigurations"]){
34+
if (k[$-3 .. $] != "-vm" &&
35+
k != "gitlab-runner-container" &&
36+
k != "minimal-container" )
37+
{
38+
machines ~= k;
39+
}
40+
}
41+
42+
string gitRoot = getTopLevel();
43+
string worktreeBaseBranch = gitRoot~"-"~params.baseBranch;
44+
45+
if (execute("git rev-parse --abbrev-ref HEAD") == params.baseBranch){
46+
errorAndExit("Trying to compare branch "~params.baseBranch~" with itself. Quitting.");
47+
}
48+
49+
execute(["git", "worktree" , "add", worktreeBaseBranch, params.baseBranch]);
50+
string freshTestsDir = gitRoot~"/disko-tests";
51+
string staleTestsDir = worktreeBaseBranch~"/disko-tests";
52+
mkdirRecurse(staleTestsDir);
53+
mkdirRecurse(freshTestsDir);
54+
55+
Tuple!(string , string )[] machineDiffs;
56+
string[] changedMachines;
57+
58+
foreach (string m; machines){
59+
foreach (string setting; ["_config", "_create"]){
60+
string new_setting =
61+
nix.eval(gitRoot~"#nixosConfigurations."~m~".config.disko.devices."~setting, [
62+
"--option", "warn-dirty", "false",
63+
"--accept-flake-config"]);
64+
tracef("CREATING %s_%s_new.sh FILE", m, setting);
65+
write(freshTestsDir~"/"~m~"_"~setting~"_new.sh", new_setting);
66+
string old_setting =
67+
nix.eval(worktreeBaseBranch~"#nixosConfigurations."~m~".config.disko.devices."~setting, [
68+
"--option", "warn-dirty", "false",
69+
"--accept-flake-config"]);
70+
tracef("CREATING %s_%s_old.sh FILE", m, setting);
71+
write(staleTestsDir~"/"~m~"_"~setting~"_old.sh", old_setting);
72+
73+
74+
string diff = execute([
75+
"git", "--no-pager", "diff", "--no-index",
76+
staleTestsDir~"/"~m~"_"~setting~"_old.sh",
77+
freshTestsDir~"/"~m~"_"~setting~"_new.sh"]);
78+
79+
if (diff == ""){
80+
infof("✔ NO DIFFERENCE IN %s", m);
81+
}
82+
else{
83+
infof("✖ DIFFERENCE IN %s", m);
84+
machineDiffs~=tuple(m~"."~setting, diff);
85+
}
86+
}
87+
}
88+
infof("------------------------------------------------------");
89+
if(machineDiffs.length == 0){
90+
infof("✔✔✔ NO CONFIGS WITH DIFFS");
91+
}
92+
else{
93+
infof("✖✖✖ LIST OF CONFIGS WITH DIFFS");
94+
foreach(md; machineDiffs){
95+
infof(md[0]);
96+
}
97+
}
98+
commentDiffs(machineDiffs, args[0]);
99+
100+
// Cleanup
101+
if(deleteAfter){
102+
execute(["git", "worktree" , "remove", worktreeBaseBranch, "--force"]);
103+
rmdirRecurse(freshTestsDir);
104+
}
105+
}
106+
107+
void partialCommentDiff(Tuple!(string, string)[] machineDiffs, string setting){
108+
string data;
109+
foreach(md; machineDiffs){
110+
data~="<details>";
111+
data~="\n<summary>"~md[0]~"</summary>";
112+
data~="\n\n```diff";
113+
data~="\n"~md[1];
114+
data~="\n```";
115+
data~="\n\n</details>";
116+
}
117+
write(setting~"_diffs", data);
118+
}

packages/mcl/src/src/mcl/commands/package.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ public import mcl.commands.ci : ci;
88
public import mcl.commands.host_info : host_info;
99
public import mcl.commands.machine : machine;
1010
public import mcl.commands.config : config;
11+
public import mcl.commands.compare_disko: compare_disko;

packages/mcl/src/src/mcl/utils/nix.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct NixCommand
7777

7878

7979
static if (is(T == JSONValue))
80-
args = ["--json"] ~ args;
80+
args = args ~ ["--json"];
8181

8282
auto command = [
8383
"nix", "--experimental-features", "nix-command flakes",

0 commit comments

Comments
 (0)