首页 > 解决方案 > 为什么上传文件时出错

问题描述

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

标签: rundeck

解决方案


您需要将选项定义为文件类型而不是“文本”类型选项。我留下一个工作定义示例(检查“选项”部分的“类型”属性):

<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>

按照 API文档,您需要先“加载”文件,然后使用文件选项上的文件 ID 运行作业。我已经使用jq 工具提取文件 id 以便稍后传递:

#!/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

输出是(也使用 jq 来“美化”):

{
  "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"
}

现在作业可以打印文件内容(这只是一个示例)。

另一种选择是使用RD CLI工具执行相同的过程,我认为这是最简单的。


推荐阅读