Skip to content

Commit

Permalink
cli: Allow user to specify dest filename for nomad init (#6520)
Browse files Browse the repository at this point in the history
* Allow user to specify dest filename for nomad init
* Create changelog entry for GH-6520
  • Loading branch information
angrycub committed Dec 19, 2019
1 parent c47076d commit 0ff7c64
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FEATURES:

IMPROVEMENTS:

* cli: Added option to change the name of the file created by the `nomad init` command [[GH-6520]](https://github.com/hashicorp/nomad/pull/6520)
* scheduler: Removed penalty for allocation's previous node if the allocation did not fail. [[GH-6781](https://github.com/hashicorp/nomad/issues/6781)]
* scheduler: Reduced logging verbosity during preemption [[GH-6849](https://github.com/hashicorp/nomad/issues/6849)]

Expand Down
31 changes: 19 additions & 12 deletions command/job_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type JobInitCommand struct {

func (c *JobInitCommand) Help() string {
helpText := `
Usage: nomad job init
Alias: nomad init
Usage: nomad job init <filename>
Alias: nomad init <filename>
Creates an example job file that can be used as a starting
point to customize further.
Creates an example job file that can be used as a starting point to customize
further. If no filename is given, the default of "example.nomad" will be used.
Init Options:
Expand Down Expand Up @@ -71,20 +71,27 @@ func (c *JobInitCommand) Run(args []string) int {
}

// Check for misuse
if len(flags.Args()) != 0 {
c.Ui.Error("This command takes no arguments")
// Check that we either got no filename or exactly one.
args = flags.Args()
if len(args) > 1 {
c.Ui.Error("This command takes either no arguments or one: <filename>")
c.Ui.Error(commandErrorText(c))
return 1
}

filename := DefaultInitName
if len(args) == 1 {
filename = args[0]
}

// Check if the file already exists
_, err := os.Stat(DefaultInitName)
_, err := os.Stat(filename)
if err != nil && !os.IsNotExist(err) {
c.Ui.Error(fmt.Sprintf("Failed to stat '%s': %v", DefaultInitName, err))
c.Ui.Error(fmt.Sprintf("Failed to stat '%s': %v", filename, err))
return 1
}
if !os.IsNotExist(err) {
c.Ui.Error(fmt.Sprintf("Job '%s' already exists", DefaultInitName))
c.Ui.Error(fmt.Sprintf("Job '%s' already exists", filename))
return 1
}

Expand All @@ -107,13 +114,13 @@ func (c *JobInitCommand) Run(args []string) int {
}

// Write out the example
err = ioutil.WriteFile(DefaultInitName, jobSpec, 0660)
err = ioutil.WriteFile(filename, jobSpec, 0660)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to write '%s': %v", DefaultInitName, err))
c.Ui.Error(fmt.Sprintf("Failed to write '%s': %v", filename, err))
return 1
}

// Success
c.Ui.Output(fmt.Sprintf("Example job file written to %s", DefaultInitName))
c.Ui.Output(fmt.Sprintf("Example job file written to %s", filename))
return 0
}
55 changes: 55 additions & 0 deletions command/job_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,58 @@ func TestInitCommand_defaultJob(t *testing.T) {
t.Error("default job contains tab character - please convert to spaces")
}
}

func TestInitCommand_customFilename(t *testing.T) {
t.Parallel()
ui := new(cli.MockUi)
cmd := &JobInitCommand{Meta: Meta{Ui: ui}}
filename := "custom.nomad"

// Ensure we change the cwd back
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(origDir)

// Create a temp dir and change into it
dir, err := ioutil.TempDir("", "nomad")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(dir)
if err := os.Chdir(dir); err != nil {
t.Fatalf("err: %s", err)
}

// Works if the file doesn't exist
if code := cmd.Run([]string{filename}); code != 0 {
t.Fatalf("expect exit code 0, got: %d", code)
}
content, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("err: %s", err)
}
defaultJob, _ := Asset("command/assets/example.nomad")
if string(content) != string(defaultJob) {
t.Fatalf("unexpected file content\n\n%s", string(content))
}

// Works with -short flag
os.Remove(filename)
if code := cmd.Run([]string{"-short", filename}); code != 0 {
require.Zero(t, code, "unexpected exit code: %d", code)
}
content, err = ioutil.ReadFile(filename)
require.NoError(t, err)
shortJob, _ := Asset("command/assets/example-short.nomad")
require.Equal(t, string(content), string(shortJob))

// Fails if the file exists
if code := cmd.Run([]string{filename}); code != 1 {
t.Fatalf("expect exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "exists") {
t.Fatalf("expect file exists error, got: %s", out)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ The `job init` command creates an example [job specification][jobspec] in the
current directory that demonstrates some common configurations for tasks, task
groups, runtime constraints, and resource allocation.

Please refer to the [jobspec] and [drivers] pages to learn how to customize the
template.

## Usage

```plaintext
nomad job init [options]
nomad job init [options] [filename]
```

You may optionally supply a filename for the example job to be written to. The
default filename for the generated file is "example.nomad".

Please refer to the [jobspec] and [drivers] pages to learn how to customize the
template.

## Init Options

- `-short`: If set, a minimal jobspec without comments is emitted.
Expand All @@ -38,4 +41,4 @@ Example job file written to example.nomad
```

[jobspec]: /docs/job-specification/index.html "Nomad Job Specification"
[drivers]: /docs/drivers/index.html
[drivers]: /docs/drivers/index.html "Nomad Task Drivers documentation"

0 comments on commit 0ff7c64

Please sign in to comment.