Merge ~jocave/testflinger-cli:notify-me into testflinger-cli:master

Proposed by Jonathan Cave
Status: Work in progress
Proposed branch: ~jocave/testflinger-cli:notify-me
Merge into: testflinger-cli:master
Diff against target: 122 lines (+38/-4)
3 files modified
snapcraft.yaml (+8/-0)
testflinger-cli.wrapper (+5/-0)
testflinger_cli/__init__.py (+25/-4)
Reviewer Review Type Date Requested Status
Paul Larson Approve
Review via email: mp+402027@code.launchpad.net

Description of the change

I often reserve a device to do some testing, but while the device provisions I start doing something else and forget about it!

This basically works like the undistract-me tool to popup a desktop notification and play a bell sound to tell you your device is ready for you to use.

To post a comment you must log in.
Revision history for this message
Paul Larson (pwlars) wrote :

Neat! Have you tried this on something like a server without the desktop bits to see how it behaves? I know, for instance, we probably wouldn't add -n for our polling in Jenkins jobs or anything, just want to make sure it wouldn't crash or something in that case.
It's too bad that things like print("\a") don't work reliably, that would make a nice simple solution

review: Approve
Revision history for this message
Jeff Lane  (bladernr) wrote :

This is pretty cool... I made one comment below, feel free to ignore.

Revision history for this message
Jonathan Cave (jocave) wrote :

I kinda forgot about this, partially because my local test install of it stopped working. I should possibly have a look at why.

Unmerged commits

b387364... by Jonathan Cave

Add: notify reserve ready with popup and sound

Add another flag to the submit command that allows the user to be
notifed when a job reaches the reserve phase or completes. This is
handy if you're waiting for provisioning and get distracted doing
something else.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/snapcraft.yaml b/snapcraft.yaml
2index 2a5d669..5366a1f 100644
3--- a/snapcraft.yaml
4+++ b/snapcraft.yaml
5@@ -14,6 +14,8 @@ apps:
6 plugs:
7 - home
8 - network
9+ - desktop
10+ - audio-playback
11
12 parts:
13 launcher:
14@@ -24,3 +26,9 @@ parts:
15 testflinger-cli:
16 plugin: python
17 source: .
18+ notification-support:
19+ plugin: nil
20+ stage-packages:
21+ - libnotify-bin
22+ - pulseaudio-utils
23+ - sound-theme-freedesktop
24diff --git a/testflinger-cli.wrapper b/testflinger-cli.wrapper
25index 9278862..71b3f66 100755
26--- a/testflinger-cli.wrapper
27+++ b/testflinger-cli.wrapper
28@@ -1,4 +1,9 @@
29 #!/bin/sh
30 export LC_ALL=C.UTF-8
31 export LANG=C.UTF-8
32+papath="$XDG_RUNTIME_DIR/../pulse/native"
33+if [ ! -e "$papath" ]; then
34+ echo "Cannot not find '$papath'"
35+fi
36+export PULSE_SERVER="unix:$papath"
37 exec testflinger-cli $@
38diff --git a/testflinger_cli/__init__.py b/testflinger_cli/__init__.py
39index e7d4332..da6e1ae 100644
40--- a/testflinger_cli/__init__.py
41+++ b/testflinger_cli/__init__.py
42@@ -18,6 +18,7 @@
43 import inspect
44 import json
45 import os
46+import subprocess as sp
47 import sys
48 import time
49 import yaml
50@@ -131,6 +132,7 @@ class TestflingerCli:
51 arg_submit.set_defaults(func=self.submit)
52 arg_submit.add_argument('--poll', '-p', action='store_true')
53 arg_submit.add_argument('--quiet', '-q', action='store_true')
54+ arg_submit.add_argument('--notify', '-n', action='store_true')
55 arg_submit.add_argument('filename')
56
57 self.args = parser.parse_args()
58@@ -214,6 +216,8 @@ class TestflingerCli:
59 except Exception:
60 raise SystemExit(
61 'Unable to read file: {}'.format(self.args.filename))
62+ if self.args.quiet:
63+ self.args.notify = False
64 job_id = self.submit_job_data(data)
65 queue = yaml.safe_load(data).get('job_queue')
66 self.history.new(job_id, queue)
67@@ -223,7 +227,7 @@ class TestflingerCli:
68 print('Job submitted successfully!')
69 print('job_id: {}'.format(job_id))
70 if self.args.poll:
71- self.do_poll(job_id)
72+ self.do_poll(job_id, self.args.notify)
73
74 def submit_job_data(self, data):
75 """ Submit data that was generated or read from a file as a test job
76@@ -317,10 +321,12 @@ class TestflingerCli:
77 sys.exit(0)
78 self.do_poll(self.args.job_id)
79
80- def do_poll(self, job_id):
81+ def do_poll(self, job_id, notify=False):
82 job_state = self.get_job_state(job_id)
83 self.history.update(job_id, job_state)
84 prev_queue_pos = None
85+ sound_f = os.path.expandvars(
86+ '$SNAP/usr/share/sounds/freedesktop/stereo/complete.oga')
87 if job_state == 'waiting':
88 print('This job is waiting on a node to become available.')
89 while job_state != 'complete':
90@@ -344,7 +350,17 @@ class TestflingerCli:
91 continue
92 if output:
93 print(output, end='', flush=True)
94- job_state = self.get_job_state(job_id)
95+ new_job_state = self.get_job_state(job_id)
96+ if new_job_state != job_state and new_job_state == 'reserve':
97+ if notify:
98+ cmd = ['notify-send', '-i', 'dialog-information',
99+ '-u', 'normal',
100+ 'Job reached {} phase'.format(new_job_state)]
101+ sp.run(cmd, check=False)
102+ cmd = ['paplay', sound_f]
103+ sp.run(cmd, check=False, stdout=sp.DEVNULL,
104+ stderr=sp.DEVNULL)
105+ job_state = new_job_state
106 self.history.update(job_id, job_state)
107 except KeyboardInterrupt:
108 choice = input('\nCancel job {} before exiting '
109@@ -357,7 +373,12 @@ class TestflingerCli:
110 self.do_cancel(job_id)
111 # Both y and n will allow the external handler deal with it
112 raise
113-
114+ if notify:
115+ cmd = ['notify-send', '-i', 'dialog-information', '-u', 'normal',
116+ 'Job completed']
117+ sp.run(cmd, check=False)
118+ cmd = ['paplay', sound_f]
119+ sp.run(cmd, check=False, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
120 print(job_state)
121
122 def jobs(self):

Subscribers

People subscribed via source and target branches