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
|
2014-08-01 13:31:10 -04:00
|
|
|
// to Vagrant Cloud (vagrantcloud.com) or manages self hosted boxes on the
|
|
|
|
// Vagrant Cloud
|
2014-06-16 15:53:37 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-27 17:56:22 -04:00
|
|
|
"log"
|
2017-06-07 13:40:31 -04:00
|
|
|
"os"
|
2015-05-27 17:56:22 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2014-06-23 15:48:51 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
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"`
|
|
|
|
|
2014-08-01 13:31:10 -04:00
|
|
|
BoxDownloadUrl string `mapstructure:"box_download_url"`
|
|
|
|
|
2015-05-27 17:56:22 -04:00
|
|
|
ctx interpolate.Context
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
2014-08-01 13:31:10 -04:00
|
|
|
type boxDownloadUrlTemplate struct {
|
|
|
|
ArtifactId string
|
|
|
|
Provider string
|
|
|
|
}
|
|
|
|
|
2014-06-16 15:53:37 -04:00
|
|
|
type PostProcessor struct {
|
2017-06-07 13:40:31 -04:00
|
|
|
config Config
|
|
|
|
client *VagrantCloudClient
|
|
|
|
runner multistep.Runner
|
|
|
|
warnAtlasToken bool
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
2015-05-27 17:56:22 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2015-06-22 15:24:27 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-05-27 17:56:22 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"box_download_url",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2014-06-16 15:53:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default configuration
|
|
|
|
if p.config.VagrantCloudUrl == "" {
|
|
|
|
p.config.VagrantCloudUrl = VAGRANT_CLOUD_URL
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:40:31 -04:00
|
|
|
if p.config.AccessToken == "" {
|
|
|
|
envToken := os.Getenv("VAGRANT_CLOUD_TOKEN")
|
|
|
|
if envToken == "" {
|
|
|
|
envToken = os.Getenv("ATLAS_TOKEN")
|
|
|
|
if envToken != "" {
|
|
|
|
p.warnAtlasToken = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.config.AccessToken = envToken
|
|
|
|
}
|
|
|
|
|
2014-06-16 15:53:37 -04:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:40:31 -04:00
|
|
|
if p.warnAtlasToken {
|
|
|
|
ui.Message("Warning: Using Vagrant Cloud token found in ATLAS_TOKEN. Please make sure it is correct, or set VAGRANT_CLOUD_TOKEN")
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
|
2015-05-27 17:56:22 -04:00
|
|
|
p.config.ctx.Data = &boxDownloadUrlTemplate{
|
2014-08-01 13:31:10 -04:00
|
|
|
ArtifactId: artifact.Id(),
|
|
|
|
Provider: providerName,
|
2015-05-27 17:56:22 -04:00
|
|
|
}
|
|
|
|
boxDownloadUrl, err := interpolate.Render(p.config.BoxDownloadUrl, &p.config.ctx)
|
2014-08-01 13:31:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("Error processing box_download_url: %s", err)
|
|
|
|
}
|
|
|
|
|
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-08-01 13:31:10 -04:00
|
|
|
state.Put("boxDownloadUrl", boxDownloadUrl)
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
// Build the steps
|
2014-08-01 13:31:10 -04:00
|
|
|
steps := []multistep.Step{}
|
|
|
|
if p.config.BoxDownloadUrl == "" {
|
|
|
|
steps = []multistep.Step{
|
|
|
|
new(stepVerifyBox),
|
|
|
|
new(stepCreateVersion),
|
|
|
|
new(stepCreateProvider),
|
|
|
|
new(stepPrepareUpload),
|
|
|
|
new(stepUpload),
|
|
|
|
new(stepReleaseVersion),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
steps = []multistep.Step{
|
|
|
|
new(stepVerifyBox),
|
|
|
|
new(stepCreateVersion),
|
|
|
|
new(stepCreateProvider),
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|