aboutsummaryrefslogtreecommitdiff
path: root/lava_scheduler_tool/commands.py
blob: a07e9b2ac3e42b55f40a456fb29165e155ab3273 (plain)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Michael Hudson-Doyle <michael.hudson@linaro.org>
#
# This file is part of lava-scheduler-tool.
#
# lava-scheduler-tool is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# lava-scheduler-tool is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with lava-scheduler-tool.  If not, see <http://www.gnu.org/licenses/>.

import argparse
import difflib
import jinja2
import os
import re
import signal
import subprocess
import sys
import time
import tempfile
import urlparse
import xmlrpclib
import yaml
import zmq

from lava_tool.authtoken import (
    split_xmlrpc_url,
    get_server_url,
    AuthenticatingServerProxy,
    KeyringAuthBackend
)
from lava.tool.command import Command, CommandGroup
from lava.tool.errors import CommandError
from lava_dashboard_tool.commands import DataSetRenderer, _get_pretty_renderer
from lava_scheduler_tool.scheduler import (
    devicedictionary_to_jinja2,
    jinja2_to_devicedictionary,
    validate_urls
)
from zmq.utils.strtypes import b, u


class Timeout():
    """ Timeout error class with ALARM signal. Accepts time in seconds. """
    class TimeoutError(Exception):
        pass

    def __init__(self, sec):
        self.sec = sec

    def __enter__(self):
        signal.signal(signal.SIGALRM, self.timeout_raise)
        signal.alarm(self.sec)

    def __exit__(self, *args):
        signal.alarm(0)

    def timeout_raise(self, *args):
        raise Timeout.TimeoutError()


class scheduler(CommandGroup):
    """
    Interact with LAVA Scheduler
    """

    namespace = "lava.scheduler.commands"


class submit_job(Command):
    """
    Submit a job to lava-scheduler
    """

    @classmethod
    def register_arguments(cls, parser):
        super(submit_job, cls).register_arguments(parser)
        parser.add_argument("SERVER")
        parser.add_argument("JSON_FILE")
        parser.add_argument(
            "--block",
            action="store_true",
            help="Blocks until the job gets executed. Deprecated. This option adds unnecessary load on the server. Please use wait-job-events command instead.")

    def invoke(self):
        auth_backend = KeyringAuthBackend()
        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=auth_backend)
        if not os.path.exists(self.args.JSON_FILE):
            raise CommandError("No such file: %s" % self.args.JSON_FILE)
        with open(self.args.JSON_FILE, 'rb') as stream:
            command_text = stream.read()
        try:
            validate_urls(command_text)
            job_ids = server.scheduler.submit_job(command_text)
        except xmlrpclib.Fault, e:
            raise CommandError(str(e))
        else:
            # Need to get the host in case of shortcuts usage.
            host = get_server_url(auth_backend, self.args.SERVER)
            if isinstance(job_ids, list):
                print "submitted as jobs:"
                for job_id in job_ids:
                    job = server.scheduler.job_details(job_id)
                    if 'absolute_url' in job:
                        print urlparse.urljoin(host, job["absolute_url"])
                    else:
                        print " -", job_id
            else:
                job = server.scheduler.job_details(job_ids)
                if 'absolute_url' in job:
                    print "submitted as job:", urlparse.urljoin(
                        host, job["absolute_url"])
                else:
                    print "submitted as job:", job_ids
                job_ids = [job_ids]

        if self.args.block:
            print('')
            print('This kind of polling is deprecated and will be removed in the next release. Please use "wait-for-job" command.')
            print('')
            print('Waiting for the job to run ')
            print('. = job waiting in the queue')
            print('# = job running')
            print('')
            for job_id in job_ids:
                print(job_id)
                job = {'job_status': 'Unknown'}
                progress = {'Submitted': '.', 'Running': '#'}
                while job['job_status'] in ['Unknown', 'Submitted', 'Running']:
                    job = server.scheduler.job_status(job_id)
                    sys.stdout.write(progress.get(job['job_status'], ''))
                    sys.stdout.flush()
                    time.sleep(10)  # seconds
                print('')
                print('')
                print('Job Status: %s' % job['job_status'])


class resubmit_job(Command):

    @classmethod
    def register_arguments(self, parser):
        parser.add_argument("SERVER")
        parser.add_argument("JOB_ID")

    def invoke(self):
        auth_backend = KeyringAuthBackend()
        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=auth_backend)
        try:
            job_id = server.scheduler.resubmit_job(self.args.JOB_ID)
            # Need to get the host in case of shortcuts usage.
            host = get_server_url(auth_backend, self.args.SERVER)

        except xmlrpclib.Fault, e:
            raise CommandError(str(e))
        else:
            job = server.scheduler.job_details(job_id)
            print "resubmitted as job:", urlparse.urljoin(
                host, job["absolute_url"])


