-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
220 lines (189 loc) · 5.78 KB
/
main.tf
File metadata and controls
220 lines (189 loc) · 5.78 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
terraform {
required_providers {
coder = {
source = "coder/coder"
}
kubernetes = {
source = "hashicorp/kubernetes"
}
}
}
provider "coder" {
# Configuration options
}
provider "kubernetes" {
# Configuration for your k8s cluster
# Assuming cluster config is available in the standard location
config_path = "~/.kube/config"
}
data "coder_workspace" "me" {}
variable "use_kubeconfig" {
type = bool
description = "Use host kubeconfig? (true/false)"
default = true
}
variable "namespace" {
type = string
description = "The namespace to create workspaces in (must exist prior to creating workspaces)"
default = "coder-workspaces"
}
variable "cpu" {
type = string
description = "CPU cores to allocate to workspace"
default = "2"
}
variable "memory" {
type = string
description = "Memory to allocate to workspace"
default = "4Gi"
}
module "coder-login" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/coder-login/coder"
version = "1.0.15"
agent_id = coder_agent.example.id
}
variable "anthropic_api_key" {
type = string
description = "The Anthropic API key"
sensitive = true
}
data "coder_parameter" "ai_prompt" {
type = "string"
name = "AI Prompt"
default = ""
description = "Write a prompt for Goose"
mutable = true
}
data "coder_parameter" "container_image" {
type = "string"
name = "Environment Image"
default = "ghcr.io/connellrobert/background-coder:latest"
mutable = true
icon = "/icon/docker.svg"
}
# Set the prompt and system prompt for Goose via environment variables
resource "coder_agent" "main" {
# ...
env = {
GOOSE_SYSTEM_PROMPT = <<-EOT
You are a helpful assistant that can help write code.
Run all long running tasks (e.g. npm run dev) in the background and not in the foreground.
Periodically check in on background tasks.
Notify Coder of the status of the task before and after your steps.
EOT
GOOSE_TASK_PROMPT = data.coder_parameter.ai_prompt.value
# An API key is required for experiment_auto_configure
# See https://block.github.io/goose/docs/getting-started/providers
ANTHROPIC_API_KEY = var.anthropic_api_key # or use a coder_parameter
}
}
module "goose" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/goose/coder"
version = "1.2.0"
agent_id = coder_agent.example.id
folder = "/home/coder"
install_goose = true
goose_version = "v1.0.16"
# Enable experimental features
experiment_report_tasks = true
# Run Goose in the background with screen (pick one: screen or tmux)
experiment_use_screen = true
# experiment_use_tmux = true # Alternative: use tmux instead of screen
# Optional: customize the session name (defaults to "goose")
# session_name = "goose-session"
# Avoid configuring Goose manually
experiment_auto_configure = true
# Required for experiment_auto_configure
experiment_goose_provider = "anthropic"
experiment_goose_model = "claude-3-5-sonnet-latest"
}
resource "kubernetes_pod" "main" {
count = data.coder_workspace.me.start_count
metadata {
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
namespace = var.namespace
labels = {
"app.kubernetes.io/name" = "coder-workspace"
"app.kubernetes.io/instance" = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
"app.kubernetes.io/part-of" = "coder"
"com.coder.resource" = "true"
"com.coder.workspace.id" = data.coder_workspace.me.id
"com.coder.workspace.name" = data.coder_workspace.me.name
"com.coder.workspace.owner" = data.coder_workspace.me.owner
}
}
spec {
container {
name = "dev"
image = data.coder_parameter.container_image.value
command = ["sh", "-c", coder_agent.main.init_script]
env {
name = "CODER_AGENT_TOKEN"
value = coder_agent.main.token
}
resources {
requests = {
cpu = var.cpu
memory = var.memory
}
limits = {
cpu = var.cpu
memory = var.memory
}
}
}
}
}
resource "coder_agent" "main" {
os = "linux"
arch = "amd64"
# The following metadata blocks are optional. They are used to display
# information about your workspace in the dashboard. You can remove them
# if you don't want to display any information.
metadata {
display_name = "CPU Usage"
key = "0_cpu"
script = "top -b -n 1 | awk 'NR>7 { sum += $9; } END { print sum; }'"
interval = 10
timeout = 1
}
metadata {
display_name = "Memory Usage"
key = "1_mem"
script = "free | awk '/^Mem/ { printf(\"%.0f\", ($3/$2) * 100) }'"
interval = 10
timeout = 1
}
metadata {
display_name = "Home Disk"
key = "2_disk"
script = "df /home | awk 'NR==2 { printf(\"%.0f\", ($3/$2) * 100) }'"
interval = 60
timeout = 1
}
startup_script = <<-EOT
#!/bin/sh
# Install and start code-server
curl -fsSL https://code-server.dev/install.sh | sh
code-server --auth none --port 13337 &
# Install common development tools
sudo apt-get update
sudo apt-get install -y git curl wget build-essential
EOT
}
resource "coder_app" "code-server" {
agent_id = coder_agent.main.id
slug = "code-server"
display_name = "code-server"
url = "http://localhost:13337/?folder=/home/coder"
icon = "/icon/code.svg"
subdomain = false
share = "owner"
healthcheck {
url = "http://localhost:13337/healthz"
interval = 3
threshold = 10
}
}