Skip to content

Instantly share code, notes, and snippets.

@barjo
Last active September 3, 2019 19:25
Run a heroku one off dyno with curl and listen to rendezvous
#!/bin/bash
# args: $1 is the app name, $2 is the command you want to run.
heroku_run() {
curl -H "Authorization: Bearer ${HEROKU_OAUTH_TOKEN}" \
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Content-Type: application/json" \
-d "{\"attach\": true, \"command\" : \"$2\", \"type\" : \"run\"}" \
"https://api.heroku.com/apps/$1/dynos"
}
parse_secret() {
echo $1 | grep -oe '/heroku\.com\:5000\/([a-z0-9]+)/'
}
rendez_vous() {
(echo "$1"; sleep 2) | openssl s_client -connect rendezvous.runtime.heroku.com:5000
}
echo "$1> Running $2"
output=$(heroku_run $1 $2)
echo "$1> $output"
secret=$(parse_secret "$output")
echo "$1> Rendez vous... $secret"
rendez_vous $secret
@kennethreitz
Copy link

I get an awk error

@kennethreitz
Copy link

parse_secret() {
  echo $1 | cut -d / -f4
}

@barjo
Copy link
Author

barjo commented Sep 3, 2019

@kennethreitz I haven't used that in a while, what error do you get, it's possible that Heroku changed their response format.

@barjo
Copy link
Author

barjo commented Sep 3, 2019

I just tested it, it still works well for me, on an old app though :-?

@barjo
Copy link
Author

barjo commented Sep 3, 2019

just to be sure, you do need to set your heroku api key as HEROKU_OAUTH_TOKEN env. variable.

@kennethreitz
Copy link

i think it's a gnu vs bsd awk thing, possibly.

@barjo
Copy link
Author

barjo commented Sep 3, 2019

Then you can try replacing the awk command by a grep one:

parse_secret() {
  echo $1 | grep -oe '/heroku\.com\:5000\/([a-z0-9]+)/'
}

That should work to retrieve the rendez vous attached url secret from the json response.
I just edited it to use -e option for the pattern rather than the -P which may not be available on bsd.

@barjo
Copy link
Author

barjo commented Sep 3, 2019

I edited the gist to use grep rather than awk, hope it helps.

@kennethreitz
Copy link

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment