nomad icon indicating copy to clipboard operation
nomad copied to clipboard

create/update job with consul KV using API

Open HaCStudio opened this issue 6 years ago • 2 comments

Hello, I'm trying to use Nomad's API to update a running job. This job has a KV from Consul and when I try to send it, the API denies it due to malformed JSON. It happens because the key name has to be surrounded by double-quotes, which makes it an invalid JSON. I'm using PUT on /v1/jobs (I tried also POST to /v1/job/ and got the same result) This is the JSON string I'm using:

{ "Job": { "Stop": null, "Region": null, "Namespace": null, "ID": "Notepad", "ParentID": null, "Name": "Notepad", "Type": "service", "Priority": null, "AllAtOnce": null, "Datacenters": [ "dc1" ], "Constraints": null, "Affinities": null, "TaskGroups": [ { "Name": "Notepad", "Count": 1, "Constraints": null, "Affinities": null, "Tasks": [ { "Name": "Notepad", "Driver": "raw_exec", "User": "", "Config": { "args": [ "-ignore", "-mode", "w", "-dontcheckstoptitle" ], "command": "${env["START_PATH"]}\\Notepad.exe" }, "Constraints": null, "Affinities": null, "Env": null, "Services": null, "Resources": { "CPU": 20, "MemoryMB": 10, "DiskMB": null, "Networks": null, "Devices": null, "IOPS": null }, "Meta": null, "KillTimeout": null, "LogConfig": null, "Artifacts": null, "Vault": null, "Templates": [ { "SourcePath": null, "DestPath": "secrets/file.env", "EmbeddedTmpl": "\t\t\t\t# Lines starting with a # are ignored\r\n\r\n\t\t\t\t# Empty lines are also ignored\r\n\t\t\t\tSTART_PATH={{ key "APP_ROOT_DIR" }}\r\n\t\t\t", "ChangeMode": "restart", "ChangeSignal": null, "Splay": 5000000000, "Perms": "0644", "LeftDelim": null, "RightDelim": null, "Envvars": true, "VaultGrace": null } ], "DispatchPayload": null, "VolumeMounts": null, "Leader": false, "ShutdownDelay": 0, "KillSignal": "", "Kind": "" } ], "Spreads": null, "Volumes": null, "RestartPolicy": { "Interval": 86400000000000, "Attempts": 1440, "Delay": 15000000000, "Mode": "delay" }, "ReschedulePolicy": null, "EphemeralDisk": { "Sticky": null, "Migrate": null, "SizeMB": 101 }, "Update": null, "Migrate": null, "Networks": null, "Meta": null, "Services": null, "ShutdownDelay": null } ], "Update": null, "Spreads": [ { "Attribute": "${node.unique.name}", "Weight": 100, "SpreadTarget": null } ], "Periodic": null, "ParameterizedJob": null, "Dispatched": false, "Payload": null, "Reschedule": null, "Migrate": null, "Meta": null, "ConsulToken": null, "VaultToken": null, "Status": null, "StatusDescription": null, "Stable": null, "Version": null, "SubmitTime": null, "CreateIndex": null, "ModifyIndex": null, "JobModifyIndex": null } }

This is the response I get (the S character comes from "START_PATH")

invalid character 'S' after object key:value pair

Nomad version is v0.10.4 (f750636ca68e17dcd2445c1ab9c5a34f9ac69345)

Thank you for your help.

HaCStudio avatar Mar 08 '20 17:03 HaCStudio

Hi! Thanks for reaching out. As you pointed out, it's invalid to include double-quotes inside a json string without escaping. You would need to escape the inner double-quotation with \:

$ echo '{"key": "env["START_PATH"]"}' | jq .
parse error: Invalid numeric literal at line 1, column 25
$ echo '{"key": "env[\"START_PATH\"]"}' | jq .
{
  "key": "env[\"START_PATH\"]"
}

notnoop avatar Mar 08 '20 22:03 notnoop

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

github-actions[bot] avatar Nov 12 '22 02:11 github-actions[bot]