class Suspenders::Adapters::Heroku

Attributes

app_builder[R]

Public Class Methods

new(app_builder) click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 4
def initialize(app_builder)
  @app_builder = app_builder
end

Public Instance Methods

create_heroku_pipeline() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 68
def create_heroku_pipeline
  pipelines_plugin = `heroku help | grep pipelines`
  if pipelines_plugin.empty?
    puts "You need heroku pipelines plugin. Run: brew upgrade heroku-toolbelt"
    exit 1
  end

  run_toolbelt_command(
    "pipelines:create #{heroku_app_name} \
      -a #{heroku_app_name}-staging --stage staging",
    "staging",
  )

  run_toolbelt_command(
    "pipelines:add #{heroku_app_name} \
      -a #{heroku_app_name}-production --stage production",
    "production",
  )
end
create_production_heroku_app(flags) click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 27
def create_production_heroku_app(flags)
  app_name = heroku_app_name_for("production")

  run_toolbelt_command "create #{app_name} #{flags}", "production"
end
create_staging_heroku_app(flags) click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 21
def create_staging_heroku_app(flags)
  app_name = heroku_app_name_for("staging")

  run_toolbelt_command "create #{app_name} #{flags}", "staging"
end
set_heroku_application_host() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 88
def set_heroku_application_host
  %w(staging production).each do |environment|
    run_toolbelt_command(
      "config:add APPLICATION_HOST=#{heroku_app_name}-#{environment}.herokuapp.com",
      environment,
    )
  end
end
set_heroku_auto_migrate() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 97
def set_heroku_auto_migrate
  %w(staging production).each do |environment|
    run_toolbelt_command(
      "config:add AUTO_MIGRATE_DB=true",
      environment,
    )
  end
end
set_heroku_backup_schedule() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 59
def set_heroku_backup_schedule
  %w(staging production).each do |environment|
    run_toolbelt_command(
      "pg:backups:schedule DATABASE_URL --at '2:00 UTC'",
      environment,
    )
  end
end
set_heroku_buildpacks() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 106
def set_heroku_buildpacks
  %w(staging production).each do |environment|
    run_toolbelt_command(
      "buildpacks:add --index 1 heroku/nodejs",
      environment
    )
    run_toolbelt_command(
      "buildpacks:add --index 2 heroku/ruby",
      environment
    )
  end
end
set_heroku_rails_secrets() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 33
def set_heroku_rails_secrets
  %w(staging production).each do |environment|
    run_toolbelt_command(
      "config:add SECRET_KEY_BASE=#{generate_secret}",
      environment,
    )
  end
end
set_heroku_remotes() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 8
      def set_heroku_remotes
        remotes = <<~SHELL
          echo "\n== Adding Heroku remotes =="
          #{command_to_join_heroku_app('staging')}
          #{command_to_join_heroku_app('production')}

          echo "\n== Setting default heroku remote to 'staging' =="
          git config heroku.remote staging
        SHELL

        app_builder.append_file "bin/setup", remotes
      end
set_heroku_sentry_configuration() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 51
def set_heroku_sentry_configuration
  %w(staging production).each do |environment|
    run_toolbelt_command("config:add SENTRY_ENV=#{environment}", environment)
    run_toolbelt_command("config:add SENTRY_DSN='https://changeme@sentry.io/changeme'", environment)
    run_toolbelt_command("labs:enable runtime-dyno-metadata", environment)
  end
end
set_heroku_smtp_settings() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 42
def set_heroku_smtp_settings
  %w(staging production).each do |environment|
    run_toolbelt_command("config:add SMTP_ADDRESS=smtp.example.com", environment)
    run_toolbelt_command("config:add SMTP_DOMAIN=example.com", environment)
    run_toolbelt_command("config:add SMTP_USERNAME=username", environment)
    run_toolbelt_command("config:add SMTP_PASSWORD=password", environment)
  end
end

Private Instance Methods

command_to_join_heroku_app(environment) click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 123
      def command_to_join_heroku_app(environment)
        heroku_app_name = heroku_app_name_for(environment)
        <<~SHELL

          if heroku apps | grep #{heroku_app_name} > /dev/null 2>&1; then
            git remote add -f #{environment} git@heroku.com:#{heroku_app_name}.git || true
          else
            echo "** Ask for access to the '#{heroku_app_name}' Heroku app and run this script again **"
          fi
        SHELL
      end
generate_secret() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 143
def generate_secret
  SecureRandom.hex(64)
end
heroku_app_name() click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 135
def heroku_app_name
  app_builder.app_name.dasherize
end
heroku_app_name_for(environment) click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 139
def heroku_app_name_for(environment)
  "#{heroku_app_name}-#{environment}"
end
run_toolbelt_command(command, environment) click to toggle source
# File lib/suspenders/adapters/heroku.rb, line 147
def run_toolbelt_command(command, environment)
  app_builder.run(
    "heroku #{command} --remote #{environment}",
  )
end