Sha256: a26870b24c5ebc8dd8a5ab9839521762871e5ac9d7098264c5c166638754e99c

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

module Hanzo
  class Deploy < Base
    # Classes
    UnknownEnvironment = Class.new(StandardError)
    UninstalledEnvironment = Class.new(StandardError)

  protected

    def initialize_variables
      @env = extract_argument(1)
      @env ||= Hanzo::Installers::Remotes.environments.keys.first
    end

    def initialize_cli
      initialize_help && return if @env.nil?

      deploy && run_after_deploy_commands
    rescue UnknownEnvironment
      Hanzo.unindent_print "Environment `#{@env}` doesn't exist. Add it to .hanzo.yml and run:\n  hanzo install remotes", :red
      Hanzo.unindent_print "\nFor more information, read https://github.com/mirego/hanzo#install-remotes", :red
    rescue UninstalledEnvironment
      Hanzo.unindent_print "Environment `#{@env}` has been found in your .hanzo.yml file. Before using it, you must install it:\n  hanzo install remotes", :red
    end

    def initialize_help
      @options.banner = <<-BANNER.unindent
        Usage: hanzo deploy ENVIRONMENT

        Available environments
      BANNER

      Hanzo::Installers::Remotes.environments.each do |env|
        @options.banner << "  - #{env.first}\n"
      end
    end

    def deploy
      validate_environment_existence!

      branch = Hanzo.ask("Branch to deploy in #{@env}:") { |q| q.default = 'HEAD' }

      Hanzo.run "git push -f #{@env} #{branch}:master"
    end

    def run_after_deploy_commands
      return unless after_deploy_commands.any?

      after_deploy_commands.each do |command|
        next unless Hanzo.agree("Run `#{command}` on #{@env}?")
        Hanzo.run "heroku run #{command} --remote #{@env}"
      end

      Hanzo.run "heroku ps:restart --remote #{@env}"
    end

    def after_deploy_commands
      Hanzo.config['after_deploy'] || []
    end

    def validate_environment_existence!
      raise UnknownEnvironment unless fetcher.exist?
      raise UninstalledEnvironment unless fetcher.installed?
    end

    def fetcher
      @fetcher ||= Hanzo::Fetchers::Environment.new(@env)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanzo-0.6 lib/hanzo/modules/deploy.rb