b23150057a
This changeset moves the installation of Nomad binaries out of the provisioning framework and into scripts that are installed on the remote host during AMI builds. This provides a few advantages: * The provisioning framework can be reduced in scope (with the goal of moving most of it into the Terraform stack entirely). * The scripts can be arbitrarily complex if we don't have to stuff them into ssh commands, so it's easier to make them idempotent. In this changeset, the scripts check the version of the existing binary and don't re-download when using the `--nomad_sha` or `--nomad_version` flags. * The scripts can be OS/distro specific, which helps in building new test targets.
131 lines
3.2 KiB
Go
131 lines
3.2 KiB
Go
package provisioning
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func deploy(t *testing.T, target *ProvisioningTarget) error {
|
|
platform := target.Deployment.Platform
|
|
if target.Deployment.Platform == "linux_amd64" {
|
|
return deployLinux(t, target)
|
|
} else if target.Deployment.Platform == "windows_amd64" {
|
|
return deployWindows(t, target)
|
|
}
|
|
return fmt.Errorf("invalid deployment platform: %v", platform)
|
|
}
|
|
|
|
func deployLinux(t *testing.T, target *ProvisioningTarget) error {
|
|
var err error
|
|
runner := target.runner
|
|
deployment := target.Deployment
|
|
|
|
err = runner.Open(t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer runner.Close()
|
|
|
|
if deployment.NomadLocalBinary != "" {
|
|
if deployment.RemoteBinaryPath == "" {
|
|
return fmt.Errorf("remote binary path not set")
|
|
}
|
|
err = runner.Copy(
|
|
deployment.NomadLocalBinary,
|
|
deployment.RemoteBinaryPath)
|
|
if err != nil {
|
|
return fmt.Errorf("copying Nomad failed: %v", err)
|
|
}
|
|
} else if deployment.NomadSha != "" {
|
|
script := fmt.Sprintf(
|
|
`/opt/install-nomad --nomad_sha %s --nostart`, deployment.NomadSha)
|
|
err = runner.Run(script)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if deployment.NomadVersion != "" {
|
|
script := fmt.Sprintf(
|
|
`/opt/install-nomad --nomad_version %s --nostart`, deployment.NomadVersion)
|
|
err = runner.Run(script)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
t.Log("no Nomad deployment specified, falling back to 'step' field.")
|
|
}
|
|
|
|
for _, bundle := range deployment.Bundles {
|
|
err = runner.Copy(
|
|
bundle.Source, bundle.Destination)
|
|
if err != nil {
|
|
return fmt.Errorf("copying bundle '%s' failed: %v", bundle.Source, err)
|
|
}
|
|
}
|
|
|
|
for _, step := range deployment.Steps {
|
|
err = runner.Run(step)
|
|
if err != nil {
|
|
return fmt.Errorf("deployment step %q failed: %v", step, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func deployWindows(t *testing.T, target *ProvisioningTarget) error {
|
|
var err error
|
|
runner := target.runner
|
|
deployment := target.Deployment
|
|
|
|
err = runner.Open(t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer runner.Close()
|
|
|
|
runner.Run("Stop-Service Nomad -ErrorAction Ignore; $?")
|
|
|
|
if deployment.NomadLocalBinary != "" {
|
|
if deployment.RemoteBinaryPath == "" {
|
|
return fmt.Errorf("remote binary path not set")
|
|
}
|
|
err = runner.Copy(
|
|
deployment.NomadLocalBinary,
|
|
deployment.RemoteBinaryPath)
|
|
if err != nil {
|
|
return fmt.Errorf("copying Nomad failed: %v", err)
|
|
}
|
|
} else if deployment.NomadSha != "" {
|
|
script := fmt.Sprintf(
|
|
`C:/opt/install-nomad.ps1 -nomad_sha %s -nostart`, deployment.NomadSha)
|
|
err = runner.Run(script)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if deployment.NomadVersion != "" {
|
|
script := fmt.Sprintf(
|
|
`C:/opt/install-nomad.ps1 -nomad_version %s -nostart`, deployment.NomadVersion)
|
|
err = runner.Run(script)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
t.Log("no Nomad deployment specified, falling back to 'step' field.")
|
|
}
|
|
|
|
for _, bundle := range deployment.Bundles {
|
|
err = runner.Copy(
|
|
bundle.Source, bundle.Destination)
|
|
if err != nil {
|
|
return fmt.Errorf("copying bundle '%s' failed: %v", bundle.Source, err)
|
|
}
|
|
}
|
|
|
|
for _, step := range deployment.Steps {
|
|
err = runner.Run(step)
|
|
if err != nil {
|
|
return fmt.Errorf("deployment step %q failed: %v", step, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|