Why there is an error when uploading a file

749 Views Asked by At
{
    'error': True, 
    'apiversion': 35, 
    'errorCode': 'api.error.job-upload.invalid', 
    'message': 'Job does not have file options’
}
1

There are 1 best solutions below

0
On BEST ANSWER

You need an option defined as a file type instead of a "text" type option. I leave a job definition example (check the "type" attribute on "option" section):

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='file1' type='file' />
      </options>
    </context>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <id>36eac4c7-a421-43b8-9b71-2b07530912ec</id>
    <loglevel>INFO</loglevel>
    <name>Example</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <exec>cat ${file.file1}</exec>
      </command>
    </sequence>
    <uuid>36eac4c7-a421-43b8-9b71-2b07530912ec</uuid>
  </job>
</joblist>

Following the API documentation, you need to "load" the file first and later run the job with the file id on your file option. I have used jq tool to extract the file id to pass later:

#!/bin/sh

# first, load the file and store the id on $fileid bash variable (the file is loaded on --data-binary section).
fileid=$(curl -s --location --request POST 'http://localhost:4440/api/36/job/36eac4c7-a421-43b8-9b71-2b07530912ec/input/file?optionName=file1&fileName=file.txt' \
--header 'Accept: application/json' \
--header 'X-Rundeck-Auth-Token: UkGOtMIUWjpNGcEsza9KoDZ94A9hRGKV' \
--header 'Content-Type: octet/stream' \
 --data-binary "@/path/to/your/file" | jq -r '.options[]')

# now run the job passing the id saved at $fileid variable (check the argString section).
curl -s --location --request POST "http://localhost:4440/api/36/job/36eac4c7-a421-43b8-9b71-2b07530912ec/run" \
  --header "Accept: application/json" \
  --header "X-Rundeck-Auth-Token: UkGOtMIUWjpNGcEsza9KoDZ94A9hRGKV" \
  --header "Content-Type: application/json" \
  --data "{ \"argString\":\"-file1 $fileid \" }" | jq

The output is (using jq too to "beautify"):

{
  "id": 28,
  "href": "http://localhost:4440/api/36/execution/28",
  "permalink": "http://localhost:4440/project/ProjectEXAMPLE/execution/show/28",
  "status": "running",
  "project": "ProjectEXAMPLE",
  "executionType": "user",
  "user": "admin",
  "date-started": {
    "unixtime": 1603198748079,
    "date": "2020-10-20T12:59:08Z"
  },
  "job": {
    "id": "36eac4c7-a421-43b8-9b71-2b07530912ec",
    "averageDuration": 354,
    "name": "Example",
    "group": "",
    "project": "ProjectEXAMPLE",
    "description": "",
    "options": {
      "file1": "50bb60fc-7b4d-4322-b00f-7ee4f79f1502"
    },
    "href": "http://localhost:4440/api/36/job/36eac4c7-a421-43b8-9b71-2b07530912ec",
    "permalink": "http://localhost:4440/project/ProjectEXAMPLE/job/show/36eac4c7-a421-43b8-9b71-2b07530912ec"
  },
  "description": "cat ${file.file1}",
  "argstring": "-file1 50bb60fc-7b4d-4322-b00f-7ee4f79f1502",
  "serverUUID": "d547091d-acf3-4437-bbba-2b093612a813"
}

And now the job can print the file content (it's just an example).

Another option is to do the same process using RD CLI tool, easiest I think.