mirror of https://github.com/hashicorp/packer
parent
c899051c9c
commit
26abac6999
@ -1,44 +0,0 @@
|
||||
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
|
||||
}
|
||||
@ -1,15 +1,86 @@
|
||||
package vagrantcloud
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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 {
|
||||
name string // the name of the provider
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +1,52 @@
|
||||
package vagrantcloud
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/multistep"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
type Upload struct {
|
||||
Token string `json:"token"`
|
||||
UploadPath string `json:"upload_path"`
|
||||
}
|
||||
|
||||
type stepPrepareUpload struct {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s *stepPrepareUpload) Cleanup(state multistep.StateBag) {
|
||||
// No cleanup
|
||||
}
|
||||
|
||||
@ -0,0 +1,36 @@
|
||||
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
|
||||
}
|
||||
@ -1,15 +1,33 @@
|
||||
package vagrantcloud
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/multistep"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
type stepUpload struct {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s *stepUpload) Cleanup(state multistep.StateBag) {
|
||||
// No cleanup
|
||||
}
|
||||
|
||||
@ -1,15 +1,100 @@
|
||||
package vagrantcloud
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/multistep"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type stepVerifyUpload struct {
|
||||
}
|
||||
|
||||
func (s *stepVerifyUpload) Run(state multistep.StateBag) multistep.StepAction {
|
||||
return multistep.ActionContinue
|
||||
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
|
||||
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) {
|
||||
// No cleanup
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue