From 9ebf0435ff3bb709cb195c30e27715b80eb8f0a9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 27 May 2013 21:54:19 -0700 Subject: [PATCH] provisioner/shell: Basic run --- .gitignore | 1 + TODO.md | 1 + example.json | 24 ------------------------ example.toml | 15 --------------- provisioner/shell/provisioner.go | 29 ++++++++++++++++++++++++++++- 5 files changed, 30 insertions(+), 40 deletions(-) delete mode 100644 example.json delete mode 100644 example.toml diff --git a/.gitignore b/.gitignore index d5c4f70cc..682fdcfe3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /bin +/local packerrc diff --git a/TODO.md b/TODO.md index 6a1d4f83f..6e30376f9 100644 --- a/TODO.md +++ b/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 diff --git a/example.json b/example.json deleted file mode 100644 index 385882490..000000000 --- a/example.json +++ /dev/null @@ -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" - } - ] -} diff --git a/example.toml b/example.toml deleted file mode 100644 index 27cf2c7e0..000000000 --- a/example.toml +++ /dev/null @@ -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] diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index ebd073fb0..e264f600b 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -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!") }