class cancel_job(Command):

    @classmethod
    def register_arguments(self, parser):
        parser.add_argument("SERVER")
        parser.add_argument("JOB_ID")

    def invoke(self):
        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=KeyringAuthBackend())
        server.scheduler.cancel_job(self.args.JOB_ID)


class job_output(Command):
    """
    Get job output from the scheduler.
    """

    @classmethod
    def register_arguments(cls, parser):
        super(job_output, cls).register_arguments(parser)
        parser.add_argument("SERVER")
        parser.add_argument("JOB_ID",
                            help="Job ID to download output file")
        parser.add_argument("--overwrite",
                            action="store_true",
                            help="Overwrite files on the local disk")
        parser.add_argument("--output", "-o",
                            type=argparse.FileType("wb"),
                            default=None,
                            help="Alternate name of the output file")

    def invoke(self):
        if self.args.output is None:
            filename = str(self.args.JOB_ID) + '_output.txt'
            if os.path.exists(filename) and not self.args.overwrite:
                print >> sys.stderr, "File {filename!r} already exists".format(
                    filename=filename)
                print >> sys.stderr, "You may pass --overwrite to write over it"
                return -1
            stream = open(filename, "wb")
        else:
            stream = self.args.output
            filename = self.args.output.name

        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=KeyringAuthBackend())
        try:
            stream.write(server.scheduler.job_output(self.args.JOB_ID).data)
            print "Downloaded job output of {0} to file {1!r}".format(
                self.args.JOB_ID, filename)
        except xmlrpclib.Fault as exc:
            print >> sys.stderr, exc
            return -1


class job_status(Command):
    """
    Get job status and bundle sha1, if it existed, from the scheduler.
    """

    @classmethod
    def register_arguments(cls, parser):
        super(job_status, cls).register_arguments(parser)
        parser.add_argument("SERVER")
        parser.add_argument("JOB_ID",
                            help="Job ID to check the status")

    def invoke(self):
        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=KeyringAuthBackend())
        job_status = server.scheduler.job_status(self.args.JOB_ID)

        print "Job ID: %s\nJob Status: %s\nBundle SHA1: %s" % \
            (str(self.args.JOB_ID), job_status['job_status'],
             job_status['bundle_sha1'])


class job_details(Command):
    """
    Get job details, if it existed, from the scheduler.
    """

    @classmethod
    def register_arguments(cls, parser):
        super(job_details, cls).register_arguments(parser)
        parser.add_argument("SERVER")
        parser.add_argument("JOB_ID",
                            help="Job ID to find the details")

    def invoke(self):
        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=KeyringAuthBackend())
        job_details = server.scheduler.job_details(self.args.JOB_ID)

        print "Details of job {0}: \n".format(str(self.args.JOB_ID))
        for detail in job_details:
            print "%s: %s" % (detail, job_details[detail])


class jobs_list(Command):
    """
    Get list of running and submitted jobs from the scheduler.
    """

    renderer = _get_pretty_renderer(
        order=('id', 'description', 'status', 'actual_device', 'requested_device'),
        column_map={
            'id': 'Job ID',
            'description': 'Job description',
            'status': 'Job status',
            'actual_device': 'Device',
            'requested_device': 'Requested device or device type'},
        row_formatter={
            'actual_device': lambda x: x or "",
            'requested_device': lambda x: x or ""},
        empty="There are no running or submitted jobs",
        caption="Jobs list")

    @classmethod
    def register_arguments(cls, parser):
        super(jobs_list, cls).register_arguments(parser)
        parser.add_argument("SERVER")

    def invoke(self):
        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=KeyringAuthBackend())
        jobs_list = server.scheduler.all_jobs()

        data_for_renderer = []
        for row in jobs_list:
            if row[6]:
                job_id = row[6]
            else:
                job_id = row[0]
            if row[4]:
                req_d = row[4]["hostname"]
            else:
                req_d = row[5]["name"]
            if row[3]:
                req_d = ""
                act_d = row[3]["hostname"]
            else:
                act_d = ""
            data_for_renderer.append(dict(zip(['id', 'description', 'status', 'actual_device', 'requested_device'],
                                     [job_id, row[1], row[2], act_d, req_d])))

        self.renderer.render(data_for_renderer)


