Skip to content

Commit

Permalink
Sort up usage of targetHost
Browse files Browse the repository at this point in the history
This fixes #77. It uses the target name for most error and status
messages (and appends the `targetHost` where relevant), for filtering
targets and for building the derivations.
  • Loading branch information
fooker committed Nov 7, 2019
1 parent 58676c3 commit fc6c574
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func MatchHosts(allHosts []nix.Host, pattern string) (hosts []nix.Host, err erro
g := glob.MustCompile(pattern)

for _, host := range allHosts {
if g.Match(host.TargetHost) {
if g.Match(host.Name) {
hosts = append(hosts, host)
}
}
Expand Down
2 changes: 1 addition & 1 deletion healthchecks/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func Perform(sshContext *ssh.SSHContext, host Host, timeout int) (err error) {
fmt.Fprintf(os.Stderr, "Running healthchecks on %s:\n", host.GetTargetHost())
fmt.Fprintf(os.Stderr, "Running healthchecks on %s (%s):\n", host.GetName(), host.GetTargetHost())

wg := sync.WaitGroup{}
for _, healthCheck := range host.GetHealthChecks().Cmd {
Expand Down
1 change: 1 addition & 0 deletions healthchecks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type Host interface {
GetName() string
GetTargetHost() string
GetHealthChecks() HealthChecks
}
Expand Down
20 changes: 10 additions & 10 deletions morph.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func execExecute(hosts []nix.Host) error {

for _, host := range hosts {
if host.BuildOnly {
fmt.Fprintf(os.Stderr, "Exec is disabled for build-only host: %s\n", host.TargetHost)
fmt.Fprintf(os.Stderr, "Exec is disabled for build-only host: %s\n", host.Name)
continue
}
fmt.Fprintln(os.Stderr, "** "+host.Name)
Expand Down Expand Up @@ -337,7 +337,7 @@ func execDeploy(hosts []nix.Host) (string, error) {

for _, host := range hosts {
if host.BuildOnly {
fmt.Fprintf(os.Stderr, "Deployment steps are disabled for build-only host: %s\n", host.TargetHost)
fmt.Fprintf(os.Stderr, "Deployment steps are disabled for build-only host: %s\n", host.Name)
continue
}

Expand Down Expand Up @@ -384,7 +384,7 @@ func execDeploy(hosts []nix.Host) (string, error) {
}
}

fmt.Fprintln(os.Stderr, "Done:", host.TargetHost)
fmt.Fprintln(os.Stderr, "Done:", host.Name)
}

return resultPath, nil
Expand All @@ -405,7 +405,7 @@ func execHealthCheck(hosts []nix.Host) error {
var err error
for _, host := range hosts {
if host.BuildOnly {
fmt.Fprintf(os.Stderr, "Healthchecks are disabled for build-only host: %s\n", host.TargetHost)
fmt.Fprintf(os.Stderr, "Healthchecks are disabled for build-only host: %s\n", host.Name)
continue
}
err = healthchecks.Perform(sshContext, &host, timeout)
Expand All @@ -421,7 +421,7 @@ func execHealthCheck(hosts []nix.Host) error {
func execUploadSecrets(sshContext *ssh.SSHContext, hosts []nix.Host) error {
for _, host := range hosts {
if host.BuildOnly {
fmt.Fprintf(os.Stderr, "Secret upload is disabled for build-only host: %s\n", host.TargetHost)
fmt.Fprintf(os.Stderr, "Secret upload is disabled for build-only host: %s\n", host.Name)
continue
}
singleHostInList := []nix.Host{host}
Expand Down Expand Up @@ -530,7 +530,7 @@ func getHosts(deploymentFile string) (hosts []nix.Host, err error) {

fmt.Fprintf(os.Stderr, "Selected %v/%v hosts (name filter:-%v, limits:-%v):\n", len(filteredHosts), len(allHosts), len(allHosts)-len(matchingHosts), len(matchingHosts)-len(filteredHosts))
for index, host := range filteredHosts {
fmt.Fprintf(os.Stderr, "\t%3d: %s (secrets: %d, health checks: %d)\n", index, host.TargetHost, len(host.Secrets), len(host.HealthChecks.Cmd)+len(host.HealthChecks.Http))
fmt.Fprintf(os.Stderr, "\t%3d: %s (secrets: %d, health checks: %d)\n", index, host.Name, len(host.Secrets), len(host.HealthChecks.Cmd)+len(host.HealthChecks.Http))
}
fmt.Fprintln(os.Stderr)

Expand Down Expand Up @@ -580,15 +580,15 @@ func buildHosts(hosts []nix.Host) (resultPath string, err error) {
func pushPaths(sshContext *ssh.SSHContext, filteredHosts []nix.Host, resultPath string) error {
for _, host := range filteredHosts {
if host.BuildOnly {
fmt.Fprintf(os.Stderr, "Push is disabled for build-only host: %s\n", host.TargetHost)
fmt.Fprintf(os.Stderr, "Push is disabled for build-only host: %s\n", host.Name)
continue
}

paths, err := nix.GetPathsToPush(host, resultPath)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "Pushing paths to %v:\n", host.TargetHost)
fmt.Fprintf(os.Stderr, "Pushing paths to %v (%v):\n", host.Name, host.TargetHost)
for _, path := range paths {
fmt.Fprintf(os.Stderr, "\t* %s\n", path)
}
Expand All @@ -606,7 +606,7 @@ func secretsUpload(ctx ssh.Context, filteredHosts []nix.Host) error {
// relative paths are resolved relative to the deployment file (!)
deploymentDir := filepath.Dir(deployment)
for _, host := range filteredHosts {
fmt.Fprintf(os.Stderr, "Uploading secrets to %s:\n", host.TargetHost)
fmt.Fprintf(os.Stderr, "Uploading secrets to %s (%s):\n", host.Name, host.TargetHost)
postUploadActions := make(map[string][]string, 0)
for secretName, secret := range host.Secrets {
secretSize, err := secrets.GetSecretSize(secret, deploymentDir)
Expand Down Expand Up @@ -648,7 +648,7 @@ func activateConfiguration(ctx ssh.Context, filteredHosts []nix.Host, resultPath
fmt.Fprintln(os.Stderr)
for _, host := range filteredHosts {

fmt.Fprintln(os.Stderr, "** "+host.TargetHost)
fmt.Fprintln(os.Stderr, "** "+host.Name)

configuration, err := nix.GetNixSystemPath(host, resultPath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion nix-packaging/deps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
sha256 = "06xrp05njwam4sn031fkmd4gym5wfsw5q0v24nqhs4883lsx9dwq";
};
}
]
]
6 changes: 5 additions & 1 deletion nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type NixContext struct {
KeepGCRoot bool
}

func (host *Host) GetName() string {
return host.Name
}

func (host *Host) GetTargetHost() string {
return host.TargetHost
}
Expand Down Expand Up @@ -138,7 +142,7 @@ func (ctx *NixContext) GetMachines(deploymentPath string) (hosts []Host, err err
func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArgs []string, nixBuildTargets string) (resultPath string, err error) {
hostsArg := "["
for _, host := range hosts {
hostsArg += "\"" + host.TargetHost + "\" "
hostsArg += "\"" + host.Name + "\" "
}
hostsArg += "]"

Expand Down
9 changes: 5 additions & 4 deletions ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Context interface {
}

type Host interface {
GetName() string
GetTargetHost() string
}

Expand Down Expand Up @@ -265,8 +266,8 @@ func (ctx *SSHContext) MakeTempFile(host Host) (path string, err error) {
err = cmd.Run()
if err != nil {
errorMessage := fmt.Sprintf(
"Error on remote host %s:\nCouldn't create temporary file using mktemp\n\nOriginal error:\n%s",
host.GetTargetHost(), stderr.String(),
"Error on remote host %s (%s):\nCouldn't create temporary file using mktemp\n\nOriginal error:\n%s",
host.GetName(), host.GetTargetHost(), stderr.String(),
)
return "", errors.New(errorMessage)
}
Expand All @@ -286,8 +287,8 @@ func (ctx *SSHContext) UploadFile(host Host, source string, destination string)
data, err := cmd.CombinedOutput()
if err != nil {
errorMessage := fmt.Sprintf(
"Error on remote host %s:\nCouldn't upload file: %s -> %s\n\nOriginal error:\n%s",
host.GetTargetHost(), source, destination, string(data),
"Error on remote host %s (%s):\nCouldn't upload file: %s -> %s\n\nOriginal error:\n%s",
host.GetName(), host.GetTargetHost(), source, destination, string(data),
)
return errors.New(errorMessage)
}
Expand Down

0 comments on commit fc6c574

Please sign in to comment.