Skip to content

Commit 563dbc4

Browse files
authored
feat: add post_clone_script to dotfiles in order to support startup dependencies/coordination (#679)
## Description Adds post_clone_script variable to the dotfiles module, enabling startup coordination with other scripts that depend on dotfiles. An example of how to use this, which assumes the PR has been merged: ``` module "dotfiles" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/dotfiles/coder" version = "1.3.0" agent_id = coder_agent.main.id default_dotfiles_uri = "https://github.com/someuser/somedotfiles" post_clone_script = <<-EOF coder exp sync start dotfiles && coder exp sync complete dotfiles EOF } resource "coder_script" "personalize" { count = data.coder_workspace.me.start_count agent_id = coder_agent.main.id display_name = "Personalize" icon = "/icon/personalize.svg" run_on_start = true script = <<-EOF trap 'coder exp sync complete personalize' EXIT coder exp sync want personalize dotfiles coder exp sync start personalize SCRIPT="$HOME/.config/coderv2/dotfiles/personalize" if [ -f "$SCRIPT" ] && [ -x "$SCRIPT" ]; then $SCRIPT fi EOF } ``` ## Type of Change - [ ] New module - [ ] New template - [ ] Bug fix - [x] Feature/enhancement - [ ] Documentation - [ ] Other ## Module Information **Path:** `registry/coder/modules/dotfiles` **New version:** `v1.3.0` **Breaking change:** [ ] Yes [x] No ## Testing & Validation - [ ] Tests pass (`bun test`) - [ ] Code formatted (`bun fmt`) - [x] Changes tested locally ## Related Issues #678
1 parent 39fec7c commit 563dbc4

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

registry/coder/modules/dotfiles/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Under the hood, this module uses the [coder dotfiles](https://coder.com/docs/v2/
1818
module "dotfiles" {
1919
count = data.coder_workspace.me.start_count
2020
source = "registry.coder.com/coder/dotfiles/coder"
21-
version = "1.2.4"
21+
version = "1.3.0"
2222
agent_id = coder_agent.example.id
2323
}
2424
```
@@ -31,7 +31,7 @@ module "dotfiles" {
3131
module "dotfiles" {
3232
count = data.coder_workspace.me.start_count
3333
source = "registry.coder.com/coder/dotfiles/coder"
34-
version = "1.2.4"
34+
version = "1.3.0"
3535
agent_id = coder_agent.example.id
3636
}
3737
```
@@ -42,7 +42,7 @@ module "dotfiles" {
4242
module "dotfiles" {
4343
count = data.coder_workspace.me.start_count
4444
source = "registry.coder.com/coder/dotfiles/coder"
45-
version = "1.2.4"
45+
version = "1.3.0"
4646
agent_id = coder_agent.example.id
4747
user = "root"
4848
}
@@ -54,14 +54,14 @@ module "dotfiles" {
5454
module "dotfiles" {
5555
count = data.coder_workspace.me.start_count
5656
source = "registry.coder.com/coder/dotfiles/coder"
57-
version = "1.2.4"
57+
version = "1.3.0"
5858
agent_id = coder_agent.example.id
5959
}
6060
6161
module "dotfiles-root" {
6262
count = data.coder_workspace.me.start_count
6363
source = "registry.coder.com/coder/dotfiles/coder"
64-
version = "1.2.4"
64+
version = "1.3.0"
6565
agent_id = coder_agent.example.id
6666
user = "root"
6767
dotfiles_uri = module.dotfiles.dotfiles_uri
@@ -76,7 +76,7 @@ You can set a default dotfiles repository for all users by setting the `default_
7676
module "dotfiles" {
7777
count = data.coder_workspace.me.start_count
7878
source = "registry.coder.com/coder/dotfiles/coder"
79-
version = "1.2.4"
79+
version = "1.3.0"
8080
agent_id = coder_agent.example.id
8181
default_dotfiles_uri = "https://github.com/coder/dotfiles"
8282
}

registry/coder/modules/dotfiles/main.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ variable "manual_update" {
8484
default = false
8585
}
8686

87+
variable "post_clone_script" {
88+
description = "Custom script to run after applying dotfiles. Runs every time, even if dotfiles were already applied."
89+
type = string
90+
default = null
91+
}
92+
8793
data "coder_parameter" "dotfiles_uri" {
8894
count = var.dotfiles_uri == null ? 1 : 0
8995
type = "string"
@@ -102,15 +108,17 @@ data "coder_parameter" "dotfiles_uri" {
102108
}
103109

104110
locals {
105-
dotfiles_uri = var.dotfiles_uri != null ? var.dotfiles_uri : data.coder_parameter.dotfiles_uri[0].value
106-
user = var.user != null ? var.user : ""
111+
dotfiles_uri = var.dotfiles_uri != null ? var.dotfiles_uri : data.coder_parameter.dotfiles_uri[0].value
112+
user = var.user != null ? var.user : ""
113+
encoded_post_clone_script = var.post_clone_script != null ? base64encode(var.post_clone_script) : ""
107114
}
108115

109116
resource "coder_script" "dotfiles" {
110117
agent_id = var.agent_id
111118
script = templatefile("${path.module}/run.sh", {
112119
DOTFILES_URI : local.dotfiles_uri,
113-
DOTFILES_USER : local.user
120+
DOTFILES_USER : local.user,
121+
POST_CLONE_SCRIPT : local.encoded_post_clone_script
114122
})
115123
display_name = "Dotfiles"
116124
icon = "/icon/dotfiles.svg"
@@ -127,7 +135,8 @@ resource "coder_app" "dotfiles" {
127135
group = var.group
128136
command = templatefile("${path.module}/run.sh", {
129137
DOTFILES_URI : local.dotfiles_uri,
130-
DOTFILES_USER : local.user
138+
DOTFILES_USER : local.user,
139+
POST_CLONE_SCRIPT : local.encoded_post_clone_script
131140
})
132141
}
133142

registry/coder/modules/dotfiles/run.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ if [ -n "$${DOTFILES_URI// }" ]; then
4343
sudo -u "$DOTFILES_USER" "$CODER_BIN" dotfiles "$DOTFILES_URI" -y 2>&1 | tee "$DOTFILES_USER_HOME/.dotfiles.log"
4444
fi
4545
fi
46+
47+
POST_CLONE_SCRIPT="${POST_CLONE_SCRIPT}"
48+
49+
if [ -n "$POST_CLONE_SCRIPT" ]; then
50+
echo "Running post-clone script..."
51+
POST_CLONE_TMP=$(mktemp)
52+
echo "$POST_CLONE_SCRIPT" | base64 -d > "$POST_CLONE_TMP"
53+
chmod +x "$POST_CLONE_TMP"
54+
$POST_CLONE_TMP
55+
rm "$POST_CLONE_TMP"
56+
fi

0 commit comments

Comments
 (0)