-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftware_installer2.cpp
More file actions
396 lines (337 loc) · 15 KB
/
software_installer2.cpp
File metadata and controls
396 lines (337 loc) · 15 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
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
#include "software_installer.h"
#include "environment_installer.h"
#include "config/config_manager.h"
#include "utils/wsl_process.h"
#include "utils/utils.h"
#include "utils/process.h"
#include "tinylog/tinylog.h"
#include <algorithm>
namespace parallax {
namespace environment {
// PipUpgradeManager implementation
PipUpgradeManager::PipUpgradeManager(std::shared_ptr<ExecutionContext> context,
std::shared_ptr<CommandExecutor> executor)
: BaseEnvironmentComponent(context), executor_(executor) {}
ComponentResult PipUpgradeManager::Check() {
LogOperationStart("Checking");
bool pip_available = IsPipUpToDate();
ComponentResult result =
pip_available ? CreateSkippedResult("pip is available")
: CreateFailureResult("pip is not installed", 24);
LogOperationResult("Checking", result);
return result;
}
ComponentResult PipUpgradeManager::Install() {
LogOperationStart("Upgrading");
const std::string& proxy_url = context_->GetProxyUrl();
// Check if pip is installed
if (!IsPipUpToDate()) {
info_log("[ENV] Installing python3-pip in WSL...");
// First install python3-pip
std::string install_pip_cmd =
proxy_url.empty()
? "apt-get install -y python3-pip"
: "apt-get -o Acquire::http::proxy=\"" + proxy_url +
"\" -o Acquire::https::proxy=\"" + proxy_url +
"\" install -y python3-pip";
auto [install_code, install_output] =
executor_->ExecuteWSL(install_pip_cmd, 300);
if (install_code != 0) {
ComponentResult result = CreateFailureResult(
"Failed to install python3-pip: " + install_output, 24);
LogOperationResult("Upgrading", result);
return result;
}
}
info_log("[ENV] Upgrading pip in WSL...");
// Upgrade pip
std::string upgrade_cmd =
"pip install --upgrade pip --break-system-packages --ignore-installed";
if (!proxy_url.empty()) {
upgrade_cmd =
"pip install --proxy " + proxy_url +
" --upgrade pip --break-system-packages --ignore-installed";
}
auto [upgrade_code, upgrade_output] =
executor_->ExecuteWSL(upgrade_cmd, 300);
ComponentResult result =
(upgrade_code != 0)
? CreateFailureResult("Failed to upgrade pip: " + upgrade_output,
24)
: CreateSuccessResult("pip installed and upgraded successfully");
LogOperationResult("Upgrading", result);
return result;
}
bool PipUpgradeManager::IsPipUpToDate() {
// Check if pip is installed and up to date
auto [pip_code, pip_output] = executor_->ExecuteWSL("pip --version");
return (pip_code == 0 && !pip_output.empty());
}
EnvironmentComponent PipUpgradeManager::GetComponentType() const {
return EnvironmentComponent::kPipUpgrade;
}
std::string PipUpgradeManager::GetComponentName() const {
return "pip Upgrade";
}
// ParallaxProjectInstaller implementation
ParallaxProjectInstaller::ParallaxProjectInstaller(
std::shared_ptr<ExecutionContext> context,
std::shared_ptr<CommandExecutor> executor)
: BaseEnvironmentComponent(context), executor_(executor) {}
ComponentResult ParallaxProjectInstaller::Check() {
LogOperationStart("Checking");
// Check if Parallax project is installed
bool is_installed = IsParallaxProjectInstalled();
if (is_installed) {
// Check if there are git updates
if (HasParallaxProjectGitUpdates()) {
ComponentResult result = CreateWarningResult(
"Parallax project is installed but has git updates available");
LogOperationResult("Checking", result);
return result;
} else {
ComponentResult result = CreateSkippedResult(
"Parallax project is already installed and up to date");
LogOperationResult("Checking", result);
return result;
}
}
ComponentResult result =
CreateFailureResult("Parallax project is not installed", 25);
LogOperationResult("Checking", result);
return result;
}
ComponentResult ParallaxProjectInstaller::Install() {
LogOperationStart("Installing");
// Check if Parallax project is installed
bool is_installed = IsParallaxProjectInstalled();
// Handle install command
if (is_installed) {
// Project is installed, check if there are updates
if (!HasParallaxProjectGitUpdates()) {
ComponentResult result = CreateSkippedResult(
"Parallax project is already installed and up to date");
LogOperationResult("Installing", result);
return result;
}
// Has updates, continue with update process
info_log("[ENV] Parallax project has updates available, updating...");
} else {
// Project not installed, perform full installation
info_log("[ENV] Installing Parallax project in WSL...");
}
// Get configured git repository URL
std::string repo_url =
parallax::config::ConfigManager::GetInstance().GetConfigValue(
parallax::config::KEY_PARALLAX_GIT_REPO_URL);
const std::string& proxy_url = context_->GetProxyUrl();
// Install command sequence (step name, command, timeout seconds, use
// real-time output)
std::vector<std::tuple<std::string, std::string, int, bool>> commands;
// Determine if it's update mode or fresh installation mode
bool is_update_mode = (is_installed);
if (is_update_mode) {
// Project is installed and has updates, only execute git pull update
std::string pull_cmd = "cd ~/parallax && git pull";
if (!proxy_url.empty()) {
pull_cmd = "cd ~/parallax && ALL_PROXY=" + proxy_url + " git pull";
}
commands.emplace_back("update_parallax", pull_cmd, 300, false);
} else {
// Project not installed, check if parallax directory exists, decide
// whether to clone or pull
auto [check_dir_code, check_dir_output] = executor_->ExecuteWSL(
"ls -la ~/parallax/.git 2>/dev/null || echo 'not found'", 30);
if (check_dir_code == 0 &&
check_dir_output.find("not found") == std::string::npos) {
// Directory exists, check if it's a git repository
auto [check_git_code, check_git_output] = executor_->ExecuteWSL(
"cd ~/parallax && git branch 2>/dev/null || echo 'not git'",
30);
if (check_git_code == 0 &&
check_git_output.find("not git") == std::string::npos) {
// Is a git repository, execute pull
info_log(
"[ENV] Parallax directory exists, updating with git "
"pull...");
std::string pull_cmd = "cd ~/parallax && git pull";
if (!proxy_url.empty()) {
pull_cmd =
"cd ~/parallax && ALL_PROXY=" + proxy_url + " git pull";
}
commands.emplace_back("update_parallax", pull_cmd, 300, false);
} else {
// Not a git repository, remove directory and clone again
info_log(
"[ENV] Parallax directory exists but is not a git "
"repository, "
"removing and cloning...");
commands.emplace_back("remove_old_parallax",
"rm -rf ~/parallax", 60, false);
std::string clone_cmd = "cd ~ && ";
if (!proxy_url.empty()) {
clone_cmd += "ALL_PROXY=" + proxy_url + " ";
}
clone_cmd += "git clone " + repo_url;
commands.emplace_back("clone_parallax", clone_cmd, 600, false);
}
} else {
// Directory does not exist, execute clone
info_log("[ENV] Parallax directory not found, cloning...");
std::string clone_cmd = "cd ~ && ";
if (!proxy_url.empty()) {
clone_cmd += "ALL_PROXY=" + proxy_url + " ";
}
clone_cmd += "git clone " + repo_url;
commands.emplace_back("clone_parallax", clone_cmd, 600, false);
}
}
if (!is_update_mode) {
// Only install python3-venv dependency package during first
// installation
std::string install_venv_cmd =
"apt update && apt-get install -y python3-venv";
if (!proxy_url.empty()) {
install_venv_cmd =
"apt -o Acquire::http::proxy=\"" + proxy_url +
"\" -o Acquire::https::proxy=\"" + proxy_url +
"\" update && apt-get -o Acquire::http::proxy=\"" + proxy_url +
"\" -o Acquire::https::proxy=\"" + proxy_url +
"\" install -y python3-venv";
}
commands.emplace_back("install_python3_venv", install_venv_cmd, 300,
false);
}
// Install Parallax project (use real-time output)
std::string install_base_cmd =
"cd ~/parallax && ([ -d ./venv ] || python3 -m venv ./venv) && source "
"./venv/bin/activate "
"&& pip install -e '.[gpu]'";
if (!proxy_url.empty()) {
install_base_cmd =
"cd ~/parallax && ([ -d ./venv ] || python3 -m venv ./venv) && "
"source "
"./venv/bin/activate && HTTP_PROXY=\"" +
proxy_url + "\" HTTPS_PROXY=\"" + proxy_url +
"\" pip install -e '.[gpu]'";
}
commands.emplace_back("install_parallax_base", install_base_cmd, 1800,
true);
if (!is_update_mode) {
// Add CUDA environment variable to system profile (only during first installation)
std::string add_cuda_env_cmd =
"grep -q '/usr/local/cuda-12.8/bin' ~/.bashrc || "
"echo 'export PATH=/usr/local/cuda-12.8/bin:$PATH' >> ~/.bashrc";
commands.emplace_back("add_cuda_env", add_cuda_env_cmd, 30, false);
}
// Execute command sequence
ComponentResult cmd_result =
ExecuteCommandSequence(commands, "Parallax project installation");
if (cmd_result.status != InstallationStatus::kSuccess) {
LogOperationResult("Installing", cmd_result);
return cmd_result;
}
// Verify installation
ComponentResult result =
IsParallaxProjectInstalled()
? (is_update_mode ? CreateSuccessResult(
"Parallax project updated successfully")
: CreateSuccessResult(
"Parallax project installed successfully"))
: CreateFailureResult(is_update_mode
? "Parallax project update completed but "
"verification failed"
: "Parallax project installation "
"completed but verification failed",
25);
LogOperationResult("Installing", result);
return result;
}
ComponentResult ParallaxProjectInstaller::ExecuteCommandSequence(
const std::vector<std::tuple<std::string, std::string, int, bool>>&
commands,
const std::string& operation_name) {
// Execute all commands
for (const auto& [step_name, cmd, timeout, use_realtime] : commands) {
info_log("[ENV] %s step: %s", operation_name.c_str(),
step_name.c_str());
int cmd_exit_code = 0;
if (use_realtime) {
// Use WSLProcess to get real-time output
std::string wsl_cmd = parallax::utils::BuildWSLCommand(
context_->GetUbuntuVersion(), cmd);
WSLProcess wsl_process;
cmd_exit_code = wsl_process.Execute(wsl_cmd);
} else {
// Use regular execution method
auto [exit_code, output] = executor_->ExecuteWSL(cmd, timeout);
cmd_exit_code = exit_code;
}
if (cmd_exit_code != 0) {
std::string error_msg =
"Failed at step '" + step_name + "': " + cmd;
return CreateFailureResult(error_msg, 25);
}
}
return CreateSuccessResult("Command sequence completed successfully");
}
bool ParallaxProjectInstaller::IsParallaxProjectInstalled() {
// Check if parallax project is installed (need to check in virtual
// environment)
auto [check_code, check_output] = executor_->ExecuteWSL(
"cd ~/parallax && [ -d ./venv ] && source ./venv/bin/activate && pip "
"list | grep parallax");
return (check_code == 0 && !check_output.empty());
}
bool ParallaxProjectInstaller::HasParallaxProjectGitUpdates() {
const std::string& proxy_url = context_->GetProxyUrl();
// Check if Parallax project has git updates
// First check if git repository exists
auto [check_git_code, check_git_output] = executor_->ExecuteWSL(
"cd ~/parallax && git rev-parse --is-inside-work-tree 2>/dev/null", 30);
if (check_git_code != 0) {
// Not a git repository or directory does not exist
return false;
}
// Get remote update information
std::string fetch_cmd = "cd ~/parallax && git fetch origin";
if (!proxy_url.empty()) {
fetch_cmd =
"cd ~/parallax && ALL_PROXY=" + proxy_url + " git fetch origin";
}
auto [fetch_code, fetch_output] = executor_->ExecuteWSL(fetch_cmd, 60);
if (fetch_code != 0) {
// Failed to get remote information, possibly network issue
return false;
}
// Check if there are differences between local and remote
auto [diff_code, diff_output] = executor_->ExecuteWSL(
"cd ~/parallax && git rev-list HEAD...origin/main --count 2>/dev/null",
30);
if (diff_code == 0 && !diff_output.empty()) {
try {
// Remove possible whitespace characters
std::string trimmed_output = diff_output;
trimmed_output.erase(
std::remove_if(trimmed_output.begin(), trimmed_output.end(),
::isspace),
trimmed_output.end());
if (!trimmed_output.empty()) {
int update_count = std::stoi(trimmed_output);
return update_count > 0;
}
} catch (const std::exception&) {
// Conversion failed, possibly incorrect output format
return false;
}
}
return false;
}
EnvironmentComponent ParallaxProjectInstaller::GetComponentType() const {
return EnvironmentComponent::kParallaxProject;
}
std::string ParallaxProjectInstaller::GetComponentName() const {
return "Parallax Project";
}
} // namespace environment
} // namespace parallax