SkyForm AIP的REST API服务#

系统简介#

SkyForm AIP提供了标准的RESTful Web服务aiprestd,以允许图形作业和Batch作业提交、作业信息查询、作业操作、查询和管理集群、 主机、、用户、队列等。用户既可以从集群内网,也可以通过公网访问aiprestd服务。 访问aiprestd服务的客户端主机可以是Linux也可以是Windows,不需要安装SkyForm AIP软件,只要支持标准HTTP协议。

SkyForm aiprestd服务是无状态的,不会在请求之间缓存或保存任何状态,任何请求都与SkyForm AIP调度器完全同步。

支持通过常用开源web网关,如NGINX等,让用户从公网访问aiprestd服务,确保数据安全。

图1展示了aiprestd的架构。

aiprestd架构

安装aiprestd#

安装#

aiprestd服务打包在SkyForm API安装包中。解压AIP的安装包,切换当前目录到解压包内的rest目录下, 运行安装命令:

cd rest
./installaiprestd

安装脚本安装aiprestd服务,并启动它。

配置aiprestd.yaml#

修改配置文件/opt/skyformai/etc/aiprestd.yaml

  • RESTful Web服务http端口,缺省8088:

    # Public web port for disabling https. The default is 8088
    # http_port: 8088
    
  • 启用SSL,缺省禁用:

    #-----------------------------------------------------------------
    # Enabling https. The default is 0 (disabled). Valid value is 1 or 0
    # ssl: 0
    #-----------------------------------------------------------------
    # Public web port for disabling https. The default is 8088
    # http_port: 8088
    #-----------------------------------------------------------------
    # Public web secure port for enabling https. The default is 8043
    # https_port: 8043
    #-----------------------------------------------------------------
    # Https certificate file path. If enable https, need to generate it
    # cert: /opt/cert/server.crt
    #-----------------------------------------------------------------
    # Https key file path. If enable https, need to generate it
    # key: /opt/cert/server.key
    
  • Token过期时间(分钟),缺省30分钟:

    # Token timeout value. Integer. Unit is minute. Default value is 30 min
    timeout: 300
    
  • 日志级别,缺省info:

    # Log level. Valid values are fatal, error, warn, info, debug.
    # Default value is info
    # log_level: info
    

管理aiprestd服务#

aiprestd服务使用Linux的systemd管理。

  • 检查aiprestd服务:

    systemctl status aiprestd
    
  • 查看aipresrd日志:

    more /opt/skyformai/log/aiprestd.log.$HOSTNAME
    
    journalctl -u aiprestd
    

REST接口#

本章提供几个REST接口示例,接口使用详情,请参考在线接口文档: http://$HOSTNAME:8088/aip/v9.2/swagger/index.html

../_images/rest2.png

认证#

/aip/v9.2/login 获得Token

  • 所有REST接口(除了认证接口/login和在线文档/swagger/index.html)调用都需要提供Token。

  • Token的默认过期时间为30分钟。Token过期时间指获得token后,一直没有任何操作的时间间隔。 如果有调用REST接口,Token过期时间会自动延期。如果Token已经过期, 需要重新调用/login获得新Token。

Request:

curl -s -X 'POST' \
 'http://$HOSTNAME:8088/aip/v9.2/login' \
 -H 'accept: application/json' \
 -H 'Content-Type: application/json' \
 -d '{"password": "password", "username": "cadmin"}'

Response:

{
  "msg": "Success",
  "data": {
    "token": {
      "token":
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiY2FkbWluIiwiaWF0IjoxNjQ4MTA4NjcxLCJpc3MiOiJTa3lGb3JtIEFJUCJ9.uXqBPRkcBcE0IenUZePwHsHmZTrTDv8_aYB_1Xw_8pY",
      "userName": "cadmin"
    }
  }
}

提交可视化作业#

/aip/v9.2/vis/submit

  • 请求header添加“Authorization: Bearer $token”,$token为/login接口返回的token字符串

  • 可视化作业提交成功后,返回作业号

Request:

curl -s -X 'POST' \
 'http://node1:8088/aip/v9.2/vis/submit' \
 -H 'accept: application/json' \\
 -H 'Authorization: Bearer
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiY2FkbWluIiwiaWF0IjoxNjQ4MTA4NjcxLCJpc3MiOiJTa3lGb3JtIEFJUCJ9.uXqBPRkcBcE0IenUZePwHsHmZTrTDv8_aYB_1Xw_8pY' \
 -H 'Content-Type: application/json' \
 -d '{"command": "/bin/firefox", "type": "vnc"}'

Response:

{
  "msg": "Success",
  "data": {
    "vis": {
      "jobId": 22456
    }
  }
}

查询可视化作业URL#

/aip/v9.2/vis/$job_id

  • 通过可视化作业提交返回的作业号,查询作业访问URL

  • 最终作业访问URL需要将2.5中部署的httpd服务地址和查询结果拼接,比如本示例最终作业 Web访问URL为: http://$hostname/novnc/vnc.html?autoconnect=true& host=10.211.59.111&port=42466&password=RQPxve1DcUztap5wtS3YyLltDO3kfwSX& resize=remote&quality=9&compression=5&exec=node1&sid=8

Request:

curl -s -X 'GET' \
 'http://node1:8088/aip/v9.2/vis/url/22456' \
 -H 'accept: application/json' \
 -H 'Authorization: Bearer
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiY2FkbWluIiwiaWF0IjoxNjQ4MTA4NjcxLCJpc3MiOiJTa3lGb3JtIEFJUCJ9.uXqBPRkcBcE0IenUZePwHsHmZTrTDv8_aYB_1Xw_8pY'

Response:

{
  "msg": "Success",
  "data": {
    "visurl": {
      "url": "/novnc/vnc.html?autoconnect=true&host=10.211.59.111&port=42466&password=RQPxve1DcUztap5wtS3YyLltDO3kfwSX&resize=remote&quality=9&compression=5&exec=node1&sid=8"
    }
  }
}

高可用方案#

由于REST API服务依赖于sqlite数据库/opt/skyformai/work/aiprestd.db。这个文件缺省存放在共享文件系统内。高可用方案使用多台主机上部署REST API服务,所以必须把这个sqlite数据库文件移到本地。

  1. 把sqlite数据库移到本地文件系统,并生成符号链:

    systemctl stop aiprestd
    rm -f /opt/skyformai/work/aiprestd.db
    ln -sf /var/run/aiprestd.db /opt/lsf/work/aiprestd.db
    
  2. 在集群的两到三台主机上部署REST API服务:

    cd rest
    ./installaiprestd
    
  3. 配置NGINX的一个新的端口转发,使用keepalive参数。例子/etc/nginx/conf.d/aiprest.conf:

    upstream aiprest {
       server 192.168.10.10:8088;
       server 192.168.10.11:8088;
       keepalive 32;
    }
    
    server {
       listen 18088;
       location / {
          proxy_pass http://aiprest;
          proxy_http_version 1.1;
          proxy_set_header Connection "";
       }
    }
    

    以上例子中,访问REST API的URL为: http://host_name:18088/aip/v9.2/