forked from cybergodai/cybergodai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
220 lines (195 loc) · 7.9 KB
/
Program.cs
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
using System;
using System.Diagnostics;
public class Installer
{
public void InstallChocolatey()
{
try
{
Console.WriteLine("Starting Chocolatey installation...");
ShowProgress("Installing Chocolatey...", 0);
RunCommand("powershell -Command \"Set-ExecutionPolicy RemoteSigned -Scope CurrentUser\"");
ShowProgress("Setting execution policy...", 25);
RunCommand("powershell -Command \"iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))\"");
ShowProgress("Installing Chocolatey...", 50);
ShowProgress("Chocolatey installation completed.", 100);
Console.WriteLine("Chocolatey installed successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error installing Chocolatey: {ex.Message}");
}
}
public void InstallWSL()
{
try
{
Console.WriteLine("Starting WSL2 installation...");
ShowProgress("Installing WSL2...", 0);
// Ensure WSL and Virtual Machine Platform features are enabled
RunCommand("powershell -Command \"dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all\"");
ShowProgress("Enabling WSL feature...", 25);
RunCommand("powershell -Command \"dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all\"");
ShowProgress("Enabling Virtual Machine Platform...", 50);
// Install WSL feature with admin rights
RunCommand("powershell -Command \"wsl --install --no-launch --quiet\"");
ShowProgress("Installing WSL feature...", 60);
// Set WSL2 as default version with admin rights
RunCommand("powershell -Command \"wsl --set-default-version 2\"");
ShowProgress("Setting WSL2 as default version...", 80);
// Install Ubuntu 24.04 (if needed) in non-interactive mode with admin rights
RunCommand("powershell -Command \"wsl --install -d Ubuntu-24.04 --quiet\"");
ShowProgress("Installing Ubuntu 24.04...", 90);
// Final progress update
ShowProgress("WSL2 installation completed.", 100);
Console.WriteLine("WSL2 installed successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error installing WSL2: {ex.Message}");
}
}
public class WSLInstaller
{
public void InstallUbuntu()
{
try
{
Console.WriteLine("Checking if Ubuntu 24.04 is installed...");
ShowProgress("Checking for Ubuntu 24.04...", 0);
// Check if Ubuntu is already installed
string installedDistros = RunCommand("wsl --list --verbose");
if (!installedDistros.Contains("Ubuntu-24.04"))
{
// If not installed, install Ubuntu 24.04
Console.WriteLine("Ubuntu 24.04 is not installed. Installing...");
ShowProgress("Installing Ubuntu 24.04...", 25);
// Run the WSL install command for Ubuntu 24.04
RunCommand("wsl --install -d Ubuntu-24.04");
ShowProgress("Ubuntu 24.04 installation in progress...", 75);
// Optionally set Ubuntu 24.04 as the default WSL distro
RunCommand("wsl --set-default Ubuntu-24.04");
ShowProgress("Setting Ubuntu 24.04 as default...", 100);
Console.WriteLine("Ubuntu 24.04 installed and set as default distribution!");
}
else
{
Console.WriteLine("Ubuntu 24.04 is already installed.");
ShowProgress("Ubuntu 24.04 already installed.", 100);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error installing Ubuntu 24.04: {ex.Message}");
}
}
public void InstallLXC()
{
try
{
Console.WriteLine("Starting LXC installation...");
ShowProgress("Installing LXC...", 0);
// Install LXC in Ubuntu 24.04
RunCommand("wsl sudo apt update");
ShowProgress("Updating package lists...", 25);
RunCommand("wsl sudo apt install -y lxc lxd");
ShowProgress("Installing LXC and LXD...", 50);
// Initialize LXC
RunCommand("wsl sudo lxd init --auto");
ShowProgress("Initializing LXC...", 75);
ShowProgress("LXC installation completed.", 100);
Console.WriteLine("LXC installed successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error installing LXC: {ex.Message}");
}
}
private string RunCommand(string command)
{
try
{
// Start a new process with elevated (administrator) rights
var processStartInfo = new ProcessStartInfo("cmd.exe")
{
Arguments = "/C " + command, // Use /C to run the command and then close cmd.exe
Verb = "runas", // This will prompt for Administrator privileges
UseShellExecute = true, // Required for 'runas' to work
RedirectStandardOutput = true,
CreateNoWindow = true // Don't create a window
};
using (var process = Process.Start(processStartInfo))
{
process.WaitForExit();
// Return the output of the command (if needed for debugging)
return process.StandardOutput.ReadToEnd();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error running command: {ex.Message}");
return string.Empty;
}
}
private void ShowProgress(string taskDescription, int progress)
{
Console.Clear();
Console.WriteLine(taskDescription);
Console.WriteLine($"Progress: {progress}%");
Console.Write("[");
int barLength = 50;
int progressBar = (int)((double)progress / 100 * barLength);
Console.Write(new string('#', progressBar));
Console.Write(new string('-', barLength - progressBar));
Console.Write("]");
}
}
private void RunCommand(string command)
{
try
{
// Start a new process with elevated (administrator) rights
var processStartInfo = new ProcessStartInfo("cmd.exe")
{
Arguments = "/C " + command, // Command to run
Verb = "runas", // This will prompt for Administrator privileges
UseShellExecute = true, // Required for 'runas' to work
RedirectStandardOutput = false,
CreateNoWindow = true // Don't create a window
};
// Start the process and wait for it to finish
using (var process = Process.Start(processStartInfo))
{
process.WaitForExit();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error running command: {ex.Message}");
}
}
private void ShowProgress(string taskDescription, int progress)
{
Console.Clear();
Console.WriteLine(taskDescription);
Console.WriteLine($"Progress: {progress}%");
Console.Write("[");
int barLength = 50;
int progressBar = (int)((double)progress / 100 * barLength);
Console.Write(new string('#', progressBar));
Console.Write(new string('-', barLength - progressBar));
Console.Write("]");
System.Threading.Thread.Sleep(500); // Simulate work being done with a brief delay
}
}
class Program
{
static void Main(string[] args)
{
Installer installer = new Installer();
// First, install Chocolatey
installer.InstallChocolatey();
// Second, install WSL2
installer.InstallWSL();
}
}