coder / modules

A collection of Terraform Modules to extend Coder templates.
https://registry.coder.com
Apache License 2.0
29 stars 26 forks source link

feat(Dotfiles): Default Repo #218

Closed djarbz closed 5 months ago

djarbz commented 5 months ago

I would like to (Optionally) provide a default repo in the template definition. This way I can provide a standard dotfiles repo that could be overwritten by the user upon workspace creation.

https://github.com/coder/modules/blob/838ec9587568f0d0349cbfff9aec8e9906d4eee3/dotfiles/main.tf#L21

matifali commented 5 months ago

We will happily accept contributions for this. I think it can be done quite easily if we expose default as a module variable.

djarbz commented 5 months ago

I am attempting to get this to work, but it looks like the default parameter in data "coder_parameter" "dotfiles_uri" is ignored.

This seems to be "working" but is a bit messy... https://github.com/djarbz/coder-modules/blob/74b433d9e3a09b8c9822b98e394393b8f2d64091/dotfiles/main.tf#L36

michaelbrewer commented 5 months ago

@djarbz terraform coalesce might make it cleaner. As the first value that is not empty would be used

ie:

DOTFILES_URI"="${coalesce(data.coder_parameter.dotfiles_uri.value, var.template_default_repo)}"
djarbz commented 5 months ago

Thanks, I was looking for that, but I'm not super familiar with Terraform. I still would much prefer using the default value in the coder_parameter if we can figure out why that is not working.

michaelbrewer commented 5 months ago

So this does not work?

terraform {
  required_version = ">= 1.0"

  required_providers {
    coder = {
      source  = "coder/coder"
      version = ">= 0.12"
    }
  }
}

variable "agent_id" {
  type        = string
  description = "The ID of a Coder agent."
}

variable "default_dotfiles_uri" {
  type        = string
  description = "The default dotfiles URI."
  default     = ""
}

data "coder_parameter" "dotfiles_uri" {
  type         = "string"
  name         = "dotfiles_uri"
  display_name = "Dotfiles URL (optional)"
  default      = var.default_dotfiles_uri
  description  = "Enter a URL for a [dotfiles repository](https://dotfiles.github.io) to personalize your workspace"
  mutable      = true
  icon         = "/icon/dotfiles.svg"
}

resource "coder_script" "personalize" {
  agent_id     = var.agent_id
  script       = <<-EOT
    DOTFILES_URI="${data.coder_parameter.dotfiles_uri.value}"
    if [ -n "$${DOTFILES_URI// }" ]; then
    coder dotfiles "$DOTFILES_URI" -y 2>&1 | tee -a ~/.dotfiles.log
    fi
    EOT
  display_name = "Dotfiles"
  icon         = "/icon/dotfiles.svg"
  run_on_start = true
}

output "dotfiles_uri" {
  description = "Dotfiles URI"
  value       = data.coder_parameter.dotfiles_uri.value
}
djarbz commented 5 months ago

Correct, I even hard coded the default parameter and it would not use it.

michaelbrewer commented 5 months ago

Seems to work

module "dotfiles" {
  source   = "./dotfiles/"
  agent_id = coder_agent.main.id
  default_dotfiles_uri = "https://github.com/michaelbrewer/dot-files"
}
Screenshot 2024-04-12 at 5 32 05 AM
michaelbrewer commented 5 months ago

How are you testing this with a template? Your would need to include the module into the template itself, or test via unit tests.

michaelbrewer commented 5 months ago

@djarbz - i was able to test this https://github.com/michaelbrewer/modules/commit/8181aa8235dd5bf277adf4bb2895c5a270517c6a

djarbz commented 5 months ago

I figured it out, I had misunderstood the default parameter. I did not realize that it was pre-filling in the field rather than inserting it on the backend after submit. I thought that the pre-fill was the "memory" that coder or my browser was using with a previous value. I have provided a PR, updating the variable name(better name) to your as well as your docs and test entry.

matifali commented 5 months ago

closed in #224