2 Bacula® - The Network Backup Solution
4 Copyright (C) 2004-2012 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula interface to Python for the Director
32 * Kern Sibbald, November MMIV
41 #undef _POSIX_C_SOURCE
44 #include <lib/pythonlib.h>
46 extern struct s_jl joblevels[];
48 static PyObject *set_job_events(PyObject *self, PyObject *arg);
49 static PyObject *job_run(PyObject *self, PyObject *arg);
50 static PyObject *job_write(PyObject *self, PyObject *arg);
51 static PyObject *job_cancel(PyObject *self, PyObject *arg);
52 static PyObject *job_does_vol_exist(PyObject *self, PyObject *arg);
54 PyMethodDef JobMethods[] = {
55 {"set_events", set_job_events, METH_VARARGS, "Set Job events"},
56 {"run", job_run, METH_VARARGS, "Run a Job"},
57 {"write", job_write, METH_VARARGS, "Write to output"},
58 {"cancel", job_cancel, METH_VARARGS, "Cancel a Job"},
59 {"DoesVolumeExist", job_does_vol_exist, METH_VARARGS, "Does Volume Exist"},
60 {NULL, NULL, 0, NULL} /* last item */
68 /* Read-only variables */
69 static struct s_vars getvars[] = {
84 { "CatalogRes", "(sssssis)"},
95 /* Writable variables */
96 static struct s_vars setvars[] = {
106 /* Return Job variables */
107 /* Returns: NULL if error
108 * PyObject * return value if OK
110 PyObject *job_getattr(PyObject *self, char *attrname)
118 Dmsg0(100, "In job_getattr.\n");
119 jcr = get_jcr_from_PyObject(self);
121 bstrncpy(errmsg, _("Job pointer not found."), sizeof(errmsg));
124 for (i=0; getvars[i].name; i++) {
125 if (strcmp(getvars[i].name, attrname) == 0) {
131 /* Try our methods */
132 return Py_FindMethod(JobMethods, self, attrname);
136 return Py_BuildValue((char *)getvars[i].fmt, jcr->job->hdr.name);
138 return Py_BuildValue((char *)getvars[i].fmt, job_level_to_str(jcr->getJobLevel()));
140 return Py_BuildValue((char *)getvars[i].fmt, job_type_to_str(jcr->getJobType()));
142 return Py_BuildValue((char *)getvars[i].fmt, jcr->JobId);
144 return Py_BuildValue((char *)getvars[i].fmt, jcr->client->hdr.name);
145 case 5: /* NumVols */
147 memset(&pr, 0, sizeof(pr));
148 bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
149 if (db_get_pool_record(jcr, jcr->db, &pr)) {
150 jcr->NumVols = pr.NumVols;
151 return Py_BuildValue((char *)getvars[i].fmt, jcr->NumVols);
153 bsnprintf(errmsg, sizeof(errmsg), _("Pool record not found."));
157 return Py_BuildValue((char *)getvars[i].fmt, jcr->pool->name());
158 case 7: /* Storage */
160 return Py_BuildValue((char *)getvars[i].fmt, jcr->wstore->name());
161 } else if (jcr->rstore) {
162 return Py_BuildValue((char *)getvars[i].fmt, jcr->rstore->name());
167 return Py_BuildValue((char *)getvars[i].fmt, jcr->catalog->name());
168 case 9: /* MediaType */
170 return Py_BuildValue((char *)getvars[i].fmt, jcr->wstore->media_type);
171 } else if (jcr->rstore) {
172 return Py_BuildValue((char *)getvars[i].fmt, jcr->rstore->media_type);
176 case 10: /* JobName */
177 return Py_BuildValue((char *)getvars[i].fmt, jcr->Job);
178 case 11: /* JobStatus */
180 buf[0] = jcr->JobStatus;
181 return Py_BuildValue((char *)getvars[i].fmt, buf);
182 case 12: /* Priority */
183 return Py_BuildValue((char *)getvars[i].fmt, jcr->JobPriority);
185 return Py_BuildValue((char *)getvars[i].fmt, jcr->VolumeName);
186 case 14: /* CatalogRes */
187 return Py_BuildValue((char *)getvars[i].fmt,
188 jcr->catalog->db_name, jcr->catalog->db_address,
189 jcr->catalog->db_user, jcr->catalog->db_password,
190 jcr->catalog->db_socket, jcr->catalog->db_port,
191 db_get_type(jcr->db));
192 case 15: /* JobErrors */
193 return Py_BuildValue((char *)getvars[i].fmt, jcr->JobErrors);
194 case 16: /* JobFiles */
195 return Py_BuildValue((char *)getvars[i].fmt, jcr->JobFiles);
196 case 17: /* SDJobFiles */
197 return Py_BuildValue((char *)getvars[i].fmt, jcr->SDJobFiles);
198 case 18: /* SDErrors */
199 return Py_BuildValue((char *)getvars[i].fmt, jcr->SDErrors);
200 case 19: /* FDJobStatus */
202 buf[0] = jcr->FDJobStatus;
203 return Py_BuildValue((char *)getvars[i].fmt, buf);
204 case 20: /* SDJobStatus */
206 buf[0] = jcr->SDJobStatus;
207 return Py_BuildValue((char *)getvars[i].fmt, buf);
209 bsnprintf(errmsg, sizeof(errmsg), _("Attribute %s not found."), attrname);
211 PyErr_SetString(PyExc_AttributeError, errmsg);
216 /* Set Job variables */
220 int job_setattr(PyObject *self, char *attrname, PyObject *value)
228 Dmsg2(100, "In job_setattr=%s val=%p.\n", attrname, value);
229 if (value == NULL) { /* Cannot delete variables */
232 jcr = get_jcr_from_PyObject(self);
237 /* Find attribute name in list */
238 for (i=0; setvars[i].name; i++) {
239 if (strcmp(setvars[i].name, attrname) == 0) {
247 /* Get argument value */
248 if (setvars[i].fmt != NULL) {
249 switch (setvars[i].fmt[0]) {
251 if (!PyArg_Parse(value, (char *)setvars[i].fmt, &strval)) {
252 PyErr_SetString(PyExc_TypeError, _("Read-only attribute"));
257 if (!PyArg_Parse(value, (char *)setvars[i].fmt, &intval)) {
258 PyErr_SetString(PyExc_TypeError, _("Read-only attribute"));
265 case 0: /* JobReport */
266 Jmsg(jcr, M_INFO, 0, "%s", strval);
268 case 1: /* VolumeName */
269 /* Make sure VolumeName is valid and we are in VolumeName event */
270 if (strcmp("NewVolume", jcr->event) == 0 &&
271 is_volume_name_legal(NULL, strval)) {
272 pm_strcpy(jcr->VolumeName, strval);
273 Dmsg1(100, "Set Vol=%s\n", strval);
276 jcr->VolumeName[0] = 0;
279 case 2: /* Priority */
280 Dmsg1(000, "Set priority=%d\n", intval);
281 if (intval >= 1 && intval <= 100) {
282 jcr->JobPriority = intval;
284 PyErr_SetString(PyExc_ValueError, _("Priority must be 1-100"));
287 case 3: /* Job Level */
288 if (strcmp("JobInit", jcr->event) != 0) {
289 PyErr_SetString(PyExc_RuntimeError, _("Job Level can be set only during JobInit"));
292 if (strval != NULL) {
293 for (i=0; joblevels[i].level_name; i++) {
294 if (strcmp(strval, joblevels[i].level_name) == 0) {
295 if (joblevels[i].job_type == jcr->getJobType()) {
296 jcr->setJobLevel(joblevels[i].level);
297 jcr->jr.JobLevel = jcr->getJobLevel();
303 PyErr_SetString(PyExc_ValueError, _("Bad JobLevel string"));
307 PyErr_SetString(PyExc_AttributeError, attrname);
312 * Set pointer to instantiated events class
314 static PyObject *set_job_events(PyObject *self, PyObject *arg)
319 Dmsg0(100, "In set_job_events.\n");
320 if (!PyArg_ParseTuple(arg, "O:set_events", &eObject)) {
321 Dmsg0(000, "Error in ParseTuple\n");
324 jcr = get_jcr_from_PyObject(self);
325 Py_XDECREF((PyObject *)jcr->Python_events);
327 jcr->Python_events = (void *)eObject;
332 /* Run a Bacula job */
333 static PyObject *job_run(PyObject *self, PyObject *arg)
339 if (!PyArg_ParseTuple(arg, "s:run", &item)) {
340 Dmsg0(000, "Error in ParseTuple\n");
343 /* Release lock due to recursion */
344 // PyEval_ReleaseLock();
345 jcr = get_jcr_from_PyObject(self);
346 UAContext *ua = new_ua_context(jcr);
348 pm_strcpy(ua->cmd, item); /* copy command */
349 parse_ua_args(ua); /* parse command */
350 stat = run_cmd(ua, ua->cmd);
352 // PyEval_AcquireLock();
353 return PyInt_FromLong((long)stat);
356 static PyObject *job_write(PyObject *self, PyObject *args)
360 if (!PyArg_ParseTuple(args, "s:write", &text)) {
361 Dmsg0(000, "Parse tuple error in job_write\n");
365 JCR *jcr = get_jcr_from_PyObject(self);
366 Jmsg(jcr, M_INFO, 0, "%s", text);
372 static PyObject *job_does_vol_exist(PyObject *self, PyObject *args)
374 char *VolName = NULL;
376 if (!PyArg_ParseTuple(args, "s:does_volume_exist", &VolName)) {
377 Dmsg0(000, "Parse tuple error in job_does_vol_exist\n");
383 JCR *jcr = get_jcr_from_PyObject(self);
384 bstrncpy(mr.VolumeName, VolName, sizeof(mr.VolumeName));
385 ok = db_get_media_record(jcr, jcr->db, &mr);
386 return Py_BuildValue("i", ok);
393 static PyObject *job_cancel(PyObject *self, PyObject *args)
399 if (!PyArg_ParseTuple(args, "i:cancel", &JobId)) {
400 Dmsg0(000, "Parse tuple error in job_write\n");
404 if (jcr->JobId == 0) {
407 if (jcr->JobId == JobId) {
412 /* endeach_jcr(jcr) not needed because freed below */
415 /* ***FIXME*** raise exception */
418 // PyEval_ReleaseLock();
419 UAContext *ua = new_ua_context(jcr);
421 if (!cancel_job(ua, jcr)) {
422 /* ***FIXME*** raise exception */
427 // PyEval_AcquireLock();
433 * Generate a Job event, which means look up the event
434 * method defined by the user, and if it exists,
437 int generate_job_event(JCR *jcr, const char *event)
439 PyObject *method = NULL;
440 PyObject *Job = (PyObject *)jcr->Python_job;
441 PyObject *events = (PyObject *)jcr->Python_events;
442 PyObject *result = NULL;
445 if (!Job || !events) {
450 // PyEval_AcquireLock();
452 method = find_method(events, method, event);
457 bstrncpy(jcr->event, event, sizeof(jcr->event));
458 result = PyObject_CallFunction(method, (char *)"O", Job);
459 jcr->event[0] = 0; /* no event in progress */
460 if (result == NULL) {
461 if (PyErr_Occurred()) {
463 Dmsg1(000, "Error in Python method %s\n", event);
472 // PyEval_ReleaseLock();
476 bool python_set_prog(JCR*, char const*) { return false; }
480 /* Dummy if Python not configured */
481 int generate_job_event(JCR *jcr, const char *event) { return 1; }
484 #endif /* HAVE_PYTHON */