Commit 2f65b1bb authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

command/push: create the build config if we can

parent 2f607074
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
) )
// archiveTemplateEntry is the name the template always takes within the slug. // archiveTemplateEntry is the name the template always takes within the slug.
const archiveTemplateEntry = ".packer-template.json" const archiveTemplateEntry = ".packer-template"
type PushCommand struct { type PushCommand struct {
Meta Meta
...@@ -25,10 +25,12 @@ type PushCommand struct { ...@@ -25,10 +25,12 @@ type PushCommand struct {
} }
func (c *PushCommand) Run(args []string) int { func (c *PushCommand) Run(args []string) int {
var create bool
var token string var token string
f := flag.NewFlagSet("push", flag.ContinueOnError) f := flag.NewFlagSet("push", flag.ContinueOnError)
f.Usage = func() { c.Ui.Error(c.Help()) } f.Usage = func() { c.Ui.Error(c.Help()) }
f.BoolVar(&create, "create", false, "create")
f.StringVar(&token, "token", "", "token") f.StringVar(&token, "token", "", "token")
if err := f.Parse(args); err != nil { if err := f.Parse(args); err != nil {
return 1 return 1
...@@ -88,6 +90,12 @@ func (c *PushCommand) Run(args []string) int { ...@@ -88,6 +90,12 @@ func (c *PushCommand) Run(args []string) int {
uploadOpts.Builds[b.Name] = b.Type uploadOpts.Builds[b.Name] = b.Type
} }
// Create the build config if it doesn't currently exist.
if err := c.create(uploadOpts.Slug, create); err != nil {
c.Ui.Error(err.Error())
return 1
}
// Start the archiving process // Start the archiving process
r, archiveErrCh, err := archive.Archive(path, &opts) r, archiveErrCh, err := archive.Archive(path, &opts)
if err != nil { if err != nil {
...@@ -128,8 +136,13 @@ Usage: packer push [options] TEMPLATE ...@@ -128,8 +136,13 @@ Usage: packer push [options] TEMPLATE
This will not initiate any builds, it will only update the templates This will not initiate any builds, it will only update the templates
used for builds. used for builds.
The configuration about what is pushed is configured within the
template's "push" section.
Options: Options:
-create Create the build configuration if it doesn't exist.
-token=<token> Access token to use to upload. If blank, the -token=<token> Access token to use to upload. If blank, the
TODO environmental variable will be used. TODO environmental variable will be used.
` `
...@@ -141,6 +154,39 @@ func (*PushCommand) Synopsis() string { ...@@ -141,6 +154,39 @@ func (*PushCommand) Synopsis() string {
return "push template files to a Packer build service" return "push template files to a Packer build service"
} }
func (c *PushCommand) create(name string, create bool) error {
if c.uploadFn != nil {
return nil
}
// Separate the slug into the user and name components
user, name, err := harmony.ParseSlug(name)
if err != nil {
return fmt.Errorf("Malformed push name: %s", err)
}
// Check if it exists. If so, we're done.
if _, err := c.client.BuildConfig(user, name); err == nil {
return nil
} else if err != harmony.ErrNotFound {
return err
}
// Otherwise, show an error if we're not creating.
if !create {
return fmt.Errorf(
"Push target doesn't exist: %s. Either create this online via\n" +
"the website or pass the -create flag.")
}
// Create it
if err := c.client.CreateBuildConfig(user, name); err != nil {
return err
}
return nil
}
func (c *PushCommand) upload( func (c *PushCommand) upload(
r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
if c.uploadFn != nil { if c.uploadFn != nil {
......
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