forked from tranquilit/WAPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaptget.lpr
1245 lines (1119 loc) · 40.3 KB
/
waptget.lpr
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
program waptget;
{ -----------------------------------------------------------------------
# This file is part of WAPT
# Copyright (C) 2013 Tranquil IT Systems http://www.tranquil.it
# WAPT aims to help Windows systems administrators to deploy
# setup and update applications on users PC.
#
# WAPT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# WAPT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with WAPT. If not, see <http://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------
}
{$mode objfpc}{$H+}
{$modeswitch typehelpers}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes, SysUtils, CustApp,
{ you can add units after this }
Interfaces, Windows, PythonEngine, VarPyth, superobject, soutils, tislogging, uWaptRes,
waptcommon, waptutils, tiscommon, tisstrings, LazFileUtils, FileUtil,
IdAuthentication, Variants, IniFiles,uwaptcrypto,uWaptPythonUtils,
tisinifiles,base64,IdComponent,httpsend,uWAPTPollThreads;
type
{ TDynStringArrayHelper }
TDynStringArrayHelper = type helper for TDynStringArray
procedure Add(const a:String);
end;
{ PWaptGet }
PWaptGet = class(TCustomApplication)
private
localuser,localpassword:AnsiString;
FRepoURL: String;
FLastProgressMs:DWORD;
procedure DoOnProgress(Sender: TObject);
procedure DoOnHttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
function GetIsEnterpriseEdition: Boolean;
function GetLocalWaptserverRepositoryPath: String;
function GetPythonEngine: TPythonEngine;
function GetRepoURL: String;
function Getwaptcrypto: Variant;
function Getwaptpackage: Variant;
function Getwaptdevutils: Variant;
procedure HTTPLogin(Sender: THttpSend; var ShouldRetry: Boolean;RetryCount:integer);
function NextPackageVersion(RepoPath, PackageName, BaseVersion: String
): String;
function PackageVersion(RepoPath,PackageName: String): String;
function ScanLocalWaptrepo(RepoPath: String): Variant;
procedure SetRepoURL(AValue: String);
protected
FPythonEngine: TPythonEngine;
Fwaptcrypto,
fwaptpackage,
Fwaptdevutils: Variant;
procedure DoRun; override;
public
Action : String;
RegWaptBaseDir:String;
CheckEventsThread: TCheckEventsThread;
lock:TRTLCriticalSection;
tasks:ISuperObject;
lastMessageTime : TDateTime;
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
property RepoURL:String read GetRepoURL write SetRepoURL;
procedure OnCheckEventsThreadNotify(Sender: TObject);
function remainingtasks:ISuperObject;
function GetCommonNameFromCmdLine(): String;
function CheckPersonalCertificateIsCodeSigning(PersonalCertificatePath,PrivateKeyPassword:String): Boolean;
function BuildWaptUpgrade(PackageName,Version,BuildDir,SetupFilename: String): String;
procedure UploadWaptAgentUpgrade(SetupFilename, WaptUpgradeFilename: String);
function CreateWaptAgent(BuildDir: String; Edition: String='waptagent'
): String;
function CreateKeycert(commonname: String; basedir: String='';keypassword: String='';
CodeSigning:Boolean=True;
CA:Boolean=False;
ClientAuth:Boolean=True;
Overwrite:Boolean=False): String;
function GetCAKeyPassword(crtname:String): String;
function GetPrivateKeyPassword(crtname:String=''): String;
function GetWaptServerPassword: String;
function GetWaptServerUser: String;
function GetWaptServicePassword: String;
function GetWaptServiceUser: String;
property PythonEngine:TPythonEngine read GetPythonEngine;
property waptdevutils:Variant read Getwaptdevutils;
property waptcrypto:Variant read Getwaptcrypto;
property waptpackage:Variant read Getwaptpackage;
property IsEnterpriseEdition:Boolean read GetIsEnterpriseEdition;
end;
{ PWaptGet }
var
Application: PWaptGet;
function GetPassword(const InputMask: Char = '*'): string;
var
OldMode: Cardinal;
c: char;
begin
Result:='';
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), OldMode);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), OldMode and not (ENABLE_LINE_INPUT or ENABLE_ECHO_INPUT));
try
while not Eof do
begin
Read(c);
if c = #13 then // Carriage Return
Break;
if (c = #8) then // Back Space
begin
if (Length(Result) > 0) then
begin
Delete(Result, Length(Result), 1);
Write(#8);
end;
end
else
begin
Result := Result + c;
Write(InputMask);
end;
end;
finally
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), OldMode);
end;
end;
procedure PWaptGet.HTTPLogin(Sender: THttpSend; var ShouldRetry: Boolean; RetryCount:integer);
var
newuser:AnsiString;
begin
if (RetryCount<=1) then
begin
Sender.Username := GetWaptServiceUser;
Sender.Password := GetWaptServicePassword;
end
else
begin
Write('Waptservice User ('+localuser+') :');
readln(newuser);
if newuser<>'' then
Sender.Username:=newuser
else
Sender.Username:=localuser;
if (Sender.Username='') then
raise Exception.Create('Empty user');
Write('Password: ');
Sender.Password := GetPassword;
WriteLn;
end;
// cache for next use
localuser := Sender.Username;
localpassword := Sender.Password;
ShouldRetry := (Sender.Password <> '') and (Sender.Password <> '');
end;
// return the version for new waptupgrade package, be sure it is higer version than current one in repo
function PWaptGet.NextPackageVersion(RepoPath,PackageName,BaseVersion:String):String;
var
LocalRepo,Package:Variant;
OldVersion, OldBaseVersion: String;
OldBuild: integer;
begin
if PackageName = '' then
PackageName := DefaultPackagePrefix+'-waptupgrade';
LocalRepo := waptpackage.WaptLocalRepo(RepoPath);
Package := LocalRepo.get(PackageName);
if VarIsNone(Package) then
Result := BaseVersion+'-0'
else
begin
OldVersion := VarPythonAsString(Package.version);
OldBaseVersion := StrSplit(OldVersion,'-',True,2)[0];
if OldBaseVersion=BaseVersion then
begin
OldBuild := StrToInt(StrSplit(OldVersion,'-',True,2)[1]);
Result := BaseVersion+'-'+IntToStr(OldBuild+1);
end
else
Result := BaseVersion+'-0';
end;
end;
function PWaptGet.PackageVersion(RepoPath,PackageName: String): String;
var
LocalRepo,Package:Variant;
begin
LocalRepo := waptpackage.WaptLocalRepo(RepoPath);
Package := LocalRepo.get(PackageName);
if VarIsNone(Package) then
Result := ''
else
Result := VarPythonAsString(Package.version);
end;
function PWaptGet.GetWaptServerUser: String;
begin
Result := GetCmdParams('WaptServerUser','');
if Result = '' then
Result := GetCmdParams('wapt-server-user','');
while Result='' do
begin
Write('Waptserver '+GetWaptServerURL+' Admin User ('+WaptServerUser+') :');
readln(Result);
if result = '' then
Result := WaptServerUser;
WaptServerUser := Result;
end;
WaptServerUser := Result;
end;
function PWaptGet.GetWaptServerPassword: String;
begin
if (WaptServerPassword='') and (GetCmdParams('WaptServerPassword64')<>'') then
WaptServerPassword := DecodeStringBase64(GetCmdParams('WaptServerPassword64',''));
if WaptServerPassword='' then
WaptServerPassword := GetCmdParams('WaptServerPassword','');
if WaptServerPassword='' then
WaptServerPassword := GetCmdParams('wapt-server-passwd','');
while WaptServerPassword='' do
begin
Write('Waptserver Password: ');
WaptServerPassword := GetPassword;
WriteLn;
end;
Result := WaptServerPassword;
end;
function PWaptGet.GetWaptServiceUser: String;
begin
Result := GetCmdParams('WaptServiceUser','');
if Result='' then
begin
Write('Waptservice User :');
readln(Result);
end;
end;
function PWaptGet.GetWaptServicePassword: String;
begin
Result := '';
if (GetCmdParams('WaptServicePassword64')<>'') then
result := DecodeStringBase64(GetCmdParams('WaptServicePassword64',''));
if result='' then
result := GetCmdParams('WaptServicePassword','');
if result='' then
begin
Write('Password: ');
Result := GetPassword;
WriteLn;
end;
end;
function PWaptGet.GetPrivateKeyPassword(crtname:String=''): String;
begin
if crtname ='' then
crtname:=WaptPersonalCertificatePath;
Result := '';
if GetCmdParams('PrivateKeyPassword64')<>'' then
result := DecodeStringBase64(GetCmdParams('PrivateKeyPassword64'));
if result='' then
result := GetCmdParams('PrivateKeyPassword','');
if result='' then
begin
try
result := FileToString(GetCmdParams('w',''));
except
result:=GetCmdParams('w','');
end;
end;
while Result='' do
begin
Write('Private key Password for '+crtname+' : ');
Result := GetPassword;
WriteLn;
end;
end;
function PWaptGet.GetCommonNameFromCmdLine(): String;
begin
Result := '';
if GetCmdParams('CommonName64')<>'' then
result := DecodeStringBase64(GetCmdParams('CommonName64'));
if result='' then
result := GetCmdParams('CommonName','');
end;
function PWaptGet.CheckPersonalCertificateIsCodeSigning(
PersonalCertificatePath, PrivateKeyPassword: String): Boolean;
var
Certificate,PrivateKey: Variant;
begin
try
Certificate := waptcrypto.SSLCertificate(PyUTF8Decode(PersonalCertificatePath));
// as boolean raises a invalid variant op... to
if not VarIsTrue(Certificate.is_code_signing) then
Raise Exception.CreateFmt('ERROR Personal Certificate is not a code signing certificate: %s',[PersonalCertificatePath]);
PrivateKey := Certificate.matching_key_in_dirs(private_key_password := PrivateKeyPassword);
if VarIsNull(PrivateKey) or VarIsNone(PrivateKey) then
Raise Exception.CreateFmt('ERROR No matching private key found with supplied password for : %s',[PersonalCertificatePath]);
Writeln('OK cert is codeSigning and found a matching private key path: '+VarPythonAsString(PrivateKey.private_key_filename));
Result := True;
except
on E: Exception do
begin
Writeln('ERROR CheckPersonalCertificateIsCodeSigning: '+E.Message);
Result := False;
end;
end;
end;
function PWaptGet.GetCAKeyPassword(crtname:String): String;
begin
result := GetCmdParams('CAKeyPassword','');
while Result='' do
begin
Write('CA key Password for '+crtname+' : ');
Result := GetPassword;
WriteLn;
end;
end;
function PWaptGet.GetRepoURL: String;
begin
if FRepoURL='' then
FRepoURL:=GetMainWaptRepoURL;
result := FRepoURL;
end;
function PWaptGet.Getwaptcrypto: Variant;
begin
if not Assigned(PythonEngine) then
Raise Exception.Create('No python engine available');
if VarIsEmpty(Fwaptcrypto) or VarIsNull(Fwaptcrypto) then
Fwaptcrypto:= VarPyth.Import('waptcrypto');
Result := Fwaptcrypto;
end;
function PWaptGet.Getwaptpackage: Variant;
begin
if not Assigned(PythonEngine) then
Raise Exception.Create('No python engine available');
if VarIsEmpty(Fwaptpackage) or VarIsNull(Fwaptpackage) then
Fwaptpackage:= VarPyth.Import('waptpackage');
Result := Fwaptpackage;
end;
function PWaptGet.GetIsEnterpriseEdition: Boolean;
begin
{$ifdef ENTERPRISE}
Result := True;
{$else}
Result := False;
{$endif}
end;
function PWaptGet.GetPythonEngine: TPythonEngine;
begin
if not Assigned(FPythonEngine) then
begin
// Running python stuff
FPythonEngine := TPythonEngine.Create(Nil);
RegWaptBaseDir:=WaptBaseDir();
if not FileExistsUTF8(AppendPathDelim(RegWaptBaseDir)+'python27.dll') then
RegWaptBaseDir:=RegisteredAppInstallLocation('wapt_is1');
if not FileExistsUTF8(AppendPathDelim(RegWaptBaseDir)+'python27.dll') then
RegWaptBaseDir:=RegisteredAppInstallLocation('WAPT Server_is1');
if RegWaptBaseDir='' then
RegWaptBaseDir:=ExtractFilePath(RegisteredExePath('wapt-get.exe'));
with FPythonEngine do
begin
AutoLoad:=False;
DllPath := RegWaptBaseDir;
DllName := 'python27.dll';
UseLastKnownVersion := False;
LoadDll;
end;
end;
result := FPythonEngine;
end;
procedure PWaptGet.SetRepoURL(AValue: String);
begin
if FRepoURL=AValue then Exit;
FRepoURL:=AValue;
end;
function PWaptGet.GetLocalWaptserverRepositoryPath:String;
begin
// todo use waptserver.ini config file for location
Result := AppendPathDelim(WaptBaseDir)+'waptserver\repository\wapt';
end;
procedure PWaptGet.DoRun;
var
MainModule : TStringList;
WaptAgentFilename, WaptUpgradeFilename, TmpBuildDir, logleveloption : String;
Res,task:ISuperobject;
package,sopackages:ISuperObject;
procedure SetFlag( AFlag: PInt; AValue : Boolean );
begin
if AValue then
AFlag^ := 1
else
AFlag^ := 0;
end;
var
i:integer;
NextIsParamValue:Boolean;
NewCertificateFilename,DestCertPath:String;
Args: TDynStringArray;
begin
Action:='';
sopackages := TSuperObject.Create(stArray);
NextIsParamValue := False;
for i:=1 to ParamCount do
begin
if (Pos('-',Params[i])<>1) and not NextIsParamValue then
begin
if (action='') then
Action := lowercase(Params[i])
else
sopackages.AsArray.Add(Params[i]);
NextIsParamValue := False;
end
else
NextIsParamValue := StrIsOneOf(Params[i],['-c','-r','-l','-p','-s','-e','-k','-w','-U','-g','-t','-L'])
end;
// parse parameters
if HasOption('?') or HasOption('h','help') then
begin
writeln(utf8decode(rsOptRepo));
writeln(utf8decode(rsWaptgetHelp));
end;
if HasOption('c','config') then
ReadWaptConfig(GetOptionValue('c','config'))
else
ReadWaptConfig();
if HasOption('r','repo') then
RepoURL := GetOptionValue('r','repo');
if HasOption('l','loglevel') then
begin
logleveloption := UpperCase(GetOptionValue('l','loglevel'));
if logleveloption = 'DEBUG' then
currentLogLevel := DEBUG
else if logleveloption = 'INFO' then
currentLogLevel := INFO
else if logleveloption = 'WARNING' then
currentLogLevel := WARNING
else if logleveloption = 'ERROR' then
currentLogLevel := ERROR
else if logleveloption = 'CRITICAL' then
currentLogLevel := CRITICAL;
Logger('Current loglevel : '+StrLogLevel[currentLogLevel],DEBUG);
end;
if HasOption('v','version') then
writeln(format(rsWin32exeWrapper, [ApplicationName, GetApplicationVersion]));
if (action = 'create-keycert') then
begin
ReadWaptConfig(AppIniFilename('waptconsole'));
NewCertificateFilename := CreateKeycert(GetCommonNameFromCmdLine,'','',
GetCmdParams('CodeSigning','1')='1',
GetCmdParams('CA','1')='1',
GetCmdParams('ClientAuth','1')='1',
FindCmdLineSwitch('Force',['/','-'],True) or FindCmdLineSwitch('F',['/','-'],True));
if FindCmdLineSwitch('EnrollNewCert') then
begin
DestCertPath := AppendPathDelim(WaptBaseDir)+'ssl\'+ExtractFileName(NewCertificateFilename);
if CopyFileW(PWideChar(UTF8Decode(NewCertificateFilename)),PWideChar(UTF8Decode(DestCertPath)),False) then
WriteLn('Enrolled in: '+DestCertPath)
else
begin
writeln('ERROR: Unable to copy certificate to '+DestCertPath);
ExitProcess(3);
end;
end;
if FindCmdLineSwitch('SetAsDefaultPersonalCert') then
begin
IniWriteString(AppIniFilename('waptconsole'),'global','personal_certificate_path',NewCertificateFilename);
WriteLn('Personal Certificate config filename: '+AppIniFilename);
end;
end
else
if (action = 'check-valid-codesigning-cert') then
begin
ReadWaptConfig(AppIniFilename('waptconsole'));
writeln(CheckPersonalCertificateIsCodeSigning(WaptPersonalCertificatePath,GetPrivateKeyPassword(WaptPersonalCertificatePath)));
end
else
if (action = 'build-waptagent') then
begin
ReadWaptConfig(AppIniFilename('waptconsole'));
Writeln(rsBuildWaptAgent);
TmpBuildDir := GetTempFileNameUTF8('','wapt'+FormatDateTime('yyyymmdd"T"hhnnss',Now));
try
WaptAgentFilename := CreateWaptagent(TmpBuildDir);
WaptUpgradeFilename := BuildWaptUpgrade(DefaultPackagePrefix+'-waptupgrade',
NextPackageVersion(GetLocalWaptserverRepositoryPath,DefaultPackagePrefix+'-waptupgrade',
GetApplicationVersion(WaptAgentFilename)),
TmpBuildDir,WaptAgentFilename);
if FindCmdLineSwitch('DeployWaptAgentLocally') then
begin
if not DirectoryExistsUTF8(GetLocalWaptserverRepositoryPath) then
Raise Exception.CreateFmt('Local repository %s does not exist',[GetLocalWaptserverRepositoryPath]);
if CopyFileW(
PWideChar(UTF8Decode(WaptAgentFilename)),
PWideChar(UTF8Decode(AppendPathDelim(GetLocalWaptserverRepositoryPath)+'waptagent.exe')),
False) then
Writeln('waptagent copied to: '+AppendPathDelim(GetLocalWaptserverRepositoryPath)+'waptagent.exe')
else
begin
Writeln('Fails to copy waptagent to repository location');
ExitProcess(3);
end;
if CopyFileW(
PWideChar(UTF8Decode(WaptUpgradeFilename)),
PWideChar(UTF8Decode(AppendPathDelim(GetLocalWaptserverRepositoryPath)+ExtractFileName(WaptUpgradeFilename))),
False) then
begin
Writeln('waptupgrade package copied to repository: '+WaptUpgradeFilename);
ScanLocalWaptrepo(GetLocalWaptserverRepositoryPath);
Writeln('local repository packages scan: OK');
end
else
begin
Writeln('Fails to copy waptagent to repository location');
ExitProcess(3);
end;
Terminate;
Exit;
end
else
begin
Writeln(rsBuildWaptUpgradePackage);
GetWaptServerUser;
GetWaptServerPassword;
Writeln(rsUploadWaptAgent);
UploadWaptAgentUpgrade(WaptAgentFilename,WaptUpgradeFilename);
Terminate;
Exit;
end;
finally
If DirectoryExistsUTF8(TmpBuildDir) then
DeleteDirectory(TmpBuildDir,False);
end
end
else
if (action = 'waptupgrade') then
begin
Writeln(format(rsWaptGetUpgrade, [RepoURL]));
UpdateApplication(RepoURL+'/waptagent.exe','waptagent.exe','/VERYSILENT','wapt-get.exe','');
Terminate;
Exit;
end
else
if (action = 'dnsdebug') then
begin
WriteLn(format(rsDNSserver, [Join(',',GetDNSServers)]));
WriteLn(format(rsDNSdomain, [GetDNSDomain]));
Writeln(utf8decode(format(rsMainRepoURL, [RepoURL])));
Writeln(format(rsSRVwapt, [DNSSRVQuery('_wapt._tcp.'+GetDNSDomain).AsJSon(True)]));
Writeln(format(rsSRVwaptserver, [DNSSRVQuery('_waptserver._tcp.'+GetDNSDomain).AsJSon(True)]));
Writeln(format(rsCNAME, [DNSCNAMEQuery('wapt.'+GetDNSDomain).AsJSon(True)]));
Terminate;
Exit;
end
else
// use http service mode if --service or not --direct or not (--service) and isadmin
if ((not IsAdminLoggedOn or HasOption('S','service')) and not HasOption('D','direct')) and
StrIsOneOf(action,['update','upgrade','register','install','remove','forget',
'longtask','cancel','cancel-all','tasks',
'wuascan','wuadownload','wuainstall','audit']) and
CheckOpenPort(waptservice_port,'127.0.0.1',waptservice_timeout*1000) then
begin
writeln('About to speak to waptservice...');
// launch task in waptservice, waits for its termination
CheckEventsThread :=TCheckEventsThread.Create(@Self.OnCheckEventsThreadNotify);
CheckEventsThread.Start;
lastMessageTime := Now;
tasks := TSuperObject.create(stArray);
try
try
res := Nil;
//test longtask
Args := TDynStringArray.Create(Format('notify_user=%s',[GetCmdParams('notify_user','1')]));
if HasOption('notify_server_on_start') then
Args.Add('notify_server_on_start='+GetCmdParams('notify_server_on_start','0'));
if HasOption('notify_server_on_finish') then
Args.Add('notify_server_on_finish='+GetCmdParams('notify_server_on_finish','0'));
if HasOption('f','force') then
Args.Add('force=1');
if action='longtask' then
begin
Logger('Call longtask URL...',DEBUG);
res := WAPTLocalJsonGet(Format('longtask.json?%s',[StrJoin('&',Args)]),'admin','',-1,@HTTPLogin,3);
tasks.AsArray.Add(res);
Logger('Task '+res.S['id']+' added to queue',DEBUG);
end
else
if action='tasks' then
begin
res := WAPTLocalJsonGet(Format('tasks.json?%s',[StrJoin('&',Args)]));
if (res<>Nil) then
begin
if (res['running'].DataType<>stNull) then
writeln(utf8decode(format(rsRunningTask,[ res['running'].I['id'],res['running'].S['description'],res['running'].S['runstatus']])))
else
writeln(utf8decode(rsNoRunningTask));
if res['pending'].AsArray.length>0 then
begin
writeln(utf8decode(rsPending));
for task in res['pending'] do
writeln(utf8encode(' '+task.S['id']+' '+task.S['description']));
end;
end;
end
else
if action='cancel' then
begin
res := WAPTLocalJsonGet(Format('cancel_running_task.json?%s',[StrJoin('&',Args)]));
if res.DataType<>stNull then
writeln(utf8decode(format(rsCanceledTask, [res.S['description']])))
else
writeln(rsNoRunningTask);
end
else
if (action='cancel-all') or (action='cancelall') then
begin
res := WAPTLocalJsonGet(Format('cancel_all_tasks.json?%s',[StrJoin('&',Args)]));
if res.DataType<>stNull then
begin
for task in res do
writeln(utf8decode(format(rsCanceledTask, [task.S['description']])))
end
else
writeln(utf8decode(rsNoRunningTask));
end
else
if action='update' then
begin
Logger('Call update URL...',DEBUG);
res := WAPTLocalJsonGet(Format('update.json?%s',[StrJoin('&',Args)]));
tasks.AsArray.Add(res);
Logger(UTF8Encode('Task '+res.S['id']+' added to queue'),DEBUG);
end
else
if action='audit' then
begin
Logger('Call audit URL...',DEBUG);
res := WAPTLocalJsonGet(Format('audit.json?%s',[StrJoin('&',Args)]));
WriteLn(utf8encode(res.S['message']));
for task in res['content'] do
begin
tasks.AsArray.Add(task);
Logger('Task '+task.S['id']+' added to queue',DEBUG);
end;
end
else
if action='register' then
begin
Logger('Call register URL...',DEBUG);
res := WAPTLocalJsonGet(Format('register.json?%s',[StrJoin('&',Args)]),'admin','',-1,@HTTPLogin,3);
tasks.AsArray.Add(res);
Logger('Task '+res.S['id']+' added to queue',DEBUG);
end
else
if (action='install') or (action='remove') or (action='forget') then
begin
if HasOption('only_if_not_process_running') then
Args.Add('only_if_not_process_running='+GetOptionValue('only_if_not_process_running'));
for package in sopackages do
begin
Logger('Call '+action+'?package='+package.AsString,DEBUG);
Args.Add(utf8encode('package='+package.AsString));
res := WAPTLocalJsonGet(Format(Action+'.json?%s',[StrJoin('&',Args)]),'admin','',-1,@HTTPLogin,3);
if (action='install') or (action='forget') then
begin
// single action
if (res.AsObject=Nil) or not res.AsObject.Exists('id') then
WriteLn(utf8decode(format(rsErrorWithMessage, [res.AsString])))
else
tasks.AsArray.Add(res);
end
else
if (action='remove') then
begin
// list of actions..
if (res.AsArray=Nil) then
WriteLn(utf8decode(format(rsErrorWithMessage, [res.AsString])))
else
for task in res do
begin
tasks.AsArray.Add(task);
Logger('Task '+task.S['id']+' added to queue',DEBUG);
end;
end;
end;
end
else if action='upgrade' then
begin
Logger('Call upgrade URL...',DEBUG);
if HasOption('only_priorities') then
Args.Add('only_priorities='+GetOptionValue('only_priorities'));
if HasOption('only_if_not_process_running') then
Args.Add('only_if_not_process_running='+GetOptionValue('only_if_not_process_running'));
res := WAPTLocalJsonGet(Format('upgrade.json?%s',[StrJoin('&',args)]));
Logger('Upgrade triggered...',DEBUG);
if res.S['result']<>'OK' then
WriteLn(utf8decode(format(rsErrorLaunchingUpgrade, [res.S['message']])))
else
for task in res['content'] do
begin
tasks.AsArray.Add(task);
Logger('Task '+task.S['id']+' added to queue',DEBUG);
end;
end
else
if action='wuascan' then
begin
res := WAPTLocalJsonGet(Format('waptwua_scan?%s',[StrJoin('&',Args)]));
tasks.AsArray.Add(res);
Logger('Task '+res.S['id']+' added to queue',DEBUG);
end
else
if action='wuadownload' then
begin
res := WAPTLocalJsonGet(Format('waptwua_download?%s',[StrJoin('&',Args)]));
tasks.AsArray.Add(res);
Logger('Task '+res.S['id']+' added to queue',DEBUG);
end
else
if action='wuainstall' then
begin
res := WAPTLocalJsonGet(Format('waptwua_install?%s',[StrJoin('&',Args)]));
tasks.AsArray.Add(res);
Logger('Task '+res.S['id']+' added to queue',DEBUG);
end;
if (tasks<>Nil) and (tasks.AsArray.Length>0) then
begin
while ((remainingtasks=Nil) or (remainingtasks.AsArray.Length>0)) and not CheckEventsThread.Finished do
try
//if no message from service since more that 1 min, check if remaining tasks in queue...
if (now-lastMessageTime>1*1/24/60) then
raise Exception.create('Timeout waiting for events')
else
begin
While CheckSynchronize(100) do;
sleep(1000)
end;
except
on E:Exception do
begin
writeln(Format(rsCanceledTask,[E.Message]));
for task in tasks do
WAPTLocalJsonGet('cancel_task.json?id='+task.S['id']);
end;
end;
end;
while CheckSynchronize(1000) do;
except
localpassword := '';
ExitCode:=3;
raise;
end;
finally
for task in tasks do
WAPTLocalJsonGet('cancel_task.json?id='+task.S['id']);
end;
end
else
begin
// Load main python application
try
MainModule:=TStringList.Create;
MainModule.LoadFromFile(ExtractFilePath(ParamStr(0))+'wapt-get.py');
PythonEngine.ExecStrings(MainModule);
finally
MainModule.Free;
end;
end;
// stop program loop
Terminate;
end;
constructor PWaptGet.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException:=True;
InitializeCriticalSection(lock);
end;
destructor PWaptGet.Destroy;
begin
Fwaptdevutils := Nil;
if Assigned(CheckEventsThread) and (not CheckEventsThread.Suspended) then
CheckEventsThread.Terminate;
FreeAndNil(CheckEventsThread);
if Assigned(FPythonEngine) then
FPythonEngine.Free;
DeleteCriticalSection(lock);
inherited Destroy;
end;
procedure PWaptGet.WriteHelp;
begin
{ add your help code here }
writeln(utf8decode(format(rsUsage, [ExeName])));
writeln(rsInstallOn);
end;
procedure PWaptGet.OnCheckEventsThreadNotify(Sender: TObject);
var
Step,EventType:String;
taskresult : ISuperObject;
Events,Event,EventData:ISuperObject;
//check if task with id id is in tasks list
function isInTasksList(id:integer):boolean;
var
t:ISuperObject;
begin
//writeln('check '+IntToStr(id)+' in '+tasks.AsJSon());
result := False;
for t in tasks do
if t.I['id'] = id then
begin
result := True;
break;
end;
end;
//remove task with id id from tasks list
procedure removeTask(id:integer);
var
i:integer;
begin
for i:=0 to tasks.AsArray.Length-1 do
if tasks.AsArray[i].I['id'] = id then
begin
tasks.AsArray.Delete(i);
break;
end;
end;
begin
EnterCriticalSection(lock);
try
lastMessageTime := Now;
events := (Sender as TCheckEventsThread).Events;
If Events <> Nil then
begin
for Event in Events do
try
EventType := Event.S['event_type'];
EventData := Event['data'];
if EventType.StartsWith('TASK_') then
begin
Step := EventType.Substring(5);
taskresult := EventData;
//Writeln(EventType,' ',taskresult.S['id'],' ',taskresult.S['summary']);
if isInTasksList(taskresult.I['id']) then
begin
//writeln(taskresult.AsString);
if (Step = 'START') then
writeln(#13+UTF8Encode(taskresult.S['description']));
if (Step = 'PROGRESS') then
write(#13+utf8Encode(format(rsCompletionProgress,[taskresult.S['runstatus'], taskresult.D['progress']])+#13));
if (Step = 'STATUS') then
write(#13+utf8Encode(format(rsCompletionProgress,[taskresult.S['runstatus'], taskresult.D['progress']])+#13));
//catch finish of task
if (Step = 'FINISH') or (Step = 'ERROR') or (Step = 'CANCEL') then
begin
WriteLn(UTF8Encode(taskresult.S['summary']));
if (Step = 'ERROR') or (Step = 'CANCEL') then
ExitCode:=3;
removeTask(taskresult.I['id']);
end;
end;
end
else if (EventType = 'PRINT') then
Writeln(#13+UTF8Encode(EventData.AsString));
except
on E:Exception do WriteLn(#13+Format('Error listening to events: %s',[e.Message]));
end;
end
else
Write('.');
finally
LeaveCriticalSection(lock);
end;
end;
function PWaptGet.remainingtasks: ISuperObject;
var
task,pending,res:ISuperObject;
begin
res := WAPTLocalJsonGet('tasks.json');
if res<>Nil then
begin
pending := res['pending'];
if res['running'] <> Nil then
pending.AsArray.Add(res['running']);
Result := TSuperObject.Create(stArray);
if pending.AsArray.Length > 0 then
for task in Self.tasks do
if SOArrayFindFirst(task,pending,['id']) <> Nil then
Result.AsArray.Add(task);
end
else
Result := Nil;
end;
procedure PWaptGet.DoOnProgress(Sender: TObject);
begin
if (GetTickCount-FLastProgressMs) > 1000 then
begin
Write('.');
FLastProgressMs := GetTickCount;
end;
end;
procedure PWaptGet.DoOnHttpWork(ASender: TObject; AWorkMode: TWorkMode;
AWorkCount: Int64);
begin
if (GetTickCount-FLastProgressMs) > 1000 then
begin
Write('.');
FLastProgressMs := GetTickCount;
end;
end;