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 agoshell
, and we decided to useshell
it to encapsulatepodman api
the interface. This time we encapsulated three interfacesinfo
,_ping
, ,/_images/json
Why do you need podman api
Because podman
the 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 api
call directly podman
.
Why do we need api
it? 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 podman
the 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
podman
Can socket listen unix
andtcp
Use command: podman system service
openpodman api
parameter
- --time: session timeout, in seconds, the default is 5 seconds
- --log-level: set
Log
the level, this ispodman
the syntax, notapi
the - --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 debug
on interrupt display, and unix
socket 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 unix
will 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 podman
version information (this is not important), whether to use tcp
or socket
to connect podman api
, and podman api
the 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 curl
request
Display image information
To query images
information, we first calculate podman api
how 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 shell
in 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.sh
2.source xxx.sh
So what's the difference? Why do we use source
insteadsh
Because, sh
when we go to execute, we open one 子shell
. When the sub- 子shell
execution is completed, the data will not be imported into 父shell
it, but source
it will be executed in it 父shell
. This is why we modify the environment variables. re-executed~/.bashrc
.
source
comprehension
Although shell
writing 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
.