class devices_list(Command):
    """
    Get list of devices from the scheduler.
    """

    renderer = _get_pretty_renderer(
        order=('hostname', 'device_type_name', 'status', 'job'),
        column_map={
            'hostname': 'Hostname',
            'device_type_name': 'Device type',
            'status': 'Status',
            'job': 'Job ID'},
        row_formatter={
            'job': lambda x: x or ""},
        empty="There are no devices",
        caption="Devices list")

    @classmethod
    def register_arguments(cls, parser):
        super(devices_list, cls).register_arguments(parser)
        parser.add_argument("SERVER")

    def invoke(self):
        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=KeyringAuthBackend())
        all_devices = server.scheduler.all_devices()

        data_for_renderer = []
        for row in all_devices:
            if row[3]:
                job_id = row[3]
            else:
                job_id = ""
            data_for_renderer.append(dict(zip(['hostname', 'device_type_name', 'status', 'job'],
                                     [row[0], row[1], row[2], job_id])))

        self.renderer.render(data_for_renderer)


class get_pipeline_device_config(Command):
    """
    Get the pipeline device configuration from scheduler to a local file
    or stdout.
    """

    @classmethod
    def register_arguments(cls, parser):
        super(get_pipeline_device_config, cls).register_arguments(parser)
        parser.add_argument("SERVER",
                            help="Host to download pipeline device config from")
        parser.add_argument("DEVICE_HOSTNAME",
                            help="DEVICE_HOSTNAME to download config file")
        parser.add_argument("--overwrite",
                            action="store_true",
                            help="Overwrite files on the local disk")
        parser.add_argument("--output", "-o",
                            type=argparse.FileType("wb"),
                            default=None,
                            help="Alternate name of the config file")
        parser.add_argument("--stdout",
                            action="store_true",
                            help="Write output to stdout")

    def invoke(self):
        if self.args.output is None and not self.args.stdout:
            filename = str(self.args.DEVICE_HOSTNAME) + '_config.yaml'
            if os.path.exists(filename) and not self.args.overwrite:
                print >> sys.stderr, "File {filename!r} already exists".format(
                    filename=filename)
                print >> sys.stderr, "You may pass --overwrite to write over it"
                return -1
            stream = open(filename, "wb")
        elif self.args.stdout:
            stream = sys.stdout
            filename = "stdout"
        else:
            stream = self.args.output
            filename = self.args.output.name

        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=KeyringAuthBackend())
        try:
            stream.write(server.scheduler.get_pipeline_device_config(
                self.args.DEVICE_HOSTNAME).data)
            print "Downloaded device config of {0} to file {1!r}".format(
                self.args.DEVICE_HOSTNAME, filename)
        except xmlrpclib.Fault as exc:
            print >> sys.stderr, exc
            return -1


