diff --git a/AutoScalerConfiguration.cs b/AutoScalerConfiguration.cs index 1455890..2fe22ad 100644 --- a/AutoScalerConfiguration.cs +++ b/AutoScalerConfiguration.cs @@ -6,4 +6,7 @@ public class AutoScalerConfiguration public List Sizes { get; set; } public string HetznerToken { get; set; } + public string ProvisionScriptBaseUrl { get; set; } + public string MetricPassword { get; set; } + public string MetricUser { get; set; } } \ No newline at end of file diff --git a/CloudController.cs b/CloudController.cs index 11c3769..791a179 100644 --- a/CloudController.cs +++ b/CloudController.cs @@ -19,16 +19,27 @@ public class CloudController .CreateGauge("github_machines_active", "Number of active machines", labelNames: ["org","size"]); private readonly List _configSizes; + private readonly string _provisionBaseUrl; + private readonly string _metricUser; + private readonly string _metricPassword; public CloudController(ILogger logger, string hetznerCloudToken, string persistPath, - List configSizes) + List configSizes, + string provisionScriptBaseUrl, + string metricUser, + string metricPassword) { _configSizes = configSizes; _persistentPath = Path.Combine(persistPath, "activeRunners.json"); _client = new(hetznerCloudToken); _logger = logger; + _provisionBaseUrl = provisionScriptBaseUrl; + _metricUser = metricUser; + _metricPassword = metricPassword; + + _logger.LogInformation("Loading from persistent file."); LoadActiveRunners().Wait(); _logger.LogInformation("Controller init done."); @@ -76,15 +87,23 @@ public async Task CreateNewRunner(string arch, string size, string runne List srvKeys = sshKeys.Select(x => x.Id).ToList(); // Create new server - string runnerVersion = "2.315.0"; + string runnerVersion = "2.315.0"; + string provisionVersion = "v1"; + string cloudInitcontent = new StringBuilder() .AppendLine("#cloud-config") + .AppendLine("write_files:") + .AppendLine(" - path: /data/config.env") + .AppendLine(" content: |") + .AppendLine($" export GH_VERSION='{runnerVersion}'") + .AppendLine($" export ORG_NAME='{orgName}'") + .AppendLine($" export GH_TOKEN='{runnerToken}'") + .AppendLine($" export RUNNER_SIZE='{size}'") + .AppendLine($" export METRIC_USER='{_metricUser}'") + .AppendLine($" export METRIC_PASS='{_metricPassword}'") .AppendLine("runcmd:") - .AppendLine(" - [ sh, -xc, 'curl -fsSL https://get.docker.com -o /opt/install-docker.sh']") - .AppendLine(" - [ sh, -xc, 'sh /opt/install-docker.sh']") - .AppendLine(" - [ sh, -xc, 'mkdir -p /opt/actions-runner']") - .AppendLine($" - [ sh, -xc, 'cd /opt/actions-runner && curl -o actions-runner-linux.tar.gz -L https://github.com/actions/runner/releases/download/v{runnerVersion}/actions-runner-linux-{arch}-{runnerVersion}.tar.gz && tar xzf ./actions-runner-linux.tar.gz']") - .AppendLine($" - [ sh, -xc, 'cd /opt/actions-runner && RUNNER_ALLOW_RUNASROOT=true ./config.sh --url https://github.com/{orgName} --token {runnerToken} --ephemeral --disableupdate --labels {size},self-hosted-{size} && RUNNER_ALLOW_RUNASROOT=true ./run.sh ']") + .AppendLine($" - [ sh, -xc, 'curl -fsSL {_provisionBaseUrl}/provision.{arch}.{provisionVersion}.sh -o /data/provision.sh']") + .AppendLine($" - [ sh, -xc, 'bash /data/provision.sh']") .ToString(); _logger.LogInformation($"Launching VM {name}"); var newSrv = await _client.Server.Create(eDataCenter.nbg1, imageId.Value, name, srvType.Value, userData: cloudInitcontent, sshKeysIds: srvKeys); diff --git a/Program.cs b/Program.cs index 70b813b..2d2505d 100644 --- a/Program.cs +++ b/Program.cs @@ -78,7 +78,7 @@ public static void Main(string[] args) builder.Services.AddSingleton(svc => { var logger = svc.GetRequiredService>(); - return new CloudController(logger, Config.HetznerToken, persistPath,Config.Sizes); + return new CloudController(logger, Config.HetznerToken, persistPath,Config.Sizes, Config.ProvisionScriptBaseUrl, Config.MetricUser, Config.MetricPassword); }); var app = builder.Build(); diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index af6718b..c50ea9f 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -17,7 +17,8 @@ "applicationUrl": "http://localhost:5178", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", - "CONFIG_DIR": "/Users/markuskeil/dev/tmp" + "CONFIG_DIR": "/Users/markuskeil/dev/tmp", + "PERSIST_DIR": "/Users/markuskeil/dev/tmp" } }, "https": {