Skip to content

Commit

Permalink
swich cloud-init to s3 scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed Apr 16, 2024
1 parent 5a98d84 commit d88e195
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
3 changes: 3 additions & 0 deletions AutoScalerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ public class AutoScalerConfiguration
public List<MachineSize> Sizes { get; set; }

public string HetznerToken { get; set; }
public string ProvisionScriptBaseUrl { get; set; }
public string MetricPassword { get; set; }
public string MetricUser { get; set; }
}
33 changes: 26 additions & 7 deletions CloudController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ public class CloudController
.CreateGauge("github_machines_active", "Number of active machines", labelNames: ["org","size"]);

private readonly List<MachineSize> _configSizes;
private readonly string _provisionBaseUrl;
private readonly string _metricUser;
private readonly string _metricPassword;

public CloudController(ILogger<CloudController> logger,
string hetznerCloudToken,
string persistPath,
List<MachineSize> configSizes)
List<MachineSize> 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.");
Expand Down Expand Up @@ -76,15 +87,23 @@ public async Task<string> CreateNewRunner(string arch, string size, string runne
List<long> 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);
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void Main(string[] args)
builder.Services.AddSingleton<CloudController>(svc =>
{
var logger = svc.GetRequiredService<ILogger<CloudController>>();
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();
Expand Down
3 changes: 2 additions & 1 deletion Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit d88e195

Please sign in to comment.