Skip to content

Commit

Permalink
misc: cleanup
Browse files Browse the repository at this point in the history
- ensure that finalizers are run on all program exists
- register removal of nix result tmpdir as finalizer

- cleanup code in morph.go
-- don't panic
-- re-introduce validateEnvironment
-- simplify some error handling
  • Loading branch information
Johan Thomsen authored and adamtulinius committed Nov 27, 2019
1 parent a60124b commit 9221e28
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
73 changes: 35 additions & 38 deletions morph.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (
executeCommand []string
keepGCRoot = app.Flag("keep-result", "Keep latest build in .gcroots to prevent it from being garbage collected").Default("False").Bool()

assetRoot, assetsErr = assets.Setup()
assetRoot string
)

func deploymentArg(cmd *kingpin.CmdClause) {
Expand Down Expand Up @@ -205,15 +205,34 @@ func listSecretsCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause {
return cmd
}

func init() {
if err := validateEnvironment(); err != nil {
panic(err)
func setup() {
handleError(validateEnvironment())

utils.AddFinalizer(func() {
assets.Teardown(assetRoot)
})
utils.SignalHandler()

var assetErr error
assetRoot, assetErr = assets.Setup()
handleError(assetErr)
}

func validateEnvironment() (err error) {
dependencies := []string{"nix", "scp", "ssh"}
missingDepencies := make([]string, 0)
for _, dependency := range dependencies {
_, err := exec.LookPath(dependency)
if err != nil {
missingDepencies = append(missingDepencies, dependency)
}
}

if assetsErr != nil {
fmt.Fprintln(os.Stderr, "Error unpacking assets:")
panic(assetsErr)
if len(missingDepencies) > 0 {
return errors.New("Missing dependencies: " + strings.Join(missingDepencies, ", "))
}

return nil
}

func main() {
Expand All @@ -226,15 +245,10 @@ func main() {
}

defer utils.RunFinalizers()
utils.AddFinalizer(func() {
assets.Teardown(assetRoot)
})
utils.SignalHandler()
setup()

hosts, err := getHosts(deployment)
if err != nil {
handleError(clause, hosts, err)
}
handleError(err)

switch clause {
case build.FullCommand():
Expand All @@ -257,15 +271,15 @@ func main() {
err = execExecute(hosts)
}

if err != nil {
handleError(clause, hosts, err)
}
handleError(err)
}

func handleError(cmd string, hosts []nix.Host, err error) {
func handleError(err error) {
//Stupid handling of catch-all errors for now
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
utils.Exit(1)
}
}

func execExecute(hosts []nix.Host) error {
Expand Down Expand Up @@ -378,7 +392,7 @@ func execDeploy(hosts []nix.Host) (string, error) {
if err != nil {
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Not deploying to additional hosts, since a host health check failed.")
os.Exit(1)
utils.Exit(1)
}
}

Expand Down Expand Up @@ -484,23 +498,6 @@ func execListSecretsAsJson(hosts []nix.Host) error {
return nil
}

func validateEnvironment() (err error) {
dependencies := []string{"nix", "scp", "ssh"}
missingDepencies := make([]string, 0)
for _, dependency := range dependencies {
_, err := exec.LookPath(dependency)
if err != nil {
missingDepencies = append(missingDepencies, dependency)
}
}

if len(missingDepencies) > 0 {
return errors.New("Missing dependencies: " + strings.Join(missingDepencies, ", "))
}

return nil
}

func getHosts(deploymentFile string) (hosts []nix.Host, err error) {

deployment, err := os.Open(deploymentFile)
Expand Down
7 changes: 3 additions & 4 deletions nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArg
if err != nil {
return "", err
}
defer os.Remove(tmpdir)
utils.AddFinalizer(func() {
os.RemoveAll(tmpdir)
})
resultLinkPath = filepath.Join(tmpdir, "result")
}
args := []string{ctx.EvalMachines,
Expand All @@ -195,9 +197,6 @@ func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArg
}

cmd := exec.Command("nix-build", args...)
if !ctx.KeepGCRoot {
defer os.Remove(resultLinkPath)
}

// show process output on attached stdout/stderr
cmd.Stdout = os.Stderr
Expand Down
8 changes: 6 additions & 2 deletions utils/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func SignalHandler() {
go func() {
sig := <-sigs
fmt.Fprintf(os.Stderr, "Received signal: %s\n", sig.String())
RunFinalizers()
os.Exit(130) // reserved exit code for "Interrupted"
Exit(130) // reserved exit code for "Interrupted"
}()
}

func Exit(exitCode int) {
RunFinalizers()
os.Exit(exitCode)
}

0 comments on commit 9221e28

Please sign in to comment.