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?

1 Like

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?

4 Likes

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.

2 Likes