Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filebrowser): check if already installed #334

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions filebrowser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A file browser for your workspace.
```tf
module "filebrowser" {
source = "registry.coder.com/modules/filebrowser/coder"
version = "1.0.22"
version = "1.0.23"
agent_id = coder_agent.example.id
}
```
Expand All @@ -28,7 +28,7 @@ module "filebrowser" {
```tf
module "filebrowser" {
source = "registry.coder.com/modules/filebrowser/coder"
version = "1.0.22"
version = "1.0.23"
agent_id = coder_agent.example.id
folder = "/home/coder/project"
}
Expand All @@ -39,7 +39,7 @@ module "filebrowser" {
```tf
module "filebrowser" {
source = "registry.coder.com/modules/filebrowser/coder"
version = "1.0.22"
version = "1.0.23"
agent_id = coder_agent.example.id
database_path = ".config/filebrowser.db"
}
Expand Down
37 changes: 25 additions & 12 deletions filebrowser/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ data "coder_workspace_owner" "me" {}

variable "agent_name" {
type = string
description = "The name of the main deployment. (Used to build the subpath for coder_app.)"
default = ""
validation {
# If subdomain is false, then agent_name must be set.
condition = var.subdomain || var.agent_name != ""
error_message = "The agent_name must be set."
}
description = "The name of the coder_agent resource. (Only required if subdomain is false and the template uses multiple agents.)"
default = null
}

variable "database_path" {
Expand Down Expand Up @@ -73,6 +68,12 @@ variable "order" {
default = null
}

variable "slug" {
type = string
description = "The slug of the coder_app resource."
default = "filebrowser"
}

variable "subdomain" {
type = bool
description = <<-EOT
Expand All @@ -85,26 +86,38 @@ variable "subdomain" {
resource "coder_script" "filebrowser" {
agent_id = var.agent_id
display_name = "File Browser"
icon = "https://raw.githubusercontent.com/filebrowser/logo/master/icon_raw.svg"
icon = "/icon/filebrowser.svg"
script = templatefile("${path.module}/run.sh", {
LOG_PATH : var.log_path,
PORT : var.port,
FOLDER : var.folder,
LOG_PATH : var.log_path,
DB_PATH : var.database_path,
SUBDOMAIN : var.subdomain,
SERVER_BASE_PATH : var.subdomain ? "" : format("/@%s/%s.%s/apps/filebrowser", data.coder_workspace_owner.me.name, data.coder_workspace.me.name, var.agent_name),
SERVER_BASE_PATH : local.server_base_path
})
run_on_start = true
}

resource "coder_app" "filebrowser" {
agent_id = var.agent_id
slug = "filebrowser"
slug = var.slug
display_name = "File Browser"
url = "http://localhost:${var.port}"
icon = "https://raw.githubusercontent.com/filebrowser/logo/master/icon_raw.svg"
url = local.url
icon = "/icon/filebrowser.svg"
subdomain = var.subdomain
share = var.share
order = var.order

healthcheck {
url = local.healthcheck_url
interval = 5
threshold = 6
}
}

locals {
server_base_path = var.subdomain ? "" : format("/@%s/%s%s/apps/%s", data.coder_workspace_owner.me.name, data.coder_workspace.me.name, var.agent_name != null ? ".${var.agent_name}" : "", var.slug)
url = "http://localhost:${var.port}${local.server_base_path}"
healthcheck_url = "http://localhost:${var.port}${local.server_base_path}/health"
}
6 changes: 5 additions & 1 deletion filebrowser/run.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env bash

BOLD='\033[0;1m'

printf "$${BOLD}Installing filebrowser \n\n"

curl -fsSL https://raw.githubusercontent.com/filebrowser/get/master/get.sh | bash
# Check if filebrowser is installed
if ! command -v filebrowser &> /dev/null; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(No action, just commentary.)

This is nice, but unfortunately it's rare that users will benefit since it's usually installed to /usr/local/bin. For containers it's rarely persisted.

It'd be cool if we could store this in $CODER_SCRIPT_BIN_DIR instead. Usually this is within /tmp but it's possible to change by starting the coder agent with CODER_AGENT_SCRIPT_DATA_DIR=/path/to/persistent/dir.

This is something to consider for all modules, should we track this in a new issue @matifali?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice, but unfortunately it's rare that users will benefit since it's usually installed to /usr/local/bin. For containers it's rarely persisted.

For air-gapped scenarios where the image already has it installed this is beneficial.

It'd be cool if we could store this in $CODER_SCRIPT_BIN_DIR instead. Usually this is within /tmp but it's possible to change by starting the coder agent with CODER_AGENT_SCRIPT_DATA_DIR=/path/to/persistent/dir.

I am a bit confused about $CODER_SCRIPT_BIN_DIR and $CODER_AGENT_SCRIPT_DATA_DIR. Do we have them already and how are the used? Or are you proposing this new concept.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For air-gapped scenarios where the image already has it installed this is beneficial.

👍🏻

I am a bit confused about $CODER_SCRIPT_BIN_DIR and $CODER_AGENT_SCRIPT_DATA_DIR. Do we have them already and how are the used? Or are you proposing this new concept.

We have them.

Let's say you run the agent this way:

coder agent --script-data-dir=/home/coder/.local/coder-script

Now all coder scripts will be executed with the following environment variables:

CODER_SCRIPT_DATA_DIR=/home/coder/.local/coder-script/[UUID]
CODER_SCRIPT_BIN_DIR=/home/coder/.local/coder-script/bin

And the coder agent will automatically add /home/coder/.local/coder-script/bin to the PATH.

Even without specifying the script dir, a default will be used, you can verify this in your workspace:

ls /tmp/coder-script-data

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super useful and we should start using it in our modules.

curl -fsSL https://raw.githubusercontent.com/filebrowser/get/master/get.sh | bash
fi

printf "🥳 Installation complete! \n\n"

Expand Down