Shell call to podman REST API - 1

Get into the habit of writing together! This is the 13th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the event details .

After going around, we came back and continued to read podman api. We reviewed it about two days ago shell, and we decided to use shellit to encapsulate podman apithe interface. This time we encapsulated three interfaces info, _ping, ,/_images/json

Why do you need podman api

Because podmanthe wheel is built, it is convenient for operation and maintenance/developers to call the interface. In short, the interface is provided to avoid us from building the wheel. For this, we can use the apicall directly podman.

Why do we need apiit? Suppose we are doing an automated deployment, during which we want to start a container. If not api, how should we do it? We will call podmanthe command directly, right, and then judge whether it is successful or not based on the output, which is cumbersome and troublesome to migrate (for example, the machine executes the command path is inconsistent), so if necessary api, let's take a look at the specific usage

Start the podman api service

podmanCan socket listen unixandtcp

Use command: podman system serviceopenpodman api

parameter

  • --time: session timeout, in seconds, the default is 5 seconds
  • --log-level: set Logthe level, this is podmanthe syntax, not apithe
  • --cors: Set CORS Headers (mainly used across domains)

open unix socket

Order:podman --log-level=debug system service -t0 unix:///tmp/podman.sock

The meaning of this command is, open debugon interrupt display, and unixsocket file in/tmp/podman.sock

Open TCP socket

Order:podman --log-level=debug system service -t0 tcp:127.0.0.1:8888

Similar to the above, note that we unixwill replace thetcp

如上,我们启动了podman api unix套接字 和 tcp套接字

请求方法

假设我们使用curl请求获取版本信息

TCP方式请求版本信息

格式: /版本/libpod/info 其中版本信息只要是v开头

命令: curl -s http://127.0.0.1:8881/v3.0.0/libpod/info | jq '.version'

jq是linux格式化json工具

unix套接字方式请求版本信息

格式: d/版本/libpod/info 其中版本信息只要是v开头 ,只不过需要指定unix套接字文件

命令: curl -s --unix-socket /tmp/podman.sock http://d/v3.0.0/libpod/info | jq '.version'

podman 常规请求

请求方法

  • /info: 获取podman api信息
  • /_ping: 检测podman api状态
  • /_images/json: 获取podman images信息

检测podman状态

使用路由_ping可以检测podman状态,

例如

返回OK则代表podman正常的

获取podman api信息

使用理由/info可以获取podman api信息

例如

使用jq格式化为json

例如使用命令: curl -s http://127.0.0.1:8881/v3.0.0/info | jq

获取images信息

使用理由images/json可以获取podman images信息

例如

使用命令: curl -s http://127.0.0.1:8881/v3.0.0/images/json | jq

使用shell封装podman api

脚本运行之后效果

我们通过bash shell的方式,封装了podman api之后运行的效果

我们再来查看下podman images看看是否和上面对得上

脚本列表

我们将脚本按照模块给分开了,以方便解释脚本含义

  • main.sh脚本有有个函数为 main 是该程序的入口
  • podmanEvn.sh该脚本的一些配置变量
  • checkHealth.sh检查podman api是否存活
  • showVersion.sh显示出podman版本信息
  • images.sh 镜像操作,目前仅编写了列出镜像

配置变量

我们来看podmanEvn.sh的内容

All are defined as read-only variables, the wrong podmanversion information (this is not important), whether to use tcpor socketto connect podman api, and podman apithe routing information provided

Check if podman api is alive

The function is as follows, if the request result is empty, or the string is not "OK", it will return 2 (abnormal), otherwise it will return 0 (normal)

Display podman version information

This can be used directly with the curlrequest

Display image information

To query imagesinformation, we first calculate podman apihow many records are returned, and then loop to get the information of each part, including name, id, creation time, and size

main function

Because we want to use the functions of other files, we need to introduce them. We generally execute shellin the following ways:

  • sh xxx.sh
  • ./ xxx.sh
  • . xxx.sh
  • source xxx.sh

In fact, the above implementation plans can be summarized into two types, 1. sh xxx.sh2.source xxx.sh

So what's the difference? Why do we use sourceinsteadsh

Because, shwhen we go to execute, we open one 子shell. When the sub- 子shellexecution is completed, the data will not be imported into 父shellit, but sourceit will be executed in it 父shell. This is why we modify the environment variables. re-executed~/.bashrc.source

comprehension

Although shellwriting a script itself is not difficult, and its core is only a few lines of code, but for the robustness of the script, it is not so easy to collapse, we need to write a large number of auxiliary statements to judge whether the statement is successfully executed and so on. What to do after failure, etc. We will add others later api.

おすすめ

転載: juejin.im/post/7086028608732397598