Skip to content

Commit c0690f0

Browse files
committed
WIP
1 parent 05310f6 commit c0690f0

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Sleep for a while in case the underlying provider deletes the resource on error.
2+
trap {
3+
Write-Error "=== Agent script exited with non-zero code. Sleeping 24h to preserve logs..."
4+
Start-Sleep -Seconds 86400
5+
}
6+
7+
# Attempt to download the coder agent.
8+
# This could fail for a number of reasons, many of which are likely transient.
9+
# So just keep trying!
10+
while ($true) {
11+
try {
12+
$ProgressPreference = "SilentlyContinue"
13+
14+
# executing shell to be named "sshd", otherwise it fails. See:
15+
# https://github.com/microsoft/vscode-remote-release/issues/5699
16+
$BINARY_URL="${ACCESS_URL}/bin/coder-windows-${ARCH}.exe"
17+
Write-Output "$(Get-Date) Fetching coder agent from ${BINARY_URL}"
18+
Invoke-WebRequest -Uri "${BINARY_URL}" -OutFile $env:TEMP\sshd.exe
19+
break
20+
} catch {
21+
Write-Output "$(Get-Date) error: unhandled exception fetching coder agent:"
22+
Write-Output $_
23+
Write-Output "$(Get-Date) trying again in 30 seconds..."
24+
Start-Sleep -Seconds 30
25+
}
26+
}
27+
28+
# Check if running in a Windows container
29+
if (-not (Get-Command 'Set-MpPreference' -ErrorAction SilentlyContinue)) {
30+
Write-Output "$(Get-Date) Set-MpPreference not available, skipping..."
31+
} else {
32+
Set-MpPreference -DisableRealtimeMonitoring $true -ExclusionPath $env:TEMP\sshd.exe
33+
}
34+
35+
$en
36+
$env:CODER_AGENT_URL = "${ACCESS_URL}"
37+
38+
# Check if we're running inside a Windows container!
39+
$inContainer = $false
40+
if ((Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control' -Name 'ContainerType' -ErrorAction SilentlyContinue) -ne $null) {
41+
$inContainer = $true
42+
}
43+
if ($inContainer) {
44+
# If we're in a container, run in a the foreground!
45+
Start-Process -FilePath $env:TEMP\sshd.exe -ArgumentList "agent" -Wait -NoNewWindow
46+
} else {
47+
Start-Process -FilePath $env:TEMP\sshd.exe -ArgumentList "agent" -PassThru
48+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$task = @{
2+
TaskName = 'CoderAgent'
3+
Action = (New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-sta -ExecutionPolicy Unrestricted -Command "$env:CODER_AGENT_TOKEN_FILE=\'C:\OEM\token.txt\'; & C:\OEM\CoderAgent.ps1 *>> C:\OEM\CoderAgent.log"')
4+
Trigger = (New-ScheduledTaskTrigger -AtStartup), (New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds(15))
5+
Settings = (New-ScheduledTaskSettingsSet -DontStopOnIdleEnd -ExecutionTimeLimit ([TimeSpan]::FromDays(3650)) -Compatibility Win8)
6+
Principal = (New-ScheduledTaskPrincipal -UserId 'vm\coder' -RunLevel Highest -LogonType S4U)
7+
}
8+
Register-ScheduledTask @task -Force

windows-in-docker/files/install.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@echo off
2+
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "C:\OEM\create_task.ps1"

windows-in-docker/main.tf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
}
6+
docker = {
7+
source = "kreuzwerker/docker"
8+
version = "~> 3.0.0"
9+
}
10+
}
11+
}
12+
13+
provider "docker" {}
14+
15+
provider "coder" {}
16+
17+
data "coder_workspace" "me" {}
18+
19+
resource "coder_agent" "dev" {
20+
arch = "amd64"
21+
os = "windows"
22+
connection_timeout = 600
23+
}
24+
25+
resource "local_file" "coder_agent_token" {
26+
content = coder_agent.dev.token
27+
filename = "${path.module}/files/token"
28+
}
29+
30+
data "docker_registry_image" "dockurr" {
31+
name = "dockurr/windows"
32+
}
33+
34+
resource "docker_image" "dockurr" {
35+
name = "${data.docker_registry_image.dockurr.name}@${data.docker_registry_image.dockurr.sha256_digest}"
36+
pull_triggers = [
37+
data.docker_registry_image.dockurr.sha256_digest,
38+
]
39+
keep_locally = true
40+
}
41+
42+
resource "docker_container" "dockurr" {
43+
count = data.coder_workspace.me.start_count
44+
image = docker_image.dockurr.name
45+
name = "coder-${lower(data.coder_workspace.me.owner)}-${lower(data.coder_workspace.me.name)}"
46+
hostname = data.coder_workspace.me.name
47+
env = [
48+
"RAM_SIZE=16G",
49+
"CPU_CORES=4",
50+
]
51+
destroy_grace_seconds = 120
52+
stop_timeout = 120
53+
stop_signal = "SIGINT"
54+
host {
55+
host = "host.docker.internal"
56+
ip = "host-gateway"
57+
}
58+
volumes {
59+
container_path = "/storage"
60+
host_path = "/home/ubuntu/dockurr"
61+
read_only = false
62+
}
63+
volumes {
64+
container_path = "/storage/oem"
65+
host_path = "${abspath(path.module)}/files"
66+
read_only = true
67+
}
68+
69+
devices {
70+
host_path = "/dev/kvm"
71+
}
72+
capabilities {
73+
add = ["NET_ADMIN"]
74+
}
75+
}

0 commit comments

Comments
 (0)