-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitLabRunnerService.cs
98 lines (89 loc) · 3.74 KB
/
GitLabRunnerService.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Management.Automation;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace GitlabRunnerWrapper
{
public partial class GitLabRunnerService : ServiceBase
{
private EventLog eventLog;
public GitLabRunnerService()
{
this.ServiceName = "GitlabRunnerWrapper";
this.CanStop = true;
this.CanPauseAndContinue = false;
this.AutoLog = false;
eventLog = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists("GitLabRunnerWrapper"))
{
System.Diagnostics.EventLog.CreateEventSource(
"GitLabRunnerWrapper", "Application");
}
// configure the event log instance to use this source name
eventLog.Source = "GitLabRunnerWrapper";
eventLog.Log = "Application";
}
protected override void OnStart(string[] args)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
var appSettings = ConfigurationManager.AppSettings;
var script = "New-Item -Path \"C:\\\" -Name \"GitLab-Runner\" -ItemType \"directory\";" +
"Invoke-WebRequest -Uri " + appSettings["gitlab_runner_bin"] + " -OutFile C:\\GitLab-Runner\\gitlab-runner.exe;" +
"C:\\GitLab-Runner\\gitlab-runner.exe register " +
"--non-interactive " +
"--url \"" + appSettings["gitlab_url"] + "\" " +
"--registration-token " + appSettings["gitlab_registration_token"] + " " +
"--tag-list " + appSettings["gitlab_tags"] + " " +
"--locked=\"false\" " +
"--executor " + appSettings["gitlab_executor"] + ";" +
"C:\\GitLab-Runner\\gitlab-runner.exe install;" +
"C:\\GitLab-Runner\\gitlab-runner.exe start";
PowerShellInstance.AddScript(script);
//PowerShellInstance.AddScript(script);
Collection <PSObject> PSOutput = PowerShellInstance.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
eventLog.WriteEntry(outputItem.BaseObject.ToString());
}
}
}
eventLog.WriteEntry("Service started");
}
protected override void OnStop()
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
var script = "C:\\GitLab-Runner\\gitlab-runner.exe stop;" +
"C:\\GitLab-Runner\\gitlab-runner.exe uninstall;" +
"rmdir /s C:\\GitLab-Runner";
PowerShellInstance.AddScript(script);
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
eventLog.WriteEntry(outputItem.BaseObject.ToString());
}
}
}
eventLog.WriteEntry("Service stoped");
}
internal void TestStartupAndStop()
{
this.OnStart(new String[] { });
Console.ReadLine();
this.OnStop();
}
}
}