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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews