2015-09-21 00:06:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-23 00:06:23 +00:00
|
|
|
"io/ioutil"
|
2015-09-21 00:06:02 +00:00
|
|
|
"os"
|
2015-09-23 00:06:23 +00:00
|
|
|
"strings"
|
2018-05-11 03:19:08 +00:00
|
|
|
|
|
|
|
"github.com/posener/complete"
|
2015-09-23 00:06:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultInitName is the default name we use when
|
|
|
|
// initializing the example file
|
|
|
|
DefaultInitName = "example.nomad"
|
2015-09-21 00:06:02 +00:00
|
|
|
)
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
// JobInitCommand generates a new job template that you can customize to your
|
2015-09-21 00:06:02 +00:00
|
|
|
// liking, like vagrant init
|
2018-03-21 00:37:28 +00:00
|
|
|
type JobInitCommand struct {
|
2015-09-21 00:06:02 +00:00
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobInitCommand) Help() string {
|
2015-09-23 00:06:23 +00:00
|
|
|
helpText := `
|
2018-03-21 00:37:28 +00:00
|
|
|
Usage: nomad job init
|
2018-03-21 01:28:14 +00:00
|
|
|
Alias: nomad init
|
2015-09-23 00:06:23 +00:00
|
|
|
|
|
|
|
Creates an example job file that can be used as a starting
|
|
|
|
point to customize further.
|
2018-05-01 16:51:13 +00:00
|
|
|
|
|
|
|
Init Options:
|
|
|
|
|
|
|
|
-short
|
|
|
|
If the short flag is set, a minimal jobspec without comments is emitted.
|
2019-08-14 18:07:52 +00:00
|
|
|
|
|
|
|
-connect
|
|
|
|
If the connect flag is set, the jobspec includes Consul Connect integration.
|
2015-09-23 00:06:23 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
2015-09-21 00:06:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobInitCommand) Synopsis() string {
|
2015-09-23 00:06:23 +00:00
|
|
|
return "Create an example job file"
|
|
|
|
}
|
2015-09-21 00:06:02 +00:00
|
|
|
|
2018-05-11 03:19:08 +00:00
|
|
|
func (c *JobInitCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-short": complete.PredictNothing,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:19:16 +00:00
|
|
|
func (c *JobInitCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *JobInitCommand) Name() string { return "job init" }
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobInitCommand) Run(args []string) int {
|
2018-05-01 16:51:13 +00:00
|
|
|
var short bool
|
2019-08-14 18:07:52 +00:00
|
|
|
var connect bool
|
2018-05-01 16:51:13 +00:00
|
|
|
|
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&short, "short", false, "")
|
2019-08-14 18:07:52 +00:00
|
|
|
flags.BoolVar(&connect, "connect", false, "")
|
2018-05-01 16:51:13 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-09-30 21:21:50 +00:00
|
|
|
// Check for misuse
|
2018-05-01 16:51:13 +00:00
|
|
|
if len(flags.Args()) != 0 {
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error("This command takes no arguments")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
2015-09-30 21:21:50 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-09-23 00:06:23 +00:00
|
|
|
// Check if the file already exists
|
|
|
|
_, err := os.Stat(DefaultInitName)
|
2015-09-30 21:21:50 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2015-09-23 00:06:23 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to stat '%s': %v", DefaultInitName, err))
|
2015-09-21 00:06:02 +00:00
|
|
|
return 1
|
|
|
|
}
|
2015-09-30 21:21:50 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Job '%s' already exists", DefaultInitName))
|
|
|
|
return 1
|
|
|
|
}
|
2015-09-21 00:06:02 +00:00
|
|
|
|
2018-05-01 16:51:13 +00:00
|
|
|
var jobSpec []byte
|
2019-08-14 18:07:52 +00:00
|
|
|
switch {
|
|
|
|
case connect && !short:
|
|
|
|
jobSpec, err = Asset("command/assets/connect.nomad")
|
|
|
|
case connect && short:
|
|
|
|
jobSpec, err = Asset("command/assets/connect-short.nomad")
|
|
|
|
case !connect && short:
|
2019-08-14 14:23:33 +00:00
|
|
|
jobSpec, err = Asset("command/assets/example-short.nomad")
|
2019-08-14 18:07:52 +00:00
|
|
|
default:
|
2019-08-14 14:23:33 +00:00
|
|
|
jobSpec, err = Asset("command/assets/example.nomad")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// should never see this because we've precompiled the assets
|
|
|
|
// as part of `make generate-examples`
|
|
|
|
c.Ui.Error(fmt.Sprintf("Accessed non-existent asset: %s", err))
|
|
|
|
return 1
|
2018-05-01 16:51:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 00:06:23 +00:00
|
|
|
// Write out the example
|
2018-05-01 16:51:13 +00:00
|
|
|
err = ioutil.WriteFile(DefaultInitName, jobSpec, 0660)
|
2015-09-21 00:06:02 +00:00
|
|
|
if err != nil {
|
2015-09-23 00:06:23 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to write '%s': %v", DefaultInitName, err))
|
2015-09-21 00:06:02 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-09-23 00:06:23 +00:00
|
|
|
// Success
|
|
|
|
c.Ui.Output(fmt.Sprintf("Example job file written to %s", DefaultInitName))
|
2015-09-21 00:06:02 +00:00
|
|
|
return 0
|
|
|
|
}
|