mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-08 16:22:15 -04:00
provisioner/shell: Basic run
This commit is contained in:
parent
33f8d29571
commit
9ebf0435ff
5 changed files with 30 additions and 40 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
|||
/bin
|
||||
/local
|
||||
packerrc
|
||||
|
|
|
|||
1
TODO.md
1
TODO.md
|
|
@ -6,5 +6,6 @@
|
|||
* packer: Communicator should have Close() method
|
||||
* packer: Ui input
|
||||
* packer/plugin: Better error messages/detection if plugin crashes
|
||||
* packer/plugin: Testing of client struct/methods
|
||||
* provisioner/shell: Upload file
|
||||
* provisioner/shell: Arguments
|
||||
|
|
|
|||
24
example.json
24
example.json
|
|
@ -1,24 +0,0 @@
|
|||
{
|
||||
"name": "my-custom-image",
|
||||
|
||||
"builders": [
|
||||
{
|
||||
"type": "amazon-ebs",
|
||||
"region": "us-east-1",
|
||||
"source_ami": "ami-de0d9eb7"
|
||||
}
|
||||
],
|
||||
|
||||
"provisioners": [
|
||||
{
|
||||
"type": "shell",
|
||||
"path": "script.sh"
|
||||
}
|
||||
],
|
||||
|
||||
"outputs": [
|
||||
{
|
||||
"type": "vagrant"
|
||||
}
|
||||
]
|
||||
}
|
||||
15
example.toml
15
example.toml
|
|
@ -1,15 +0,0 @@
|
|||
name = "my-custom-image"
|
||||
|
||||
[builder.amazon-ebs]
|
||||
region = "us-east-1"
|
||||
source = "ami-de0d9eb7"
|
||||
|
||||
[provision]
|
||||
|
||||
[provision.shell]
|
||||
type = "shell"
|
||||
path = "script.sh"
|
||||
|
||||
[output]
|
||||
|
||||
[output.vagrant]
|
||||
|
|
@ -3,8 +3,11 @@
|
|||
package shell
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
const DefaultRemotePath = "/tmp/script.sh"
|
||||
|
|
@ -33,5 +36,29 @@ func (p *Provisioner) Prepare(raw interface{}, ui packer.Ui) {
|
|||
}
|
||||
|
||||
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
|
||||
ui.Say("PROVISIONING SOME STUFF")
|
||||
log.Printf("Opening %s for reading", p.config.Path)
|
||||
f, err := os.Open(p.config.Path)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Error opening shell script: %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Uploading %s => %s", p.config.Path, p.config.RemotePath)
|
||||
err = comm.Upload(p.config.RemotePath, f)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Error uploading shell script: %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
command := fmt.Sprintf("chmod +x %s && %s", p.config.RemotePath, p.config.RemotePath)
|
||||
log.Printf("Executing command: %s", command)
|
||||
cmd, err := comm.Start(command)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Sprintf("Failed executing command: %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
ui.Say("Waiting for remote command to finish...")
|
||||
cmd.Wait()
|
||||
ui.Say("Command run!")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue