ondemand icon indicating copy to clipboard operation
ondemand copied to clipboard

Add shortcut to Files menu using environment variables

Open kcgthb opened this issue 2 years ago • 3 comments

Hi!

The documentation and all the examples I found online to add additional shortcuts to the dashboard's Files menu are constructing file paths explicitly. Like

FavoritePath.new("/fs/project/#{p}")

We have a pretty elaborate process to setup some data paths for our users, and store those paths in environment variables in the user environment. We can't really reproduce the path-building logic in /etc/ood/config/apps/dashboard/initializers/ood.rb, so we'd like to re-use the environment variables directly.

We tried this, but without success:

FavoritePath.new(ENV["DATA_DIR"]. title: "Data directory")

Is there a better way to do this? How can we access user's environment variables in the dashboard initializers?

Thanks!

┆Issue is synchronized with this Asana task by Unito

kcgthb avatar Sep 20 '22 01:09 kcgthb

The environment available to any part of the Ruby code for OnDemand is going to be dictated by nginx_stage as that tool will strip the environment before launching the PUN which determines the environment the Passenger processes launching OnDemand will have available to them.

What makes a per-user environment variable tricky is nginx_stage first runs as Apache user and then via sudo runs nginx_stage command as root to launch the PUN's NIGNX instance as the logged in user. That nginx_stage command can source files like /etc/ood/profile but it's going to be sourced as Apache user, not the logged in user.

I think your best bet would be to get the initializers to generate the paths since those will be run as the logged in user. I'm curious why you'd be able to generate the path in Bash but not Ruby.

treydock avatar Sep 20 '22 19:09 treydock

Hi Trey!

Thanks for the feedback, that's helpful.

I think your best bet would be to get the initializers to generate the paths since those will be run as the logged in user. I'm curious why you'd be able to generate the path in Bash but not Ruby.

It's not so much a question of being able to, but rather a matter of duplicating this in multiple places and keeping it in sync if it changes. :)

But I get the point about the nginx_stage command being started as Apache user. I initially thought that the user's $HOME directory was determined from the environment variable, but I see that it actually comes from Ruby's Dir instead.

kcgthb avatar Sep 20 '22 20:09 kcgthb

So one possibly ugly solution would be to create some kind of wrapper script that initializes a login environment and just spits out the desired path and call that script in the initializer to get the path. Maybe that way you're not duplicating logic. I'm not a fan of forking processes during Rails initialization but it might work. If your paths are in something where you can source the file into another bash script, should be pretty simple wrapper script.

If you have many you could do something like this:

#!/bin/bash

source /path/to/your/setup

env

Then in initializer do something like:

require 'tempfile'
require 'dotenv'

output = `/path/to/wrapper`
env_file = Tempfile.new('env')
File.write(env_file.path, output)
env = Dotenv.parse(env_file.path)
FavoritePath.new(env['DATA_DIR'])

There might be ways to take a string variable and parse it with dotenv to avoid the temporary file.

treydock avatar Sep 20 '22 20:09 treydock