|
| 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 | +} |
0 commit comments