forked from philips-labs/terraform-aws-github-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add config for windows ami (philips-labs#1525)
* Add config for windows ami * Update packer build to validate windows
- Loading branch information
Showing
12 changed files
with
208 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
provider "aws" { | ||
region = local.aws_region | ||
region = var.aws_region | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash -e | ||
|
||
user_name=ec2-user | ||
|
||
## This wrapper file re-uses scripts in the /modules/runners/templates directory | ||
## of this repo. These are the same that are used by the user_data functionality | ||
## to bootstrap the instance if it is started from an existing AMI. | ||
${install_runner} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Start-Transcript -Path "C:\runner-startup.log" -Append | ||
${start_runner} | ||
Stop-Transcript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<powershell> | ||
|
||
Write-Output "Running User Data Script" | ||
Write-Host "(host) Running User Data Script" | ||
|
||
Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force -ErrorAction Ignore | ||
|
||
# Don't set this before Set-ExecutionPolicy as it throws an error | ||
$ErrorActionPreference = "stop" | ||
|
||
# Remove HTTP listener | ||
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse | ||
|
||
# Create a self-signed certificate to let ssl work | ||
$Cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName "packer" | ||
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $Cert.Thumbprint -Force | ||
|
||
# WinRM | ||
Write-Output "Setting up WinRM" | ||
Write-Host "(host) setting up WinRM" | ||
|
||
# I'm not really sure why we need the cmd.exe wrapper, but it works with it and doesn't work without it | ||
cmd.exe /c winrm quickconfig -q | ||
cmd.exe /c winrm set "winrm/config" '@{MaxTimeoutms="1800000"}' | ||
cmd.exe /c winrm set "winrm/config/winrs" '@{MaxMemoryPerShellMB="1024"}' | ||
cmd.exe /c winrm set "winrm/config/service" '@{AllowUnencrypted="true"}' | ||
cmd.exe /c winrm set "winrm/config/client" '@{AllowUnencrypted="true"}' | ||
cmd.exe /c winrm set "winrm/config/service/auth" '@{Basic="true"}' | ||
cmd.exe /c winrm set "winrm/config/client/auth" '@{Basic="true"}' | ||
cmd.exe /c winrm set "winrm/config/service/auth" '@{CredSSP="true"}' | ||
cmd.exe /c winrm set "winrm/config/listener?Address=*+Transport=HTTPS" "@{Port=`"5986`";Hostname=`"packer`";CertificateThumbprint=`"$($Cert.Thumbprint)`"}" | ||
cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes | ||
cmd.exe /c netsh firewall add portopening TCP 5986 "Port 5986" | ||
cmd.exe /c net stop winrm | ||
cmd.exe /c sc config winrm start= auto | ||
cmd.exe /c net start winrm | ||
|
||
</powershell> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
packer { | ||
required_plugins { | ||
amazon = { | ||
version = ">= 0.0.2" | ||
source = "github.com/hashicorp/amazon" | ||
} | ||
} | ||
} | ||
|
||
variable "action_runner_url" { | ||
description = "The URL to the tarball of the action runner" | ||
type = string | ||
default = "https://github.com/actions/runner/releases/download/v2.285.1/actions-runner-win-x64-2.285.1.zip" | ||
} | ||
|
||
variable "region" { | ||
description = "The region to build the image in" | ||
type = string | ||
default = "eu-west-1" | ||
} | ||
|
||
source "amazon-ebs" "githubrunner" { | ||
ami_name = "github-runner-windows-core-2019-${formatdate("YYYYMMDDhhmm", timestamp())}" | ||
communicator = "winrm" | ||
instance_type = "t3a.medium" | ||
region = var.region | ||
source_ami_filter { | ||
filters = { | ||
name = "Windows_Server-2019-English-Core-ContainersLatest-*" | ||
root-device-type = "ebs" | ||
virtualization-type = "hvm" | ||
} | ||
most_recent = true | ||
owners = ["amazon"] | ||
} | ||
tags = { | ||
OS_Version = "windows-core-2019" | ||
Release = "Latest" | ||
Base_AMI_Name = "{{ .SourceAMIName }}" | ||
} | ||
user_data_file = "./bootstrap_win.ps1" | ||
winrm_insecure = true | ||
winrm_port = 5986 | ||
winrm_use_ssl = true | ||
winrm_username = "Administrator" | ||
} | ||
|
||
build { | ||
name = "githubactions-runner" | ||
sources = [ | ||
"source.amazon-ebs.githubrunner" | ||
] | ||
|
||
provisioner "file" { | ||
content = templatefile("../start-runner.ps1", { | ||
start_runner = templatefile("../../modules/runners/templates/start-runner.ps1", {}) | ||
}) | ||
destination = "C:\\start-runner.ps1" | ||
} | ||
|
||
provisioner "powershell" { | ||
inline = [templatefile("./windows-provisioner.ps1", { | ||
action_runner_url = var.action_runner_url | ||
})] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
$ErrorActionPreference = "Continue" | ||
$VerbosePreference = "Continue" | ||
|
||
# Install Chocolatey | ||
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 | ||
$env:chocolateyUseWindowsCompression = 'true' | ||
Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression | ||
|
||
# Add Chocolatey to powershell profile | ||
$ChocoProfileValue = @' | ||
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" | ||
if (Test-Path($ChocolateyProfile)) { | ||
Import-Module "$ChocolateyProfile" | ||
} | ||
refreshenv | ||
'@ | ||
# Write it to the $profile location | ||
Set-Content -Path "$PsHome\Microsoft.PowerShell_profile.ps1" -Value $ChocoProfileValue -Force | ||
# Source it | ||
. "$PsHome\Microsoft.PowerShell_profile.ps1" | ||
|
||
refreshenv | ||
|
||
Write-Host "Installing cloudwatch agent..." | ||
Invoke-WebRequest -Uri https://s3.amazonaws.com/amazoncloudwatch-agent/windows/amd64/latest/amazon-cloudwatch-agent.msi -OutFile C:\amazon-cloudwatch-agent.msi | ||
$cloudwatchParams = '/i', 'C:\amazon-cloudwatch-agent.msi', '/qn', '/L*v', 'C:\CloudwatchInstall.log' | ||
Start-Process "msiexec.exe" $cloudwatchParams -Wait -NoNewWindow | ||
Remove-Item C:\amazon-cloudwatch-agent.msi | ||
|
||
# Install dependent tools | ||
Write-Host "Installing additional development tools" | ||
choco install git awscli -y | ||
refreshenv | ||
|
||
Write-Host "Creating actions-runner directory for the GH Action installtion" | ||
New-Item -ItemType Directory -Path C:\actions-runner ; Set-Location C:\actions-runner | ||
|
||
Write-Host "Downloading the GH Action runner from ${action_runner_url}" | ||
Invoke-WebRequest -Uri ${action_runner_url} -OutFile actions-runner.zip | ||
|
||
Write-Host "Un-zip action runner" | ||
Expand-Archive -Path actions-runner.zip -DestinationPath . | ||
|
||
Write-Host "Delete zip file" | ||
Remove-Item actions-runner.zip | ||
|
||
$action = New-ScheduledTaskAction -WorkingDirectory "C:\actions-runner" -Execute "PowerShell.exe" -Argument "-File C:\start-runner.ps1" | ||
$trigger = New-ScheduledTaskTrigger -AtStartup | ||
Register-ScheduledTask -TaskName "runnerinit" -Action $action -Trigger $trigger -User System -RunLevel Highest -Force | ||
|
||
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule |