|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + openstack = { |
| 4 | + source = "terraform-provider-openstack/openstack" |
| 5 | + version = "~> 1.51.1" |
| 6 | + } |
| 7 | + coder = { |
| 8 | + source = "coder/coder" |
| 9 | + version = "~> 0.7.0" |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +variable "password" { |
| 15 | + sensitive = true |
| 16 | +} |
| 17 | + |
| 18 | +provider "coder" { |
| 19 | + feature_use_managed_variables = "true" |
| 20 | +} |
| 21 | + |
| 22 | +provider "openstack" { |
| 23 | + # Configuration is set via environment variables |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +data "coder_workspace" "me" { |
| 28 | +} |
| 29 | + |
| 30 | +resource "coder_agent" "main" { |
| 31 | + count = data.coder_workspace.me.start_count |
| 32 | + arch = "amd64" |
| 33 | + os = "linux" |
| 34 | + dir = "/home/ubuntu" |
| 35 | + |
| 36 | + startup_script = <<EOT |
| 37 | + #!/bin/bash |
| 38 | + # install code-server |
| 39 | + curl -fsSL https://code-server.dev/install.sh | sh |
| 40 | + code-server --auth none --port 13337 & |
| 41 | + # use coder CLI to clone and install dotfiles |
| 42 | + EOT |
| 43 | + |
| 44 | + |
| 45 | + metadata { |
| 46 | + display_name = "CPU Usage" |
| 47 | + key = "cpu" |
| 48 | + # calculates CPU usage by summing the "us", "sy" and "id" columns of |
| 49 | + # vmstat. |
| 50 | + script = <<EOT |
| 51 | +top -bn1 | awk 'FNR==3 {printf "%2.0f%%", $2+$3+$4}' |
| 52 | +EOT |
| 53 | + |
| 54 | + interval = 1 |
| 55 | + timeout = 1 |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + metadata { |
| 60 | + display_name = "Disk Usage" |
| 61 | + key = "disk" |
| 62 | + script = "df -h | awk '$6 ~ /^\\/$/ { print $5 }'" |
| 63 | + interval = 1 |
| 64 | + timeout = 1 |
| 65 | + } |
| 66 | + |
| 67 | + metadata { |
| 68 | + display_name = "Memory Usage" |
| 69 | + key = "mem" |
| 70 | + script = <<EOT |
| 71 | +free | awk '/^Mem/ { printf("%.0f%%", $4/$2 * 100.0) }' |
| 72 | +EOT |
| 73 | + interval = 1 |
| 74 | + timeout = 1 |
| 75 | + } |
| 76 | + |
| 77 | + metadata { |
| 78 | + display_name = "Load Average" |
| 79 | + key = "load" |
| 80 | + script = <<EOT |
| 81 | +awk '{print $1,$2,$3}' /proc/loadavg |
| 82 | +EOT |
| 83 | + interval = 1 |
| 84 | + timeout = 1 |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +resource "coder_app" "code-server" { |
| 90 | + count = data.coder_workspace.me.start_count |
| 91 | + agent_id = coder_agent.main[0].id |
| 92 | + name = "VS Code" |
| 93 | + slug = "code-server" |
| 94 | + url = "http://localhost:13337/?folder=/home/${lower(data.coder_workspace.me.owner)}" |
| 95 | + icon = "/icon/code.svg" |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +locals { |
| 100 | + |
| 101 | + # User data is used to stop/start AWS instances. See: |
| 102 | + # https://github.com/hashicorp/terraform-provider-aws/issues/22 |
| 103 | + |
| 104 | + user_data = <<EOT |
| 105 | +Content-Type: multipart/mixed; boundary="//" |
| 106 | +MIME-Version: 1.0 |
| 107 | +--// |
| 108 | +Content-Type: text/cloud-config; charset="us-ascii" |
| 109 | +MIME-Version: 1.0 |
| 110 | +Content-Transfer-Encoding: 7bit |
| 111 | +Content-Disposition: attachment; filename="cloud-config.txt" |
| 112 | +#cloud-config |
| 113 | +cloud_final_modules: |
| 114 | +- [scripts-user, always] |
| 115 | +hostname: ${lower(data.coder_workspace.me.name)} |
| 116 | +--// |
| 117 | +Content-Type: text/x-shellscript; charset="us-ascii" |
| 118 | +MIME-Version: 1.0 |
| 119 | +Content-Transfer-Encoding: 7bit |
| 120 | +Content-Disposition: attachment; filename="userdata.txt" |
| 121 | +#!/bin/bash |
| 122 | +
|
| 123 | +set -eux pipefail |
| 124 | +
|
| 125 | +apt-get update |
| 126 | +apt-get install -y jq |
| 127 | +
|
| 128 | +# Grabs token via the internal metadata server. This IP address is the same for all instances, no need to change it |
| 129 | +# https://docs.openstack.org/nova/rocky/user/metadata-service.html |
| 130 | +sudo CODER_AGENT_TOKEN=$(curl -s http://169.254.169.254/openstack/latest/meta_data.json | jq -r .meta.coder_agent_token) -u ubuntu sh -c '${try(coder_agent.main[0].init_script, "")}' |
| 131 | +--//-- |
| 132 | +EOT |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +resource "openstack_compute_instance_v2" "dev" { |
| 138 | + name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}" |
| 139 | + image_id = "bdf0bfbe-8ee3-40c1-9695-2f97ba31441f" |
| 140 | + flavor_name = "gen.micro" |
| 141 | + key_pair = "bens-macbook" |
| 142 | + |
| 143 | + block_device { |
| 144 | + uuid = "bdf0bfbe-8ee3-40c1-9695-2f97ba31441f" |
| 145 | + source_type = "image" |
| 146 | + volume_size = 20 |
| 147 | + destination_type = "local" |
| 148 | + boot_index = 0 |
| 149 | + delete_on_termination = true |
| 150 | + } |
| 151 | + |
| 152 | + metadata = { |
| 153 | + coder_agent_token = try(coder_agent.main[0].token, "") |
| 154 | + } |
| 155 | + |
| 156 | + security_groups = ["default"] |
| 157 | + power_state = data.coder_workspace.me.transition == "start" ? "active" : "shutoff" |
| 158 | + user_data = local.user_data |
| 159 | + |
| 160 | + tags = ["Name=coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}", "Coder_Provisioned=true"] |
| 161 | + |
| 162 | + lifecycle { |
| 163 | + ignore_changes = [user_data] |
| 164 | + } |
| 165 | + |
| 166 | + network { |
| 167 | + name = "External" |
| 168 | + } |
| 169 | +} |
0 commit comments