Skip to content

Instantly share code, notes, and snippets.

@wiseleyb
Created August 26, 2020 23:16
Show Gist options
  • Save wiseleyb/46d199b2c17f9c1a406d8c4cf56c246d to your computer and use it in GitHub Desktop.
Save wiseleyb/46d199b2c17f9c1a406d8c4cf56c246d to your computer and use it in GitHub Desktop.
class HerokuUtils
class << self
# example
# create_dyno(cmd: 'rake --tasks',
# size: 'performance-l',
# type: 'jobs')
# cmd: any command you can pass to heroku - you don't need to include
# run:detached
# size: any heroku size https://devcenter.heroku.com/articles/dyno-types
# type: this can be anything - when you run "heroku ps" it organizes things
# by this type. For example you'll see sidekiq, run, etc. If you're
# running a ton of jobs give it a unique name and they'll be easier
# to kill/monitor
# app: zeemee-stg | zeemee-prod || nil (will default to stg or prod)
#
# Returns something like
# {
# "attach_url"=>nil,
# "command"=>"bundle exec rake --tasks",
# "created_at"=>"2018-03-01T23:11:01Z",
# "id"=>"d7275e42-482e-4cb2-b6b3-ffb0ffc0bf30",
# "name"=>"jobs.9144",
# "app"=>{
# "id"=>"506a69c7-9601-49b7-809b-943559497984",
# "name"=>"zeemee-stg"
# },
# "release"=>{
# "id"=>"8492206c-f96c-4f7e-9f00-f14e49641c27",
# "version"=>2532
# },
# "size"=>"Standard-1X",
# "state"=>"starting",
# "type"=>"jobs",
# "updated_at"=>"2018-03-01T23:11:01Z"
# }
#
# Doc on this:
# https://devcenter.heroku.com/articles/platform-api-reference#dyno
#
# Tou can monitor jobs by looking for the 'name' in this example jobs.9144
# * tail logs: heroku logs --tail -p jobs.9144 --app zeemee-stg
# * see all jobs running: heroku ps --app zeemee-stg
# * kill the job: heroku ps:stop jobs.9144 --app zeemee-stg
# TODO: move un/pw to config when gem'ifying
def create_dyno(cmd,
size: 'performance-l',
type: 'jobs',
app: nil,
envs: {})
app ||= Rails.env.production? ? 'zeemee-prod' : 'zeemee-stg'
unless envs.empty?
envs.each do |k, v|
cmd = "#{k}=#{v} #{cmd}"
end
end
JSON.parse(`#{url(app, cmd, size, type)}`)
end
def url(app, cmd, size, type)
app ||= Rails.env.production? ? 'zeemee-prod' : 'zeemee-stg'
d = { command: cmd, size: size, type: type }.to_json
%(curl -n -X POST https://api.heroku.com/apps/#{app}/dynos \
-d '#{d}' \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Authorization: Basic #{auth}")
end
def auth
un = ENV['ZEEMEE_USER']
pw = ENV['ZEEMEE_PASSWORD']
Base64.encode64("#{un}:#{pw}").chomp
end
# Does the equivalent of: heroku restart
# TODO: move un/pw to config when gem'ifying
def restart_app
app ||= Rails.env.production? ? 'zeemee-prod' : 'zeemee-stg'
un = ENV['ZEEMEE_USER']
pw = ENV['ZEEMEE_PASSWORD']
auth = Base64.encode64("#{un}:#{pw}").chomp
url = %(curl -n -X DELETE https://api.heroku.com/apps/#{app}/dynos \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Authorization: Basic #{auth}")
JSON.parse(`#{url}`)
end
# TODO: make this a yaml config when gem'ifying
def app_from_env
case Rails.env
when 'development', 'test'
'n/a'
when 'staging'
'zeemee-stg'
when 'production'
'zeemee-prod'
else
raise 'Unknown env'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment