root/trunk/tjobfunc.h

Revision 1691, 15.9 KB (checked in by Gary, 14 months ago)

unxsVZ working on template wizard.

  • Property svn:keywords set to id
Line 
1/*
2FILE
3        $Id$
4        (Built initially by unixservice.com mysqlRAD2)
5PURPOSE
6        Non schema-dependent table and application table related functions.
7AUTHOR/LEGAL
8        (C) 2001-2010 Gary Wallis for Unixservice, LLC.
9        GPLv2 license applies. See LICENSE file.
10 
11*/
12
13//ModuleFunctionProtos()
14
15static unsigned uForClient=0;
16static char cForClientPullDown[256]={""};
17static char cuClientPullDown[256]={""};
18static char cuDatacenterPullDown[256]={""};
19static char cuNodePullDown[256]={""};
20static char cuContainerPullDown[256]={""};
21static unsigned uMin=0,uHour=0,uDayOfWeek=0,uDayOfMonth=0,uMonth=0;
22static char cStartDate[32]={""};
23time_t luStartDate=0;
24
25void tJobNavList(void);
26char *strptime(const char *s, const char *format, struct tm *tm);
27time_t cStartDateToUnixTime(char *cStartDate);
28
29void ExtProcesstJobVars(pentry entries[], int x)
30{
31        register int i;
32        for(i=0;i<x;i++)
33        {
34                if(!strcmp(entries[i].name,"cuDatacenterPullDown"))
35                {
36                        sprintf(cuDatacenterPullDown,"%.255s",entries[i].val);
37                        uDatacenter=ReadPullDown("tDatacenter","cLabel",cuDatacenterPullDown);
38                }
39                else if(!strcmp(entries[i].name,"uForClient"))
40                        sscanf(entries[i].val,"%u",&uForClient);
41                else if(!strcmp(entries[i].name,"cuClientPullDown"))
42                {
43                        sprintf(cuClientPullDown,"%.255s",entries[i].val);
44                        uForClient=ReadPullDown("tClient","cLabel",cuClientPullDown);
45                }
46                else if(!strcmp(entries[i].name,"cuNodePullDown"))
47                {
48                        sprintf(cuNodePullDown,"%.255s",entries[i].val);
49                        uNode=ReadPullDown("tNode","cLabel",cuNodePullDown);
50                }
51                else if(!strcmp(entries[i].name,"cuContainerPullDown"))
52                {
53                        sprintf(cuContainerPullDown,"%.255s",entries[i].val);
54                        uContainer=ReadPullDown("tContainer","cLabel",cuContainerPullDown);
55                }
56                else if(!strcmp(entries[i].name,"cForClientPullDown"))
57                {
58                        sprintf(cForClientPullDown,"%.255s",entries[i].val);
59                        uForClient=ReadPullDown("tClient","cLabel",cForClientPullDown);
60                }
61                else if(!strcmp(entries[i].name,"uMin"))
62                        sscanf(entries[i].val,"%u",&uMin);
63                else if(!strcmp(entries[i].name,"uHour"))
64                        sscanf(entries[i].val,"%u",&uHour);
65                else if(!strcmp(entries[i].name,"uDayOfWeek"))
66                        sscanf(entries[i].val,"%u",&uDayOfWeek);
67                else if(!strcmp(entries[i].name,"uDayOfMonth"))
68                        sscanf(entries[i].val,"%u",&uDayOfMonth);
69                else if(!strcmp(entries[i].name,"uMonth"))
70                        sscanf(entries[i].val,"%u",&uMonth);
71                else if(!strcmp(entries[i].name,"cStartDate"))
72                        sprintf(cStartDate,"%.31s",entries[i].val);
73        }
74}//void ExtProcesstJobVars(pentry entries[], int x)
75
76
77void ExttJobCommands(pentry entries[], int x)
78{
79
80        if(!strcmp(gcFunction,"tJobTools"))
81        {
82                //ModuleFunctionProcess()
83
84                if(!strcmp(gcCommand,LANG_NB_NEW))
85                {
86                        if(guPermLevel>=12)
87                        {
88                                ProcesstJobVars(entries,x);
89                                guMode=2000;
90                                tJob(LANG_NB_CONFIRMNEW);
91                        }
92                }
93                else if(!strcmp(gcCommand,LANG_NB_CONFIRMNEW))
94                {
95                        if(guPermLevel>=12)
96                        {
97                                ProcesstJobVars(entries,x);
98
99                                guMode=2000;
100                                //Check entries here
101                                guMode=0;
102
103                                uJob=0;
104                                uCreatedBy=guLoginClient;
105                                uOwner=guCompany;
106                                uModBy=0;//Never modified
107                                uModDate=0;//Never modified
108                                NewtJob(0);
109                        }
110                }
111                else if(!strcmp(gcCommand,LANG_NB_DELETE))
112                {
113                        ProcesstJobVars(entries,x);
114                        if(uAllowDel(uOwner,uCreatedBy))
115                        {
116                                guMode=2001;
117                                tJob(LANG_NB_CONFIRMDEL);
118                        }
119                }
120                else if(!strcmp(gcCommand,LANG_NB_CONFIRMDEL))
121                {
122                        ProcesstJobVars(entries,x);
123                        if(uAllowDel(uOwner,uCreatedBy))
124                        {
125                                guMode=5;
126                                DeletetJob();
127                        }
128                }
129                else if(!strcmp(gcCommand,LANG_NB_MODIFY))
130                {
131                        ProcesstJobVars(entries,x);
132                        if(uAllowMod(uOwner,uCreatedBy))
133                        {
134                                guMode=2002;
135                                tJob(LANG_NB_CONFIRMMOD);
136                        }
137                }
138                else if(!strcmp(gcCommand,LANG_NB_CONFIRMMOD))
139                {
140                        ProcesstJobVars(entries,x);
141                        if(uAllowMod(uOwner,uCreatedBy))
142                        {
143                                guMode=2002;
144                                //Check entries here
145                                guMode=0;
146
147                                uModBy=guLoginClient;
148                                ModtJob();
149                        }
150                }
151
152                //Recurring Job Wizard
153                else if(!strcmp(gcCommand,"Recurring Job Wizard"))
154                {
155                        if(guPermLevel>=9)
156                        {
157                                ProcesstJobVars(entries,x);
158                                guMode=9001;
159                                tJob("Recurring job wizard step 1");
160                        }
161                        else
162                        {
163                                tJob("<blink>Error:</blink> Denied by permissions settings");
164                        }
165                }
166                else if(!strcmp(gcCommand,"Select Datacenter/Org"))
167                {
168                        if(guPermLevel>=9)
169                        {
170                                ProcesstJobVars(entries,x);
171                                guMode=9001;
172                                if(!uDatacenter)
173                                        tJob("<blink>Error:</blink> Must select a datacenter.");
174                                if(!uForClient)
175                                        tJob("<blink>Error:</blink> Must select an organization"
176                                                        " (company, NGO or similar.)");
177                                guMode=9002;
178                                tJob("Recurring job wizard step 2");
179                        }
180                        else
181                        {
182                                tJob("<blink>Error:</blink> Denied by permissions settings");
183                        }
184                }
185                else if(!strcmp(gcCommand,"Confirm Fields"))
186                {
187                        if(guPermLevel>=9)
188                        {
189
190                                unsigned uYear=0,uMon=0,uDay=0;
191                                char cBuffer[512];
192
193                                ProcesstJobVars(entries,x);
194                                guMode=9002;
195                                if(!uDatacenter)
196                                        tJob("<blink>Error:</blink> Must select a datacenter.");
197                                if(!uForClient)
198                                        tJob("<blink>Error:</blink> Must select an organization"
199                                                        " (company, NGO or similar.)");
200                                if(uContainer && !uNode)
201                                        tJob("<blink>Unexpected error:</blink> Must select a node if selecting container!");
202                                if(uNode)
203                                {
204                                        unsigned uNodeDatacenter=0;
205
206                                        sscanf(ForeignKey("tNode","uDatacenter",uNode),"%u",&uNodeDatacenter);
207                                        if(uDatacenter!=uNodeDatacenter)
208                                                tJob("<blink>Unexpected error:</blink> The specified node does not "
209                                                        "belong to the specified datacenter!");
210                                }
211                                if(uNode && uContainer)
212                                {
213                                        unsigned uNodeContainer=0;
214
215                                        sscanf(ForeignKey("tContainer","uNode",uContainer),"%u",&uNodeContainer);
216                                        if(uNode!=uNodeContainer)
217                                                tJob("<blink>Error:</blink> The specified container does not "
218                                                        "belong to the specified node.");
219                                }
220                                if(!uRecurringJob)
221                                        tJob("<blink>Error:</blink> Must specify a recurring job.");
222                                if(!cLabel[0])
223                                        tJob("<blink>Error:</blink> Must specify a cLabel.");
224                                if(uMin>59)
225                                        tJob("<blink>Error:</blink> Must specify a minute from 0-59.");
226                                if(uHour>24)
227                                        tJob("<blink>Error:</blink> Must specify an hour from 0-24. 0 for all hours.");
228                                if(uDayOfWeek>7)
229                                        tJob("<blink>Error:</blink> Must specify a day of week number from 0 to 7."
230                                                " 0 for all days. Sunday is day 1.");
231                                if(uDayOfMonth>31)
232                                        tJob("<blink>Error:</blink> Must specify a day of month number from 0-31."
233                                                " 0 for all days. If not 0 this value trumps and day of week number set.");
234                                if(uMonth>12)
235                                        tJob("<blink>Error:</blink> Must specify a month number from 0-12."
236                                                " 0 for all months.");
237                                if(!cStartDate[0])
238                                        tJob("<blink>Error:</blink> Must specify a year-month-day start date for "
239                                                                "initial job.");
240
241                                sscanf(cStartDate,"%u-%u-%u",&uYear,&uMon,&uDay);
242                                if(uYear>3010 || uYear<2010)
243                                        tJob("<blink>Error:</blink> Year out-of-range, ex. 2010-01-22");
244                                if(uMon>12 || uMon<1)
245                                        tJob("<blink>Error:</blink> Mon out-of-range, ex. 2010-01-22");
246                                if(uDay>31 || uDay<1)
247                                        tJob("<blink>Error:</blink> Day out-of-range, ex. 2010-01-22");
248
249                                luStartDate=cStartDateToUnixTime(cStartDate);
250                                if(luStartDate == (time_t)(-1))
251                                        tJob("<blink>Unexpected error:</blink> mktime() failed!");
252                                if(!luStartDate)
253                                        tJob("<blink>Unexpected error:</blink> luStartDate==0!");
254
255                                uJob=0;
256                                guMode=0;
257                                uOwner=uForClient;
258                                uCreatedBy=guLoginClient;
259                                sprintf(cJobName,"RecurringJob");
260                                cJobData=cBuffer;
261                                uJobStatus=uWAITING;
262                                sprintf(cJobData,"uMin=%u;\nuHour=%u;\nuDayOfWeek=%u;\nuDayOfMonth=%u;\nuMonth=%u;\n"
263                                                "cRecurringJob=%s;\n",
264                                                uMin,uHour,uDayOfWeek,uDayOfMonth,uMonth,cRecurringJobDropDown);
265                                if(uHour==24) uHour=0;//Adjust for start after posting correct value above.
266                                luStartDate+=(uMin*60)+(uHour*3600);
267                                uJobDate=luStartDate;
268                                NewtJob(1);
269                                if(uJob)
270                                        tJob("Recurring job added");
271                                else
272                                        tJob("<blink>Unexpected error:</blink> uJob==0!");
273                        }
274                }
275                else if(!strcmp(gcCommand,"Cancel"))
276                {
277                        guMode=0;
278                }
279        }
280
281}//void ExttJobCommands(pentry entries[], int x)
282
283
284void ExttJobButtons(void)
285{
286        OpenFieldSet("tJob Aux Panel",100);
287        switch(guMode)
288        {
289                case 9001:
290                        printf("<u>Recurring Job Datacenter and Owner</u><br>");
291                        printf("Select the datacenter and the company/organization (owner) of the job"
292                                " to be created.<p>");
293                        printf("<input type=submit class=largeButton title='Select datacenter and organization'"
294                                " name=gcCommand value='Select Datacenter/Org'>\n");
295                        printf("<p><input type=submit class=largeButton title='Cancel this operation'"
296                                " name=gcCommand value='Cancel'>\n");
297                break;
298
299                case 9002:
300                        printf("<u>Recurring Job Data</u><br>");
301                        printf("Enter the recurring job data."
302                                "<p>Starting date has to be in year-mon-day number format."
303                                "<p>Recurring hour/week-day/month-day and month numbers use number 0 as wildcard."
304                                " Day of month number trumps day of week number if used."
305                                "<p>The job label should be meaningful.<p>");
306                        printf("<input type=submit class=largeButton title='Select hardware node'"
307                                " name=gcCommand value='Confirm Fields'>\n");
308                        printf("<p><input type=submit class=largeButton title='Cancel this operation'"
309                                " name=gcCommand value='Cancel'>\n");
310                break;
311
312                case 2000:
313                        printf("<p><u>Enter/mod data</u><br>");
314                        printf(LANG_NBB_CONFIRMNEW);
315                break;
316
317                case 2001:
318                        printf("<p><u>Think twice</u><br>");
319                        printf(LANG_NBB_CONFIRMDEL);
320                break;
321
322                case 2002:
323                        printf("<p><u>Review changes</u><br>");
324                        printf(LANG_NBB_CONFIRMMOD);
325                break;
326
327                default:
328                        printf("<u>Table Tips</u><br>");
329                        printf("This is the job queue table. A good way to scale these infrastructure management"
330                                " systems is to use asynchronous job queues. Everything that needs to get done is queued"
331                                " here and then executed by the jobqueue.c on local or remote servers."
332                                "<p>Most jobs are created in their own areas, an exception are recurring jobs that are"
333                                " added here.");
334                        printf("<p><input title='Create a recurring job for containers, nodes or datacenters.'"
335                                        " type=submit class=largeButton"
336                                        " name=gcCommand value='Recurring Job Wizard'>\n");
337                        printf("<p><u>Record Context Info</u><br>");
338                        if(uJob)
339                                tJobNavList();
340        }
341        CloseFieldSet();
342
343}//void ExttJobButtons(void)
344
345
346void ExttJobAuxTable(void)
347{
348
349}//void ExttJobAuxTable(void)
350
351
352void ExttJobGetHook(entry gentries[], int x)
353{
354        register int i;
355
356        for(i=0;i<x;i++)
357        {
358                if(!strcmp(gentries[i].name,"uJob"))
359                {
360                        sscanf(gentries[i].val,"%u",&uJob);
361                        guMode=6;
362                }
363        }
364        tJob("");
365
366}//void ExttJobGetHook(entry gentries[], int x)
367
368
369void ExttJobSelect(void)
370{
371        ExtSelect("tJob",VAR_LIST_tJob);
372
373}//void ExttJobSelect(void)
374
375
376void ExttJobSelectRow(void)
377{
378        ExtSelectRow("tJob",VAR_LIST_tJob,uJob);
379
380}//void ExttJobSelectRow(void)
381
382
383void ExttJobListSelect(void)
384{
385        char cCat[512];
386
387        ExtListSelect("tJob",VAR_LIST_tJob);
388       
389        //Changes here must be reflected below in ExttJobListFilter()
390        if(!strcmp(gcFilter,"uJob"))
391        {
392                sscanf(gcCommand,"%u",&uJob);
393                if(guLoginClient==1 && guPermLevel>11)
394                        strcat(gcQuery," WHERE ");
395                else
396                        strcat(gcQuery," AND ");
397                sprintf(cCat,"tJob.uJob=%u",uJob);
398                strcat(gcQuery,cCat);
399        }
400        else if(!strcmp(gcFilter,"uContainer"))
401        {
402                sscanf(gcCommand,"%u",&uContainer);
403                if(guLoginClient==1 && guPermLevel>11)
404                        strcat(gcQuery," WHERE ");
405                else
406                        strcat(gcQuery," AND ");
407                sprintf(cCat,"tJob.uContainer=%u ORDER BY uJob",uContainer);
408                strcat(gcQuery,cCat);
409        }
410        else if(!strcmp(gcFilter,"cLabel"))
411        {
412                if(guLoginClient==1 && guPermLevel>11)
413                        strcat(gcQuery," WHERE ");
414                else
415                        strcat(gcQuery," AND ");
416                sprintf(cCat,"tJob.cLabel LIKE '%s' ORDER BY uJob",gcCommand);
417                strcat(gcQuery,cCat);
418        }
419        else if(1)
420        {
421                //None NO FILTER
422                strcpy(gcFilter,"None");
423                strcat(gcQuery," ORDER BY uJob");
424        }
425
426}//void ExttJobListSelect(void)
427
428
429void ExttJobListFilter(void)
430{
431        //Filter
432        printf("&nbsp;&nbsp;&nbsp;Filter on ");
433        printf("<select name=gcFilter>");
434        if(strcmp(gcFilter,"uJob"))
435                printf("<option>uJob</option>");
436        else
437                printf("<option selected>uJob</option>");
438        if(strcmp(gcFilter,"uContainer"))
439                printf("<option>uContainer</option>");
440        else
441                printf("<option selected>uContainer</option>");
442        if(strcmp(gcFilter,"cLabel"))
443                printf("<option>cLabel</option>");
444        else
445                printf("<option selected>cLabel</option>");
446        if(strcmp(gcFilter,"None"))
447                printf("<option>None</option>");
448        else
449                printf("<option selected>None</option>");
450        printf("</select>");
451
452}//void ExttJobListFilter(void)
453
454
455void ExttJobNavBar(void)
456{
457        if(guMode>=9000)
458                return;
459
460        printf(LANG_NBB_SKIPFIRST);
461        printf(LANG_NBB_SKIPBACK);
462        printf(LANG_NBB_SEARCH);
463
464        if(guPermLevel>=12 && !guListMode)
465                printf(LANG_NBB_NEW);
466
467        if(uAllowMod(uOwner,uCreatedBy) && !guListMode)
468                printf(LANG_NBB_MODIFY);
469
470        if(uAllowDel(uOwner,uCreatedBy) && !guListMode)
471                printf(LANG_NBB_DELETE);
472
473        if(uOwner)
474                printf(LANG_NBB_LIST);
475
476        printf(LANG_NBB_SKIPNEXT);
477        printf(LANG_NBB_SKIPLAST);
478        printf("&nbsp;&nbsp;&nbsp;\n");
479
480}//void ExttJobNavBar(void)
481
482
483void tJobNavList(void)
484{
485        MYSQL_RES *res;
486        MYSQL_ROW field;
487
488        //More magic numbers
489/*
490#define uWAITING        1
491#define uRUNNING        2
492#define uDONEOK         3
493#define uDONEERROR      4
494#define uSUSPENDED      5
495#define uREDUNDANT      6
496#define uCANCELED       7
497#define uREMOTEWAITING  10
498#define uERROR          14
499*/
500        if(guPermLevel<10)
501                sprintf(gcQuery,"SELECT tJob.uJob,tJob.cLabel,tNode.cLabel,tJobStatus.cLabel FROM tJob,tNode,tClient,tJobStatus WHERE"
502                                " tJob.uOwner=tClient.uClient AND tJob.uNode=tNode.uNode AND"
503                                " tJob.uJobStatus=tJobStatus.uJobStatus AND"
504                                " (tClient.uOwner=%u OR tClient.uClient=%u) AND"
505                                " (tJob.uJobStatus=1 OR tJob.uJobStatus=14 OR tJob.uJobStatus=2 OR"
506                                " tJob.uJobStatus=10 OR tJob.uJobStatus=4)",
507                                                guLoginClient,guLoginClient);
508        else
509                sprintf(gcQuery,"SELECT tJob.uJob,tJob.cLabel,tNode.cLabel,tJobStatus.cLabel FROM tJob,tNode,tJobStatus"
510                                        " WHERE tJob.uNode=tNode.uNode AND"
511                                        " tJob.uJobStatus=tJobStatus.uJobStatus AND"
512                                        " (tJob.uJobStatus=1 OR tJob.uJobStatus=14 OR tJob.uJobStatus=2 OR"
513                                        " tJob.uJobStatus=10 OR tJob.uJobStatus=4)"
514                                                                                                        );
515
516        mysql_query(&gMysql,gcQuery);
517        if(mysql_errno(&gMysql))
518        {
519                printf("<p><u>tJobNavList</u><br>\n");
520                printf("%s",mysql_error(&gMysql));
521                return;
522        }
523
524        res=mysql_store_result(&gMysql);
525        if(mysql_num_rows(res))
526        {       
527                printf("<p><u>tJobNavList (Waiting and Error)</u><br>\n");
528
529                while((field=mysql_fetch_row(res)))
530                        printf("<a class=darkLink href=unxsVZ.cgi?gcFunction=tJob&uJob=%s>%s/%s/%s</a><br>\n",
531                                field[0],field[1],field[2],field[3]);
532        }
533        mysql_free_result(res);
534
535}//void tJobNavList(void)
536
537
538time_t cStartDateToUnixTime(char *cStartDate)
539{
540        struct  tm locTime;
541        time_t  res;
542
543        bzero(&locTime, sizeof(struct tm));
544        if(strchr(cStartDate,'-'))
545                strptime(cStartDate,"%Y-%m-%d", &locTime);
546        locTime.tm_sec = 0;
547        locTime.tm_min = 0;
548        locTime.tm_hour = 0;
549        res = mktime(&locTime);
550        return(res);
551}//time_t cStartDateToUnixTime(char *cDate)
Note: See TracBrowser for help on using the browser.