Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/ssh: add random number to upload path for script [GH-1545] #1588

Merged
merged 1 commit into from
Apr 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions builtin/provisioners/remote-exec/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,13 @@ func (p *ResourceProvisioner) runScripts(
go p.copyOutput(o, errR, errDoneCh)

err := retryFunc(conf.TimeoutVal, func() error {
if err := comm.Upload(conf.ScriptPath, script); err != nil {
remotePath := conf.RemotePath()

if err := comm.Upload(remotePath, script); err != nil {
return fmt.Errorf("Failed to upload script: %v", err)
}
cmd = &helper.RemoteCmd{
Command: fmt.Sprintf("chmod 0777 %s", conf.ScriptPath),
Command: fmt.Sprintf("chmod 0777 %s", remotePath),
}
if err := comm.Start(cmd); err != nil {
return fmt.Errorf(
Expand All @@ -227,7 +229,7 @@ func (p *ResourceProvisioner) runScripts(
cmd.Wait()

cmd = &helper.RemoteCmd{
Command: conf.ScriptPath,
Command: remotePath,
Stdout: outW,
Stderr: errW,
}
Expand Down
11 changes: 10 additions & 1 deletion helper/ssh/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform/terraform"
Expand All @@ -25,7 +28,7 @@ const (

// DefaultScriptPath is used as the path to copy the file to
// for remote execution if not provided otherwise.
DefaultScriptPath = "/tmp/script.sh"
DefaultScriptPath = "/tmp/script_%RAND%.sh"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of weird but I've decided not to document this since it is a very advanced use case. And it looks like most people don't use this arg anyways. I'd rather not promise any backwards compat on the syntax of this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed +1


// DefaultTimeout is used if there is no timeout given
DefaultTimeout = 5 * time.Minute
Expand All @@ -46,6 +49,12 @@ type SSHConfig struct {
TimeoutVal time.Duration `mapstructure:"-"`
}

func (c *SSHConfig) RemotePath() string {
return strings.Replace(
c.ScriptPath, "%RAND%",
strconv.FormatInt(int64(rand.Int31()), 10), -1)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a bit of potential for future confusion that this isn't memoized, but this area of the code is simple enough that I think it's not a big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, yeah. I think it'll be okay to get started though.


// VerifySSH is used to verify the ConnInfo is usable by remote-exec
func VerifySSH(s *terraform.InstanceState) error {
connType := s.Ephemeral.ConnInfo["type"]
Expand Down
30 changes: 30 additions & 0 deletions helper/ssh/provisioner_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
package ssh

import (
"regexp"
"testing"

"github.com/hashicorp/terraform/terraform"
)

func TestSSHConfig_RemotePath(t *testing.T) {
cases := []struct {
Input string
Pattern string
}{
{
"/tmp/script.sh",
`^/tmp/script\.sh$`,
},
{
"/tmp/script_%RAND%.sh",
`^/tmp/script_(\d+)\.sh$`,
},
}

for _, tc := range cases {
config := &SSHConfig{ScriptPath: tc.Input}
output := config.RemotePath()

match, err := regexp.Match(tc.Pattern, []byte(output))
if err != nil {
t.Fatalf("bad: %s\n\nerr: %s", tc.Input, err)
}
if !match {
t.Fatalf("bad: %s\n\n%s", tc.Input, output)
}
}
}

func TestResourceProvider_verifySSH(t *testing.T) {
r := &terraform.InstanceState{
Ephemeral: terraform.EphemeralState{
Expand Down