Aduril

Aduril

Scripting with User Switch Command

Hello there,
to improve the workflow of my team I want to make it easier for them to connect to a remote system (aka our stage environment and our production environment).

Currently the workflow looks as following

./start-iex me 127.0.0.1 #script that set cookies and stuff
iex(me@127.0.0.1)1> Node.connect :"app@IP.OF.STAGE.ENV"
true
iex(me@127.0.0.1)3> #CTRL+G
User switch command
 --> r 'app@IP.OF.STAGE.ENV'
 --> c 2
Eshell V10.5  (abort with ^G)
(app@IP.OF.STAGE.ENV)1> #CTRL+G again
User switch command
 --> r 'app@IP.OF.STAGE.ENV' 'Elixir.IEx'
 --> c 3
iex(app@IP.OF.STAGE.ENV)1> #iex shell on remote system

As you can see this is rather tedious and error prone (how often I used the wrong quotes here…).
So my question is:
Does anybody know a way to enter the User Switch Command in a way that is scriptable. My overall goal would be a script that allows people to simply connect to one of our deployed stages.

Any ideas?

Marked As Solved

Nicd

Nicd

I have used Expect for this, as it is designed for automating usage of interactive programs.

I no longer use the script but here it is for reference: https://gitlab.com/Nicd/mebe/blob/master/refresh.exp – it should help you get started in case you go with Expect.

Note that on line 14, send " " sends the ctrl+g IIRC. It may not be copied correctly if you copy it with a browser so you may want to download the file directly. (And indeed if I copy it to this forum, it’s rendered as send "a"!)

EDIT: Note that there is also a --remsh option for IEx. Maybe that already does what you need, at least partly?

Also Liked

Aduril

Aduril

Thanks @Nicd,

I have implemented it as expect-script now and will try to improve it. For others with a similar problem, here is my current solution that seems to do the trick:

set username [lindex $argv 0]
set app_ip [lindex $argv 1] 
spawn ./start-iex $username

expect "iex" { send "Node.connect :\"app@$app_ip\"" }
expect "iex" { send "^G" } # this might break while copy-pasting - it is the CTRL+G command
expect -- " -->" { send "r 'app@$app_ip' \n" }
expect -- " -->" { send "c 2\n" }
expect "(app@*" { send "^G" } # see above
expect -- " -->" { send "r 'app@$app_ip' 'Elixir.IEx'\n" }
expect -- " -->" { send "c 3\n" }

interact

I have not used expect before, so if you see any unnecessary parts let me know :slight_smile:.

And if anybody knows a way to enter the user switch command mode within an elixir script, let me know.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement