Heroku tips

Posted on 2018-10-03 in Trucs et astuces

Use an editor

By default there is no editor on Heroku. To get one, you can use the heroku-nano plugin like this (or by installing as a plugin):

mkdir bin
cd bin
curl https://github.com/Ehryk/heroku-nano/raw/master/heroku-nano-2.5.1/nano.tar.gz --location --silent > nano.tar.gz
tar -xvf nano.tar.gz
chmod +x nano
cd -
export PATH="$PATH:$(realpath bin)"

Then use nano README.md.

Papertail

Download logs

This requires httpie. But you can adapt this to use curl instead.

#!/usr/bin/env bash
set -eu

# Change this to start at another date.
start_date=2019-08-24
nb_days_to_download=18
# Set your paper trail token here.
papertrail_token=''

for i in $(seq 0 ${nb_days_to_download}); do
  log_date=$(date -I -d "${start_date} +${i} days")
  mkdir -p "${log_date}"
  pushd "${log_date}"
    for log_hour in $(seq -f "%02g" 0 23); do
      if [[ ! -f "${log_date}-${log_hour}.tsv.gz" ]]; then
        http --follow "https://papertrailapp.com/api/v1/archives/${log_date}-${log_hour}/download" "X-Papertrail-Token:${papertrail_token}" > "${log_date}-${log_hour}.tsv.gz"
      fi
    done
  popd
done

Download files

From google drive

Define this function:

gdrive-download() {
    file_id=$1
    file=$2
    base_url="https://drive.google.com/uc?export=download&id=${file_id}"
    echo "Downloading ${base_url}"
    CONFIRM=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate "${base_url}" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1/p')
    wget --load-cookies /tmp/cookies.txt "https://drive.google.com/uc?export=download&confirm=${CONFIRM}&id=${file_id}" -O "${file}"
    rm -rf /tmp/cookies.txt
}

And use it like this: gdrive-download FILE_ID_FROM_THE_SHARE_LINK DEST_FILENAME.