Skip to content

feat(dotfiles): add ability to apply dotfiles as any user #133

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

Merged
merged 21 commits into from
Apr 26, 2024

Conversation

phorcys420
Copy link
Member

@phorcys420 phorcys420 commented Jan 26, 2024

In my template (basic-env), dotfiles are also applied to root.
I like doing this because whenever switching to a root shell for more than one command (e.g installing packages, etc), my shell is consistent with my user.

I have added an optional user parameter that applies the dotfiles as a specific user.

I have added an optional dotfiles_uri parameter that, if passed, hides the coder_parameter that asks the end user for their dotfiles URI, this way you can "daisy chain" modules together.

Running dotfiles as root only

module "dotfiles" {
  source = "git::https://github.com/phorcys420/coder-modules.git//dotfiles?ref=dotfiles-root"
  agent_id = coder_agent.dev.id
  user = "root"
}

"Daisy chaining" to run as the default user (usually "coder") and as root without prompting the user twice.

module "dotfiles" {
  source = "git::https://github.com/phorcys420/coder-modules.git//dotfiles?ref=dotfiles-root"
  agent_id = coder_agent.dev.id
}

module "dotfiles-root" {
  source       = "git::https://github.com/phorcys420/coder-modules.git//dotfiles?ref=dotfiles-root"
  agent_id     = coder_agent.dev.id
  user         = "root"
  dotfiles_uri = module.dotfiles.dotfiles_uri
}

Copy link
Member

@matifali matifali left a comment

Choose a reason for hiding this comment

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

Hmm. Can you run bun fmt and bun lint to fix the formatting issues?

@phorcys420
Copy link
Member Author

Hmm. Can you run bun fmt and bun lint to fix the formatting issues?

Yes, sorry, I'm not used to formatting every commit yet.

@phorcys420

This comment was marked as resolved.

@phorcys420

This comment was marked as outdated.

@phorcys420 phorcys420 force-pushed the dotfiles-root branch 2 times, most recently from a059f11 to ede72ca Compare January 28, 2024 09:22
@phorcys420 phorcys420 changed the title feat(dotfiles): add ability to run apply dotfiles as root feat(dotfiles): add ability to apply dotfiles as root Jan 28, 2024
@phorcys420

This comment was marked as outdated.

@matifali matifali requested a review from mafredri January 28, 2024 20:09
@phorcys420
Copy link
Member Author

I didn't mean to close, i'm starting off of a clean slate, which is probably what triggered it to close.

@phorcys420 phorcys420 reopened this Jan 30, 2024
@phorcys420 phorcys420 changed the title feat(dotfiles): add ability to apply dotfiles as root feat(dotfiles): add ability to apply dotfiles as any user Jan 30, 2024
@phorcys420
Copy link
Member Author

hey @mafredri, while writing this PR, I realized that the script running the dotfiles is named "personalize", do you want me to rename it to "dotfiles" in this PR even though it's not really it's scope, or should I open another PR ? to fix it

resource "coder_script" "personalize" {
  agent_id = var.agent_id
  script = templatefile("${path.module}/run.sh", {
    DOTFILES_URI : local.dotfiles_uri,
  })
  display_name = "Dotfiles"
  icon         = "/icon/dotfiles.svg"
  run_on_start = true
}

@mafredri
Copy link
Member

hey @mafredri, while writing this PR, I realized that the script running the dotfiles is named "personalize", do you want me to rename it to "dotfiles" in this PR even though it's not really it's scope, or should I open another PR ? to fix it

resource "coder_script" "personalize" {
  agent_id = var.agent_id
  script = templatefile("${path.module}/run.sh", {
    DOTFILES_URI : local.dotfiles_uri,
  })
  display_name = "Dotfiles"
  icon         = "/icon/dotfiles.svg"
  run_on_start = true
}

@phorcys420 feel free to do it here, no need for another PR IMO. Thanks for spotting.

@phorcys420
Copy link
Member Author

sorry for all the noise, but given that terraform modules can only be loaded via an URL, I have to push debug commits and then force-push to clean my branch.

here is an example of what you can do with this PR :

module "dotfiles" {
  source = "git::https://github.com/phorcys420/coder-modules.git//dotfiles?ref=dotfiles-root"
  agent_id = coder_agent.dev.id
  #dotfiles_uri = "<defined by template admin>"
}

module "dotfiles-root" {
  source = "git::https://github.com/phorcys420/coder-modules.git//dotfiles?ref=dotfiles-root"
  agent_id = coder_agent.dev.id
  user = "root"
  dotfiles_uri = module.dotfiles.dotfiles_uri # "<defined by template admin>" if uncommented, or the value the end user provided in template settings
}

