77 char BuildVersion[]=
"buckets build version: " VERSION_S
" r(" COMMIT_HASH_S
").\n";
83 int main(
int argc,
char **argv)
85 char *agentDesc =
"Bucket agent";
88 int ReadFromStdin = 1;
89 int head_uploadtree_pk = 0;
94 char *Delims =
",= \t\n\r";
95 char *token, *saveptr;
97 int nomos_agent_pk = 0;
98 int bucketpool_pk = 0;
104 char *bucketpool_name;
108 char agent_rev[myBUFSIZ];
124 while ((cmdopt = getopt(argc, argv,
"rin:p:t:u:vc:hV")) != -1)
133 bucketpool_name = optarg;
139 printf(
"%s is not an active bucketpool name.\n", bucketpool_name);
144 bucketpool_pk = atoi(optarg);
146 sprintf(sqlbuf,
"select bucketpool_pk from bucketpool where bucketpool_pk=%d and active='Y'", bucketpool_pk);
149 printf(
"%d is not an active bucketpool_pk.\n", atoi(optarg));
154 head_uploadtree_pk = atoi(optarg);
156 sprintf(sqlbuf,
"select uploadtree_pk from uploadtree where uploadtree_pk=%d", head_uploadtree_pk);
158 if (!head_uploadtree_pk)
159 printf(
"%d is not an active uploadtree_pk.\n", atoi(optarg));
163 if (!head_uploadtree_pk)
167 sprintf(sqlbuf,
"select upload_pk from upload where upload_pk=%d", uploadtree.
upload_fk);
170 printf(
"%d is not an valid upload_pk.\n", atoi(optarg));
173 sprintf(sqlbuf,
"select uploadtree_pk from uploadtree where upload_fk=%d and parent is null", uploadtree.
upload_fk);
198 if (!bucketpool_pk && !ReadFromStdin)
200 printf(
"FATAL: You must specify an active bucketpool.\n");
204 if (!head_uploadtree_pk && !ReadFromStdin)
206 printf(
"FATAL: You must specify a valid uploadtree_pk or upload_pk.\n");
216 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
227 printf(
"FATAL: Bucket agent could not allocate license_ref table cache.\n");
246 while (token && (!uploadtree.
upload_fk || !bucketpool_pk))
248 if (strcmp(token,
"bppk") == 0)
250 bucketpool_pk = atoi(strtok_r(NULL, Delims, &saveptr));
253 if (strcmp(token,
"upk") == 0)
255 uploadtree.
upload_fk = atoi(strtok_r(NULL, Delims, &saveptr));
257 token = strtok_r(NULL, Delims, &saveptr);
263 LOG_ERROR(
"You have no update permissions on upload %d", uploadtree.
upload_fk);
268 sprintf(sqlbuf,
"select uploadtree_pk, pfile_fk, ufile_name, ufile_mode,lft,rgt from uploadtree \ 269 where upload_fk='%d' and parent is null limit 1", uploadtree.
upload_fk);
270 topresult = PQexec(pgConn, sqlbuf);
271 if (
fo_checkPQresult(pgConn, topresult, sqlbuf, agentDesc, __LINE__))
return -1;
272 if (PQntuples(topresult) == 0)
274 printf(
"ERROR: %s.%s missing upload_pk %d.\nsql: %s",
275 __FILE__, agentDesc, uploadtree.
upload_fk, sqlbuf);
279 head_uploadtree_pk = atol(PQgetvalue(topresult, 0, 0));
282 uploadtree.
pfile_fk = atol(PQgetvalue(topresult, 0, 1));
283 uploadtree.
ufile_name = strdup(PQgetvalue(topresult, 0, 2));
284 uploadtree.
ufile_mode = atoi(PQgetvalue(topresult, 0, 3));
285 uploadtree.
lft = atoi(PQgetvalue(topresult, 0, 4));
286 uploadtree.
rgt = atoi(PQgetvalue(topresult, 0, 5));
292 if (readnum > 1)
break;
297 sprintf(sqlbuf,
"select pfile_fk, ufile_name, ufile_mode,lft,rgt, upload_fk from uploadtree where uploadtree_pk=%d", head_uploadtree_pk);
298 topresult = PQexec(pgConn, sqlbuf);
304 if (PQntuples(topresult) == 0)
306 printf(
"FATAL: %s.%s missing root uploadtree_pk %d\n",
307 __FILE__, agentDesc, head_uploadtree_pk);
312 uploadtree.
pfile_fk = atol(PQgetvalue(topresult, 0, 0));
313 uploadtree.
ufile_name = strdup(PQgetvalue(topresult, 0, 1));
314 uploadtree.
ufile_mode = atoi(PQgetvalue(topresult, 0, 2));
315 uploadtree.
lft = atoi(PQgetvalue(topresult, 0, 3));
316 uploadtree.
rgt = atoi(PQgetvalue(topresult, 0, 4));
317 uploadtree.
upload_fk = atoi(PQgetvalue(topresult, 0, 5));
325 if (nomos_agent_pk == 0)
327 printf(
"WARNING: Bucket agent called on treeitem (%d), but the latest nomos agent hasn't created any license data for this tree.\n",
343 if (1 == rerun)
break;
344 printf(
"LOG: Duplicate request for bucket agent to process upload_pk: %d, uploadtree_pk: %d, bucketpool_pk: %d, bucket agent_pk: %d, nomos agent_pk: %d, pfile_pk: %d ignored.\n",
345 uploadtree.
upload_fk, head_uploadtree_pk, bucketpool_pk, agent_pk, nomos_agent_pk, uploadtree.
pfile_fk);
354 bucketDefArray =
initBuckets(pgConn, bucketpool_pk, &cacheroot);
355 if (bucketDefArray == 0)
357 printf(
"FATAL: %s.%d Bucket definition for pool %d could not be initialized.\n",
358 __FILE__, __LINE__, bucketpool_pk);
366 if (!(uploadtree_tablename))
368 LOG_FATAL(
"buckets passed invalid upload, upload_pk = %d", uploadtree.
upload_fk);
373 for (tmpbucketDefArray = bucketDefArray; tmpbucketDefArray->
bucket_pk; tmpbucketDefArray++)
380 for (tmpbucketDefArray = bucketDefArray; tmpbucketDefArray->
bucket_pk; tmpbucketDefArray++)
390 sprintf(sqlbuf,
"select mimetype_pk from mimetype where mimetype_name='application/x-debian-package'");
391 result = PQexec(pgConn, sqlbuf);
392 if (
fo_checkPQresult(pgConn, result, sqlbuf, __FILE__, __LINE__))
return -1;
393 if (PQntuples(result) == 0)
395 printf(
"FATAL: (%s.%d) Missing application/x-debian-package mimetype.\n",__FILE__,__LINE__);
401 sprintf(sqlbuf,
"select mimetype_pk from mimetype where mimetype_name='application/x-debian-source'");
402 result = PQexec(pgConn, sqlbuf);
403 if (
fo_checkPQresult(pgConn, result, sqlbuf, __FILE__, __LINE__))
return -1;
404 if (PQntuples(result) == 0)
406 printf(
"FATAL: (%s.%d) Missing application/x-debian-source mimetype.\n",__FILE__,__LINE__);
415 snprintf(sqlbuf,
sizeof(sqlbuf),
416 "insert into bucket_ars (agent_fk, upload_fk, ars_success, nomosagent_fk, bucketpool_fk) values(%d,%d,'%s',%d,%d)",
417 agent_pk, uploadtree.
upload_fk,
"false", nomos_agent_pk, bucketpool_pk);
419 printf(
"%s(%d): %s\n", __FILE__, __LINE__, sqlbuf);
421 result = PQexec(pgConn, sqlbuf);
426 sprintf(sqlbuf,
"select ars_pk from bucket_ars where agent_fk='%d' and upload_fk='%d' and ars_success='%s' and nomosagent_fk='%d' \ 427 and bucketpool_fk='%d' and ars_endtime is null \ 428 order by ars_starttime desc limit 1",
429 agent_pk, uploadtree.
upload_fk,
"false", nomos_agent_pk, bucketpool_pk);
430 result = PQexec(pgConn, sqlbuf);
431 if (
fo_checkPQresult(pgConn, result, sqlbuf, __FILE__, __LINE__))
return -1;
432 if (PQntuples(result) == 0)
434 printf(
"FATAL: (%s.%d) Missing bucket_ars record.\n%s\n",__FILE__,__LINE__,sqlbuf);
437 ars_pk = atol(PQgetvalue(result, 0, 0));
442 if (
debug) printf(
"%s sql: %s\n",__FILE__, sqlbuf);
450 rv =
walkTree(pgConn, bucketDefArray, agent_pk, head_uploadtree_pk, 0,
453 if ((!rv) && (IsContainer(uploadtree.
ufile_mode)))
455 rv =
processFile(pgConn, bucketDefArray, &uploadtree, agent_pk, hasPrules);
459 if (0 == rerun && ars_pk)
462 snprintf(sqlbuf,
sizeof(sqlbuf),
463 "update bucket_ars set ars_endtime=now(), ars_success=false where ars_pk='%d'",
466 snprintf(sqlbuf,
sizeof(sqlbuf),
467 "update bucket_ars set ars_endtime=now(), ars_success=true where ars_pk='%d'",
471 printf(
"%s(%d): %s\n", __FILE__, __LINE__, sqlbuf);
473 result = PQexec(pgConn, sqlbuf);
480 free(bucketDefArray);
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree"...
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int maxnodes
No. of nodes in the list.
cachenode_t * nodes
Array of nodes.
int main(int argc, char **argv)
PGconn * pgConn
Database connection.
char * fo_scheduler_current()
Get the last read string from the scheduler.
FUNCTION int LatestNomosAgent(PGconn *pgConn, int upload_pk)
Get the latest nomos agent_pk that has data for this this uploadtree.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
FUNCTION int processFile(PGconn *pgConn, pbucketdef_t bucketDefArray, puploadtree_t puploadtree, int agent_pk, int hasPrules)
Process a file.
FUNCTION int getBucketpool_pk(PGconn *pgConn, char *bucketpool_name)
Get a bucketpool_pk based on the bucketpool_name.
FUNCTION void lrcache_free(cacheroot_t *pcroot)
Free the hash table.
FUNCTION int validate_pk(PGconn *pgConn, char *sql)
Verify a primary key exists.
FUNCTION int lrcache_init(PGconn *pgConn, cacheroot_t *pcroot)
Build a cache the license ref db table.
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
int verbose
The verbose flag for the cli.
Usage()
Print Usage statement.
Contains information required by uploadtree elements.
char * uploadtree_tablename
upload.uploadtree_tablename
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
FUNCTION int walkTree(PGconn *pgConn, pbucketdef_t bucketDefArray, int agent_pk, int uploadtree_pk, int skipProcessedCheck, int hasPrules)
This function does a recursive depth first walk through a file tree (uploadtree). ...
FUNCTION int UploadProcessed(PGconn *pgConn, int bucketagent_pk, int nomosagent_pk, int pfile_pk, int uploadtree_pk, int upload_pk, int bucketpool_pk)
Has this upload already been bucket processed? This function checks buckets_ars to see if the upload ...
FUNCTION pbucketdef_t initBuckets(PGconn *pgConn, int bucketpool_pk, cacheroot_t *pcroot)
Initialize the bucket definition list. If an error occured, write the error to stdout.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
#define PERM_WRITE
Read-Write permission.