-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpodman_utils.sh
More file actions
executable file
·330 lines (293 loc) · 8.11 KB
/
podman_utils.sh
File metadata and controls
executable file
·330 lines (293 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env bash
podman.commit() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} CONTAINER_NAME [IMAGE_NAME]
Commit container to an image.
--help Show help";
return 0;
}
local container_name="${1:?"Container Name is required"}"
local uname="$(id --user --name)"
local suffix="${uname}"
local image_name=${2:-"img_${container_name}_${suffix}"}
#local image_id_file_path="$(pwd)/${image_name}.iidfile"
local image_id=$(podman commit \
--quiet \
--pause=true \
--author "${uname}" \
--format oci \
--squash \
"${container_name}" \
"${image_name}")
# --iidfile ${image_id_file_path} \
echo -e "${image_name}\t${image_id}"
}
podman.root.dir() {
[[ ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]}
Show container location for current context.
--help Show help";
return 0;
}
podman info | yq '.store.graphRoot'
}
podman.conts.manif.path() {
[[ ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]}
Show containers manifest path for current context.
--help Show help";
return 0;
}
echo "$(podman.root.dir)/overlay-containers/containers.json"
}
podman.conts.manif() {
[[ ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]}
Show containers manifest for current context.
--help Show help";
return 0;
}
echo "$(jq --raw-input 'fromjson' "$(podman.conts.manif.path)")"
}
podman.cont.manif() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} CONTAINER_NAME
Show container manifest for current context.
--help Show help";
return 0;
}
local container_name="${1:?"Container Name is required"}"
podman.conts.manif | jq --arg name "${container_name}" '.[] | select(.names[0] == $name)'
}
podman.cont.conf() {
local container_name="${1:?"Container Name is required"}"
podman container inspect "${container_name}" --format json | jq -r '.[0]'
# | jq -r '.[0].Mounts[]'
return $?
}
podman.cont.cmd() {
local container_name="${1:?"Container Name is required"}"
podman.cont.conf "${container_name}" | jq -r '.Config.Cmd[0]'
return $?
}
podman.cont.env() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} IMG_NAME
Show container internal environment for current context.
--help Show help";
return 0;
}
(($# > 1)) && { echo -e "Command accepts 1 argument"; return 0; }
local img_name="${1:?"Image Name is required"}"
podman.cont.conf "${img_name}" | jq --raw-output '.Config.Env[]'
}
podman.cont.id() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} CONTAINER_NAME
Show container ID for current context.
--help Show help";
return 0;
}
local container_name="${1:?"Container Name is required"}"
podman.cont.manif "${container_name}" | jq --raw-output '.id'
}
podman.cont.dir() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} CONTAINER_NAME
Show container location for current context.
--help Show help";
return 0;
}
local container_name="${1:?"Container Name is required"}"
echo "$(podman.root.dir)/overlay-containers/$(podman.cont.id "${container_name}")"
}
podman.imgs.ls() {
podman image ls --format '{{.Repository}}:{{.Tag}}'
return $?
}
podman.imgs.pullAll() {
podman.imgs.ls | xargs -n 1 podman pull
return $?
}
podman.imgs.manif.path() {
[[ ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]}
Show images manifest path for current context.
--help Show help";
return 0;
}
echo "$(podman.root.dir)/overlay-images/images.json"
}
podman.imgs.manif() {
[[ ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]}
Show images manifest for current context.
--help Show help";
return 0;
}
echo "$(jq --raw-input 'fromjson' "$(podman.imgs.manif.path)")"
}
podman.img.manif() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} IMG_NAME
Show image manifest for current context.
Params:
IMG_NAME image name WITH VERSION (e.g. host/image:latest)
Options:
--help Show help";
return 0;
}
local img_name="${1:?"Image Name is required"}"
podman.imgs.manif | jq --arg name "${img_name}" '.[] | select(.names[0] == $name)'
}
podman.img.id() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} IMG_NAME
Show image ID for current context.
Params:
IMG_NAME image name WITH VERSION (e.g. host/image:latest)
Options:
--help Show help";
return 0;
}
local img_name="${1:?"Image Name is required"}"
podman.img.manif "${img_name}" | jq --raw-output '.id'
}
podman.img.dir() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} IMG_NAME
Show image location for current context.
--help Show help";
return 0;
}
local image_name="${1:?"Image Name is required"}"
echo "$(podman.root.dir)/overlay-images/$(podman.img.id "${image_name}")"
}
# args.parse() {
# # What do I even put here???
# # It's probably supposed to somehow support at least --help option and print a helptext that uses a structured list of params and their descriptions, maybe in a JSON
# }
# podman.img.fs.ls() {
# # Supposed to just list files inside on an OCI container image
# read imagename lsopts <<< $(args.parse "imagename,lsopts" "$@")
# local mountpoint="$(podman image mount "${imagename}")"
# ls ${lsopts[*]} "${mountpoint}"
# # maybe should cleanup after istelf with `podman image unmount`?
# return $?
# }
podman.img.conf() {
local img_name="${1:?"Image Name is required"}"
podman image inspect "${img_name}" --format json | jq -r '.[0]'
return $?
}
podman.img.digest() {
podman.img.conf "$@" | jq -r ".Digest"
return $?
}
podman.img.sha() {
podman.img.digest "$@"
return $?
}
podman.img.cmd() {
local img_name="${1:?"Image Name is required"}"
podman.img.conf "${img_name}" | jq -r '.Config.Cmd[0]'
return $?
}
podman.img.entry() {
local img_name="${1:?"Image Name is required"}"
podman.img.conf "${img_name}" | jq -r '.Config.Entrypoint | join(" ")'
return $?
}
podman.img.env() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} IMG_NAME
Show image internal environment for current context.
--help Show help";
return 0;
}
(($# > 1)) && { echo -e "Command accepts 1 argument"; return 0; }
local img_name="${1:?"Image Name is required"}"
podman.img.conf "${img_name}" | jq --raw-output '.Config.Env[]'
}
podman.img.mv.root() {
[[ ( (( $# == 0 )) ) || ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]} IMG_NAME
Move image to root.
Params:
IMG_NAME image name WITH VERSION (e.g. host/image:latest)
Options:
--help Show help";
return 0;
}
local img_name="${1:?"Image Name is required"}"
podman save "${img_name}" | podman load
}
podman.conf.path() {
[[ ( " $* " =~ ' --help ' ) ]] && {
echo -e \
"Usage: ${FUNCNAME[0]}
Show config location for current context.
--help Show help";
return 0;
}
local podman_yaml="$(podman info)"
local podman_path_config="$(dirname "$(echo "${podman_yaml}" | yq '.store.configFile')")"
echo "${podman_path_config}"
}
# podman.conf.merge() {
# local conf_path_1="${1:?"Config 1 path is required"}"
# local conf_path_2="${2:?"Config 2 path is required"}"
# local merged_conf=""
# echo "${merged_conf}"
# }
# podman.img.importAll() {
# :
# # local stdin=???
# # cat $stdin | xargs podman pull
# }
# export -f args.parse
export -f podman.commit
export -f podman.root.dir
export -f podman.conts.manif.path
export -f podman.conts.manif
export -f podman.cont.manif
export -f podman.cont.id
export -f podman.cont.dir
export -f podman.cont.conf
# export -f podman.cont.digest
# export -f podman.cont.sha
export -f podman.cont.cmd
# export -f podman.cont.entry
export -f podman.cont.env
# export -f podman.img.mv.root
export -f podman.imgs.manif.path
export -f podman.imgs.manif
export -f podman.img.manif
export -f podman.img.id
export -f podman.img.dir
export -f podman.img.conf
export -f podman.img.digest
export -f podman.img.sha
export -f podman.img.cmd
export -f podman.img.entry
export -f podman.img.env
export -f podman.img.mv.root
# export -f podman.img.fs.ls
export -f podman.conf.path
# export -f podman.conf.merge
# export -f podman.img.importAll