Skip to content

Commit 0911582

Browse files
[docker] Catch errors. Fail graciously
1 parent 675cdec commit 0911582

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

docker/__init__.py

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from albert import *
99

1010
md_iid = "2.0"
11-
md_version = "1.5"
11+
md_version = "1.6"
1212
md_name = "Docker"
1313
md_description = "Manage docker images and containers"
1414
md_license = "BSD-3"
@@ -27,60 +27,61 @@ def __init__(self):
2727
defaultTrigger='d ',
2828
synopsis='<image tag|container name>')
2929
PluginInstance.__init__(self, extensions=[self])
30-
3130
self.icon_urls_running = [f"file:{Path(__file__).parent}/running.png"]
3231
self.icon_urls_stopped = [f"file:{Path(__file__).parent}/stopped.png"]
33-
self.client = docker.from_env()
34-
if not self.client:
35-
self.client = docker.DockerClient(base_url='unix://var/run/docker.sock')
36-
if not self.client:
37-
raise "Failed to initialize client."
32+
self.client = None
3833

3934
def handleGlobalQuery(self, query):
4035
rank_items = []
36+
try:
37+
if not self.client:
38+
self.client = docker.from_env()
4139

42-
for container in self.client.containers.list(all=True):
43-
if query.string in container.name:
44-
# Create dynamic actions
45-
if container.status == 'running':
46-
actions = [Action("stop", "Stop container", lambda c=container: c.stop()),
47-
Action("restart", "Restart container", lambda c=container: c.restart())]
48-
else:
49-
actions = [Action("start", "Start container", lambda c=container: c.start())]
50-
actions.extend([
51-
Action("logs", "Logs",
52-
lambda c=container.id: runTerminal("docker logs -f %s" % c, close_on_exit=False)),
53-
Action("remove", "Remove (forced, with volumes)",
54-
lambda c=container: c.remove(v=True, force=True)),
55-
Action("copy-id", "Copy id to clipboard",
56-
lambda id=container.id: setClipboardText(id))
57-
])
58-
59-
rank_items.append(RankItem(
60-
item=StandardItem(
61-
id=container.id,
62-
text="%s (%s)" % (container.name, ", ".join(container.image.tags)),
63-
subtext="Container: %s" % container.id,
64-
iconUrls=self.icon_urls_running if container.status == 'running' else self.icon_urls_stopped,
65-
actions=actions
66-
),
67-
score=len(query.string)/len(container.name)
68-
))
40+
for container in self.client.containers.list(all=True):
41+
if query.string in container.name:
42+
# Create dynamic actions
43+
if container.status == 'running':
44+
actions = [Action("stop", "Stop container", lambda c=container: c.stop()),
45+
Action("restart", "Restart container", lambda c=container: c.restart())]
46+
else:
47+
actions = [Action("start", "Start container", lambda c=container: c.start())]
48+
actions.extend([
49+
Action("logs", "Logs",
50+
lambda c=container.id: runTerminal("docker logs -f %s" % c, close_on_exit=False)),
51+
Action("remove", "Remove (forced, with volumes)",
52+
lambda c=container: c.remove(v=True, force=True)),
53+
Action("copy-id", "Copy id to clipboard",
54+
lambda id=container.id: setClipboardText(id))
55+
])
6956

70-
for image in reversed(self.client.images.list()):
71-
for tag in sorted(image.tags, key=len): # order by resulting score
72-
if query.string in tag:
7357
rank_items.append(RankItem(
7458
item=StandardItem(
75-
id=image.short_id,
76-
text=", ".join(image.tags),
77-
subtext="Image: %s" % image.id,
78-
iconUrls=self.icon_urls_stopped,
79-
actions=[Action("run", "Run with command: %s" % query.string,
80-
lambda i=image, s=query.string: client.containers.run(i, s)),
81-
Action("rmi", "Remove image", lambda i=image: i.remove())]
59+
id=container.id,
60+
text="%s (%s)" % (container.name, ", ".join(container.image.tags)),
61+
subtext="Container: %s" % container.id,
62+
iconUrls=self.icon_urls_running if container.status == 'running' else self.icon_urls_stopped,
63+
actions=actions
8264
),
83-
score=len(query.string)/len(tag)
65+
score=len(query.string)/len(container.name)
8466
))
8567

68+
for image in reversed(self.client.images.list()):
69+
for tag in sorted(image.tags, key=len): # order by resulting score
70+
if query.string in tag:
71+
rank_items.append(RankItem(
72+
item=StandardItem(
73+
id=image.short_id,
74+
text=", ".join(image.tags),
75+
subtext="Image: %s" % image.id,
76+
iconUrls=self.icon_urls_stopped,
77+
actions=[Action("run", "Run with command: %s" % query.string,
78+
lambda i=image, s=query.string: client.containers.run(i, s)),
79+
Action("rmi", "Remove image", lambda i=image: i.remove())]
80+
),
81+
score=len(query.string)/len(tag)
82+
))
83+
except Exception as e:
84+
warning(e)
85+
self.client = None
86+
8687
return rank_items

0 commit comments

Comments
 (0)