From 7c83c4f48804a253dcee474f2607394dff98df2a Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Mon, 27 Jun 2005 09:49:28 +0000 Subject: [PATCH] - Add Database vendor to CatalogRes tuple for Python. - Update doc - Implement DoesVolumeExist(Vol) for Python. - Prevent python command from seg faulting if no arg given. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@2167 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/examples/python/DirStartUp.py | 5 +++- bacula/kes-1.37 | 7 +++++ bacula/src/cats/protos.h | 4 +-- bacula/src/cats/sql_get.c | 28 +++++++++--------- bacula/src/dird/pythondir.c | 43 +++++++++++++++++++++++++--- bacula/src/dird/ua_cmds.c | 2 +- bacula/src/lib/pythonlib.c | 10 ++++--- 7 files changed, 73 insertions(+), 26 deletions(-) diff --git a/bacula/examples/python/DirStartUp.py b/bacula/examples/python/DirStartUp.py index 5a9942b66c..e364f7b8ee 100644 --- a/bacula/examples/python/DirStartUp.py +++ b/bacula/examples/python/DirStartUp.py @@ -62,9 +62,12 @@ class JobEvents: jobid = job.JobId client = job.Client numvol = job.NumVols; + print job.CatalogRes job.JobReport = "JobId=%d Client=%s NumVols=%d" % (jobid, client, numvol) job.JobReport="Python before New Volume set for Job.\n" - job.VolumeName="TestA-001" + Vol = "TestA-%d" % numvol + job.JobReport = "Exists=%d TestA-%d" % (job.DoesVolumeExist(Vol), numvol) + job.VolumeName="TestA-%d" % numvol job.JobReport="Python after New Volume set for Job.\n" return 1 diff --git a/bacula/kes-1.37 b/bacula/kes-1.37 index d180aec85a..0ec0c7c862 100644 --- a/bacula/kes-1.37 +++ b/bacula/kes-1.37 @@ -3,6 +3,13 @@ General: +Changes to 1.37.27: +27Jun05 +- Add Database vendor to CatalogRes tuple for Python. +- Update doc +- Implement DoesVolumeExist(Vol) for Python. +- Prevent python command from seg faulting if no arg given. + Changes to 1.37.26: 26Jun05 - Add set_mode method in DEVICE. diff --git a/bacula/src/cats/protos.h b/bacula/src/cats/protos.h index 2712ed76a6..bc301d22ef 100644 --- a/bacula/src/cats/protos.h +++ b/bacula/src/cats/protos.h @@ -76,12 +76,12 @@ int db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr); int db_get_job_volume_names(JCR *jcr, B_DB *mdb, JobId_t JobId, POOLMEM **VolumeNames); int db_get_file_attributes_record(JCR *jcr, B_DB *mdb, char *fname, JOB_DBR *jr, FILE_DBR *fdbr); int db_get_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr); -int db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr); +bool db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr); int db_get_num_media_records(JCR *jcr, B_DB *mdb); int db_get_num_pool_records(JCR *jcr, B_DB *mdb); int db_get_pool_ids(JCR *jcr, B_DB *mdb, int *num_ids, DBId_t **ids); int db_get_client_ids(JCR *jcr, B_DB *mdb, int *num_ids, DBId_t **ids); -int db_get_media_ids(JCR *jcr, B_DB *mdb, DBId_t PoolId, int *num_ids, uint32_t **ids); +bool db_get_media_ids(JCR *jcr, B_DB *mdb, DBId_t PoolId, int *num_ids, uint32_t **ids); int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS **VolParams); int db_get_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cdbr); int db_get_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr); diff --git a/bacula/src/cats/sql_get.c b/bacula/src/cats/sql_get.c index acd1d886cf..425e1a003c 100644 --- a/bacula/src/cats/sql_get.c +++ b/bacula/src/cats/sql_get.c @@ -782,16 +782,16 @@ int db_get_num_media_records(JCR *jcr, B_DB *mdb) * the current Pool. * The caller must free ids if non-NULL. * - * Returns 0: on failure - * 1: on success + * Returns false: on failure + * true: on success */ -int db_get_media_ids(JCR *jcr, B_DB *mdb, uint32_t PoolId, int *num_ids, uint32_t *ids[]) +bool db_get_media_ids(JCR *jcr, B_DB *mdb, uint32_t PoolId, int *num_ids, uint32_t *ids[]) { SQL_ROW row; - int stat = 0; int i = 0; uint32_t *id; char ed1[50]; + bool ok = false; db_lock(mdb); *ids = NULL; @@ -807,34 +807,34 @@ int db_get_media_ids(JCR *jcr, B_DB *mdb, uint32_t PoolId, int *num_ids, uint32_ *ids = id; } sql_free_result(mdb); - stat = 1; + ok = true; } else { Mmsg(mdb->errmsg, _("Media id select failed: ERR=%s\n"), sql_strerror(mdb)); Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg); - stat = 0; + ok = false; } db_unlock(mdb); - return stat; + return ok; } /* Get Media Record * - * Returns: 0 on failure - * id on success + * Returns: false: on failure + * true: on success */ -int db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) +bool db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) { SQL_ROW row; - int stat = 0; char ed1[50]; + bool ok = false; db_lock(mdb); if (mr->MediaId == 0 && mr->VolumeName[0] == 0) { Mmsg(mdb->cmd, "SELECT count(*) from Media"); mr->MediaId = get_sql_record_max(jcr, mdb); db_unlock(mdb); - return 1; + return true; } if (mr->MediaId != 0) { /* find by id */ Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks," @@ -898,7 +898,7 @@ int db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) bstrncpy(mr->cLabelDate, row[27]!=NULL?row[27]:"", sizeof(mr->cLabelDate)); mr->LabelDate = (time_t)str_to_utime(mr->cLabelDate); mr->StorageId = str_to_int64(row[28]); - stat = mr->MediaId; + ok = true; } } else { if (mr->MediaId != 0) { @@ -919,7 +919,7 @@ int db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) mr->VolumeName); } } db_unlock(mdb); - return stat; + return ok; } diff --git a/bacula/src/dird/pythondir.c b/bacula/src/dird/pythondir.c index 749d4b922b..f8dec89911 100644 --- a/bacula/src/dird/pythondir.c +++ b/bacula/src/dird/pythondir.c @@ -39,12 +39,14 @@ static PyObject *set_job_events(PyObject *self, PyObject *arg); static PyObject *job_run(PyObject *self, PyObject *arg); static PyObject *job_write(PyObject *self, PyObject *arg); static PyObject *job_cancel(PyObject *self, PyObject *arg); +static PyObject *job_does_vol_exist(PyObject *self, PyObject *arg); PyMethodDef JobMethods[] = { {"set_events", set_job_events, METH_VARARGS, "Set Job events"}, {"run", job_run, METH_VARARGS, "Run a Job"}, {"write", job_write, METH_VARARGS, "Write to output"}, {"cancel", job_cancel, METH_VARARGS, "Cancel a Job"}, + {"DoesVolumeExist", job_does_vol_exist, METH_VARARGS, "Does Volume Exist"}, {NULL, NULL, 0, NULL} /* last item */ }; @@ -73,7 +75,7 @@ static struct s_vars getvars[] = { { N_("Version"), "(ss)"}, { N_("ConfigFile"), "s"}, { N_("WorkingDir"), "s"}, - { N_("CatalogRes"), "(sssssi)"}, + { N_("CatalogRes"), "(sssssis)"}, { NULL, NULL} }; @@ -130,7 +132,16 @@ PyObject *job_getattr(PyObject *self, char *attrname) case 5: /* Client */ return Py_BuildValue(getvars[i].fmt, jcr->client->hdr.name); case 6: /* NumVols */ - return Py_BuildValue(getvars[i].fmt, jcr->NumVols); + POOL_DBR pr; + memset(&pr, 0, sizeof(pr)); + bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name)); + if (db_get_pool_record(jcr, jcr->db, &pr)) { + jcr->NumVols = pr.NumVols; + return Py_BuildValue(getvars[i].fmt, jcr->NumVols); + } else { + bsnprintf(errmsg, sizeof(errmsg), "Pool record not found."); + goto bail_out; + } case 7: /* Pool */ return Py_BuildValue(getvars[i].fmt, jcr->pool->hdr.name); case 8: /* Storage */ @@ -157,7 +168,8 @@ PyObject *job_getattr(PyObject *self, char *attrname) return Py_BuildValue(getvars[i].fmt, jcr->catalog->db_name, jcr->catalog->db_address, jcr->catalog->db_user, jcr->catalog->db_password, - jcr->catalog->db_socket, jcr->catalog->db_port); + jcr->catalog->db_socket, jcr->catalog->db_port, + catalog_db); } bsnprintf(errmsg, sizeof(errmsg), "Attribute %s not found.", attrname); @@ -291,12 +303,35 @@ static PyObject *job_write(PyObject *self, PyObject *args) return NULL; } if (text) { - Jmsg(NULL, M_INFO, 0, "%s", text); + JCR *jcr = get_jcr_from_PyObject(self); + Jmsg(jcr, M_INFO, 0, "%s", text); + } + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject *job_does_vol_exist(PyObject *self, PyObject *args) +{ + char *VolName = NULL; + + if (!PyArg_ParseTuple(args, "s:does_volume_exist", &VolName)) { + Dmsg0(000, "Parse tuple error in job_does_vol_exist\n"); + return NULL; + } + if (VolName) { + MEDIA_DBR mr; + int ok; + JCR *jcr = get_jcr_from_PyObject(self); + memset(&mr, 0, sizeof(mr)); + bstrncpy(mr.VolumeName, VolName, sizeof(mr.VolumeName)); + ok = db_get_media_record(jcr, jcr->db, &mr); + return Py_BuildValue("i", ok); } Py_INCREF(Py_None); return Py_None; } + static PyObject *job_cancel(PyObject *self, PyObject *args) { JobId_t JobId = 0; diff --git a/bacula/src/dird/ua_cmds.c b/bacula/src/dird/ua_cmds.c index 8d243c888d..38a2655e41 100644 --- a/bacula/src/dird/ua_cmds.c +++ b/bacula/src/dird/ua_cmds.c @@ -559,7 +559,7 @@ extern DIRRES *director; */ static int python_cmd(UAContext *ua, const char *cmd) { - if (strcasecmp(ua->argk[1], _("restart")) == 0) { + if (ua->argc >= 1 && strcasecmp(ua->argk[1], _("restart")) == 0) { term_python_interpreter(); init_python_interpreter(director->hdr.name, director->scripts_directory, "DirStartUp"); diff --git a/bacula/src/lib/pythonlib.c b/bacula/src/lib/pythonlib.c index 8d00e343d9..8cf4981c5a 100644 --- a/bacula/src/lib/pythonlib.c +++ b/bacula/src/lib/pythonlib.c @@ -277,10 +277,12 @@ int generate_daemon_event(JCR *jcr, const char *event) } bail_out: - Py_XDECREF((PyObject *)jcr->Python_job); - jcr->Python_job = NULL; - Py_XDECREF((PyObject *)jcr->Python_events); - jcr->Python_events = NULL; + if (jcr) { + Py_XDECREF((PyObject *)jcr->Python_job); + jcr->Python_job = NULL; + Py_XDECREF((PyObject *)jcr->Python_events); + jcr->Python_events = NULL; + } /* Fall through */ jobstart_ok: Py_XDECREF(result); -- 2.39.5