feat(dotfiles): add ability to apply dotfiles as any user (#133)
Co-authored-by: Mathias Fredriksson <mafredri@gmail.com> Co-authored-by: Muhammad Atif Ali <atif@coder.com>
This commit is contained in:
@@ -9,7 +9,11 @@ tags: [helper]
|
|||||||
|
|
||||||
# Dotfiles
|
# Dotfiles
|
||||||
|
|
||||||
Allow developers to optionally bring their own [dotfiles repository](https://dotfiles.github.io)! Under the hood, this module uses the [coder dotfiles](https://coder.com/docs/v2/latest/dotfiles) command.
|
Allow developers to optionally bring their own [dotfiles repository](https://dotfiles.github.io).
|
||||||
|
|
||||||
|
This will prompt the user for their dotfiles repository URL on template creation using a `coder_parameter`.
|
||||||
|
|
||||||
|
Under the hood, this module uses the [coder dotfiles](https://coder.com/docs/v2/latest/dotfiles) command.
|
||||||
|
|
||||||
```tf
|
```tf
|
||||||
module "dotfiles" {
|
module "dotfiles" {
|
||||||
@@ -19,6 +23,47 @@ module "dotfiles" {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Apply dotfiles as the current user
|
||||||
|
|
||||||
|
```tf
|
||||||
|
module "dotfiles" {
|
||||||
|
source = "registry.coder.com/modules/dotfiles/coder"
|
||||||
|
version = "1.0.0"
|
||||||
|
agent_id = coder_agent.example.id
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Apply dotfiles as another user (only works if sudo is passwordless)
|
||||||
|
|
||||||
|
```tf
|
||||||
|
module "dotfiles" {
|
||||||
|
source = "registry.coder.com/modules/dotfiles/coder"
|
||||||
|
version = "1.0.0"
|
||||||
|
agent_id = coder_agent.example.id
|
||||||
|
user = "root"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Apply the same dotfiles as the current user and root (the root dotfiles can only be applied if sudo is passwordless)
|
||||||
|
|
||||||
|
```tf
|
||||||
|
module "dotfiles" {
|
||||||
|
source = "registry.coder.com/modules/dotfiles/coder"
|
||||||
|
version = "1.0.0"
|
||||||
|
agent_id = coder_agent.example.id
|
||||||
|
}
|
||||||
|
|
||||||
|
module "dotfiles-root" {
|
||||||
|
source = "registry.coder.com/modules/dotfiles/coder"
|
||||||
|
version = "1.0.0"
|
||||||
|
agent_id = coder_agent.example.id
|
||||||
|
user = "root"
|
||||||
|
dotfiles_uri = module.dotfiles.dotfiles_uri
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Setting a default dotfiles repository
|
## Setting a default dotfiles repository
|
||||||
|
|
||||||
You can set a default dotfiles repository for all users by setting the `default_dotfiles_uri` variable:
|
You can set a default dotfiles repository for all users by setting the `default_dotfiles_uri` variable:
|
||||||
|
|||||||
@@ -16,10 +16,23 @@ variable "agent_id" {
|
|||||||
|
|
||||||
variable "default_dotfiles_uri" {
|
variable "default_dotfiles_uri" {
|
||||||
type = string
|
type = string
|
||||||
description = "The default dotfiles URI if the workspace user does not provide one."
|
description = "The default dotfiles URI if the workspace user does not provide one"
|
||||||
default = ""
|
default = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "dotfiles_uri" {
|
||||||
|
type = string
|
||||||
|
description = "The URL to a dotfiles repository. (optional, when set, the user isn't prompted for their dotfiles)"
|
||||||
|
|
||||||
|
default = null
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "user" {
|
||||||
|
type = string
|
||||||
|
description = "The name of the user to apply the dotfiles to. (optional, applies to the current user by default)"
|
||||||
|
default = null
|
||||||
|
}
|
||||||
|
|
||||||
variable "coder_parameter_order" {
|
variable "coder_parameter_order" {
|
||||||
type = number
|
type = number
|
||||||
description = "The order determines the position of a template parameter in the UI/CLI presentation. The lowest order is shown first and parameters with equal order are sorted by name (ascending order)."
|
description = "The order determines the position of a template parameter in the UI/CLI presentation. The lowest order is shown first and parameters with equal order are sorted by name (ascending order)."
|
||||||
@@ -27,6 +40,8 @@ variable "coder_parameter_order" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
data "coder_parameter" "dotfiles_uri" {
|
data "coder_parameter" "dotfiles_uri" {
|
||||||
|
count = var.dotfiles_uri == null ? 1 : 0
|
||||||
|
|
||||||
type = "string"
|
type = "string"
|
||||||
name = "dotfiles_uri"
|
name = "dotfiles_uri"
|
||||||
display_name = "Dotfiles URL (optional)"
|
display_name = "Dotfiles URL (optional)"
|
||||||
@@ -37,14 +52,17 @@ data "coder_parameter" "dotfiles_uri" {
|
|||||||
icon = "/icon/dotfiles.svg"
|
icon = "/icon/dotfiles.svg"
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "coder_script" "personalize" {
|
locals {
|
||||||
agent_id = var.agent_id
|
dotfiles_uri = var.dotfiles_uri != null ? var.dotfiles_uri : data.coder_parameter.dotfiles_uri[0].value
|
||||||
script = <<-EOT
|
user = var.user != null ? var.user : ""
|
||||||
DOTFILES_URI="${data.coder_parameter.dotfiles_uri.value}"
|
}
|
||||||
if [ -n "$${DOTFILES_URI// }" ]; then
|
|
||||||
coder dotfiles "$DOTFILES_URI" -y 2>&1 | tee -a ~/.dotfiles.log
|
resource "coder_script" "dotfiles" {
|
||||||
fi
|
agent_id = var.agent_id
|
||||||
EOT
|
script = templatefile("${path.module}/run.sh", {
|
||||||
|
DOTFILES_URI : local.dotfiles_uri,
|
||||||
|
DOTFILES_USER : local.user
|
||||||
|
})
|
||||||
display_name = "Dotfiles"
|
display_name = "Dotfiles"
|
||||||
icon = "/icon/dotfiles.svg"
|
icon = "/icon/dotfiles.svg"
|
||||||
run_on_start = true
|
run_on_start = true
|
||||||
@@ -52,5 +70,5 @@ resource "coder_script" "personalize" {
|
|||||||
|
|
||||||
output "dotfiles_uri" {
|
output "dotfiles_uri" {
|
||||||
description = "Dotfiles URI"
|
description = "Dotfiles URI"
|
||||||
value = data.coder_parameter.dotfiles_uri.value
|
value = local.dotfiles_uri
|
||||||
}
|
}
|
||||||
|
|||||||
23
dotfiles/run.sh
Normal file
23
dotfiles/run.sh
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
DOTFILES_URI="${DOTFILES_URI}"
|
||||||
|
DOTFILES_USER="${DOTFILES_USER}"
|
||||||
|
|
||||||
|
if [ -n "$${DOTFILES_URI// }" ]; then
|
||||||
|
if [ -z "$DOTFILES_USER" ]; then
|
||||||
|
DOTFILES_USER="$USER"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✨ Applying dotfiles for user $DOTFILES_USER"
|
||||||
|
|
||||||
|
if [ "$DOTFILES_USER" = "$USER" ]; then
|
||||||
|
coder dotfiles "$DOTFILES_URI" -y 2>&1 | tee ~/.dotfiles.log
|
||||||
|
else
|
||||||
|
# The `eval echo ~"$DOTFILES_USER"` part is used to dynamically get the home directory of the user, see https://superuser.com/a/484280
|
||||||
|
# eval echo ~coder -> "/home/coder"
|
||||||
|
# eval echo ~root -> "/root"
|
||||||
|
|
||||||
|
CODER_BIN=$(which coder)
|
||||||
|
DOTFILES_USER_HOME=$(eval echo ~"$DOTFILES_USER")
|
||||||
|
sudo -u "$DOTFILES_USER" sh -c "'$CODER_BIN' dotfiles '$DOTFILES_URI' -y 2>&1 | tee '$DOTFILES_USER_HOME'/.dotfiles.log"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user