Skip to content
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

Support nomad CLI output with JSON and template format #1503

Merged
merged 8 commits into from
Aug 9, 2016
Merged

Support nomad CLI output with JSON and template format #1503

merged 8 commits into from
Aug 9, 2016

Conversation

nak3
Copy link
Contributor

@nak3 nak3 commented Aug 2, 2016

No description provided.

@nak3
Copy link
Contributor Author

nak3 commented Aug 2, 2016

For example...

e.g. JSON output (same with HTTP API access and -format json status <JOB> is same with inspect)

$ nomad eval-status -format json a63bbfde
{
    "ID": "a63bbfde-088c-4256-2a8b-bed149e20134",
    "Priority": 50,
    "Type": "service",
    "TriggeredBy": "job-register",
    "JobID": "example",
    "JobModifyIndex": 6,
    "NodeID": "",
    "NodeModifyIndex": 0,
    "Status": "complete",
    "StatusDescription": "",
    "Wait": 0,
    "NextEval": "",
    "PreviousEval": "",
    "BlockedEval": "",
    "FailedTGAllocs": null,
    "CreateIndex": 7,
    "ModifyIndex": 9
}

e.g. template output

$ nomad node-status -format template -t "{{.Attributes.arch}}"  8267c455
amd64

@dadgar
Copy link
Contributor

dadgar commented Aug 3, 2016

@nak3 can you look into the panics? If you give an invalid template it panics.

Also I wonder if it is better to have a -json option and -t which automatically sets the format to template. I don't think we gain much with the format flag because when would you use format template and not provide the -t?

@nak3
Copy link
Contributor Author

nak3 commented Aug 4, 2016

Thank you. I updated. If they are alright, I will squash the commits.

@dadgar
Copy link
Contributor

dadgar commented Aug 4, 2016

Yeah that looks good. Okay so some comments. It would be nice if this could work on arrays of elements (aka nomad node-status -t '{{range .}} {{.Attributes}} {{end}}. It should be possible!

These all make sense to me except nomad status since that command outputs quite a bit (job_summary, evaluations, allocations, etc). So maybe we remove the -json and -t flag from there. Would like your feedback.

Once we settle on what we are doing, please add docs to the website. And tests

@nak3
Copy link
Contributor Author

nak3 commented Aug 5, 2016

Yes, it is possible. You can get the info as below. (I'm sorry, node-status had my mistake and I updated.)

$ nomad node-status  -t '{{range .Attributes}} {{.}}{{end}}' 57b58140
 amd64 1425 Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz 4 5700 1 1.9.1 1 1 OpenJDK Runtime Environment (build 1.8.0_91-b14) openjdk version "1.8.0_91 OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode) 1 2.4.1 1 1 0.8.5 1.11.0 linux 4.4.8-300.fc23.x86_64 16512901120 'eac5d65fcc2fa5470af2d73f01c143dd28d1123c+CHANGES' 0.4.1dev fedora 23 /sys/fs/cgroup nak3.example.com 127.0.0.1 8166834176 8256450560 tmpfs

Agree about node status doesn't need the options. I removed them from node status. The options was implemented in alloc-status eval-status and node-status now.

I added the docs. But do we need tests for each commands? I already added command/data_format_test.go, though.

@dadgar
Copy link
Contributor

dadgar commented Aug 5, 2016

@nak3 I meant it would be nice if this worked without specify the individual node, alloc, or evaluation. If it could work on all of them! We should probably add this to nomad inspect as well.

I would add a test that if you pass the -json that -t can't be specified as well.

@dadgar
Copy link
Contributor

dadgar commented Aug 5, 2016

For first cut we can leave as is though without support for the lists of elements


-verbose
Show full information.

-json
Display information with json format.
Copy link
Contributor

@dadgar dadgar Aug 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output the allocation in its JSON format.

Similar for all the others and docs

@nak3
Copy link
Contributor Author

nak3 commented Aug 6, 2016

Thank you. (Sorry, I have already included the lists of elements.)

  • Support -json and -t optinos for list elements of alloc-status, eval-status, node-status and inspect. (Please see example below)
  • Support -json and -t options for indivisual alloc-status, eval-status, node-status
  • Add test to check -json and -t are not specified
  • Update documentation and help output

(e.g)

$ nomad alloc-status -t "{{range .}} {{.ID}} {{end}}"
 b0f97fea-7ec0-a03d-3dbb-f9a771a5d829  518a1a10-7f2f-fc8a-e6e4-1b070749b491  ffa14d59-9faf-a972-b218-5e0005ccd626  95203acf-2409-5c33-eb26-7eb7cd12f37d 

@dadgar
Copy link
Contributor

dadgar commented Aug 9, 2016

This is pretty awesome. Not sure if this is the best way to do it by this lets you do basic bash autocomplete:

$ echo complete.sh
allocs=`nomad alloc-status -t '{{range .}}{{.ID | printf "%s\n"}}{{end}}'`
COMREPLY=( $(compgen -W "${allocs[*]}" -- "${ENTRY}") )
echo $COMREPLY

nomad alloc-status ""
Prefix matched multiple allocations

ID        Eval ID   Job ID   Task Group  Desired Status  Client Status
3b5d8bb3  81ba9bd1  example  cache       run             running
3ecacf5e  81ba9bd1  example  cache       run             running
57d182d0  81ba9bd1  example  cache       run             running
700f01ac  81ba9bd1  example  cache       run             running
803b2685  81ba9bd1  example  cache       run             running
a32028f3  81ba9bd1  example  cache       run             running
bf001125  81ba9bd1  example  cache       run             running
c16540e1  81ba9bd1  example  cache       run             running
f7870dee  81ba9bd1  example  cache       run             running
fe4505da  81ba9bd1  example  cache       run             running

$ ENTRY=c1 ./complete.sh
c16540e1-1d31-ba13-6324-871c1ae35bda

We could definitely use this to build an autocomplete

t.Fatalf("expected output: %s, actual: %s", expectOutput[k], result)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test that you get an error with an invalid go template format

@nak3
Copy link
Contributor Author

nak3 commented Aug 9, 2016

Thank you @dadgar I updated.

@dadgar
Copy link
Contributor

dadgar commented Aug 9, 2016

Awesome work!

@dadgar dadgar merged commit 1ad8d72 into hashicorp:master Aug 9, 2016
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants