Skip to content

Commit

Permalink
Added --show-trace flag to morph
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Johan Thomsen authored and adamtulinius committed Feb 18, 2019
1 parent feff7d4 commit 29b975d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
39 changes: 32 additions & 7 deletions morph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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)
}

Expand All @@ -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").
Expand All @@ -108,19 +116,22 @@ func asJsonFlag(cmd *kingpin.CmdClause) {

func buildCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause {
selectorFlags(cmd)
showTraceFlag(cmd)
nixBuildArgFlag(cmd)
deploymentArg(cmd)
return cmd
}

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)
Expand All @@ -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)
Expand All @@ -157,13 +169,15 @@ func deployCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause {

func healthCheckCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause {
selectorFlags(cmd)
showTraceFlag(cmd)
deploymentArg(cmd)
timeoutFlag(cmd)
return cmd
}

func uploadSecretsCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause {
selectorFlags(cmd)
showTraceFlag(cmd)
askForSudoPasswdFlag(cmd)
skipHealthChecksFlag(cmd)
deploymentArg(cmd)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
29 changes: 21 additions & 8 deletions nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type Host struct {
BuildOnly bool
}

type NixContext struct {
EvalMachines string
ShowTrace bool
}

func (host *Host) GetTargetHost() string {
return host.TargetHost
}
Expand All @@ -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
Expand All @@ -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 + "\" "
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 29b975d

Please sign in to comment.