-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.iss
More file actions
296 lines (262 loc) · 8.49 KB
/
setup.iss
File metadata and controls
296 lines (262 loc) · 8.49 KB
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
; Inno Setup script for Infrastructure Agent
#define MyAppName "Infrastructure Agent"
#define MyAppVersion "1.0"
#define MyAppPublisher "thien.dinh-infra"
#define MyAppURL "https://10.1.32.66"
#define MyAppExeName "InfraAgent.exe"
[Setup]
AppId={{8B4D8F1D-4F4E-4A26-B8E4-6A7D8E1D4F2A}}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
PrivilegesRequired=admin
OutputDir=.
OutputBaseFilename=InfraAgentSetup
SetupIconFile=agentlogo.ico
UninstallDisplayIcon={app}\agentlogo.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "startup"; Description: "Run at Windows startup for all users"; GroupDescription: "Startup settings:"
Name: "desktopicon"; Description: "Create a desktop icon"; GroupDescription: "Additional icons:"
[Files]
Source: "dist\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "agentlogo.ico"; DestDir: "{app}"; Flags: ignoreversion
[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#MyAppName}}"; Flags: nowait postinstall skipifsilent
[UninstallDelete]
Type: filesandordirs; Name: "{app}"
[Code]
var
ServerPage: TInputQueryWizardPage;
CredentialsPage: TInputQueryWizardPage;
procedure InitializeWizard;
begin
ServerPage := CreateInputQueryPage(wpSelectTasks,
'Server Configuration', 'Enter server connection details',
'Please specify the server URL where the agent will send data.');
ServerPage.Add('Server URL:', False);
CredentialsPage := CreateInputQueryPage(ServerPage.ID,
'Login Credentials', 'Enter authentication details',
'Please specify the email and password for agent authentication.');
CredentialsPage.Add('Email:', False);
CredentialsPage.Add('Password:', True);
ServerPage.Values[0] := 'https://10.1.32.66';
CredentialsPage.Values[0] := 'infraagent@localhost.com';
CredentialsPage.Values[1] := 'Infraagent@2025';
end;
function NextButtonClick(CurPageID: Integer): Boolean;
begin
Result := True;
end;
function IsAppRunning(const FileName : string): Boolean;
var
FSWbemLocator: Variant;
FWMIService: Variant;
FWbemObjectSet: Variant;
begin
Result := false;
try
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('', 'root\CIMV2', '', '');
FWbemObjectSet := FWMIService.ExecQuery(Format('SELECT Name FROM Win32_Process Where Name="%s"', [FileName]));
Result := (FWbemObjectSet.Count > 0);
FWbemObjectSet := Unassigned;
FWMIService := Unassigned;
FSWbemLocator := Unassigned;
except
Result := false;
end;
end;
function StopApp(const FileName : string): Boolean;
var
FSWbemLocator: Variant;
FWMIService: Variant;
FWbemObject: Variant;
FWbemObjectSet: Variant;
begin
Result := false;
try
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('', 'root\CIMV2', '', '');
FWbemObjectSet := FWMIService.ExecQuery(Format('SELECT * FROM Win32_Process Where Name="%s"', [FileName]));
if FWbemObjectSet.Count > 0 then
begin
FWbemObject := FWbemObjectSet.ItemIndex(0);
Result := (FWbemObject.Terminate(0) = 0);
end;
FWbemObjectSet := Unassigned;
FWbemObject := Unassigned;
FWMIService := Unassigned;
FSWbemLocator := Unassigned;
except
Result := false;
end;
end;
function UpdateConfigFile(const ConfigPath, ServerURL, Email, Password: String): Boolean;
var
Lines: TArrayOfString;
Content: String;
I: Integer;
p: Integer;
begin
Result := False;
// Load file content using the correct function
if LoadStringsFromFile(ConfigPath, Lines) then
begin
// Join all lines into a single string
Content := '';
for I := 0 to GetArrayLength(Lines) - 1 do
begin
Content := Content + Lines[I] + #13#10;
end;
// Simple string replacement without using StringReplace function
// Replace server URL
p := Pos('"https://10.1.32.66"', Content);
while p > 0 do
begin
Delete(Content, p, 21); // Length of '"https://10.1.32.66"'
Insert('"' + ServerURL + '"', Content, p);
p := Pos('"https://10.1.32.66"', Content);
end;
// Replace email
p := Pos('"infraagent@localhost.com"', Content);
while p > 0 do
begin
Delete(Content, p, 27); // Length of '"infraagent@localhost.com"'
Insert('"' + Email + '"', Content, p);
p := Pos('"infraagent@localhost.com"', Content);
end;
// Replace password
p := Pos('"Infraagent@2025"', Content);
while p > 0 do
begin
Delete(Content, p, 18); // Length of '"Infraagent@2025"'
Insert('"' + Password + '"', Content, p);
p := Pos('"Infraagent@2025"', Content);
end;
// Save updated content
if SaveStringToFile(ConfigPath, Content, False) then
begin
Result := True;
end;
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
var
ConfigFile: String;
ServerURL, Email, Password: String;
begin
if CurStep = ssInstall then
begin
// Stop any running instances of the agent before installation
if IsAppRunning('InfraAgent.exe') then
begin
// Try to stop the application gracefully
StopApp('InfraAgent.exe');
// Wait a moment for the process to terminate
Sleep(1000);
end;
end
else if CurStep = ssPostInstall then
begin
ServerURL := ServerPage.Values[0];
Email := CredentialsPage.Values[0];
Password := CredentialsPage.Values[1];
// Update config file with user-provided values
ConfigFile := ExpandConstant('{app}\config.json');
UpdateConfigFile(ConfigFile, ServerURL, Email, Password);
// Add startup shortcut if selected
if WizardIsTaskSelected('startup') then
begin
CreateShellLink(
ExpandConstant('{commonstartup}\Infrastructure Agent.lnk'),
'Infrastructure Agent',
ExpandConstant('{app}\InfraAgent.exe'),
'',
ExpandConstant('{app}'),
ExpandConstant('{app}\agentlogo.ico'),
0,
SW_SHOWNORMAL);
end;
end;
end;
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
AppPath: String;
ResultCode: Integer;
begin
case CurUninstallStep of
usUninstall:
begin
// Stop any running instances of the agent before uninstallation
if IsAppRunning('InfraAgent.exe') then
begin
StopApp('InfraAgent.exe');
Sleep(3000); // Give more time for the process to terminate
end;
// Force kill any remaining processes
try
Exec('taskkill', '/F /IM InfraAgent.exe', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
except
// Ignore any errors
end;
// Remove startup shortcut
DeleteFile(ExpandConstant('{commonstartup}\Infrastructure Agent.lnk'));
// Remove desktop shortcut
DeleteFile(ExpandConstant('{autodesktop}\Infrastructure Agent.lnk'));
// Remove programs menu shortcut
DeleteFile(ExpandConstant('{autoprograms}\Infrastructure Agent.lnk'));
end;
usPostUninstall:
begin
// Additional cleanup can be done here if needed
// Force removal of application directory
AppPath := ExpandConstant('{app}');
if DirExists(AppPath) then
begin
DelTree(AppPath, True, True, True);
end;
// Remove any remaining registry entries
try
RegDeleteKeyIncludingSubkeys(HKLM, 'SOFTWARE\{#MyAppName}');
except
// Ignore any errors
end;
try
RegDeleteKeyIncludingSubkeys(HKCU, 'SOFTWARE\{#MyAppName}');
except
// Ignore any errors
end;
end;
end;
end;
function InitializeUninstall(): Boolean;
var
ResultCode: Integer;
begin
// Stop any running instances of the agent before uninstallation
if IsAppRunning('InfraAgent.exe') then
begin
StopApp('InfraAgent.exe');
Sleep(3000); // Give more time for the process to terminate
end;
// Force kill any remaining processes
try
Exec('taskkill', '/F /IM InfraAgent.exe', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
except
// Ignore any errors
end;
Result := True;
end;