Revision fcafd191

b/Makefile.am
373 373

  
374 374
client_PYTHON = \
375 375
	lib/client/__init__.py \
376
	lib/client/base.py \
376 377
	lib/client/gnt_backup.py \
377 378
	lib/client/gnt_cluster.py \
378 379
	lib/client/gnt_debug.py \
b/lib/client/base.py
1
#
2
#
3

  
4
# Copyright (C) 2014 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Utils for CLI commands"""
23

  
24
from ganeti import cli
25
from ganeti import constants
26
from ganeti import ht
27

  
28

  
29
def GetResult(cl, opts, result):
30
  """Waits for jobs and returns whether they have succeeded
31

  
32
  Some OpCodes return of list of jobs.  This function can be used
33
  after issueing a given OpCode to look at the OpCode's result and, if
34
  it is of type L{ht.TJobIdListOnly}, then it will wait for the jobs
35
  to complete, otherwise just return L{constants.EXIT_SUCCESS}.
36

  
37
  @type cl: L{ganeti.luxi.Client}
38
  @param cl: client that was used to submit the OpCode, which will
39
             also be used to poll the jobs
40

  
41
  @param opts: CLI options
42

  
43
  @param result: result of the opcode which might contain job
44
         information, in which case the jobs will be polled, or simply
45
         the result of the opcode
46

  
47
  @rtype: int
48
  @return: L{constants.EXIT_SUCCESS} if all jobs completed
49
           successfully, L{constants.EXIT_FAILURE} otherwise
50

  
51
  """
52
  if not ht.TJobIdListOnly(result):
53
    return constants.EXIT_SUCCESS
54

  
55
  jex = cli.JobExecutor(cl=cl, opts=opts)
56

  
57
  for (status, job_id) in result[constants.JOB_IDS_KEY]:
58
    jex.AddJobId(None, status, job_id)
59

  
60
  bad_jobs = [job_result
61
              for success, job_result in jex.GetResults()
62
              if not success]
63

  
64
  if len(bad_jobs) > 0:
65
    for job in bad_jobs:
66
      cli.ToStdout("Job failed, result is '%s'.", job)
67
    cli.ToStdout("%s job(s) failed.", bad_jobs)
68
    return constants.EXIT_FAILURE
69
  else:
70
    return constants.EXIT_SUCCESS

Also available in: Unified diff