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 """Utils for CLI commands"""
32
33 from ganeti import cli
34 from ganeti import constants
35 from ganeti import ht
36
37
39 """Waits for jobs and returns whether they have succeeded
40
41 Some OpCodes return of list of jobs. This function can be used
42 after issueing a given OpCode to look at the OpCode's result and, if
43 it is of type L{ht.TJobIdListOnly}, then it will wait for the jobs
44 to complete, otherwise just return L{constants.EXIT_SUCCESS}.
45
46 @type cl: L{ganeti.luxi.Client}
47 @param cl: client that was used to submit the OpCode, which will
48 also be used to poll the jobs
49
50 @param opts: CLI options
51
52 @param result: result of the opcode which might contain job
53 information, in which case the jobs will be polled, or simply
54 the result of the opcode
55
56 @rtype: int
57 @return: L{constants.EXIT_SUCCESS} if all jobs completed
58 successfully, L{constants.EXIT_FAILURE} otherwise
59
60 """
61 if not ht.TJobIdListOnly(result):
62 return constants.EXIT_SUCCESS
63
64 jex = cli.JobExecutor(cl=cl, opts=opts)
65
66 for (status, job_id) in result[constants.JOB_IDS_KEY]:
67 jex.AddJobId(None, status, job_id)
68
69 bad_jobs = [job_result
70 for success, job_result in jex.GetResults()
71 if not success]
72
73 if len(bad_jobs) > 0:
74 for job in bad_jobs:
75 cli.ToStdout("Job failed, result is '%s'.", job)
76 cli.ToStdout("%s job(s) failed.", bad_jobs)
77 return constants.EXIT_FAILURE
78 else:
79 return constants.EXIT_SUCCESS
80