github.com/rohankumardubey/nomad@v0.11.8/e2e/framework/provisioning/deploy.go (about)

     1  package provisioning
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  func deploy(t *testing.T, target *ProvisioningTarget) error {
    10  	platform := target.Deployment.Platform
    11  	if target.Deployment.Platform == "linux_amd64" {
    12  		return deployLinux(t, target)
    13  	} else if target.Deployment.Platform == "windows_amd64" {
    14  		return deployWindows(t, target)
    15  	}
    16  	return fmt.Errorf("invalid deployment platform: %v", platform)
    17  }
    18  
    19  func deployLinux(t *testing.T, target *ProvisioningTarget) error {
    20  	var err error
    21  	runner := target.runner
    22  	deployment := target.Deployment
    23  
    24  	err = runner.Open(t)
    25  	if err != nil {
    26  		return err
    27  	}
    28  	defer runner.Close()
    29  
    30  	if deployment.NomadLocalBinary != "" {
    31  		if deployment.RemoteBinaryPath == "" {
    32  			return fmt.Errorf("remote binary path not set")
    33  		}
    34  		err = runner.Copy(
    35  			deployment.NomadLocalBinary,
    36  			deployment.RemoteBinaryPath)
    37  		if err != nil {
    38  			return fmt.Errorf("copying Nomad failed: %v", err)
    39  		}
    40  	} else if deployment.NomadSha != "" {
    41  		if deployment.RemoteBinaryPath == "" {
    42  			return fmt.Errorf("remote binary path not set")
    43  		}
    44  		s3_url := fmt.Sprintf("s3://nomad-team-test-binary/builds-oss/nomad_%s_%s.tar.gz",
    45  			deployment.Platform, deployment.NomadSha,
    46  		)
    47  		remoteDir := filepath.Dir(deployment.RemoteBinaryPath)
    48  		script := fmt.Sprintf(`aws s3 cp --quiet %s nomad.tar.gz
    49  			sudo tar -zxvf nomad.tar.gz -C %s
    50  			sudo chmod 0755 %s
    51  			sudo chown root:root %s`,
    52  			s3_url, remoteDir, deployment.RemoteBinaryPath, deployment.RemoteBinaryPath)
    53  		err = runner.Run(script)
    54  		if err != nil {
    55  			return err
    56  		}
    57  	} else if deployment.NomadVersion != "" {
    58  		if deployment.RemoteBinaryPath == "" {
    59  			return fmt.Errorf("remote binary path not set")
    60  		}
    61  		url := fmt.Sprintf("https://releases.hashicorp.com/nomad/%s/nomad_%s_%s.zip",
    62  			deployment.NomadVersion, deployment.NomadVersion, deployment.Platform,
    63  		)
    64  		remoteDir := filepath.Dir(deployment.RemoteBinaryPath)
    65  		script := fmt.Sprintf(`curl -L --fail -o /tmp/nomad.zip %s
    66  			sudo unzip -o /tmp/nomad.zip -d %s
    67  			sudo chmod 0755 %s
    68  			sudo chown root:root %s`,
    69  			url, remoteDir, deployment.RemoteBinaryPath, deployment.RemoteBinaryPath)
    70  		err = runner.Run(script)
    71  		if err != nil {
    72  			return err
    73  		}
    74  	} else {
    75  		t.Log("no Nomad deployment specified, falling back to 'step' field.")
    76  	}
    77  
    78  	for _, bundle := range deployment.Bundles {
    79  		err = runner.Copy(
    80  			bundle.Source, bundle.Destination)
    81  		if err != nil {
    82  			return fmt.Errorf("copying bundle '%s' failed: %v", bundle.Source, err)
    83  		}
    84  	}
    85  	for _, step := range deployment.Steps {
    86  		err = runner.Run(step)
    87  		if err != nil {
    88  			return fmt.Errorf("deployment step %q failed: %v", step, err)
    89  		}
    90  	}
    91  	return nil
    92  }
    93  
    94  func deployWindows(t *testing.T, target *ProvisioningTarget) error {
    95  	var err error
    96  	runner := target.runner
    97  	deployment := target.Deployment
    98  
    99  	err = runner.Open(t)
   100  	if err != nil {
   101  		return err
   102  	}
   103  	defer runner.Close()
   104  
   105  	runner.Run("Stop-Service Nomad -ErrorAction Ignore; $?")
   106  
   107  	if deployment.NomadLocalBinary != "" {
   108  		if deployment.RemoteBinaryPath == "" {
   109  			return fmt.Errorf("remote binary path not set")
   110  		}
   111  		err = runner.Copy(
   112  			deployment.NomadLocalBinary,
   113  			deployment.RemoteBinaryPath)
   114  		if err != nil {
   115  			return fmt.Errorf("copying Nomad failed: %v", err)
   116  		}
   117  	} else if deployment.NomadSha != "" {
   118  		if deployment.RemoteBinaryPath == "" {
   119  			return fmt.Errorf("remote binary path not set")
   120  		}
   121  		script := fmt.Sprintf(`
   122  			Read-S3Object -BucketName nomad-team-test-binary -Key "builds-oss/nomad_windows_amd64_%s.zip" -File ./nomad.zip
   123  			Expand-Archive ./nomad.zip ./ -Force
   124  			Remove-Item %s  -ErrorAction Ignore
   125  			Move-Item -Path .\pkg\windows_amd64\nomad.exe -Destination %s -Force`,
   126  			deployment.NomadSha, deployment.RemoteBinaryPath,
   127  			deployment.RemoteBinaryPath)
   128  		err = runner.Run(script)
   129  		if err != nil {
   130  			return err
   131  		}
   132  	} else if deployment.NomadVersion != "" {
   133  		if deployment.RemoteBinaryPath == "" {
   134  			return fmt.Errorf("remote binary path not set")
   135  		}
   136  		url := fmt.Sprintf("https://releases.hashicorp.com/nomad/%s/nomad_%s_%s.zip",
   137  			deployment.NomadVersion, deployment.NomadVersion, deployment.Platform,
   138  		)
   139  		script := fmt.Sprintf(`
   140  			Invoke-WebRequest -Uri "%s" -Outfile /.nomad.zip
   141  			Expand-Archive ./nomad.zip ./ -Force
   142  			Remove-Item %s  -ErrorAction Ignore
   143  			Move-Item -Path .\pkg\windows_amd64\nomad.exe -Destination %s -Force`,
   144  			url, deployment.RemoteBinaryPath, deployment.RemoteBinaryPath)
   145  		err = runner.Run(script)
   146  		if err != nil {
   147  			return err
   148  		}
   149  	} else {
   150  		t.Log("no Nomad deployment specified, falling back to 'step' field.")
   151  	}
   152  
   153  	for _, bundle := range deployment.Bundles {
   154  		err = runner.Copy(
   155  			bundle.Source, bundle.Destination)
   156  		if err != nil {
   157  			return fmt.Errorf("copying bundle '%s' failed: %v", bundle.Source, err)
   158  		}
   159  	}
   160  	for _, step := range deployment.Steps {
   161  		err = runner.Run(step)
   162  		if err != nil {
   163  			return fmt.Errorf("deployment step %q failed: %v", step, err)
   164  		}
   165  	}
   166  	return nil
   167  }