forked from crc-org/tray-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRCTray.cs
executable file
·335 lines (275 loc) · 11.5 KB
/
CRCTray.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
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
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Collections.Generic;
using CRCTray.Communication;
using CRCTray.Helpers;
namespace CRCTray
{
internal class CRCTray : ApplicationContext
{
private TrayIcon trayIcon;
// MenuStrip Items
private ToolStripLabel status;
private ToolStripItem detailedStatusMenu;
// cluster commands
private ToolStripItem startMenu;
private ToolStripItem stopMenu;
private ToolStripItem deleteMenu;
// application commands
private ToolStripItem aboutMenu;
private ToolStripItem exitMenu;
private ToolStripItem settingsMenu;
// developer commands
private ToolStripItem openWebConsoleMenu;
private ToolStripItem copyOCLoginCommand;
private ToolStripItem copyOCLoginForDeveloperMenu;
private ToolStripItem copyOCLoginForKubeadminMenu;
// Forms
private Form settingsWindow;
private Form about;
private Form statusForm;
private readonly double pollInterval = 5000; // 5 seconds poll interval
private readonly string InitialState = @"Stopped";
// Initialize tray
public CRCTray()
{
trayIcon = new TrayIcon(Resource.ocp_logo, "CodeReady Containers");
StartDaemon();
// Keep polling status and updating the statusMenuItem
var statusPollingTimer = new System.Timers.Timer(pollInterval);
statusPollingTimer.Enabled = true;
statusPollingTimer.Elapsed += pollStatusTimerEventHandler;
TaskHandlers.StatusChanged += UpdateReceived;
TaskHandlers.StopReceived += StopReceived;
TaskHandlers.DeleteReceived += DeleteReceived;
SetContextMenu();
}
private void StartDaemon()
{
Task.Run(() => DaemonLauncher.Start(QuitApp));
}
private static void pollStatusTimerEventHandler(object source, System.Timers.ElapsedEventArgs e)
{
try
{
Task.Run(TaskHandlers.Status);
}
catch
{
// Status failed, but ignoring
}
}
// populate the context menu for tray icon
private void SetContextMenu()
{
ContextMenuStrip cm = new ContextMenuStrip();
cm.AccessibleName = "menu";
// Status Menu
ToolStripLabel status = new ToolStripLabel(InitialState); //TODO: actually "Unknown"
status.AccessibleName = status.Text;
status.TextChanged += Status_TextChanged;
status.Enabled = false;
cm.Items.Add(status);
cm.Items.Add(new ToolStripSeparator());
// Detailed status menu
detailedStatusMenu = cm.Items.Add(@"Status and Logs");
detailedStatusMenu.AccessibleName = "status-detailed";
detailedStatusMenu.Click += ShowDetailedStatusForm;
cm.Items.Add(new ToolStripSeparator());
// Start Menu
startMenu = cm.Items.Add(@"Start");
startMenu.AccessibleName = "start";
startMenu.Click += StartMenu_Click;
// Stop Menu
stopMenu = cm.Items.Add(@"Stop");
stopMenu.AccessibleName = "stop";
stopMenu.Click += StopMenu_Click;
// Delete Menu
deleteMenu = cm.Items.Add(@"Delete");
deleteMenu.AccessibleName = "delete";
deleteMenu.Click += DeleteMenu_Click;
cm.Items.Add(new ToolStripSeparator());
// Open web console menu
openWebConsoleMenu = cm.Items.Add(@"Launch Web Console");
openWebConsoleMenu.AccessibleName = "open-web-console";
openWebConsoleMenu.Click += OpenWebConsoleMenu_Click;
// Copy oc login command
copyOCLoginCommand = cm.Items.Add(@"Copy OC Login Command");
copyOCLoginCommand.AccessibleName = "copy-oc-login";
// Copy oc login command: developer
copyOCLoginForDeveloperMenu = (copyOCLoginCommand as ToolStripMenuItem).DropDownItems.Add(@"Developer");
copyOCLoginForDeveloperMenu.AccessibleName = "developer";
copyOCLoginForDeveloperMenu.Click += CopyOCLoginForDeveloperMenu_Click;
// Copy oc login command: kubeadmin
copyOCLoginForKubeadminMenu = (copyOCLoginCommand as ToolStripMenuItem).DropDownItems.Add(@"Kubeadmin");
copyOCLoginForKubeadminMenu.AccessibleName = "kubeadmin";
copyOCLoginForKubeadminMenu.Click += CopyOCLoginForKubeadminMenu_Click;
cm.Items.Add(new ToolStripSeparator());
// Settings menu
settingsMenu = cm.Items.Add(@"Settings");
settingsMenu.AccessibleName = "settings";
settingsMenu.Click += SettingsMenu_Click;
cm.Items.Add(new ToolStripSeparator());
// About menu
aboutMenu = cm.Items.Add(@"About");
aboutMenu.AccessibleName = "about";
aboutMenu.Click += ShowAboutForm;
// Exit menu
exitMenu = cm.Items.Add(@"Exit");
exitMenu.AccessibleName = "exit";
exitMenu.Click += ExitMenu_Click;
// set context menu on trayicon
trayIcon.ContextMenuStrip = cm;
// enable items
aboutMenu.Enabled = true;
detailedStatusMenu.Enabled = true;
}
private void Status_TextChanged(object sender, EventArgs e)
{
status.AccessibleName = status.Text;
}
private void SettingsMenu_Click(object sender, EventArgs e)
{
if (settingsWindow == null)
settingsWindow = new CrcSettingsForm();
if (!settingsWindow.Visible)
settingsWindow.Show();
settingsWindow.Focus();
}
async private void CopyOCLoginForKubeadminMenu_Click(object sender, EventArgs e)
{
try
{
var consoleResult = await Task.Run(TaskHandlers.LoginForKubeadmin);
Clipboard.SetText(string.Format("oc.exe login -u kubeadmin -p {0} {1}",
consoleResult.ClusterConfig.KubeAdminPass, consoleResult.ClusterConfig.ClusterAPI));
}
catch
{
TrayIcon.NotifyError(@"Could not find credentials. Is CRC runnning?");
}
}
async private void CopyOCLoginForDeveloperMenu_Click(object sender, EventArgs e)
{
try
{
var consoleResult = await Task.Run(TaskHandlers.LoginForDeveloper);
Clipboard.SetText(string.Format("oc.exe login -u developer -p developer {0}",
consoleResult.ClusterConfig.ClusterAPI));
}
catch
{
TrayIcon.NotifyError(@"Could not find credentials. Is CRC running?");
}
}
async private void OpenWebConsoleMenu_Click(object sender, EventArgs e)
{
try
{
var consoleResult = await Task.Run(TaskHandlers.WebConsole);
Process.Start(consoleResult.ClusterConfig.WebConsoleURL);
}
catch
{
TrayIcon.NotifyError(@"Could not open web console. Is CRC running?");
}
}
async private void DeleteMenu_Click(object sender, EventArgs e)
{
TrayIcon.NotifyInfo(@"Deleting cluster");
await TaskHelpers.TryTaskAndNotify(TaskHandlers.Delete,
"Cluster deleted",
"Could not delete the cluster",
String.Empty);
}
async private void StopMenu_Click(object sender, EventArgs e)
{
TrayIcon.NotifyInfo(@"Stopping cluster");
await TaskHelpers.TryTaskAndNotify(TaskHandlers.Stop,
"Cluster stopped",
"Cluster could not be stopped",
String.Empty);
}
async private void StartMenu_Click(object sender, EventArgs e)
{
// Check using get-config if pullSecret is configured
var configs = await TaskHelpers.TryTaskAndNotify(TaskHandlers.ConfigView,
String.Empty,
String.Empty,
String.Empty);
if(configs == null)
{
// no config was returned, does this mean a communication error?
TrayIcon.NotifyError("Unable to read configuration. Is the CRC daemon running?");
return;
}
if (configs != null && configs.Configs.PullSecretFile == String.Empty)
{
var pullSecretForm = new PullSecretPickerForm();
var pullSecretPath = pullSecretForm.ShowFilePicker();
if (pullSecretPath == String.Empty)
{
TrayIcon.NotifyWarn(@"No Pull Secret was provided, Cannot start cluster without pull secret.");
return;
}
Dictionary<String, dynamic> pullSecretConfig = new Dictionary<String, dynamic>
{
["pull-secret-file"] = pullSecretPath
};
await TaskHelpers.TryTaskAndNotify(TaskHandlers.SetConfig, pullSecretConfig,
"Pull Secret stored",
"Pull Secret not stored",
String.Empty);
}
TrayIcon.NotifyInfo(@"Starting Cluster");
var startResult = await TaskHelpers.TryTaskAndNotify(TaskHandlers.Start,
String.Empty,
"Cluster did not start",
"Cluster still starting. Please check detailed status.");
if (startResult != null && startResult.KubeletStarted)
TrayIcon.NotifyInfo(@"CodeReady Containers Cluster has started");
}
private void ExitMenu_Click(object sender, EventArgs e)
{
QuitApp();
}
private void ShowAboutForm(object sender, EventArgs e)
{
if (about == null)
about = new AboutForm();
if (!about.Visible)
about.Show();
about.Focus();
}
private void ShowDetailedStatusForm(object sender, EventArgs e)
{
if (statusForm == null)
statusForm = new StatusForm();
if (!statusForm.Visible)
statusForm.Show();
statusForm.Focus();
}
private void UpdateReceived(StatusResult statusResult)
{
status.Text = statusResult.CrcStatus;
// TODO: enable based on status
//startMenu.Enabled = false;
}
private void StopReceived(StopResult result)
{
status.Text = InitialState;
}
private void DeleteReceived(DeleteResult result)
{
status.Text = InitialState;
}
private void QuitApp()
{
trayIcon.Visible = false;
trayIcon.Dispose();
Environment.Exit(-1);
}
}
}