-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobqueue.c
8096 lines (7255 loc) · 232 KB
/
jobqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
FILE
jobqueue.c
svn ID removed
PURPOSE
Command line processing of jobs in the tJob queue.
AUTHOR/LEGAL
(C) 2008-2015 Gary Wallis for Unxiservice, LLC.
GPLv2 license applies. See LICENSE file included.
NOTES
We still use KISS code, var naming conventions, and Allman (ANSI) style C
indentation to make our software readable and writable by any programmer.
At the same time this approach (although with some redundant code) has kept these
programs lean and faster than anything available in any other language.
TODO
Create more #define based "macros," to help the compiler optimize the
many simple, fast but redundant code blocks.
Get rid of any goto statements that do not add too many layers of nested
logic that makes the code hard to maintain by non-authors.
Use uNotValidSystemCallArg() before all system() calls where any args
come from db and are not formatted (sprintf) as numbers.
FILE CONTENTS
1-. Top redundant protos as the appear in file for a simple TOC.
2-. More protos: Major external functions used.
3-. File scoped vars.
4-. Top level functions.
5-. Rest of functions.
*/
#include "libunxsvz.h"
#include <openisp/template.h>
#include <sys/sysinfo.h>
//
//The following prototype declarations should provide a
// table of contents
//local protos, order=ret type, in file
void TestJob(char const *cJobname);
void ProcessJobQueue(unsigned uDebug);
void ProcessJob(unsigned uJob,unsigned uDatacenter,unsigned uNode,
unsigned uContainer,char *cJobName,char *cJobData);
void tJobErrorUpdate(unsigned uJob, const char *cErrorMsg);
void tJobDoneUpdate(unsigned uJob);
void tJobWaitingUpdate(unsigned uJob);
void NewContainer(unsigned uJob,unsigned uContainer,char const *cJobData);
void DestroyContainer(unsigned uJob,unsigned uContainer);
void ChangeIPContainer(unsigned uJob,unsigned uContainer,char *cJobData);
void SwapIPContainer(unsigned uJob,unsigned uContainer,char *cJobData);
void ChangeHostnameContainer(unsigned uJob,unsigned uContainer,char *cJobData);
void ExecuteCommands(unsigned uJob,unsigned uContainer,char *cJobData);
void StopContainer(unsigned uJob,unsigned uContainer);
void StartContainer(unsigned uJob,unsigned uContainer);
void MigrateContainer(unsigned uJob,unsigned uContainer,char *cJobData);
void DNSMoveContainer(unsigned uJob,unsigned uContainer,char *cJobData,unsigned uDatacenter,unsigned uNode);
void GetGroupProp(const unsigned uGroup,const char *cName,char *cValue);
void GetContainerProp(const unsigned uContainer,const char *cName,char *cValue);
void GetContainerPropUBC(const unsigned uContainer,const char *cName,char *cValue);
void UpdateContainerUBC(unsigned uJob,unsigned uContainer,const char *cJobData);
void UpdateContainerUBCDown(unsigned uJob,unsigned uContainer,const char *cJobData);
void SetContainerUBC(unsigned uJob,unsigned uContainer,const char *cJobData);
void TemplateContainer(unsigned uJob,unsigned uContainer,const char *cJobData);
void ActionScripts(unsigned uJob,unsigned uContainer);
void AllowAccess(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void AllowAllAccess(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void BlockAccess(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void UndoBlockAccess(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void RemoveDropFromIPTables(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void RemoveAcceptFromIPTables(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void DenyAccess(unsigned uJob,const char *cJobData);
void UpdateIPFWStatus(const char *cIPv4,unsigned uFWStatus);
void ConditionalAddIPAndFWStatus(const char *cIPv4,unsigned uFWStatus,unsigned uIPType);
void CloneContainer(unsigned uJob,unsigned uContainer,char *cJobData);
void CloneRemoteContainer(unsigned uJob,unsigned uContainer,char *cJobData,unsigned uNewVeid);
void AppFunctions(FILE *fp,char *cFunction);
void LocalImportTemplate(unsigned uJob,unsigned uDatacenter,const char *cJobData);
void LocalImportConfig(unsigned uJob,unsigned uDatacenter,const char *cJobData);
void FailoverTo(unsigned uJob,unsigned uContainer,const char *cJobData);
void FailoverFrom(unsigned uJob,unsigned uContainer,const char *cJobData);
void GetIPFromtIP(const unsigned uIPv4,char *cIP);
void GetNodeProp(const unsigned uNode,const char *cName,char *cValue);
void GetDatacenterProp(const unsigned uDatacenter,const char *cName,char *cValue);
void logfileLine(const char *cFunction,const char *cLogline);
void LogError(char *cErrorMsg,unsigned uKey);
void RecurringJob(unsigned uJob,unsigned uDatacenter,unsigned uNode,unsigned uContainer,const char *cJobData);
void SetJobStatus(unsigned uJob,unsigned uJobStatus);
void LoginFirewallJobHTTP(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void LogoutFirewallJobHTTP(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void LoginFirewallJobSSH(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void LogoutFirewallJobSSH(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void LoginFirewallJob(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void LogoutFirewallJob(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void StartIptables(unsigned uJob,const char *cJobData,unsigned uDatacenter,unsigned uNode);
void RemoveAcceptsFromChainIfNotSession(char const *cChain);
unsigned uNotValidSystemCallArg(char *cSSHOptions);
unsigned GetContainerStatus(const unsigned uContainer, unsigned *uStatus);
unsigned GetContainerMainIP(const unsigned uContainer,char *cIP);
unsigned GetContainerSource(const unsigned uContainer, unsigned *uSource);
unsigned SetContainerIP(const unsigned uContainer,char *cIP);
unsigned SetContainerSource(const unsigned uContainer,const unsigned uSource);
unsigned SetContainerHostname(const unsigned uContainer,
const char *cHostname,const char *cLabel);
unsigned GetContainerNames(const unsigned uContainer,char *cHostname,char *cLabel);
unsigned GetContainerNodeStatus(const unsigned uContainer, unsigned *uStatus);
unsigned SetContainerPropertyUBC(const unsigned uContainer,const char *cPropertyName,const char *cPropertyValue);
unsigned SetContainerProperty(const unsigned uContainer,const char *cPropertyName,const char *cPropertyValue);
unsigned FailToJobDone(unsigned uJob);
//Clone maintenance clone UPDATE functions
unsigned ProcessCloneSyncJob(unsigned uNode,unsigned uContainer,unsigned uCloneContainer);
unsigned ProcessApplianceSyncJob(unsigned uNode,unsigned uContainer,unsigned uCloneContainer);
int CreateActionScripts(unsigned uContainer, unsigned uOverwrite);
void NodeCommandJob(unsigned uJob,unsigned uContainer,char *cJobData,unsigned uNode,unsigned uDatacenter);
void RestartContainer(unsigned uJob,unsigned uContainer);
void GetGroupBasedPropertyValue(unsigned uContainer,char const *cName,char *cValue);
void ActivateNATContainer(unsigned uJob,unsigned uContainer,unsigned uNode);
void ActivateNATNode(unsigned uJob,unsigned uContainer,unsigned uNode);
void ShutdownNode(unsigned uJob,unsigned uNode);
void AlwaysRunTheseJobs(unsigned uNode);
//extern protos
unsigned TextConnectDb(void); //mysqlconnect.c
void SetContainerStatus(unsigned uContainer,unsigned uStatus);
void SetContainerNode(unsigned uContainer,unsigned uNode);
void SetContainerDatacenter(unsigned uContainer,unsigned uDatacenter);
unsigned uNodeCommandJob(unsigned uDatacenter, unsigned uNode, unsigned uContainer,
unsigned uOwner, unsigned uLoginClient, unsigned uConfiguration, char *cArgs);
unsigned uCheckMaxContainers(unsigned uNode);
unsigned uCheckMaxCloneContainers(unsigned uNode);
unsigned uContainerStatus(unsigned uContainer);
//file scoped vars.
static unsigned gfuNode=0;
static unsigned guDebug=0;
static char cHostname[100]={""};//file scope
#define LINUX_SYSINFO_LOADS_SCALE 65536
//Load and which we do nothing at all.
#define JOBQUEUE_MASTER_MAXLOAD 3000
//Load at which we do not run clone jobs
#define JOBQUEUE_CLONE_MAXLOAD 5
//Low priority job load limit
#define JOBQUEUE_NORMAL_MAXLOAD 10
//New load control system allows for assigning specific load limits on
// a job per job basis. E.g. firewall block job should run no matter what
// since external DoS may raise load but we need to block the bad actor IP.
static unsigned guSystemLoad=0;//sysinfo() load
//Just for easy local testing of certain things
void TestJob(char const *cJobName)
{
guDebug=1;
if(!strcmp(cJobName,"RemoveAcceptsFromChainIfNotSession"))
{
if(TextConnectDb())
exit(1);
RemoveAcceptsFromChainIfNotSession("UnxsVZ-HTTP");
}
else
{
printf("No %s found in TestJob()\n",cJobName);
}
}//void TestJob(char const *cJobname)
//Using the local server hostname get max 32 jobs for this node from the tJob queue.
//Then dispatch jobs via ProcessJob() this function in turn calls specific functions for
//each known cJobName.
void ProcessJobQueue(unsigned uDebug)
{
MYSQL_RES *res;
MYSQL_ROW field;
unsigned uDatacenter=0;
unsigned uNode=0;
unsigned uContainer=0;
unsigned uJob=0;
struct sysinfo structSysinfo;
if(uDebug) guDebug=1;
if((gLfp=fopen(cLOGFILE,"a"))==NULL)
{
fprintf(stderr,"Could not open logfile: %s\n",cLOGFILE);
exit(300);
}
if(gethostname(cHostname,99)!=0)
{
logfileLine("ProcessJobQueue","gethostname() failed");
exit(1);
}
if(sysinfo(&structSysinfo))
{
logfileLine("ProcessJobQueue","sysinfo() failed");
exit(1);
}
guSystemLoad=structSysinfo.loads[1]/LINUX_SYSINFO_LOADS_SCALE;
if(guSystemLoad>JOBQUEUE_MASTER_MAXLOAD)
{
sprintf(gcQuery,"Load %u larger than master load limit %u. Exiting now.",guSystemLoad,JOBQUEUE_MASTER_MAXLOAD);
logfileLine("ProcessJobQueue",gcQuery);
exit(1);
}
//debug only
//sprintf(gcQuery,"Load %u, load limit %u.",guSystemLoad,JOBQUEUE_MASTER_MAXLOAD);
//logfileLine("debug",gcQuery);
char cRandom[8]={""};
int fd=open("/dev/urandom",O_RDONLY);
if(fd>0)
{
if(!read(fd,cRandom,sizeof(cRandom))!=sizeof(cRandom))
{
(void)srand((unsigned int)cRandom[0]);
}
else
{
logfileLine("ProcessJobQueue","/dev/urandom read error");
(void)srand((int)time((time_t *)NULL));
}
}
else
{
logfileLine("ProcessJobQueue","/dev/urandom file error");
(void)srand((int)time((time_t *)NULL));
}
unsigned uDelay=0;
uDelay=rand() % 60;
if(guDebug)
{
sprintf(gcQuery,"random delay of %us added",uDelay);
logfileLine("ProcessJobQueue",gcQuery);
}
sleep(uDelay);
//Uses login data from local.h
if(TextConnectDb())
exit(1);
guLoginClient=1;//Root user
//Get node and datacenter via hostname
sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("ProcessJobQueue",mysql_error(&gMysql));
mysql_close(&gMysql);
exit(2);
}
res=mysql_store_result(&gMysql);
if((field=mysql_fetch_row(res)))
{
sscanf(field[0],"%u",&uNode);
sscanf(field[1],"%u",&uDatacenter);
}
mysql_free_result(res);
//FQDN vs short name of 2nd NIC mess
if(!uNode)
{
char *cp;
if((cp=strchr(cHostname,'.')))
*cp=0;
sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("ProcessJobQueue",mysql_error(&gMysql));
mysql_close(&gMysql);
exit(2);
}
res=mysql_store_result(&gMysql);
if((field=mysql_fetch_row(res)))
{
sscanf(field[0],"%u",&uNode);
sscanf(field[1],"%u",&uDatacenter);
}
mysql_free_result(res);
}
if(!uNode)
{
logfileLine("ProcessJobQueue","could not determine uNode: aborted");
mysql_close(&gMysql);
exit(1);
}
//Some file scoped globals we need to cleanout someday
gfuNode=uNode;
guDatacenter=uDatacenter;
AlwaysRunTheseJobs(uNode);
if(guDebug)
{
sprintf(gcQuery,"Start %s(uNode=%u,uDatacenter=%u)",cHostname,uNode,uDatacenter);
logfileLine("ProcessJobQueue",gcQuery);
}
//
//Main loop normal jobs
//uWAITING==1
//TODO can the LIMIT partition related jobs that need to run close together?
//Testing allow only one to run at the same time.
if(mkdir("/var/run/unxsvz.lock",S_IRWXU))
{
logfileLine("ProcessJobQueue","/var/run/unxsvz.lock");
exit(127);
}
sprintf(gcQuery,"SELECT uJob,uContainer,cJobName,cJobData FROM tJob WHERE uJobStatus=1"
" AND (uDatacenter=%u OR uDatacenter=0) AND (uNode=%u OR uNode=0)" //uDatacenter,uNode=0 all type jobs
" AND uJobDate<=UNIX_TIMESTAMP(NOW()) ORDER BY uJob LIMIT 128",
uDatacenter,uNode);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("ProcessJobQueue",mysql_error(&gMysql));
mysql_close(&gMysql);
if(rmdir("/var/run/unxsvz.lock"))
logfileLine("ProcessJobQueue","/var/run/unxsvz.lock rmdir error");
exit(2);
}
res=mysql_store_result(&gMysql);
while((field=mysql_fetch_row(res)))
{
if(sysinfo(&structSysinfo))
{
logfileLine("ProcessJobQueue","sysinfo() failed");
exit(1);
}
guSystemLoad=structSysinfo.loads[1]/LINUX_SYSINFO_LOADS_SCALE;
if(guSystemLoad>JOBQUEUE_MASTER_MAXLOAD)
{
sprintf(gcQuery,"Load %u larger than master load limit %u. Exiting now.",guSystemLoad,JOBQUEUE_MASTER_MAXLOAD);
logfileLine("ProcessJobQueue",gcQuery);
mysql_free_result(res);
if(rmdir("/var/run/unxsvz.lock"))
logfileLine("ProcessJobQueue-mainloop","/var/run/unxsvz.lock rmdir error");
fclose(gLfp);
mysql_close(&gMysql);
exit(0);
}
sscanf(field[0],"%u",&uJob);
sscanf(field[1],"%u",&uContainer);
//Job dispatcher based on cJobName
//These log entries combined with the output of system calls will provide framing
sprintf(gcQuery,"Start %s",field[2]);
logfileLine("ProcessJobQueue",gcQuery);
ProcessJob(uJob,uDatacenter,uNode,uContainer,field[2],field[3]);
logfileLine("ProcessJobQueue","End");
}
mysql_free_result(res);
if(rmdir("/var/run/unxsvz.lock"))
logfileLine("ProcessJobQueue","/var/run/unxsvz.lock rmdir error");
if(guDebug) logfileLine("ProcessJobQueue","End");
fclose(gLfp);
mysql_close(&gMysql);
exit(0);
}//void ProcessJobQueue()
void ProcessJob(unsigned uJob,unsigned uDatacenter,unsigned uNode,
unsigned uContainer,char *cJobName,char *cJobData)
{
//Some jobs may take quite some time, we need to make sure we don't run again!
sprintf(gcQuery,"UPDATE tJob SET uJobStatus=2,cRemoteMsg='Running',uModBy=1,"
"uModDate=UNIX_TIMESTAMP(NOW()) WHERE uJob=%u",uJob);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
logfileLine("ProcessJob()",mysql_error(&gMysql));
//if debug
//printf("%3.3u uJob=%u uContainer=%u cJobName=%s; cJobData=%s;\n",
// uCount++,uJob,uContainer,cJobName,cJobData);
//Is priority order needed in some cases?
//Only run special high priority jobs if load is high
if(guSystemLoad>JOBQUEUE_NORMAL_MAXLOAD)
{
//Must also be in normal load section
if(!strcmp(cJobName,"LoginFirewallJob") && uNode)
{
LoginFirewallJob(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"LoginFirewallJobHTTP") && uNode)
{
LoginFirewallJobHTTP(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"LoginFirewallJobSSH") && uNode)
{
LoginFirewallJobSSH(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"AllowAllAccess") && uNode)
{
AllowAllAccess(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"AllowAccess") && uNode)
{
AllowAccess(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"UndoBlockAccess") && uNode)
{
UndoBlockAccess(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"RemoveDropFromIPTables") && uNode)
{
RemoveDropFromIPTables(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"RemoveAcceptFromIPTables") && uNode)
{
RemoveAcceptFromIPTables(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"BlockAccess") && uNode)
{
BlockAccess(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"DenyAccess") && uNode)
{
DenyAccess(uJob,cJobData);
}
else if(1)
{
logfileLine("ProcessJob() highload",cJobName);
tJobWaitingUpdate(uJob);
}
}
else
{
//normal load jobs
if(!strcmp(cJobName,"FailoverTo"))
{
FailoverTo(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"FailoverFrom"))
{
FailoverFrom(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"DNSMoveContainer"))
{
DNSMoveContainer(uJob,uContainer,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"MigrateContainer"))
{
MigrateContainer(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"CloneContainer"))
{
CloneContainer(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"NewContainer"))
{
NewContainer(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"ActionScripts"))
{
//OpenVZ action scripts
ActionScripts(uJob,uContainer);
}
else if(!strcmp(cJobName,"StartContainer"))
{
StartContainer(uJob,uContainer);
}
else if(!strcmp(cJobName,"ActivateNATContainer"))
{
ActivateNATContainer(uJob,uContainer,uNode);
}
else if(!strcmp(cJobName,"ShutdownNode"))
{
ShutdownNode(uJob,uNode);
}
else if(!strcmp(cJobName,"ActivateNATNode"))
{
ActivateNATNode(uJob,uContainer,uNode);
}
else if(!strcmp(cJobName,"ChangeHostnameContainer"))
{
ChangeHostnameContainer(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"ExecuteCommands"))
{
ExecuteCommands(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"ChangeIPContainer"))
{
ChangeIPContainer(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"SwapIPContainer"))
{
SwapIPContainer(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"StopContainer"))
{
StopContainer(uJob,uContainer);
}
else if(!strcmp(cJobName,"RestartContainer"))
{
RestartContainer(uJob,uContainer);
}
else if(!strcmp(cJobName,"DestroyContainer"))
{
DestroyContainer(uJob,uContainer);
}
else if(!strcmp(cJobName,"UpdateContainerUBCDownJob"))
{
UpdateContainerUBCDown(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"UpdateContainerUBCJob"))
{
UpdateContainerUBC(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"SetUBCJob"))
{
SetContainerUBC(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"TemplateContainer"))
{
TemplateContainer(uJob,uContainer,cJobData);
}
else if(!strcmp(cJobName,"LocalImportTemplateJob"))
{
LocalImportTemplate(uJob,uDatacenter,cJobData);
}
else if(!strcmp(cJobName,"LocalImportConfigJob"))
{
LocalImportConfig(uJob,uDatacenter,cJobData);
}
else if(!strcmp(cJobName,"RecurringJob"))
{
RecurringJob(uJob,uDatacenter,uNode,uContainer,cJobData);
}
else if(!strcmp(cJobName,"NodeCommandJob"))
{
NodeCommandJob(uJob,uContainer,cJobData,uNode,uDatacenter);
}
else if(!strcmp(cJobName,"LogoutFirewallJob") && uNode)
{
LogoutFirewallJob(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"LogoutFirewallJobHTTP") && uNode)
{
LogoutFirewallJobHTTP(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"LogoutFirewallJobSSH") && uNode)
{
LogoutFirewallJobSSH(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"StartIptables") && uNode)
{
StartIptables(uJob,cJobData,uDatacenter,uNode);
}
//also in high load section
else if(!strcmp(cJobName,"LoginFirewallJob") && uNode)
{
LoginFirewallJob(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"LoginFirewallJobHTTP") && uNode)
{
LoginFirewallJobHTTP(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"LoginFirewallJobSSH") && uNode)
{
LoginFirewallJobSSH(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"AllowAllAccess") && uNode)
{
AllowAllAccess(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"AllowAccess") && uNode)
{
AllowAccess(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"UndoBlockAccess") && uNode)
{
UndoBlockAccess(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"RemoveDropFromIPTables") && uNode)
{
RemoveDropFromIPTables(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"RemoveAcceptFromIPTables") && uNode)
{
RemoveAcceptFromIPTables(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"BlockAccess") && uNode)
{
BlockAccess(uJob,cJobData,uDatacenter,uNode);
}
else if(!strcmp(cJobName,"DenyAccess") && uNode)
{
DenyAccess(uJob,cJobData);
}
//high load section
else if(1)
{
logfileLine("ProcessJob() not found",cJobName);
tJobErrorUpdate(uJob,cJobName);
}
}//normal load jobs
}//ProcessJob(...)
//Shared functions
void tJobErrorUpdate(unsigned uJob, const char *cErrorMsg)
{
sprintf(gcQuery,"UPDATE tJob SET uJobStatus=14,cRemoteMsg='%.31s',uModBy=1,"
"uModDate=UNIX_TIMESTAMP(NOW()) WHERE uJob=%u",
cErrorMsg,uJob);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("tJobErrorUpdate",mysql_error(&gMysql));
exit(2);
}
sprintf(gcQuery,"INSERT INTO tLog SET"
" cLabel='unxsVZ.cgi ProcessJobQueue Error',"
"uLogType=4,uLoginClient=1,"
"cLogin='unxsVZ.cgi',cMessage=\"%s uJob=%u\","
"cServer='%s',uOwner=1,uCreatedBy=1,"
"uCreatedDate=UNIX_TIMESTAMP(NOW())",
cErrorMsg,uJob,cHostname);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("tJobErrorUpdate",mysql_error(&gMysql));
exit(2);
}
}//void tJobErrorUpdate()
void tJobDoneUpdate(unsigned uJob)
{
sprintf(gcQuery,"UPDATE tJob SET uJobStatus=3,uModBy=1,cRemoteMsg='tJobDoneUpdate() ok',"
"uModDate=UNIX_TIMESTAMP(NOW()) WHERE uJob=%u",uJob);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("tJobDoneUpdate",mysql_error(&gMysql));
exit(2);
}
}//void tJobDoneUpdate(unsigned uJob, char *cErrorMsg)
void tJobWaitingUpdate(unsigned uJob)
{
sprintf(gcQuery,"UPDATE tJob SET uJobStatus=1,uModBy=1,cRemoteMsg='tJobWaitingUpdate()',"
"uModDate=UNIX_TIMESTAMP(NOW()) WHERE uJob=%u",uJob);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("tJobWaitingUpdate",mysql_error(&gMysql));
exit(2);
}
}//void tJobWaitingUpdate(unsigned uJob, char *cErrorMsg)
//Specific job handlers
void NewContainer(unsigned uJob,unsigned uContainer,char const *cJobData)
{
MYSQL_RES *res;
MYSQL_ROW field;
unsigned uVeth=0;
unsigned uSource=0;
unsigned uDeployStopped=0;
char cDeployOptions[256]={""};
//Must wait for clone or template operations to finish.
if(access("/var/run/vzdump.lock",R_OK)==0)
{
logfileLine("NewContainer","/var/run/vzdump.lock exists");
tJobWaitingUpdate(uJob);
return;
}
sprintf(gcQuery,"SELECT"
" tContainer.cLabel,tContainer.cHostname,tIP.cLabel"
",tOSTemplate.cLabel,tNameserver.cLabel,tSearchdomain.cLabel"
",tConfig.cLabel,tContainer.uVeth,tContainer.uSource"
" FROM tContainer,tOSTemplate,tNameserver,tSearchdomain,tConfig,tIP WHERE uContainer=%u"
" AND tContainer.uOSTemplate=tOSTemplate.uOSTemplate"
" AND tContainer.uNameserver=tNameserver.uNameserver"
" AND tContainer.uConfig=tConfig.uConfig"
" AND tContainer.uIPv4=tIP.uIP"
" AND tContainer.uSearchdomain=tSearchdomain.uSearchdomain",uContainer);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("NewContainer",mysql_error(&gMysql));
exit(2);
}
res=mysql_store_result(&gMysql);
if((field=mysql_fetch_row(res)))
{
sscanf(field[7],"%u",&uVeth);
sscanf(field[8],"%u",&uSource);
//0-. Create vz conf action script files if applicable. 1 is for overwriting existing files
//OpenVZ action scripts
if(CreateActionScripts(uContainer,1))
{
logfileLine("NewContainer","CreateActionScripts(x,1) failed");
tJobErrorUpdate(uJob,"CreateActionScripts(x,1) failed");
goto CommonExit;
}
//vzctl [flags] create veid --ostemplate name] [--config name] [--private path]
//[--root path] [--ipadd addr] [--hostname name]
//1-.
if( uNotValidSystemCallArg(field[0]) ||
uNotValidSystemCallArg(field[1]) ||
uNotValidSystemCallArg(field[2]) ||
uNotValidSystemCallArg(field[3]) ||
uNotValidSystemCallArg(field[4]) ||
uNotValidSystemCallArg(field[5]) ||
uNotValidSystemCallArg(field[6]) )
{
logfileLine("NewContainer","security alert");
tJobErrorUpdate(uJob,"failed sec alert!");
goto CommonExit;
}
//rename out of the way old conf file
sprintf(gcQuery,"mv /etc/vz/conf/%1$u.conf /etc/vz/conf/%1$u.conf.NewContainer > /dev/null 2>&1",uContainer);
system(gcQuery);
if(uVeth)
sprintf(gcQuery,"/usr/sbin/vzctl --verbose create %u --ostemplate %s --hostname %s"
" --name %s --config %s",
uContainer,field[3],field[1],field[0],field[6]);
else
sprintf(gcQuery,"/usr/sbin/vzctl --verbose create %u --ostemplate %s --hostname %s"
" --ipadd %s --name %s --config %s",
uContainer,field[3],field[1],field[2],field[0],field[6]);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"vzctl create failed");
goto CommonExit;
}
//2-.
sprintf(gcQuery,"/usr/sbin/vzctl --verbose set %u --nameserver \"%.99s\" --searchdomain \"%.32s\" --save",
uContainer,field[4],field[5]);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"vzctl set failed");
//Roll back step 1-.
sprintf(gcQuery,"/usr/sbin/vzctl destroy %u",uContainer);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"rb0: vzctl destroy failed");
}
goto CommonExit;
}
//3-.
//TODO hardcoded eth0 problem. Solution is easy get from node properties.
//problem remains if datacenter nodes have diff eth device!
GetContainerProp(uContainer,"cDeployOptions",cDeployOptions);
if(strstr(cDeployOptions,"uDeployStopped=1;"))
uDeployStopped=1;
//always deploy stopped clone containers
if(strstr(field[0],"-clone"))
uDeployStopped=1;
if(uVeth)
{
char cIPv4[32]={""};
sprintf(gcQuery,"/usr/sbin/vzctl --verbose start %u",uContainer);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"vzctl start1 failed");
//Roll back step 1-.
sprintf(gcQuery,"/usr/sbin/vzctl destroy %u",uContainer);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"rb1: vzctl destroy failed");
}
goto CommonExit;
}
sprintf(gcQuery,"/usr/sbin/vzctl --verbose set %u --netif_add eth0,,,,vmbr0 --save",uContainer);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"vzctl --netif_add failed");
//Roll back step 2-.
sprintf(gcQuery,"/usr/sbin/vzctl stop %u",uContainer);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"rb2: vzctl stop failed");
}
//Roll back step 1-.
sprintf(gcQuery,"/usr/sbin/vzctl destroy %u",uContainer);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"rb2: vzctl destroy failed");
}
goto CommonExit;
}
if(GetContainerMainIP(uContainer,cIPv4))
logfileLine("NewContainer","Empty cIPv4");
sprintf(gcQuery,"/usr/sbin/vzctl exec %u \"/sbin/ifconfig eth0 0\"",uContainer);
if(system(gcQuery))
logfileLine("NewContainer",gcQuery);
sprintf(gcQuery,"/usr/sbin/vzctl exec %u \"/sbin/ip addr add %s dev eth0\"",
uContainer,cIPv4);
if(system(gcQuery))
logfileLine("NewContainer",gcQuery);
sprintf(gcQuery,"/usr/sbin/vzctl exec %u \"/sbin/ip route add default dev eth0\"",uContainer);
if(system(gcQuery))
logfileLine("NewContainer",gcQuery);
if(uDeployStopped)
{
sprintf(gcQuery,"/usr/sbin/vzctl --verbose stop %u",uContainer);
if(system(gcQuery))
logfileLine("NewContainer",gcQuery);
sprintf(gcQuery,"/usr/sbin/vzctl --verbose set %u --onboot=no --save",uContainer);
if(system(gcQuery))
logfileLine("NewContainer",gcQuery);
}
}
else
{
if(!uDeployStopped)
{
sprintf(gcQuery,"/usr/sbin/vzctl --verbose start %u",uContainer);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"vzctl start failed");
//Roll back step 1-.
sprintf(gcQuery,"/usr/sbin/vzctl destroy %u",uContainer);
if(system(gcQuery))
{
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"rb3: vzctl destroy failed");
}
goto CommonExit;
}
}
else
{
sprintf(gcQuery,"/usr/sbin/vzctl set %u --onboot no --save",uContainer);
//warn via logfile only
if(system(gcQuery))
logfileLine("NewContainer",gcQuery);
}
}
}
else
{
sprintf(gcQuery,"Select for %u failed",uContainer);
logfileLine("NewContainer",gcQuery);
tJobErrorUpdate(uJob,"Select failed");
goto CommonExit;
}
//4-. Optional container password set
//This option requires MySQL SSL replication and
//much more security measures in place to avoid a db penetration leading
//to multiple container breaches.
char cPasswd[256]={""};
GetContainerProp(uContainer,"cPasswd",cPasswd);
if(cPasswd[0] && !uNotValidSystemCallArg(cPasswd) )
{
//sprintf(gcQuery,"/usr/sbin/vzctl --userpasswd \"root:%s\" %u",cPasswd,uContainer);
//This works on older vzctl also
sprintf(gcQuery,"/usr/sbin/vzctl set %u --userpasswd \"root:%s\"",uContainer,cPasswd);
if(system(gcQuery))
logfileLine("NewContainer","Container passwd not changed!");
}
//5-. Optional container CentOS linux timezone set
//Example (cOrg_TimeZone) cTimezone "Europe/Zurich"
//For /usr/share/zoneinfo/Europe/Zurich
char cTimezone[256]={""};
GetContainerProp(uContainer,"cOrg_TimeZone",cTimezone);
if(cTimezone[0] && !uNotValidSystemCallArg(cTimezone) )
{
sprintf(gcQuery,"cp /vz/root/%u/usr/share/zoneinfo/%s /vz/root/%u/etc/localtime",
uContainer,cTimezone,uContainer);
if(system(gcQuery))
logfileLine("NewContainer",gcQuery);
else
logfileLine("NewContainer","Container timezone changed");
}
//6-.
//Optional group based script may exist to be executed.
//
//Primary group is oldest tGroupGlue entry.
//UBC safe
sprintf(gcQuery,"SELECT tProperty.cValue FROM tProperty,tGroupGlue WHERE tProperty.uType=%u"
" AND tProperty.uKey=tGroupGlue.uGroup"
" AND tGroupGlue.uContainer=%u"
" AND tProperty.cName='cJob_OnNewScript' ORDER BY tGroupGlue.uGroupGlue LIMIT 1",uPROP_GROUP,uContainer);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("NewContainer",mysql_error(&gMysql));
exit(2);
}
res=mysql_store_result(&gMysql);
if((field=mysql_fetch_row(res)))
{
char cOnScriptCall[386];
struct stat statInfo;
char cCommand[256];
char *cp;
sprintf(cCommand,"%.255s",field[0]);
//Remove trailing junk
if((cp=strchr(cCommand,'\n')) || (cp=strchr(cCommand,'\r'))) *cp=0;
if(uNotValidSystemCallArg(cCommand))
{
logfileLine("NewContainer","cJob_OnNewScript security alert");
goto CommonExit2;
}
//Only run if command is chmod 500 and owned by root for extra security reasons.
if(stat(cCommand,&statInfo))
{
logfileLine("NewContainer","stat failed for cJob_OnNewScript");
logfileLine("NewContainer",cCommand);
goto CommonExit2;
}
if(statInfo.st_uid!=0)
{
logfileLine("NewContainer","cJob_OnNewScript is not owned by root");
goto CommonExit2;
}
if(statInfo.st_mode & ( S_IWOTH | S_IWGRP | S_IWUSR | S_IXOTH | S_IROTH | S_IXGRP | S_IRGRP ) )
{
logfileLine("NewContainer","cJob_OnNewScript is not chmod 500");
goto CommonExit2;
}
char cHostname[100]={""};
sprintf(gcQuery,"SELECT tContainer.cHostname"
" FROM tContainer WHERE uContainer=%u",uContainer);
mysql_query(&gMysql,gcQuery);
if(mysql_errno(&gMysql))
{
logfileLine("",mysql_error(&gMysql));
exit(2);
}
res=mysql_store_result(&gMysql);
if((field=mysql_fetch_row(res)))
{
sprintf(cHostname,"%.99s",field[0]);
//Please note that any script here must not use uStatus=1 as a condition.
sprintf(cOnScriptCall,"%.255s %.64s %u",cCommand,cHostname,uContainer);
if(system(cOnScriptCall))
{
logfileLine("NewContainer",cOnScriptCall);
}
}