Skip to content

Instantly share code, notes, and snippets.

@vwbusguy
Created February 11, 2020 18:09
Show Gist options
  • Save vwbusguy/94919b49e39eb9122d678b4c00643b98 to your computer and use it in GitHub Desktop.
Save vwbusguy/94919b49e39eb9122d678b4c00643b98 to your computer and use it in GitHub Desktop.
Gnome Notification for running podman containers with button to kill them
#!env/bin/python
import gi, subprocess, sys, time
gi.require_version('Notify', '0.7')
from gi.repository import GLib,Notify
class App():
def __init__(self):
self.containers = []
self.last_notification = None
Notify.init('Containers')
self.check_notify()
def check_notify(self):
self.containers = self.get_containers()
if self.containers:
message = str()
for container in self.containers:
message = message + "{name} ({image})\n - Running since {created}\n".format(**container)
notification = self.last_notification = Notify.Notification.new("Containers are running",message,'dialog-information')
notification.set_timeout(20)
notification.add_action(
"action_click",
"Kill Running Containers",
self.kill_containers,
None
)
notification.show()
GLib.timeout_add_seconds(3600, self.check_notify)
def get_containers(self):
containers = []
keys=['created','name','image','id']
pman_out = subprocess.run(["podman","ps","--format",'{{.Created}}|{{.Names}}|{{.Image}}|{{.ID}}'],capture_output=True)
if pman_out.returncode == 0:
if not pman_out.stdout:
print("No containers are currently running.")
return containers
containers_inp = pman_out.stdout.decode("utf-8")
containers_lines = containers_inp.split("\n")
for line in containers_lines:
if line:
container = dict(zip(keys,line.split('|')))
containers.append(container)
else:
print(pman_out.stderr.decode("utf-8"))
return containers
def kill_containers(self, notification, action_name, data):
ids = []
for container in self.containers:
ids.append(container["id"])
if ids:
pman_out = subprocess.run(["podman","rm","-fv",*ids],capture_output=True)
print(str(pman_out.stderr) + str(pman_out.stdout))
notification.close()
app = App()
GLib.MainLoop().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment