2014-06-16 15:53:37 -04:00
|
|
|
// vagrant_cloud implements the packer.PostProcessor interface and adds a
|
|
|
|
// post-processor that uploads artifacts from the vagrant post-processor
|
|
|
|
// to Vagrant Cloud (vagrantcloud.com)
|
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-23 15:48:51 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2014-06-16 15:53:37 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2014-06-23 15:48:51 -04:00
|
|
|
"log"
|
2014-06-24 15:58:45 -04:00
|
|
|
"strings"
|
2014-06-16 15:53:37 -04:00
|
|
|
)
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
const VAGRANT_CLOUD_URL = "https://vagrantcloud.com/api/v1"
|
2014-06-16 15:53:37 -04:00
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2014-06-25 10:32:17 -04:00
|
|
|
Tag string `mapstructure:"box_tag"`
|
|
|
|
Version string `mapstructure:"version"`
|
|
|
|
VersionDescription string `mapstructure:"version_description"`
|
|
|
|
NoRelease bool `mapstructure:"no_release"`
|
2014-06-16 15:53:37 -04:00
|
|
|
|
|
|
|
AccessToken string `mapstructure:"access_token"`
|
|
|
|
VagrantCloudUrl string `mapstructure:"vagrant_cloud_url"`
|
|
|
|
|
|
|
|
tpl *packer.ConfigTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
|
|
|
config Config
|
|
|
|
client *VagrantCloudClient
|
2014-06-23 15:48:51 -04:00
|
|
|
runner multistep.Runner
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
_, err := common.DecodeConfig(&p.config, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.config.tpl, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.config.tpl.UserVars = p.config.PackerUserVars
|
|
|
|
|
|
|
|
// Default configuration
|
|
|
|
if p.config.VagrantCloudUrl == "" {
|
|
|
|
p.config.VagrantCloudUrl = VAGRANT_CLOUD_URL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate any errors
|
|
|
|
errs := new(packer.MultiError)
|
|
|
|
|
|
|
|
// required configuration
|
|
|
|
templates := map[string]*string{
|
|
|
|
"box_tag": &p.config.Tag,
|
|
|
|
"version": &p.config.Version,
|
|
|
|
"access_token": &p.config.AccessToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, ptr := range templates {
|
|
|
|
if *ptr == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("%s must be set", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Template process
|
|
|
|
for key, ptr := range templates {
|
|
|
|
*ptr, err = p.config.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", key, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
|
|
|
// Only accepts input from the vagrant post-processor
|
|
|
|
if artifact.BuilderId() != "mitchellh.post-processor.vagrant" {
|
|
|
|
return nil, false, fmt.Errorf(
|
|
|
|
"Unknown artifact type, requires box from vagrant post-processor: %s", artifact.BuilderId())
|
|
|
|
}
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2014-06-16 15:53:37 -04:00
|
|
|
// create the HTTP client
|
|
|
|
p.client = VagrantCloudClient{}.New(p.config.VagrantCloudUrl, p.config.AccessToken)
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
// The name of the provider for vagrant cloud, and vagrant
|
|
|
|
providerName := providerFromBuilderName(artifact.Id())
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
// Set up the state
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", p.config)
|
|
|
|
state.Put("client", p.client)
|
|
|
|
state.Put("artifact", artifact)
|
2014-06-24 15:58:45 -04:00
|
|
|
state.Put("artifactFilePath", artifact.Files()[0])
|
2014-06-23 15:48:51 -04:00
|
|
|
state.Put("ui", ui)
|
2014-06-24 15:58:45 -04:00
|
|
|
state.Put("providerName", providerName)
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
|
|
|
new(stepVerifyBox),
|
|
|
|
new(stepCreateVersion),
|
|
|
|
new(stepCreateProvider),
|
|
|
|
new(stepPrepareUpload),
|
|
|
|
new(stepUpload),
|
|
|
|
new(stepVerifyUpload),
|
2014-06-24 15:58:45 -04:00
|
|
|
new(stepReleaseVersion),
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
// Run the steps
|
|
|
|
if p.config.PackerDebug {
|
|
|
|
p.runner = &multistep.DebugRunner{
|
|
|
|
Steps: steps,
|
|
|
|
PauseFn: common.MultistepDebugFn(ui),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.runner = &multistep.BasicRunner{Steps: steps}
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
p.runner.Run(state)
|
2014-06-20 11:20:27 -04:00
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, false, rawErr.(error)
|
2014-06-20 11:20:27 -04:00
|
|
|
}
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
return NewArtifact(providerName, p.config.Tag), true, nil
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs a cleanup if the post processor fails to upload
|
2014-06-23 15:48:51 -04:00
|
|
|
func (p *PostProcessor) Cancel() {
|
|
|
|
if p.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
p.runner.Cancel()
|
|
|
|
}
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// converts a packer builder name to the corresponding vagrant
|
|
|
|
// provider
|
|
|
|
func providerFromBuilderName(name string) string {
|
|
|
|
switch name {
|
|
|
|
case "aws":
|
|
|
|
return "aws"
|
|
|
|
case "digitalocean":
|
|
|
|
return "digitalocean"
|
|
|
|
case "virtualbox":
|
|
|
|
return "virtualbox"
|
|
|
|
case "vmware":
|
|
|
|
return "vmware_desktop"
|
|
|
|
case "parallels":
|
|
|
|
return "parallels"
|
|
|
|
default:
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|