garrison

garrison

I’m prepping some Phoenix apps for deployment (hype!), and for one of them I would like to have a small CLI script to perform some simple tasks which are security sensitive enough that I’d rather not have them on the admin panel.

I understand that I can create eval scripts similar to the “migrate” script that you get with mix phx.gen.release, but I haven’t been able to find any documentation or previous forum threads about passing arguments to these scripts, so I thought it would be a good topic for a new thread :slight_smile:

One approach which should work would be to inject the args directly into the eval as a literal string and then parse them back out in Elixir.

Essentially the following (haven’t tested this yet):

In the bash script:

exec ./my_app eval "MyApp.Release.eval_cli(\"$@\")"

Then in the release.ex:

def eval_cli(raw_args) do
  args = OptionParser.split(raw_args)
  # ...
end

Does anyone have any better ideas? I feel like I can’t be the first person to have this problem, but everything I’ve been able to find essentially just suggests writing evals manually in the shell which I don’t want to do.

Showing Posts 1 to 3

t0t0

t0t0

I think, unless you provide arguments in an elixir form, it’s impossible to have a reliable result, you’d need to realize a proper Elixir (shell) escaping from the shell/script.

I guess with this way you can’t put space in arguments?

#!/bin/sh

elixir -e "OptionParser.split(\"$@\") |> IO.inspect()"

Doesn’t work (./test.sh foo "abc def" bar):

** (TokenMissingError) token missing on nofile:1:24:
    error: missing terminator: " (for string starting at line 1)
    │
  1 │ OptionParser.split("foo
    │                    │   └ missing closing delimiter (expected ")
    │                    └ unclosed delimiter
    │
    └─ nofile:1:24
    (elixir 1.16.3) lib/code.ex:571: Code.validated_eval_string/3

Not much success with:

/bin/sh -ac ". /home/user/app/shared/env.sh && cd /home/user/app/current/lib/app-0.0.1 && ../../bin/app eval 'System.argv() |> IO.inspect()' '${@}'"
krasenyp

krasenyp

I’d save myself the hassle and create a control Unix domain socket in a well-known location, like /var/run. Have a process listen for incoming messages and you can basically do whatever you want. You can create a CLI program, in bash, C, whatever, and implement any “protocol” you want. You can send JSON {"mod": "MyApp.Whatever", "fun": "some_fun", "args": ["some", "arg"]}. An even more flexible approach would be to encode erlang terms in a binary message but then you’ll be basically reimplementing GitHub - nerves-networking/beam_notify: Send a message from a shell script to the BEAM · GitHub.

garrison

garrison OP

You are correct, of course, and my disappointment with this was what prompted me to post :slight_smile:

I believe what’s going on there is that "$@" properly quotes the args that it outputs, which then promptly breaks up the string that you pass to elixir -e. Using "$*" works as expected because it joins the args into one big (space-delimited) string, except you then destroy any information about which args are which, meaning your args can no longer have spaces etc. This would actually be perfectly fine for my use case (which is very simple), but it would be nice to have a real solution.

I did consider trying to inject the args into argv somehow, which is clever, but even if it worked for eval I don’t see how it could be made to work for rpc.

I think you could also manually quote out each argument (which may actually be the simplest working solution), like:

./my_app eval "MyApp.func(\"$1\", \"$2\")"

But there are still so many obvious ways to break this.

This is indeed a much saner solution, though I think we have different concepts of avoiding hassle :slight_smile: I think this would be overkill for my case (something I intend to run maybe once every couple months), but if I had to use it every day I’d probably go this way.

Honestly the best strategy in my case may just be to use Mix scripts in prod (I’ll have Mix installed anyway as I intend to build the releases on each server), but it would be nice if there was a proper way to use “scripts in releases” built into Elixir.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews