Skip to content

Commit

Permalink
Merge branch 'sftp' of https://github.com/2opremio/packer into f-sftp
Browse files Browse the repository at this point in the history
  • Loading branch information
cbednarski committed Feb 2, 2016
2 parents dab36cb + a59c82d commit 3d9410f
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 106 deletions.
365 changes: 273 additions & 92 deletions communicator/ssh/communicator.go

Large diffs are not rendered by default.

39 changes: 25 additions & 14 deletions helper/communicator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ type Config struct {
Type string `mapstructure:"communicator"`

// SSH
SSHHost string `mapstructure:"ssh_host"`
SSHPort int `mapstructure:"ssh_port"`
SSHUsername string `mapstructure:"ssh_username"`
SSHPassword string `mapstructure:"ssh_password"`
SSHPrivateKey string `mapstructure:"ssh_private_key_file"`
SSHPty bool `mapstructure:"ssh_pty"`
SSHTimeout time.Duration `mapstructure:"ssh_timeout"`
SSHDisableAgent bool `mapstructure:"ssh_disable_agent"`
SSHHandshakeAttempts int `mapstructure:"ssh_handshake_attempts"`
SSHBastionHost string `mapstructure:"ssh_bastion_host"`
SSHBastionPort int `mapstructure:"ssh_bastion_port"`
SSHBastionUsername string `mapstructure:"ssh_bastion_username"`
SSHBastionPassword string `mapstructure:"ssh_bastion_password"`
SSHBastionPrivateKey string `mapstructure:"ssh_bastion_private_key_file"`
SSHHost string `mapstructure:"ssh_host"`
SSHPort int `mapstructure:"ssh_port"`
SSHUsername string `mapstructure:"ssh_username"`
SSHPassword string `mapstructure:"ssh_password"`
SSHPrivateKey string `mapstructure:"ssh_private_key_file"`
SSHPty bool `mapstructure:"ssh_pty"`
SSHTimeout time.Duration `mapstructure:"ssh_timeout"`
SSHDisableAgent bool `mapstructure:"ssh_disable_agent"`
SSHHandshakeAttempts int `mapstructure:"ssh_handshake_attempts"`
SSHBastionHost string `mapstructure:"ssh_bastion_host"`
SSHBastionPort int `mapstructure:"ssh_bastion_port"`
SSHBastionUsername string `mapstructure:"ssh_bastion_username"`
SSHBastionPassword string `mapstructure:"ssh_bastion_password"`
SSHBastionPrivateKey string `mapstructure:"ssh_bastion_private_key_file"`
SSHFileTransferMethod string `mapstructure:"ssh_file_transfer_method"`

// WinRM
WinRMUser string `mapstructure:"winrm_username"`
Expand Down Expand Up @@ -99,6 +100,10 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error {
}
}

if c.SSHFileTransferMethod == "" {
c.SSHFileTransferMethod = "scp"
}

// Validation
var errs []error
if c.SSHUsername == "" {
Expand All @@ -122,6 +127,12 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error {
}
}

if c.SSHFileTransferMethod != "scp" && c.SSHFileTransferMethod != "sftp" {
errs = append(errs, fmt.Errorf(
"ssh_file_transfer_method ('%s') is invalid, valid methods: sftp, scp",
c.SSHFileTransferMethod))
}

return errs
}

Expand Down
1 change: 1 addition & 0 deletions helper/communicator/step_connect_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (s *StepConnectSSH) waitForSSH(state multistep.StateBag, cancel <-chan stru
SSHConfig: sshConfig,
Pty: s.Config.SSHPty,
DisableAgent: s.Config.SSHDisableAgent,
UseSftp: s.Config.SSHFileTransferMethod == "sftp",
}

log.Println("[INFO] Attempting SSH connection...")
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/provisioner-file/dir_no_trailing_sftp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"builders": [{
"type": "amazon-ebs",
"ami_name": "packer-test {{timestamp}}",
"instance_type": "t2.micro",
"region": "us-east-1",
"ssh_username": "ec2-user",
"ssh_file_transfer_method": "sftp",
"source_ami": "ami-8da458e6",
"tags": {
"packer-test": "true"
}
}],

"provisioners": [{
"type": "file",
"source": "dir",
"destination": "/tmp"
}, {
"type": "shell",
"inline": ["cat /tmp/dir/file.txt"]
}]
}
23 changes: 23 additions & 0 deletions test/fixtures/provisioner-file/dir_with_trailing_sftp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"builders": [{
"type": "amazon-ebs",
"ami_name": "packer-test {{timestamp}}",
"instance_type": "t2.micro",
"region": "us-east-1",
"ssh_username": "ec2-user",
"ssh_file_transfer_method": "sftp",
"source_ami": "ami-8da458e6",
"tags": {
"packer-test": "true"
}
}],

"provisioners": [{
"type": "file",
"source": "dir/",
"destination": "/tmp"
}, {
"type": "shell",
"inline": ["cat /tmp/file.txt"]
}]
}
23 changes: 23 additions & 0 deletions test/fixtures/provisioner-file/file_sftp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"builders": [{
"type": "amazon-ebs",
"ami_name": "packer-test {{timestamp}}",
"instance_type": "t2.micro",
"region": "us-east-1",
"ssh_username": "ec2-user",
"ssh_file_transfer_method": "sftp",
"source_ami": "ami-8da458e6",
"tags": {
"packer-test": "true"
}
}],

"provisioners": [{
"type": "file",
"source": "file.txt",
"destination": "/tmp/file.txt"
}, {
"type": "shell",
"inline": ["cat /tmp/file.txt"]
}]
}
18 changes: 18 additions & 0 deletions test/provisioner_file.bats
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ teardown() {
[ "$status" -eq 0 ]
[[ "$output" == *"337 miles"* ]]
}

@test "file provisioner: single file through sftp" {
run packer build $FIXTURE_ROOT/file_sftp.json
[ "$status" -eq 0 ]
[[ "$output" == *"24901 miles"* ]]
}

@test "file provisioner: directory through sftp (no trailing slash)" {
run packer build $FIXTURE_ROOT/dir_no_trailing_sftp.json
[ "$status" -eq 0 ]
[[ "$output" == *"337 miles"* ]]
}

@test "file provisioner: directory through sftp (with trailing slash)" {
run packer build $FIXTURE_ROOT/dir_with_trailing_sftp.json
[ "$status" -eq 0 ]
[[ "$output" == *"337 miles"* ]]
}
3 changes: 3 additions & 0 deletions website/source/docs/templates/communicator.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ The SSH communicator has the following options:

* `ssh_bastion_private_key_file` (string) - A private key file to use
to authenticate with the bastion host.

* `ssh_file_transfer_method` (`scp` or `sftp`) - How to transfer files, Secure
copy (default) or SSH File Transfer Protocol.

## WinRM Communicator

Expand Down

0 comments on commit 3d9410f

Please sign in to comment.