mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: Add templates to create working release (#422)
* Add templates
* Move API structs to codersdk
* Back to green tests!
* It all works, but now with tea! 🧋
* It works!
* Add cancellation to provisionerd
* Tests pass!
* Add deletion of workspaces and projects
* Fix agent lock
* Add clog
* Fix linting errors
* Remove unused CLI tests
* Rename daemon to start
* Fix leaking command
* Fix promptui test
* Update agent connection frequency
* Skip login tests on Windows
* Increase tunnel connect timeout
* Fix templater
* Lower test requirements
* Fix embed
* Disable promptui tests for Windows
* Fix write newline
* Fix PTY write newline
* Fix CloseReader
* Fix compilation on Windows
* Fix linting error
* Remove bubbletea
* Cleanup readwriter
* Use embedded templates instead of serving over API
* Move templates to examples
* Improve workspace create flow
* Fix Windows build
* Fix tests
* Fix linting errors
* Fix untar with extracting max size
* Fix newline char
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
package examples
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"embed"
|
||||
"path"
|
||||
"sync"
|
||||
|
||||
"github.com/gohugoio/hugo/parser/pageparser"
|
||||
"golang.org/x/sync/singleflight"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed */*.md
|
||||
//go:embed */*.tf
|
||||
files embed.FS
|
||||
|
||||
examples = make([]Example, 0)
|
||||
parseExamples sync.Once
|
||||
archives = singleflight.Group{}
|
||||
)
|
||||
|
||||
type Example struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Markdown string `json:"markdown"`
|
||||
}
|
||||
|
||||
// List returns all embedded examples.
|
||||
func List() ([]Example, error) {
|
||||
var returnError error
|
||||
parseExamples.Do(func() {
|
||||
dirs, err := files.ReadDir(".")
|
||||
if err != nil {
|
||||
returnError = xerrors.Errorf("read dir: %w", err)
|
||||
return
|
||||
}
|
||||
for _, dir := range dirs {
|
||||
exampleID := dir.Name()
|
||||
// Each one of these is a example!
|
||||
readme, err := files.ReadFile(path.Join(dir.Name(), "README.md"))
|
||||
if err != nil {
|
||||
returnError = xerrors.Errorf("example %q does not contain README.md", exampleID)
|
||||
return
|
||||
}
|
||||
frontMatter, err := pageparser.ParseFrontMatterAndContent(bytes.NewReader(readme))
|
||||
if err != nil {
|
||||
returnError = xerrors.Errorf("parse example %q front matter: %w", exampleID, err)
|
||||
return
|
||||
}
|
||||
nameRaw, exists := frontMatter.FrontMatter["name"]
|
||||
if !exists {
|
||||
returnError = xerrors.Errorf("example %q front matter does not contain name", exampleID)
|
||||
return
|
||||
}
|
||||
name, valid := nameRaw.(string)
|
||||
if !valid {
|
||||
returnError = xerrors.Errorf("example %q name isn't a string", exampleID)
|
||||
return
|
||||
}
|
||||
descriptionRaw, exists := frontMatter.FrontMatter["description"]
|
||||
if !exists {
|
||||
returnError = xerrors.Errorf("example %q front matter does not contain name", exampleID)
|
||||
return
|
||||
}
|
||||
description, valid := descriptionRaw.(string)
|
||||
if !valid {
|
||||
returnError = xerrors.Errorf("example %q description isn't a string", exampleID)
|
||||
return
|
||||
}
|
||||
examples = append(examples, Example{
|
||||
ID: exampleID,
|
||||
Name: name,
|
||||
Description: description,
|
||||
Markdown: string(frontMatter.Content),
|
||||
})
|
||||
}
|
||||
})
|
||||
return examples, returnError
|
||||
}
|
||||
|
||||
// Archive returns a tar by example ID.
|
||||
func Archive(exampleID string) ([]byte, error) {
|
||||
rawData, err, _ := archives.Do(exampleID, func() (interface{}, error) {
|
||||
examples, err := List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var selected Example
|
||||
for _, example := range examples {
|
||||
if example.ID != exampleID {
|
||||
continue
|
||||
}
|
||||
selected = example
|
||||
break
|
||||
}
|
||||
if selected.ID == "" {
|
||||
return nil, xerrors.Errorf("example with id %q not found", exampleID)
|
||||
}
|
||||
|
||||
entries, err := files.ReadDir(exampleID)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("read dir: %w", err)
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
tarWriter := tar.NewWriter(&buffer)
|
||||
|
||||
for _, entry := range entries {
|
||||
file, err := files.Open(path.Join(exampleID, entry.Name()))
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("open file: %w", err)
|
||||
}
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("stat file: %w", err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
continue
|
||||
}
|
||||
data := make([]byte, info.Size())
|
||||
_, err = file.Read(data)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("read data: %w", err)
|
||||
}
|
||||
header, err := tar.FileInfoHeader(info, entry.Name())
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("get file header: %w", err)
|
||||
}
|
||||
header.Mode = 0644
|
||||
err = tarWriter.WriteHeader(header)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("write file: %w", err)
|
||||
}
|
||||
_, err = tarWriter.Write(data)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("write: %w", err)
|
||||
}
|
||||
}
|
||||
err = tarWriter.Flush()
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("flush archive: %w", err)
|
||||
}
|
||||
return buffer.Bytes(), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, valid := rawData.([]byte)
|
||||
if !valid {
|
||||
panic("dev error: data must be a byte slice")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package examples_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/examples"
|
||||
)
|
||||
|
||||
func TestTemplate(t *testing.T) {
|
||||
t.Parallel()
|
||||
list, err := examples.List()
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, len(list), 0)
|
||||
|
||||
_, err = examples.Archive(list[0].ID)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
name: Develop in Linux on Google Cloud
|
||||
description: Get started with Linux development on Google Cloud.
|
||||
tags: [cloud, google]
|
||||
---
|
||||
@@ -0,0 +1,93 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
coder = {
|
||||
source = "coder/coder"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variable "service_account" {
|
||||
description = <<EOT
|
||||
Coder requires a Google Cloud Service Account to provision workspaces.
|
||||
|
||||
1. Create a service account:
|
||||
https://console.cloud.google.com/projectselector/iam-admin/serviceaccounts/create
|
||||
2. Add the roles:
|
||||
- Compute Admin
|
||||
- Service Account User
|
||||
3. Click on the created key, and navigate to the "Keys" tab.
|
||||
4. Click "Add key", then "Create new key".
|
||||
5. Generate a JSON private key, and paste the contents in \'\' quotes below.
|
||||
EOT
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "zone" {
|
||||
description = "What region should your workspace live in?"
|
||||
default = "us-central1-a"
|
||||
validation {
|
||||
condition = contains(["northamerica-northeast1-a", "us-central1-a", "us-west2-c", "europe-west4-b", "southamerica-east1-a"], var.zone)
|
||||
error_message = "Invalid zone!"
|
||||
}
|
||||
}
|
||||
|
||||
provider "google" {
|
||||
zone = var.zone
|
||||
credentials = var.service_account
|
||||
project = jsondecode(var.service_account).project_id
|
||||
}
|
||||
|
||||
data "coder_workspace" "me" {
|
||||
}
|
||||
|
||||
data "coder_agent_script" "dev" {
|
||||
arch = "amd64"
|
||||
os = "linux"
|
||||
}
|
||||
|
||||
data "google_compute_default_service_account" "default" {
|
||||
}
|
||||
|
||||
resource "random_string" "random" {
|
||||
length = 8
|
||||
special = false
|
||||
}
|
||||
|
||||
resource "google_compute_disk" "root" {
|
||||
name = "coder-${lower(random_string.random.result)}"
|
||||
type = "pd-ssd"
|
||||
zone = var.zone
|
||||
image = "debian-cloud/debian-9"
|
||||
lifecycle {
|
||||
ignore_changes = [image]
|
||||
}
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "dev" {
|
||||
zone = var.zone
|
||||
count = data.coder_workspace.me.transition == "start" ? 1 : 0
|
||||
name = "coder-${lower(random_string.random.result)}"
|
||||
machine_type = "e2-medium"
|
||||
network_interface {
|
||||
network = "default"
|
||||
access_config {
|
||||
// Ephemeral public IP
|
||||
}
|
||||
}
|
||||
boot_disk {
|
||||
source = google_compute_disk.root.name
|
||||
}
|
||||
service_account {
|
||||
email = data.google_compute_default_service_account.default.email
|
||||
scopes = ["cloud-platform"]
|
||||
}
|
||||
metadata_startup_script = data.coder_agent_script.dev.value
|
||||
}
|
||||
|
||||
resource "coder_agent" "dev" {
|
||||
count = length(google_compute_instance.dev)
|
||||
auth {
|
||||
type = "google-instance-identity"
|
||||
instance_id = google_compute_instance.dev[0].instance_id
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
name: Develop in Windows on Google Cloud
|
||||
description: Get started with Windows development on Google Cloud.
|
||||
tags: [cloud, google]
|
||||
---
|
||||
@@ -0,0 +1,76 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
coder = {
|
||||
source = "coder/coder"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variable "gcp_credentials" {
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "gcp_project" {
|
||||
description = "The Google Cloud project to manage resources in."
|
||||
}
|
||||
|
||||
variable "gcp_region" {
|
||||
default = "us-central1"
|
||||
}
|
||||
|
||||
provider "google" {
|
||||
project = var.gcp_project
|
||||
region = var.gcp_region
|
||||
credentials = var.gcp_credentials
|
||||
}
|
||||
|
||||
data "coder_workspace" "me" {
|
||||
}
|
||||
|
||||
data "coder_agent_script" "dev" {
|
||||
arch = "amd64"
|
||||
os = "windows"
|
||||
}
|
||||
|
||||
data "google_compute_default_service_account" "default" {
|
||||
}
|
||||
|
||||
resource "random_string" "random" {
|
||||
count = data.coder_workspace.me.transition == "start" ? 1 : 0
|
||||
length = 8
|
||||
special = false
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "dev" {
|
||||
zone = "us-central1-a"
|
||||
count = data.coder_workspace.me.transition == "start" ? 1 : 0
|
||||
name = "coder-${lower(random_string.random[0].result)}"
|
||||
machine_type = "e2-medium"
|
||||
network_interface {
|
||||
network = "default"
|
||||
access_config {
|
||||
// Ephemeral public IP
|
||||
}
|
||||
}
|
||||
boot_disk {
|
||||
initialize_params {
|
||||
image = "projects/windows-cloud/global/images/windows-server-2022-dc-core-v20220215"
|
||||
}
|
||||
}
|
||||
service_account {
|
||||
email = data.google_compute_default_service_account.default.email
|
||||
scopes = ["cloud-platform"]
|
||||
}
|
||||
metadata = {
|
||||
windows-startup-script-ps1 = data.coder_agent_script.dev.value
|
||||
serial-port-enable = "TRUE"
|
||||
}
|
||||
}
|
||||
|
||||
resource "coder_agent" "dev" {
|
||||
count = length(google_compute_instance.dev)
|
||||
auth {
|
||||
type = "google-instance-identity"
|
||||
instance_id = google_compute_instance.dev[0].instance_id
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
# The simplest possible Terraform module: it just outputs "Hello, World!"
|
||||
output "hello_world" {
|
||||
value = "Hello, World!"
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
# For interesting types of variables, check out the terraform docs:
|
||||
# https://www.terraform.io/language/values/variables#declaring-an-input-variable
|
||||
variable "message" {
|
||||
type = string
|
||||
}
|
||||
|
||||
# And refer to the docs on using expressions to make use of variables:
|
||||
# https://www.terraform.io/language/expressions/strings#interpolation
|
||||
output "hello_provisioner" {
|
||||
value = "Hello, provisioner: ${var.message}"
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
# For interesting types of variables, check out the terraform docs:
|
||||
# https://www.terraform.io/language/values/variables#declaring-an-input-variable
|
||||
variable "message" {
|
||||
type = string
|
||||
}
|
||||
|
||||
# We can use a "null_resource" to test resources without a cloud provider:
|
||||
# https://www.terraform.io/language/resources/provisioners/null_resource
|
||||
resource "null_resource" "minimal_resource" {
|
||||
|
||||
# Note that Terraform's `provisioner` concept is generally an anti-pattern -
|
||||
# more info here: https://www.terraform.io/language/resources/provisioners/syntax
|
||||
# But it's helpful here for testing a resource.
|
||||
provisioner "local-exec" {
|
||||
command = "echo ${var.message}"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user