Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / client_utils.py @ 7db266bb

History | View | Annotate | Download (2.5 kB)

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

24 20b509de Michael Hanselmann
"""
25 20b509de Michael Hanselmann
26 20b509de Michael Hanselmann
from ganeti import constants
27 20b509de Michael Hanselmann
from ganeti import cli
28 20b509de Michael Hanselmann
29 20b509de Michael Hanselmann
from ganeti.rapi import client
30 20b509de Michael Hanselmann
31 20b509de Michael Hanselmann
# Local constant to avoid importing ganeti.http
32 20b509de Michael Hanselmann
HTTP_NOT_FOUND = 404
33 20b509de Michael Hanselmann
34 20b509de Michael Hanselmann
35 20b509de Michael Hanselmann
class RapiJobPollCb(cli.JobPollCbBase):
36 20b509de Michael Hanselmann
  def __init__(self, cl):
37 20b509de Michael Hanselmann
    """Initializes this class.
38 20b509de Michael Hanselmann

39 20b509de Michael Hanselmann
    @param cl: RAPI client instance
40 20b509de Michael Hanselmann

41 20b509de Michael Hanselmann
    """
42 20b509de Michael Hanselmann
    cli.JobPollCbBase.__init__(self)
43 20b509de Michael Hanselmann
44 20b509de Michael Hanselmann
    self.cl = cl
45 20b509de Michael Hanselmann
46 20b509de Michael Hanselmann
  def WaitForJobChangeOnce(self, job_id, fields,
47 20b509de Michael Hanselmann
                           prev_job_info, prev_log_serial):
48 20b509de Michael Hanselmann
    """Waits for changes on a job.
49 20b509de Michael Hanselmann

50 20b509de Michael Hanselmann
    """
51 20b509de Michael Hanselmann
    try:
52 20b509de Michael Hanselmann
      result = self.cl.WaitForJobChange(job_id, fields,
53 20b509de Michael Hanselmann
                                        prev_job_info, prev_log_serial)
54 20b509de Michael Hanselmann
    except client.GanetiApiError, err:
55 20b509de Michael Hanselmann
      if err.code == HTTP_NOT_FOUND:
56 20b509de Michael Hanselmann
        return None
57 20b509de Michael Hanselmann
58 20b509de Michael Hanselmann
      raise
59 20b509de Michael Hanselmann
60 20b509de Michael Hanselmann
    if result is None:
61 20b509de Michael Hanselmann
      return constants.JOB_NOTCHANGED
62 20b509de Michael Hanselmann
63 20b509de Michael Hanselmann
    return (result["job_info"], result["log_entries"])
64 20b509de Michael Hanselmann
65 20b509de Michael Hanselmann
  def QueryJobs(self, job_ids, fields):
66 20b509de Michael Hanselmann
    """Returns the given fields for the selected job IDs.
67 20b509de Michael Hanselmann

68 20b509de Michael Hanselmann
    @type job_ids: list of numbers
69 20b509de Michael Hanselmann
    @param job_ids: Job IDs
70 20b509de Michael Hanselmann
    @type fields: list of strings
71 20b509de Michael Hanselmann
    @param fields: Fields
72 20b509de Michael Hanselmann

73 20b509de Michael Hanselmann
    """
74 20b509de Michael Hanselmann
    if len(job_ids) != 1:
75 20b509de Michael Hanselmann
      raise NotImplementedError("Only one job supported at this time")
76 20b509de Michael Hanselmann
77 20b509de Michael Hanselmann
    try:
78 20b509de Michael Hanselmann
      result = self.cl.GetJobStatus(job_ids[0])
79 20b509de Michael Hanselmann
    except client.GanetiApiError, err:
80 20b509de Michael Hanselmann
      if err.code == HTTP_NOT_FOUND:
81 20b509de Michael Hanselmann
        return [None]
82 20b509de Michael Hanselmann
83 20b509de Michael Hanselmann
      raise
84 20b509de Michael Hanselmann
85 20b509de Michael Hanselmann
    return [[result[name] for name in fields], ]
86 20b509de Michael Hanselmann
87 20b509de Michael Hanselmann
88 20b509de Michael Hanselmann
def PollJob(rapi_client, job_id, reporter):
89 20b509de Michael Hanselmann
  """Function to poll for the result of a job.
90 20b509de Michael Hanselmann

91 20b509de Michael Hanselmann
  @param rapi_client: RAPI client instance
92 20b509de Michael Hanselmann
  @type job_id: number
93 20b509de Michael Hanselmann
  @param job_id: Job ID
94 20b509de Michael Hanselmann
  @type reporter: L{cli.JobPollReportCbBase}
95 20b509de Michael Hanselmann
  @param reporter: PollJob reporter instance
96 20b509de Michael Hanselmann

97 20b509de Michael Hanselmann
  """
98 20b509de Michael Hanselmann
  return cli.GenericPollJob(job_id, RapiJobPollCb(rapi_client), reporter)