class compare_device_conf(Command):
    """
    Compare device config YAML files.
    """

    @classmethod
    def register_arguments(cls, parser):
        super(compare_device_conf, cls).register_arguments(parser)
        parser.add_argument("--wdiff", "-w",
                            action='store_true',
                            help="Use wdiff for parsing output")

        parser.add_argument("--use-stored", "-u",
                            default=None,
                            help="Use stored device config with specified device")
        parser.add_argument("--dispatcher-config-dir",
                            default="/etc/lava-server/dispatcher-config/",
                            help="Where to find the device_type templates.")
        parser.add_argument("CONFIGS",
                            nargs='*',
                            help="List of device config paths, at least one, max two.")

    def invoke(self):

        configs = self.args.CONFIGS

        # Validate number of arguments depending on the options.
        if self.args.use_stored is None:
            if len(self.args.CONFIGS) != 2:
                print >> sys.stderr, "Please input two arguments with config file paths"
                print >> sys.stderr, "You may use --use-stored with one config file path"
                return -1

            for path in self.args.CONFIGS:
                if not os.path.exists(path):
                    print >> sys.stderr, "File {path!r} does not exist".format(
                        path=path)
                    return -1

        else:
            if len(self.args.CONFIGS) != 1:
                print >> sys.stderr, "Please input one argument with config file path"
                print >> sys.stderr, "You may omit --use-stored and use two config file paths"
                return -1

            path = self.args.CONFIGS[0]
            if not os.path.exists(path):
                print >> sys.stderr, "File {path!r} does not exist".format(
                    path=path)
                return -1

            # Run device-dictionary --preview and load it into tmp file.
            args = [
                "lava-server",
                "manage",
                "device-dictionary",
                "--hostname=%s" % self.args.use_stored,
                "--export"
            ]

            config_handle, config_path = tempfile.mkstemp()

            return_code = subprocess.call(
                args,
                stdout=open(config_path, "w")
            )

            if return_code != 0:
                print >> sys.stderr, "Device config for {device!r} doesn't exists".format(device=self.args.use_stored)
                return -1

            configs.append(config_path)

        # Load templates and compare. Current output is classic unified diff.
        device_confs = []
        for path in configs:
            with open(path) as read_file:
                line = read_file.readline()

            if re.search(r'\{%\sextends\s.*%\}', line):
                # First line matches 'extends' regex. Treat it as a template.
                data = self._parse_template(path)
                config = devicedictionary_to_jinja2(
                    data,
                    data['extends']
                )
                string_loader = jinja2.DictLoader({'%s' % path: config})
                type_loader = jinja2.FileSystemLoader([
                    os.path.join(self.args.dispatcher_config_dir,
                                 'device-types')])
                env = jinja2.Environment(
                    loader=jinja2.ChoiceLoader([string_loader, type_loader]),
                    trim_blocks=True)
                template = env.get_template("%s" % path)
                device_configuration = template.render()
                device_confs.append(
                    device_configuration.strip("\n").split("\n"))
            else:
                # 'Extends' not matched. Treat this as a regular config file.
                try:
                    yaml.safe_load(file(path, 'r'))
                except yaml.YAMLError:
                    print "Please provide a valid YAML configuration file."
                    sys.exit(2)

                device_configuration = []
                with open(path) as read_file:
                    device_configuration = [line.strip('\n') for line in read_file.readlines()]

                device_confs.append(device_configuration)

        diff = difflib.unified_diff(device_confs[0], device_confs[1],
                                    fromfile=configs[0],
                                    tofile=configs[1])
        input = [line for line in diff]

        if self.args.wdiff:
            # Pass diff to wdiff for word diff output.
            diff_handle, diff_path = tempfile.mkstemp()

            args = ["wdiff", "-d"]

            proc = subprocess.Popen(
                args,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE
            )
            out, err = proc.communicate("\n".join(input))

            if out:
                print out

        if not self.args.wdiff:
            for line in input:
                print line

        if not input:
            print "Success. The configuration files are identical."

        return 0

    def _parse_template(self, device_file):

        if not os.path.exists(os.path.realpath(device_file)):
            print "Unable to find file '%s'\n" % device_file
            sys.exit(2)
        with open(device_file, 'r') as fileh:
            content = fileh.read()
        return jinja2_to_devicedictionary(content)


class device_dictionary(Command):
    """
    Update or export device dictionary data as jinja2 data.
    [Superusers only.]
    """

    def invoke(self):
        if self.args.update and self.args.export:
            print >> sys.stderr, "Please use either update or export."
            return 1
        elif self.args.export:
            hostname = str(self.args.DEVICE_HOSTNAME)
            stream = sys.stdout
            server = AuthenticatingServerProxy(
                self.args.SERVER, auth_backend=KeyringAuthBackend())
            if self.args.export:
                try:
                    stream.write(server.scheduler.export_device_dictionary(hostname).data)
                except xmlrpclib.Fault as exc:
                    print >> sys.stderr, exc
                    return -1
        elif self.args.update:
            server = AuthenticatingServerProxy(
                self.args.SERVER, auth_backend=KeyringAuthBackend())
            hostname = str(self.args.DEVICE_HOSTNAME)
            filename = self.args.update
            if not os.path.exists(filename):
                print >> sys.stderr, "File {filename!r} does not exist".format(
                    filename=filename)
                return -1
            with open(filename, "r") as reader:
                data = reader.read()
            print "Updating device dictionary for %s on %s" % (hostname, self.args.SERVER)
            try:
                print server.scheduler.import_device_dictionary(hostname, data)
            except xmlrpclib.Fault as exc:
                print >> sys.stderr, exc
                return -1
        else:
            print >> sys.stderr, "Unrecognised options."
            return 2

    def reparse_arguments(self, parser, raw_args):
        pass

    @classmethod
    def register_arguments(cls, parser):
        super(device_dictionary, cls).register_arguments(parser)
        parser.add_argument("SERVER",
                            help="Host to query or update the device dictionary on")
        parser.add_argument("DEVICE_HOSTNAME",
                            help="DEVICE_HOSTNAME to query or update")
        parser.add_argument("--update", "-u",
                            help="Load a jinja2 file to update the device dictionary")
        parser.add_argument("--export", "-e", action='store_true',
                            help="Export the device dictionary for this device as jinja2")


