Sha256: c21cce1cff7ccefa5258e860c3476c40ac10a97b6990a1c2fa040ab408d945be

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

class Fourchette::Heroku
  include Fourchette::Logger

  def app_exists? name
    client.app.list.collect { |app| app if app['name'] == name }.reject(&:nil?).any?
  end

  def fork from, to
    create_app(to)
    copy_config(from, to)
    copy_add_ons(from, to)
    copy_pg(from, to)
  end

  def delete app_name
    logger.info "Deleting #{app_name}"
    client.app.delete(app_name)
  end

  def client
    unless @heroku_client
      api_key = ENV['FOURCHETTE_HEROKU_API_KEY']
      @heroku_client = PlatformAPI.connect(api_key)
    end
    @heroku_client
  end

  def config_vars app_name
    client.config_var.info(app_name)
  end

  def git_url app_name
    client.app.info(app_name)['git_url']
  end

  private
  def create_app name
    logger.info "Creating #{name}"
    client.app.create({ name: name })
  end

  def copy_config from, to
    logger.info "Copying configs from #{from} to #{to}"
    from_congig_vars = config_vars(from)
    # WE SHOULD NOT MOVE THE HEROKU_POSTGRES_*_URL...
    from_congig_vars.reject! { |k, v| k.start_with?('HEROKU_POSTGRESQL_') && k.end_with?('_URL') }
    client.config_var.update(to, from_congig_vars)
  end

  def copy_add_ons from, to
    logger.info "Copying addons from #{from} to #{to}"
    from_addons = client.addon.list(from)
    from_addons.each do |addon|
      name = addon['plan']['name']
      begin
        logger.info "Adding #{name} to #{to}"
        client.addon.create(to, { plan: name })
      rescue Excon::Errors::UnprocessableEntity => e
        logger.error "Failed to copy addon #{name}"
        logger.error e
      end
    end
  end

  def copy_pg from, to
    logger.info "Copying Postgres's data from #{from} to #{to}"
    backup = Fourchette::Pgbackups.new
    backup.copy(from, to)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fourchette-0.0.7 lib/fourchette/heroku.rb
fourchette-0.0.6 lib/fourchette/heroku.rb
fourchette-0.0.5 lib/fourchette/heroku.rb
fourchette-0.0.4 lib/fourchette/heroku.rb
fourchette-0.0.3 lib/fourchette/heroku.rb