Skip to content

Commit 9d3f343

Browse files
committed
Add container submenu
Add container submenu for each VM listing running containers, with kill, stop, pause, and unpause options. Signed-off-by: Joe Block <[email protected]>
1 parent 0640d99 commit 9d3f343

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

Diff for: lima-plugin

+112
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ function printMenu() {
150150
if [[ $vmstatus == 'Running' ]]; then
151151
echo "$name VM is running | color=$RUNNING_VM_COLOR"
152152
echo "--⛔ Stop $name VM | bash=$XBAR_PLUGIN param1=stop param2=$name terminal=false refresh=true"
153+
154+
echo "-- Containers"
155+
for container in $(vmContainers)
156+
do
157+
echo "---- $container"
158+
# echo "------ start | bash=$XBAR_PLUGIN param1=startContainer param2=$name param3=$container terminal=false refresh=true"
159+
echo "------ stop | bash=$XBAR_PLUGIN param1=stopContainer param2=$name param3=$container terminal=false refresh=true"
160+
echo "------ kill | bash=$XBAR_PLUGIN param1=killContainer param2=$name param3=$container terminal=false refresh=true"
161+
echo "------ pause | bash=$XBAR_PLUGIN param1=pauseContainer param2=$name param3=$container terminal=false refresh=true"
162+
echo "------ unpause | bash=$XBAR_PLUGIN param1=unpauseContainer param2=$name param3=$container terminal=false refresh=true"
163+
done
164+
153165
echo "-- Images"
154166
for image in $(vmImages)
155167
do
@@ -168,6 +180,22 @@ function printMenu() {
168180
limactl --version
169181
}
170182

183+
function vmContainers() {
184+
# default VM doesn't need to be specified
185+
if [[ "$VM" != 'default' ]]; then
186+
export LIMA_INSTANCE="$VM"
187+
fi
188+
# shellcheck disable=SC2001,SC2046,SC2005
189+
containerList="[$(echo $(lima nerdctl ps --format '{{json .}},') | sed 's/,$//')]"
190+
# Can have spaces in our data, deal by using base64 (ugly)
191+
for row in $(echo "${containerList}" | jq -r '.[] | @base64'); do
192+
_jq() {
193+
echo "${row}" | base64 --decode | jq -r "${1}"
194+
}
195+
_jq '.Names'
196+
done
197+
}
198+
171199
function vmImages() {
172200
# default VM doesn't need to be specified
173201
if [[ "$VM" != 'default' ]]; then
@@ -220,6 +248,78 @@ function rmImage() {
220248
fi
221249
}
222250

251+
function stopContainer() {
252+
# arg1 = container
253+
# arg2 = VM
254+
local containerName
255+
local VM
256+
containerName="$1"
257+
VM="$2"
258+
if [[ "$VM" != 'default' ]]; then
259+
export LIMA_INSTANCE="$VM"
260+
fi
261+
displayNotification lima "Stopping ${containerName} on ${VM}..."
262+
if lima nerdctl container stop "${containerName}"; then
263+
displayNotification Lima "Stopped ${containerName}"
264+
else
265+
displayAlert Lima "Failed to stop ${containerName} on ${VM}"
266+
fi
267+
}
268+
269+
function killContainer() {
270+
# arg1 = container
271+
# arg2 = VM
272+
local containerName
273+
local VM
274+
containerName="$1"
275+
VM="$2"
276+
if [[ "$VM" != 'default' ]]; then
277+
export LIMA_INSTANCE="$VM"
278+
fi
279+
displayNotification lima "Killing ${containerName} on ${VM}..."
280+
if lima nerdctl container kill "${containerName}"; then
281+
displayNotification Lima "Killed ${containerName}"
282+
else
283+
displayAlert Lima "Failed to kill ${containerName} on ${VM}"
284+
fi
285+
}
286+
287+
function pauseContainer() {
288+
# arg1 = container
289+
# arg2 = VM
290+
local containerName
291+
local VM
292+
containerName="$1"
293+
VM="$2"
294+
if [[ "$VM" != 'default' ]]; then
295+
export LIMA_INSTANCE="$VM"
296+
fi
297+
displayNotification lima "Pausing ${containerName} on ${VM}..."
298+
if lima nerdctl container pause "${containerName}"; then
299+
displayNotification Lima "Paused ${containerName}"
300+
else
301+
displayAlert Lima "Failed to pause ${containerName} on ${VM}"
302+
fi
303+
}
304+
305+
function unpauseContainer() {
306+
# arg1 = container
307+
# arg2 = VM
308+
local containerName
309+
local VM
310+
containerName="$1"
311+
VM="$2"
312+
if [[ "$VM" != 'default' ]]; then
313+
export LIMA_INSTANCE="$VM"
314+
fi
315+
displayNotification lima "Unpausing ${containerName} on ${VM}..."
316+
if lima nerdctl container unpause "${containerName}"; then
317+
displayNotification Lima "Unpaused ${containerName}"
318+
else
319+
displayAlert Lima "Failed to unpause ${containerName} on ${VM}"
320+
fi
321+
}
322+
223323
function processMenuCommand() {
224324
case "$1" in
225325
images)
@@ -228,6 +328,18 @@ function processMenuCommand() {
228328
pull)
229329
pullImage "$2" "$3" # pull imagename vmname
230330
;;
331+
stopContainer)
332+
stopContainer "$3" "$2" # stopContainer containerName VMname
333+
;;
334+
killContainer)
335+
killContainer "$3" "$2" # killContainer containerName VMname
336+
;;
337+
pauseContainer)
338+
pauseContainer "$3" "$2" # pauseContainer containerName VMname
339+
;;
340+
unpauseContainer)
341+
unpauseContainer "$3" "$2" # unpauseContainer containerName VMname
342+
;;
231343
rmImage)
232344
rmImage "$2" "$3" # pull imagename vmname
233345
;;

0 commit comments

Comments
 (0)