class wait_job_events(Command):
    """
    Wait for job to finish and return job details.
    """

    FINISHED_JOB_STATUS = ["Complete", "Incomplete", "Canceled"]
    PUBLISHER_PROTOCOL = "tcp://"
    PUBLISHER_PORT = 5500

    @classmethod
    def register_arguments(cls, parser):
        super(wait_job_events, cls).register_arguments(parser)
        parser.add_argument("SERVER")
        action = parser.add_mutually_exclusive_group(required=True)
        action.add_argument("--job-definition",
                            help="Definition file path of the job that will be submitted.")
        action.add_argument("--job-id",
                            type=int,
                            help="ID of the job to wait for. If this argument is provided, this command will not submit new job.")
        parser.add_argument("--port", "-p",
                            type=int,
                            help="Alternative publisher port to use")
        parser.add_argument("--timeout",
                            type=int,
                            default=0,
                            help="Time to wait until the job is finished. Default will wait until manually canceled.")
        parser.add_argument(
            "--quiet", "-q",
            action="store_true",
            help="Do not print out interim messages (when job becomes Submitted or Running) but only when the job is finished.")

    def invoke(self):
        auth_backend = KeyringAuthBackend()
        server = AuthenticatingServerProxy(
            self.args.SERVER, auth_backend=auth_backend)

        # Need to get the host in case of shortcuts usage.
        parsed_host = urlparse.urlparse(
            get_server_url(auth_backend, self.args.SERVER))
        host = parsed_host.netloc

        if self.args.job_id:
            job_ids = [self.args.job_id]
            try:
                job_details = server.scheduler.job_details(self.args.job_id)

                if job_details["status"] in self.FINISHED_JOB_STATUS:
                    print "Job %s already finished with status %s." % (
                        self.args.job_id, job_details["status"])
                    return

            except xmlrpclib.Fault as e:
                print "Job details did not found specified job. Please make sure job-id exists or that this job is about to get submitted."

        else:
            if not os.path.exists(self.args.job_definition):
                raise CommandError(
                    "No such file: %s" % self.args.job_definition)
            try:
                with open(self.args.job_definition, 'rb') as stream:
                    command_text = stream.read()
            except IOError as e:
                print >> sys.stderr, e
                return -1
            try:
                validate_urls(command_text)
                job_ids = server.scheduler.submit_job(command_text)
                print "submitted as job id(s):", job_ids
                if not isinstance(job_ids, list):
                    job_ids = [job_ids]

            except xmlrpclib.Fault, e:
                raise CommandError(str(e))

        port = self.args.port if self.args.port else self.PUBLISHER_PORT
        event_socket = "%s%s:%s" % (self.PUBLISHER_PROTOCOL, host, port)

        try:
            with Timeout(self.args.timeout):
                context = zmq.Context.instance()
                sock = context.socket(zmq.SUB)

                sock.setsockopt(zmq.SUBSCRIBE, b"")
                sock.connect(event_socket)

                print >> sys.stdout, "Now waiting for job events..."
                while True:
                    msg = sock.recv_multipart()
                    try:
                        (topic, uuid, dt, username, data) = msg[:]
                    except IndexError:
                        # Droping invalid message
                        continue

                    data = yaml.safe_load(data)
                    if "job" in data:
                        # Filter out device events.
                        if "submitter" in data:
                            if data["job"] in job_ids:
                                if data["status"] in self.FINISHED_JOB_STATUS:
                                    print data
                                    break
                                elif not self.args.quiet:
                                    print data

        except zmq.error.ZMQError as e:
            print >> sys.stderr, "wait_job_events() error: %s" % e
            return -1
        except KeyboardInterrupt:
            print >> sys.stderr, "wait_job_events() cancelled"
            return -1
        except Timeout.TimeoutError:
            print >> sys.stderr, "wait_job_events() timed out after %s seconds." % self.args.timeout
            try:
                for job_id in job_ids:
                    job_details = server.scheduler.job_details(job_id)
                    print >> sys.stdout, "Job %s currently has status %s." % (
                        job_id, job_details["status"])
                    print >> sys.stdout, "You can check the job details with 'lava-tool job-details' command."
                return 1
            except xmlrpclib.Fault as e:
                # Keep silent, message about non-existing job is already
                # printed during first job_details() call.
                return -1

        return 0