-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
command: init -short flag #4239
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,11 @@ Alias: nomad init | |
|
||
Creates an example job file that can be used as a starting | ||
point to customize further. | ||
|
||
Init Options: | ||
|
||
-short | ||
If the short flag is set, a minimal jobspec without comments is emitted. | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
@@ -37,8 +42,18 @@ func (c *JobInitCommand) Synopsis() string { | |
func (c *JobInitCommand) Name() string { return "job init" } | ||
|
||
func (c *JobInitCommand) Run(args []string) int { | ||
var short bool | ||
|
||
flags := c.Meta.FlagSet(c.Name(), FlagSetClient) | ||
flags.Usage = func() { c.Ui.Output(c.Help()) } | ||
flags.BoolVar(&short, "short", false, "") | ||
|
||
if err := flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
// Check for misuse | ||
if len(args) != 0 { | ||
if len(flags.Args()) != 0 { | ||
c.Ui.Error("This command takes no arguments") | ||
c.Ui.Error(commandErrorText(c)) | ||
return 1 | ||
|
@@ -55,8 +70,16 @@ func (c *JobInitCommand) Run(args []string) int { | |
return 1 | ||
} | ||
|
||
var jobSpec []byte | ||
|
||
if short { | ||
jobSpec = []byte(shortJob) | ||
} else { | ||
jobSpec = []byte(defaultJob) | ||
} | ||
|
||
// Write out the example | ||
err = ioutil.WriteFile(DefaultInitName, []byte(defaultJob), 0660) | ||
err = ioutil.WriteFile(DefaultInitName, jobSpec, 0660) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Failed to write '%s': %v", DefaultInitName, err)) | ||
return 1 | ||
|
@@ -67,6 +90,50 @@ func (c *JobInitCommand) Run(args []string) int { | |
return 0 | ||
} | ||
|
||
var shortJob = strings.TrimSpace(` | ||
job "example" { | ||
datacenters = ["dc1"] | ||
|
||
group "cache" { | ||
ephemeral_disk { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the ephemeral disk too |
||
size = 300 | ||
} | ||
|
||
task "redis" { | ||
driver = "docker" | ||
|
||
config { | ||
image = "redis:3.2" | ||
port_map { | ||
db = 6379 | ||
} | ||
} | ||
|
||
resources { | ||
cpu = 500 # 500 MHz | ||
memory = 256 # 256MB | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove these comments to. Just about every job file I see still has them and they're almost always wrong because people tweak the resources without touching the comments 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should leave the units in, but remove the numbers. |
||
network { | ||
mbits = 10 | ||
port "db" {} | ||
} | ||
} | ||
|
||
service { | ||
name = "redis-cache" | ||
tags = ["global", "cache"] | ||
port = "db" | ||
check { | ||
name = "alive" | ||
type = "tcp" | ||
interval = "10s" | ||
timeout = "2s" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
|
||
var defaultJob = strings.TrimSpace(` | ||
# There can only be a single job definition per file. This job is named | ||
# "example" so it will create a job with the ID and Name "example". | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add to the website: https://www.nomadproject.io/docs/commands/job/init.html