From 29b975d40e7bb1460e24c707ccc80aacb8bb8966 Mon Sep 17 00:00:00 2001 From: Johan Thomsen Date: Mon, 18 Feb 2019 14:37:08 +0100 Subject: [PATCH] Added --show-trace flag to morph - Put eval-machines path and show trace option in a new struct called NixContext for the purpose of minimising the number of flags we need to pass to nix.GetMachines() and nix.BuildMachines() respectively. - Deprecated the --build-arg flag, because.. it was introduced pretty much only to be able to pass --show-trace, and it only works on "nix build" anyway (not eval) with a lot of existing limitations. fixes #44 --- morph.go | 39 ++++++++++++++++++++++++++++++++------- nix/nix.go | 29 +++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/morph.go b/morph.go index d64d120..d69b1f4 100644 --- a/morph.go +++ b/morph.go @@ -42,6 +42,7 @@ var ( deployUploadSecrets bool deployReboot bool skipHealthChecks bool + showTrace bool healthCheck = healthCheckCmd(app.Command("check-health", "Run health checks")) uploadSecrets = uploadSecretsCmd(app.Command("upload-secrets", "Upload secrets")) listSecrets = listSecretsCmd(app.Command("list-secrets", "List secrets")) @@ -88,7 +89,7 @@ func selectorFlags(cmd *kingpin.CmdClause) { } func nixBuildArgFlag(cmd *kingpin.CmdClause) { - cmd.Flag("build-arg", "Extra argument to pass on to nix-build command."). + cmd.Flag("build-arg", "Extra argument to pass on to nix-build command. **DEPRECATED**"). StringsVar(&nixBuildArg) } @@ -99,6 +100,13 @@ func skipHealthChecksFlag(cmd *kingpin.CmdClause) { BoolVar(&skipHealthChecks) } +func showTraceFlag(cmd *kingpin.CmdClause) { + cmd. + Flag("show-trace", "Whether to pass --show-trace to all nix commands"). + Default("False"). + BoolVar(&showTrace) +} + func asJsonFlag(cmd *kingpin.CmdClause) { cmd. Flag("json", "Whether to format the output as JSON instead of plaintext"). @@ -108,6 +116,7 @@ func asJsonFlag(cmd *kingpin.CmdClause) { func buildCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { selectorFlags(cmd) + showTraceFlag(cmd) nixBuildArgFlag(cmd) deploymentArg(cmd) return cmd @@ -115,12 +124,14 @@ func buildCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { func pushCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { selectorFlags(cmd) + showTraceFlag(cmd) deploymentArg(cmd) return cmd } func executeCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { selectorFlags(cmd) + showTraceFlag(cmd) askForSudoPasswdFlag(cmd) timeoutFlag(cmd) deploymentArg(cmd) @@ -134,6 +145,7 @@ func executeCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { func deployCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { selectorFlags(cmd) + showTraceFlag(cmd) nixBuildArgFlag(cmd) deploymentArg(cmd) timeoutFlag(cmd) @@ -157,6 +169,7 @@ func deployCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { func healthCheckCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { selectorFlags(cmd) + showTraceFlag(cmd) deploymentArg(cmd) timeoutFlag(cmd) return cmd @@ -164,6 +177,7 @@ func healthCheckCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { func uploadSecretsCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { selectorFlags(cmd) + showTraceFlag(cmd) askForSudoPasswdFlag(cmd) skipHealthChecksFlag(cmd) deploymentArg(cmd) @@ -172,6 +186,7 @@ func uploadSecretsCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { func listSecretsCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause { selectorFlags(cmd) + showTraceFlag(cmd) deploymentArg(cmd) asJsonFlag(cmd) return cmd @@ -196,6 +211,11 @@ func main() { clause := kingpin.MustParse(app.Parse(os.Args[1:])) + //TODO: Remove deprecation warning when removing --build-arg flag + if len(nixBuildArg) > 0 { + fmt.Fprintln(os.Stderr, "Deprecation: The --build-arg flag will be removed in a future release.") + } + hosts, err := getHosts(deployment) if err != nil { handleError(clause, hosts, err) @@ -489,14 +509,13 @@ func getHosts(deploymentFile string) (hosts []nix.Host, err error) { return hosts, err } - evalMachinesPath := filepath.Join(assetRoot, "eval-machines.nix") - deploymentPath, err := filepath.Abs(deployment.Name()) if err != nil { return hosts, err } - allHosts, err := nix.GetMachines(evalMachinesPath, deploymentPath) + ctx := getNixContext() + allHosts, err := ctx.GetMachines(deploymentPath) if err != nil { return hosts, err } @@ -517,9 +536,14 @@ func getHosts(deploymentFile string) (hosts []nix.Host, err error) { return filteredHosts, nil } -func buildHosts(hosts []nix.Host) (resultPath string, err error) { - evalMachinesPath := filepath.Join(assetRoot, "eval-machines.nix") +func getNixContext() *nix.NixContext { + return &nix.NixContext{ + EvalMachines: filepath.Join(assetRoot, "eval-machines.nix"), + ShowTrace: showTrace, + } +} +func buildHosts(hosts []nix.Host) (resultPath string, err error) { if len(hosts) == 0 { err = errors.New("No hosts selected") return @@ -530,7 +554,8 @@ func buildHosts(hosts []nix.Host) (resultPath string, err error) { return } - resultPath, err = nix.BuildMachines(evalMachinesPath, deploymentPath, hosts, nixBuildArg) + ctx := getNixContext() + resultPath, err = ctx.BuildMachines(deploymentPath, hosts, nixBuildArg) if err != nil { return } diff --git a/nix/nix.go b/nix/nix.go index ae0faf8..1b61e0f 100644 --- a/nix/nix.go +++ b/nix/nix.go @@ -23,6 +23,11 @@ type Host struct { BuildOnly bool } +type NixContext struct { + EvalMachines string + ShowTrace bool +} + func (host *Host) GetTargetHost() string { return host.TargetHost } @@ -31,13 +36,18 @@ func (host *Host) GetHealthChecks() healthchecks.HealthChecks { return host.HealthChecks } -func GetMachines(evalMachines string, deploymentPath string) (hosts []Host, err error) { - cmd := exec.Command( - "nix", "eval", - "-f", evalMachines, "info.machineList", +func (ctx *NixContext) GetMachines(deploymentPath string) (hosts []Host, err error) { + + args := []string{"eval", + "-f", ctx.EvalMachines, "info.machineList", "--arg", "networkExpr", deploymentPath, - "--json", - ) + "--json"} + + if ctx.ShowTrace { + args = append(args, "--show-trace") + } + + cmd := exec.Command("nix", args...) var stdout bytes.Buffer cmd.Stdout = &stdout @@ -59,7 +69,7 @@ func GetMachines(evalMachines string, deploymentPath string) (hosts []Host, err return hosts, nil } -func BuildMachines(evalMachines string, deploymentPath string, hosts []Host, nixArgs []string) (path string, err error) { +func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArgs []string) (path string, err error) { hostsArg := "[" for _, host := range hosts { hostsArg += "\"" + host.TargetHost + "\" " @@ -75,7 +85,7 @@ func BuildMachines(evalMachines string, deploymentPath string, hosts []Host, nix resultLinkPath := filepath.Join(tmpdir, "result") - args := []string{evalMachines, + args := []string{ctx.EvalMachines, "-A", "machines", "--arg", "networkExpr", deploymentPath, "--arg", "names", hostsArg, @@ -84,6 +94,9 @@ func BuildMachines(evalMachines string, deploymentPath string, hosts []Host, nix if len(nixArgs) > 0 { args = append(args, nixArgs...) } + if ctx.ShowTrace { + args = append(args, "--show-trace") + } cmd := exec.Command("nix-build", args...) defer os.Remove(resultLinkPath)