Commit 26abac69 authored by Jack Pearkes's avatar Jack Pearkes

post-processor/vagrant-cloud: steps for create, upload and release

parent c899051c
...@@ -6,8 +6,11 @@ import ( ...@@ -6,8 +6,11 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"mime/multipart"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path/filepath"
"strings" "strings"
) )
...@@ -74,6 +77,7 @@ func (v VagrantCloudClient) Get(path string) (*http.Response, error) { ...@@ -74,6 +77,7 @@ func (v VagrantCloudClient) Get(path string) (*http.Response, error) {
log.Printf("Post-Processor Vagrant Cloud API GET: %s", scrubbedUrl) log.Printf("Post-Processor Vagrant Cloud API GET: %s", scrubbedUrl)
req, err := http.NewRequest("GET", reqUrl, nil) req, err := http.NewRequest("GET", reqUrl, nil)
req.Header.Add("Content-Type", "application/json")
resp, err := v.client.Do(req) resp, err := v.client.Do(req)
log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%s", resp) log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%s", resp)
...@@ -91,6 +95,7 @@ func (v VagrantCloudClient) Delete(path string) (*http.Response, error) { ...@@ -91,6 +95,7 @@ func (v VagrantCloudClient) Delete(path string) (*http.Response, error) {
log.Printf("Post-Processor Vagrant Cloud API DELETE: %s", scrubbedUrl) log.Printf("Post-Processor Vagrant Cloud API DELETE: %s", scrubbedUrl)
req, err := http.NewRequest("DELETE", reqUrl, nil) req, err := http.NewRequest("DELETE", reqUrl, nil)
req.Header.Add("Content-Type", "application/json")
resp, err := v.client.Do(req) resp, err := v.client.Do(req)
log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%s", resp) log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%s", resp)
...@@ -98,13 +103,48 @@ func (v VagrantCloudClient) Delete(path string) (*http.Response, error) { ...@@ -98,13 +103,48 @@ func (v VagrantCloudClient) Delete(path string) (*http.Response, error) {
return resp, err return resp, err
} }
func (v VagrantCloudClient) Upload(path string, url string) (*http.Response, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("Error opening file for upload: %s", err)
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, fmt.Errorf("Error uploading file: %s", err)
}
request, err := http.NewRequest("PUT", url, body)
if err != nil {
return nil, fmt.Errorf("Error preparing upload request: %s", err)
}
log.Printf("Post-Processor Vagrant Cloud API Upload: %s %s", path, url)
resp, err := v.client.Do(request)
log.Printf("Post-Processor Vagrant Cloud Upload Response: \n\n%s", resp)
return resp, err
}
func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response, error) { func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response, error) {
params := url.Values{} params := url.Values{}
params.Set("access_token", v.AccessToken) params.Set("access_token", v.AccessToken)
reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode()) reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode())
log.Println(reqUrl)
encBody, err := encodeBody(body) encBody, err := encodeBody(body)
log.Println(encBody) log.Println(encBody)
...@@ -126,3 +166,22 @@ func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response, ...@@ -126,3 +166,22 @@ func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response,
return resp, err return resp, err
} }
func (v VagrantCloudClient) Put(path string) (*http.Response, error) {
params := url.Values{}
params.Set("access_token", v.AccessToken)
reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode())
// Scrub API key for logs
scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
log.Printf("Post-Processor Vagrant Cloud API PUT: %s", scrubbedUrl)
req, err := http.NewRequest("PUT", reqUrl, nil)
req.Header.Add("Content-Type", "application/json")
resp, err := v.client.Do(req)
log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%s", resp)
return resp, err
}
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"strings"
) )
const VAGRANT_CLOUD_URL = "https://vagrantcloud.com/api/v1" const VAGRANT_CLOUD_URL = "https://vagrantcloud.com/api/v1"
...@@ -88,15 +89,26 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ...@@ -88,15 +89,26 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
"Unknown artifact type, requires box from vagrant post-processor: %s", artifact.BuilderId()) "Unknown artifact type, requires box from vagrant post-processor: %s", artifact.BuilderId())
} }
// We assume that there is only one .box file to upload
if !strings.HasSuffix(artifact.Files()[0], ".box") {
return nil, false, fmt.Errorf(
"Unknown files in artifact from vagrant post-processor: %s", artifact.Files())
}
// create the HTTP client // create the HTTP client
p.client = VagrantCloudClient{}.New(p.config.VagrantCloudUrl, p.config.AccessToken) p.client = VagrantCloudClient{}.New(p.config.VagrantCloudUrl, p.config.AccessToken)
// The name of the provider for vagrant cloud, and vagrant
providerName := providerFromBuilderName(artifact.Id())
// Set up the state // Set up the state
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("config", p.config) state.Put("config", p.config)
state.Put("client", p.client) state.Put("client", p.client)
state.Put("artifact", artifact) state.Put("artifact", artifact)
state.Put("artifactFilePath", artifact.Files()[0])
state.Put("ui", ui) state.Put("ui", ui)
state.Put("providerName", providerName)
// Build the steps // Build the steps
steps := []multistep.Step{ steps := []multistep.Step{
...@@ -106,6 +118,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ...@@ -106,6 +118,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
new(stepPrepareUpload), new(stepPrepareUpload),
new(stepUpload), new(stepUpload),
new(stepVerifyUpload), new(stepVerifyUpload),
new(stepReleaseVersion),
} }
// Run the steps // Run the steps
...@@ -125,10 +138,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ...@@ -125,10 +138,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
return nil, false, rawErr.(error) return nil, false, rawErr.(error)
} }
// // The name of the provider for vagrant cloud, and vagrant return NewArtifact(providerName, p.config.Tag), true, nil
provider := providerFromBuilderName(artifact.Id())
return NewArtifact(provider, p.config.Tag), true, nil
} }
// Runs a cleanup if the post processor fails to upload // Runs a cleanup if the post processor fails to upload
......
package vagrantcloud
import (
"fmt"
)
type Provider struct {
client *VagrantCloudClient
Name string `json:"name"`
}
// https://vagrantcloud.com/docs/providers
func (v VagrantCloudClient) Provider(tag string) (*Box, error) {
resp, err := v.Get(tag)
if err != nil {
return nil, fmt.Errorf("Error retrieving box: %s", err)
}
box := &Box{}
if err = decodeBody(resp, box); err != nil {
return nil, fmt.Errorf("Error parsing box response: %s", err)
}
return box, nil
}
// Save persist the box over HTTP to Vagrant Cloud
func (p Provider) Save(name string) (bool, error) {
resp, err := p.client.Get(name)
if err != nil {
return false, fmt.Errorf("Error retrieving box: %s", err)
}
provider := &Provider{}
if err = decodeBody(resp, provider); err != nil {
return false, fmt.Errorf("Error parsing box response: %s", err)
}
return true, nil
}
package vagrantcloud package vagrantcloud
import ( import (
"fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
) )
type Provider struct {
Name string `json:"name"`
HostedToken string `json:"hosted_token,omitempty"`
UploadUrl string `json:"upload_url,omitempty"`
}
type stepCreateProvider struct { type stepCreateProvider struct {
name string // the name of the provider
} }
func (s *stepCreateProvider) Run(state multistep.StateBag) multistep.StepAction { func (s *stepCreateProvider) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*VagrantCloudClient)
ui := state.Get("ui").(packer.Ui)
box := state.Get("box").(*Box)
version := state.Get("version").(*Version)
providerName := state.Get("providerName").(string)
path := fmt.Sprintf("box/%s/version/%v/providers", box.Tag, version.Number)
provider := &Provider{Name: providerName}
// Wrap the provider in a provider object for the API
wrapper := make(map[string]interface{})
wrapper["provider"] = provider
ui.Say(fmt.Sprintf("Creating provider: %s", providerName))
resp, err := client.Post(path, wrapper)
if err != nil || (resp.StatusCode != 200) {
cloudErrors := &VagrantCloudErrors{}
err = decodeBody(resp, cloudErrors)
state.Put("error", fmt.Errorf("Error creating provider: %s", cloudErrors.FormatErrors()))
return multistep.ActionHalt
}
if err = decodeBody(resp, provider); err != nil {
state.Put("error", fmt.Errorf("Error parsing provider response: %s", err))
return multistep.ActionHalt
}
// Save the name for cleanup
s.name = provider.Name
state.Put("provider", provider)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepCreateProvider) Cleanup(state multistep.StateBag) { func (s *stepCreateProvider) Cleanup(state multistep.StateBag) {
// If we didn't save the provider name, it likely doesn't exist
if s.name == "" {
return
}
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
// Return if we didn't cancel or halt, and thus need
// no cleanup
if !cancelled && !halted {
return
}
client := state.Get("client").(*VagrantCloudClient)
ui := state.Get("ui").(packer.Ui)
box := state.Get("box").(*Box)
version := state.Get("version").(*Version)
path := fmt.Sprintf("box/%s/version/%v/provider/%s", box.Tag, version.Number, s.name)
// No need for resp from the cleanup DELETE
_, err := client.Delete(path)
if err != nil {
ui.Error(fmt.Sprintf("Error destroying provider: %s", err))
}
} }
...@@ -8,11 +8,7 @@ import ( ...@@ -8,11 +8,7 @@ import (
type Version struct { type Version struct {
Version string `json:"version"` Version string `json:"version"`
Number uint `json:"number"` Number uint `json:"number,omitempty"`
}
type NewVersion struct {
Version string `json:"version"`
} }
type stepCreateVersion struct { type stepCreateVersion struct {
...@@ -25,20 +21,27 @@ func (s *stepCreateVersion) Run(state multistep.StateBag) multistep.StepAction { ...@@ -25,20 +21,27 @@ func (s *stepCreateVersion) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(Config) config := state.Get("config").(Config)
box := state.Get("box").(*Box) box := state.Get("box").(*Box)
if hasVersion, v := box.HasVersion(config.Version); hasVersion {
ui.Say(fmt.Sprintf("Version exists: %s", config.Version))
state.Put("version", v)
return multistep.ActionContinue
}
path := fmt.Sprintf("box/%s/versions", box.Tag) path := fmt.Sprintf("box/%s/versions", box.Tag)
version := &Version{Version: config.Version}
// Wrap the version in a version object for the API // Wrap the version in a version object for the API
wrapper := make(map[string]interface{}) wrapper := make(map[string]interface{})
wrapper["version"] = NewVersion{Version: config.Version} wrapper["version"] = version
ui.Say(fmt.Sprintf("Creating version: %s", config.Version)) ui.Say(fmt.Sprintf("Creating version: %s", config.Version))
resp, err := client.Post(path, wrapper) resp, err := client.Post(path, wrapper)
version := &Version{}
if err != nil || (resp.StatusCode != 200) { if err != nil || (resp.StatusCode != 200) {
cloudErrors := &VagrantCloudErrors{}; cloudErrors := &VagrantCloudErrors{}
err = decodeBody(resp, cloudErrors); err = decodeBody(resp, cloudErrors)
state.Put("error", fmt.Errorf("Error creating version: %s", cloudErrors.FormatErrors())) state.Put("error", fmt.Errorf("Error creating version: %s", cloudErrors.FormatErrors()))
return multistep.ActionHalt return multistep.ActionHalt
} }
...@@ -57,8 +60,18 @@ func (s *stepCreateVersion) Run(state multistep.StateBag) multistep.StepAction { ...@@ -57,8 +60,18 @@ func (s *stepCreateVersion) Run(state multistep.StateBag) multistep.StepAction {
} }
func (s *stepCreateVersion) Cleanup(state multistep.StateBag) { func (s *stepCreateVersion) Cleanup(state multistep.StateBag) {
// If we didn't save the version number, it likely doesn't exist // If we didn't save the version number, it likely doesn't exist or
if (s.number == 0) { // already existed
if s.number == 0 {
return
}
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
// Return if we didn't cancel or halt, and thus need
// no cleanup
if !cancelled && !halted {
return return
} }
...@@ -66,7 +79,7 @@ func (s *stepCreateVersion) Cleanup(state multistep.StateBag) { ...@@ -66,7 +79,7 @@ func (s *stepCreateVersion) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
box := state.Get("box").(*Box) box := state.Get("box").(*Box)
path := fmt.Sprintf("box/%s/version/%s", box.Tag, s.number) path := fmt.Sprintf("box/%s/version/%v", box.Tag, s.number)
// No need for resp from the cleanup DELETE // No need for resp from the cleanup DELETE
_, err := client.Delete(path) _, err := client.Delete(path)
...@@ -74,4 +87,5 @@ func (s *stepCreateVersion) Cleanup(state multistep.StateBag) { ...@@ -74,4 +87,5 @@ func (s *stepCreateVersion) Cleanup(state multistep.StateBag) {
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error destroying version: %s", err)) ui.Error(fmt.Sprintf("Error destroying version: %s", err))
} }
} }
package vagrantcloud package vagrantcloud
import ( import (
"fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
) )
type Upload struct {
Token string `json:"token"`
UploadPath string `json:"upload_path"`
}
type stepPrepareUpload struct { type stepPrepareUpload struct {
} }
func (s *stepPrepareUpload) Run(state multistep.StateBag) multistep.StepAction { func (s *stepPrepareUpload) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*VagrantCloudClient)
ui := state.Get("ui").(packer.Ui)
box := state.Get("box").(*Box)
version := state.Get("version").(*Version)
provider := state.Get("provider").(*Provider)
artifactFilePath := state.Get("artifactFilePath").(string)
path := fmt.Sprintf("box/%s/version/%v/provider/%s/upload", box.Tag, version.Number, provider.Name)
upload := &Upload{}
ui.Say(fmt.Sprintf("Preparing upload of box: %s", artifactFilePath))
resp, err := client.Get(path)
if err != nil || (resp.StatusCode != 200) {
cloudErrors := &VagrantCloudErrors{}
err = decodeBody(resp, cloudErrors)
state.Put("error", fmt.Errorf("Error preparing upload: %s", cloudErrors.FormatErrors()))
return multistep.ActionHalt
}
if err = decodeBody(resp, upload); err != nil {
state.Put("error", fmt.Errorf("Error parsing upload response: %s", err))
return multistep.ActionHalt
}
// Save the upload details to the state
state.Put("upload", upload)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepPrepareUpload) Cleanup(state multistep.StateBag) { func (s *stepPrepareUpload) Cleanup(state multistep.StateBag) {
// No cleanup
} }
package vagrantcloud
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type stepReleaseVersion struct {
}
func (s *stepReleaseVersion) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*VagrantCloudClient)
ui := state.Get("ui").(packer.Ui)
box := state.Get("box").(*Box)
version := state.Get("version").(*Version)
path := fmt.Sprintf("box/%s/version/%v/release", box.Tag, version.Number)
ui.Say(fmt.Sprintf("Releasing version: %s", version.Version))
resp, err := client.Put(path)
if err != nil || (resp.StatusCode != 200) {
cloudErrors := &VagrantCloudErrors{}
err = decodeBody(resp, cloudErrors)
state.Put("error", fmt.Errorf("Error releasing version: %s", cloudErrors.FormatErrors()))
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *stepReleaseVersion) Cleanup(state multistep.StateBag) {
// No cleanup
}
package vagrantcloud package vagrantcloud
import ( import (
"fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
) )
type stepUpload struct { type stepUpload struct {
} }
func (s *stepUpload) Run(state multistep.StateBag) multistep.StepAction { func (s *stepUpload) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*VagrantCloudClient)
ui := state.Get("ui").(packer.Ui)
upload := state.Get("upload").(*Upload)
artifactFilePath := state.Get("artifactFilePath").(string)
url := upload.UploadPath
ui.Say(fmt.Sprintf("Uploading box: %s", artifactFilePath))
resp, err := client.Upload(artifactFilePath, url)
if err != nil || (resp.StatusCode != 200) {
state.Put("error", fmt.Errorf("Error uploading Box: %s", resp.Body))
return multistep.ActionHalt
}
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepUpload) Cleanup(state multistep.StateBag) { func (s *stepUpload) Cleanup(state multistep.StateBag) {
// No cleanup
} }
...@@ -8,6 +8,16 @@ import ( ...@@ -8,6 +8,16 @@ import (
type Box struct { type Box struct {
Tag string `json:"tag"` Tag string `json:"tag"`
Versions []*Version `json:"versions"`
}
func (b *Box) HasVersion(version string) (bool, *Version) {
for _, v := range b.Versions {
if v.Version == version {
return true, v
}
}
return false, nil
} }
type stepVerifyBox struct { type stepVerifyBox struct {
......
package vagrantcloud package vagrantcloud
import ( import (
"fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"time"
) )
type stepVerifyUpload struct { type stepVerifyUpload struct {
} }
func (s *stepVerifyUpload) Run(state multistep.StateBag) multistep.StepAction { func (s *stepVerifyUpload) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*VagrantCloudClient)
ui := state.Get("ui").(packer.Ui)
box := state.Get("box").(*Box)
version := state.Get("version").(*Version)
upload := state.Get("upload").(*Upload)
provider := state.Get("provider").(*Provider)
path := fmt.Sprintf("box/%s/version/%v/provider/%s", box.Tag, version.Number, provider.Name)
providerCheck := &Provider{}
ui.Say(fmt.Sprintf("Verifying provider upload: %s", provider.Name))
done := make(chan struct{})
defer close(done)
result := make(chan error, 1)
go func() {
attempts := 0
for {
attempts += 1
log.Printf("Checking token match for provider.. (attempt: %d)", attempts)
resp, err := client.Get(path)
if err != nil || (resp.StatusCode != 200) {
cloudErrors := &VagrantCloudErrors{}
err = decodeBody(resp, cloudErrors)
err = fmt.Errorf("Error retrieving provider: %s", cloudErrors.FormatErrors())
result <- err
return
}
if err = decodeBody(resp, providerCheck); err != nil {
err = fmt.Errorf("Error parsing provider response: %s", err)
result <- err
return
}
if err != nil {
result <- err
return
}
if upload.Token == providerCheck.HostedToken {
// success!
result <- nil
return
}
// Wait 3 seconds in between
time.Sleep(3 * time.Second)
// Verify we shouldn't exit
select {
case <-done:
// We finished, so just exit the goroutine
return
default:
// Keep going
}
}
}()
log.Printf("Waiting for up to 600 seconds for provider hosted token to match %s", upload.Token)
select {
case err := <-result:
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
log.Printf("Box succesfully verified %s == %s", upload.Token, providerCheck.HostedToken)
return multistep.ActionContinue return multistep.ActionContinue
case <-time.After(600 * time.Second):
state.Put("error", fmt.Errorf("Timeout while waiting to for upload to verify token '%s'", upload.Token))
return multistep.ActionHalt
}
} }
func (s *stepVerifyUpload) Cleanup(state multistep.StateBag) { func (s *stepVerifyUpload) Cleanup(state multistep.StateBag) {
// No cleanup
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment