]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/pythondir.c
- Move test for MaxStartDelay as suggested by Peter.
[bacula/bacula] / bacula / src / dird / pythondir.c
1 /*
2  *
3  * Bacula interface to Python for the Director
4  *
5  * Kern Sibbald, November MMIV
6  *
7  *   Version $Id$
8  *
9  */
10
11 /*
12    Copyright (C) 2004-2005 Kern Sibbald
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "dird.h"
33
34 #ifdef HAVE_PYTHON
35 #undef _POSIX_C_SOURCE
36 #include <Python.h>
37
38 extern JCR *get_jcr_from_PyObject(PyObject *self);
39 extern PyObject *find_method(PyObject *eventsObject, PyObject *method, 
40          const char *name);
41
42
43 static PyObject *set_job_events(PyObject *self, PyObject *arg);
44 static PyObject *job_run(PyObject *self, PyObject *arg);
45 static PyObject *job_write(PyObject *self, PyObject *arg);
46
47 PyMethodDef JobMethods[] = {
48     {"set_events", set_job_events, METH_VARARGS, "Set Job events"},
49     {"run", job_run, METH_VARARGS, "Run a Job"},
50     {"write", job_write, METH_VARARGS, "Write to output"},
51     {NULL, NULL, 0, NULL}             /* last item */
52 };
53  
54
55 struct s_vars {
56    const char *name;
57    char *fmt;
58 };
59
60 /* Read-only variables */
61 static struct s_vars getvars[] = {
62    { N_("Job"),        "s"},
63    { N_("DirName"),    "s"},
64    { N_("Level"),      "s"},
65    { N_("Type"),       "s"},
66    { N_("JobId"),      "i"},
67    { N_("Client"),     "s"},
68    { N_("NumVols"),    "i"},
69    { N_("Pool"),       "s"},
70    { N_("Storage"),    "s"},
71    { N_("Catalog"),    "s"},
72    { N_("MediaType"),  "s"},
73    { N_("JobName"),    "s"},
74    { N_("JobStatus"),  "s"},
75    { N_("Priority"),   "i"},
76
77    { NULL,             NULL}
78 };
79
80 /* Writable variables */
81 static struct s_vars setvars[] = {
82    { N_("JobReport"),   "s"},
83    { N_("VolumeName"),  "s"},
84    { N_("Priority"),    "i"},
85
86    { NULL,             NULL}
87 };
88
89
90 /* Return Job variables */
91 /* Returns:  NULL if error
92  *           PyObject * return value if OK
93  */
94 PyObject *job_getattr(PyObject *self, char *attrname)
95 {
96    JCR *jcr;
97    bool found = false;
98    int i;
99    char buf[10];
100    char errmsg[200];
101
102    Dmsg0(100, "In job_getattr.\n");
103    jcr = get_jcr_from_PyObject(self);
104    if (!jcr) {
105       bstrncpy(errmsg, "Job pointer not found.", sizeof(errmsg));
106       goto bail_out;
107    }
108    for (i=0; getvars[i].name; i++) {
109       if (strcmp(getvars[i].name, attrname) == 0) {
110          found = true;
111          break;
112       }
113    }
114    if (!found) {
115       /* Try our methods */
116       return Py_FindMethod(JobMethods, self, attrname);
117    }
118    switch (i) {
119    case 0:                            /* Job */
120       return Py_BuildValue(getvars[i].fmt, jcr->job->hdr.name);
121    case 1:                            /* Director's name */
122       return Py_BuildValue(getvars[i].fmt, my_name);
123    case 2:                            /* level */
124       return Py_BuildValue(getvars[i].fmt, job_level_to_str(jcr->JobLevel));
125    case 3:                            /* type */
126       return Py_BuildValue(getvars[i].fmt, job_type_to_str(jcr->JobType));
127    case 4:                            /* JobId */
128       return Py_BuildValue(getvars[i].fmt, jcr->JobId);
129    case 5:                            /* Client */
130       return Py_BuildValue(getvars[i].fmt, jcr->client->hdr.name);
131    case 6:                            /* NumVols */
132       return Py_BuildValue(getvars[i].fmt, jcr->NumVols);
133    case 7:                            /* Pool */
134       return Py_BuildValue(getvars[i].fmt, jcr->pool->hdr.name);
135    case 8:                            /* Storage */
136       return Py_BuildValue(getvars[i].fmt, jcr->store->hdr.name);
137    case 9:
138       return Py_BuildValue(getvars[i].fmt, jcr->catalog->hdr.name);
139    case 10:                           /* MediaType */
140       return Py_BuildValue(getvars[i].fmt, jcr->store->media_type);
141    case 11:                           /* JobName */
142       return Py_BuildValue(getvars[i].fmt, jcr->Job);
143    case 12:                           /* JobStatus */
144       buf[1] = 0;
145       buf[0] = jcr->JobStatus;
146       return Py_BuildValue(getvars[i].fmt, buf);
147    case 13:                           /* Priority */
148       return Py_BuildValue(getvars[i].fmt, jcr->JobPriority);
149    }
150    bsnprintf(errmsg, sizeof(errmsg), "Attribute %s not found.", attrname);
151 bail_out:
152    PyErr_SetString(PyExc_AttributeError, errmsg);
153    return NULL;
154 }
155
156
157 /* Set Job variables */
158 /*  Returns:   0 for OK
159  *            -1 for error
160  */
161 int job_setattr(PyObject *self, char *attrname, PyObject *value)
162 {
163    JCR *jcr;
164    bool found = false;
165    char *strval = NULL;
166    int intval = 0;
167    int i;
168
169    Dmsg2(100, "In job_setattr=%s val=%p.\n", attrname, value);
170    if (value == NULL) {                /* Cannot delete variables */
171        goto bail_out;
172    }
173    jcr = get_jcr_from_PyObject(self);
174    if (!jcr) {
175       goto bail_out;
176    }
177
178    /* Find attribute name in list */
179    for (i=0; setvars[i].name; i++) {
180       if (strcmp(setvars[i].name, attrname) == 0) {
181          found = true;
182          break;
183       }
184    }
185    if (!found) {
186       goto bail_out;
187    }
188    /* Get argument value */
189    if (setvars[i].fmt != NULL) {
190       switch (setvars[i].fmt[0]) {
191       case 's':
192          if (!PyArg_Parse(value, setvars[i].fmt, &strval)) {
193             PyErr_SetString(PyExc_TypeError, "Read-only attribute");
194             return -1;
195          }
196          break;
197       case 'i':
198          if (!PyArg_Parse(value, setvars[i].fmt, &intval)) {
199             PyErr_SetString(PyExc_TypeError, "Read-only attribute");
200             return -1;
201          }
202          break;
203       }
204    }   
205    switch (i) {
206    case 0:                            /* JobReport */
207       Jmsg(jcr, M_INFO, 0, "%s", strval);
208       return 0;
209    case 1:                            /* VolumeName */
210       /* Make sure VolumeName is valid and we are in VolumeName event */
211       if (strcmp("NewVolume", jcr->event) == 0 &&
212           is_volume_name_legal(NULL, strval)) {
213          pm_strcpy(jcr->VolumeName, strval);
214          Dmsg1(100, "Set Vol=%s\n", strval);
215          return 0;
216       } else {
217          jcr->VolumeName[0] = 0;
218       }
219       break;
220    case 2:                            /* Priority */
221       Dmsg1(000, "Set priority=%d\n", intval);
222       return 0;
223    }
224 bail_out:
225    PyErr_SetString(PyExc_AttributeError, attrname);
226    return -1;
227 }
228
229
230 static PyObject *set_job_events(PyObject *self, PyObject *arg)
231 {
232    PyObject *eObject;
233    JCR *jcr;
234
235    Dmsg0(100, "In set_job_events.\n");
236    if (!PyArg_ParseTuple(arg, "O:set_events", &eObject)) {
237       Dmsg0(000, "Error in ParseTuple\n");
238       return NULL;
239    }
240    jcr = get_jcr_from_PyObject(self);
241    Py_XDECREF((PyObject *)jcr->Python_events);
242    Py_INCREF(eObject);
243    jcr->Python_events = (void *)eObject;
244    Py_INCREF(Py_None);
245    return Py_None;
246 }
247
248 /* Run a Bacula command */
249 static PyObject *job_run(PyObject *self, PyObject *arg)
250 {
251    JCR *jcr;
252    char *item;
253    int stat;
254
255    if (!PyArg_ParseTuple(arg, "s:run", &item)) {
256       Dmsg0(000, "Error in ParseTuple\n");
257       return NULL;
258    }
259    /* Release lock due to recursion */
260    PyEval_ReleaseLock();
261    jcr = get_jcr_from_PyObject(self);
262    UAContext *ua = new_ua_context(jcr);
263    ua->batch = true;
264    pm_strcpy(ua->cmd, item);          /* copy command */
265    parse_ua_args(ua);                 /* parse command */
266    stat = run_cmd(ua, ua->cmd);
267    free_ua_context(ua);
268    PyEval_AcquireLock();
269    return PyInt_FromLong((long)stat);
270 }
271
272 static PyObject *job_write(PyObject *self, PyObject *args)
273 {
274    char *text = NULL;
275
276    if (!PyArg_ParseTuple(args, "s:write", &text)) {
277       Dmsg0(000, "Parse tuple error in job_write\n");
278       return NULL;
279    }
280    if (text) {
281       Jmsg(NULL, M_INFO, 0, "%s", text);
282    }
283    Py_INCREF(Py_None);
284    return Py_None;
285 }
286
287
288 /*
289  * Generate a Job event, which means look up the event
290  *  method defined by the user, and if it exists, 
291  *  call it.
292  */
293 int generate_job_event(JCR *jcr, const char *event)
294 {
295    PyObject *method = NULL;
296    PyObject *Job = (PyObject *)jcr->Python_job;
297    PyObject *events = (PyObject *)jcr->Python_events;
298    PyObject *result = NULL;
299    int stat = 0;
300
301    if (!Job || !events) {
302       return 0;
303    }
304
305    PyEval_AcquireLock();
306
307    method = find_method(events, method, event);
308    if (!method) {
309       goto bail_out;
310    }
311
312    bstrncpy(jcr->event, event, sizeof(jcr->event));
313    result = PyObject_CallFunction(method, "O", Job);
314    jcr->event[0] = 0;             /* no event in progress */
315    if (result == NULL) {
316       if (PyErr_Occurred()) {
317          PyErr_Print();
318          Dmsg1(000, "Error in Python method %s\n", event);
319       }
320    } else {
321       stat = 1;
322    }
323    Py_XDECREF(result);
324
325 bail_out:
326    PyEval_ReleaseLock();
327    return stat;
328 }
329
330 bool python_set_prog(JCR*, char const*) { return false; }
331
332 #else
333
334 /* Dummy if Python not configured */
335 int generate_job_event(JCR *jcr, const char *event) { return 1; }
336    
337
338 #endif /* HAVE_PYTHON */