I have tested all the following scenarios and they are working :

  1. single module without dotfiles_uri defined
    • asks the user on template creation
    • applies dotfiles to the default user (coder)
  2. single module with dotfiles_uri defined
    • doesn't ask the user on template creation
    • applies dotfiles to the default user (coder)
  3. single module without dotfiles_uri defined with user = "root"
    • asks the user on template creation
    • applies dotfiles to the root user
  4. single module with dotfiles_uri defined with `user = "root"
    • doesn't ask the user on template creation
    • applies dotfiles to the root user
  5. module without dotfiles_uri defined + module with dotfiles_uri = module.dotfiles.dotfiles_uri
    • asks the user on template creation once and uses the same URI when applying both times
    • applies dotfiles to the default user (coder)
  6. module with dotfiles_uri = "<defined by template admin>" + module with dotfiles_uri = module.dotfiles.dotfiles_uri
    • doesn't ask the user at all
    • applies dotfiles to the default user (coder)

@phorcys420
Copy link
Member Author

phorcys420 commented Jan 30, 2024

@mafredri I would like your input on these things.


main.tf

variable "dotfiles_uri" {
  type        = string
  description = "The URL to a dotfiles repository. (optional)"

  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
}

locals {
  dotfiles_uri = var.dotfiles_uri != null ? var.dotfiles_uri : data.coder_parameter.dotfiles_uri[0].value
  user         = var.user != null ? var.user : ""
}

Currently, var.user's default is null and I use a ternary operator to send an empty string ("") to templatefile() if it's null because templatefile() doesn't take null arguments.
I think this is ugly, but while I could set var.user's default to an empty string (""), I think it doesn't make sense that the default is not null if nothing was passed.


run.sh

if [ -n "$${DOTFILES_URI// }" ]; then
  if [ -z "$DOTFILES_USER" ]; then
    DOTFILES_USER="$USER"
  fi
fi

In this section, I use [ -z "$DOTFILES_USER" ] when I could theoretically use [ "$${DOTFILES_USER// }" ] to be more coherent, but I only got it working in my shell and never in the script and ran out of nerve, is it fine if it stays like this ?
Furthemore, I think that the following can look quite confusing :

if [ -n "$${DOTFILES_URI// }" ]; then
  if [ "$${DOTFILES_USER// }" ]; then
    DOTFILES_USER="$USER"
  fi
fi

@phorcys420 phorcys420 requested a review from mafredri April 14, 2024 12:42
@phorcys420
Copy link
Member Author

hey!
sorry for letting this PR become stale, I went ahead and applied the changes you suggested.

@matifali
Copy link
Member

@phorcys420 can you rebase it?

Copy link
Member

@matifali matifali left a comment

Choose a reason for hiding this comment

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

@mafredri, I approve it, but wait for you to merge in case you want a final review.

@phorcys420
Copy link
Member Author

hey @matifali, just resolved the merge conflict, there was an error in the docs where the variable name was default_dotfiles_repo instead of default_dotfiles_uri so I corrected that.

Also, does it make sense that there is a dotfiles_default_uri output? I don't see how this could be useful.

output "dotfiles_default_uri" {
  description = "Dotfiles Default URI"
  value       = var.default_dotfiles_uri
}

@phorcys420
Copy link
Member Author

Another thing, could you review the README.md to see if it makes sense to you?
I think the section about default dotfiles URI might be better somewhere else, but I don't really know.

@matifali
Copy link
Member

@phorcys420 having an output doesn't do any harm but could be helpful for some users, so I did not remove it. I agree that to doesn't add much value as its not anything computed but a direct output from an input.

Let's remove that.

@phorcys420
Copy link
Member Author

@matifali do you want me to remove it in this PR?

Copy link
Member

@matifali matifali left a comment

Choose a reason for hiding this comment

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

done

@matifali
Copy link
Member

matifali commented Apr 14, 2024

@phorcys420 We can do it in a separate PR and keep the changes related in this PR.

The new PR can fix the typo and remove output. I will do that

@phorcys420

This comment was marked as outdated.

@matifali
Copy link
Member

#225 does that but we need to wait for someone to approve that :D

@phorcys420
Copy link
Member Author

oh okay, but I already fixed the typo in this branch @matifali, do you want me to unfix it to keep the changes related to the PR? 😄

@matifali
Copy link
Member

@phorcys420 Yes, its better to do ina separate PR.

@phorcys420
Copy link
Member Author

I have tested the module again after all the changes and I can confirm that it still works as expected.

@matifali
Copy link
Member

@phorcys420 Could you rebase it again. :D

@phorcys420
Copy link
Member Author

@phorcys420 Could you rebase it again. :D

will do!

Comment on lines 17 to +28
variable "default_dotfiles_uri" {
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 = ""
}

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
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we should only have one of these

@matifali matifali merged commit 443485a into coder:main Apr 26, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants