Class: Paratrooper::Deploy

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/paratrooper/deploy.rb

Overview

Public: Entry point into the library.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#add_callback, #callbacks

Constructor Details

#initialize(app_name, options = {}, &block) ⇒ Deploy

Public: Initializes a Deploy

app_name - A String naming the Heroku application to be interacted with. options - The Hash options is used to provide additional functionality.

:screen_notifier - Object used for outputting to screen
                   (optional).
:notifiers       - Array of objects interested in being
                   notified of steps in deployment process
                   (optional).
:heroku          - Object wrapper around heroku-api (optional).
:tag             - String name to be used as a git reference
                   point for deploying from specific tag
                   (optional).
:match_tag       - String name of git reference point to match
                   :tag to (optional).
:branch          - String name to be used as a git reference
                   point for deploying from specific branch
                   (optional).
:system_caller   - Object responsible for calling system
                   commands (optional).
:protocol        - String web protocol to be used when pinging
                   application (optional, default: 'http').
:deployment_host - String host name to be used in git URL
                   (optional, default: 'heroku.com').
:migration_check - Object responsible for checking pending
                   migrations (optional).
:maintenance     - If true, show maintenance page when pending
                   migrations exists. False by default (optional).
                   migrations (optional).
:api_key         - String version of heroku api key.
                   (default: looks in local Netrc file).
:http_client     - Object responsible for making http calls
                   (optional).


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/paratrooper/deploy.rb', line 57

def initialize(app_name, options = {}, &block)
  @app_name        = app_name
  @screen_notifier = options[:screen_notifier] || Notifiers::ScreenNotifier.new
  @notifiers       = options[:notifiers] || [@screen_notifier]
  @heroku          = options[:heroku] || HerokuWrapper.new(app_name, options)
  @tag_name        = options[:tag]
  @branch_name     = options[:branch]
  @match_tag_name  = options[:match_tag] || 'master'
  @system_caller   = options[:system_caller] || SystemCaller.new(debug)
  @protocol        = options[:protocol] || 'http'
  @deployment_host = options[:deployment_host] || 'heroku.com'
  @debug           = options[:debug] || false
  @migration_check = options[:migration_check] || PendingMigrationCheck.new(match_tag_name, heroku, system_caller)
  @http_client     = options[:http_client] || HttpClientWrapper.new
  @maintenance     = options[:maintenance] || false

  block.call(self) if block_given?
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def app_name
  @app_name
end

#branch_nameObject

Returns the value of attribute branch_name.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def branch_name
  @branch_name
end

#debugObject

Returns the value of attribute debug.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def debug
  @debug
end

#deployment_hostObject

Returns the value of attribute deployment_host.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def deployment_host
  @deployment_host
end

#herokuObject

Returns the value of attribute heroku.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def heroku
  @heroku
end

#http_clientObject

Returns the value of attribute http_client.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def http_client
  @http_client
end

#maintenanceObject

Returns the value of attribute maintenance.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def maintenance
  @maintenance
end

#match_tag_nameObject

Returns the value of attribute match_tag_name.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def match_tag_name
  @match_tag_name
end

#migration_checkObject

Returns the value of attribute migration_check.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def migration_check
  @migration_check
end

#notifiersObject

Returns the value of attribute notifiers.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def notifiers
  @notifiers
end

#protocolObject

Returns the value of attribute protocol.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def protocol
  @protocol
end

#screen_notifierObject

Returns the value of attribute screen_notifier.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def screen_notifier
  @screen_notifier
end

#system_callerObject

Returns the value of attribute system_caller.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def system_caller
  @system_caller
end

#tag_nameObject

Returns the value of attribute tag_name.



15
16
17
# File 'lib/paratrooper/deploy.rb', line 15

def tag_name
  @tag_name
end

Instance Method Details

#activate_maintenance_modeObject

Public: Activates Heroku maintenance mode.



95
96
97
98
99
100
101
# File 'lib/paratrooper/deploy.rb', line 95

def activate_maintenance_mode
  return unless maintenance && pending_migrations?
  callback(:activate_maintenance_mode) do
    notify(:activate_maintenance_mode)
    heroku.app_maintenance_on
  end
end

#add_remote_task(task_name) ⇒ Object

Public: Runs task on your heroku instance.

task_name - String name of task to run on heroku instance



206
207
208
# File 'lib/paratrooper/deploy.rb', line 206

def add_remote_task(task_name)
  heroku.run_task(task_name)
end

#app_restartObject

Public: Restarts application on Heroku.



156
157
158
159
160
161
162
# File 'lib/paratrooper/deploy.rb', line 156

def app_restart
  return unless restart_required?
  callback(:app_restart) do
    notify(:app_restart)
    heroku.app_restart
  end
end

#deactivate_maintenance_modeObject

Public: Deactivates Heroku maintenance mode.



105
106
107
108
109
110
111
# File 'lib/paratrooper/deploy.rb', line 105

def deactivate_maintenance_mode
  return unless pending_migrations?
  callback(:deactivate_maintenance_mode) do
    notify(:deactivate_maintenance_mode)
    heroku.app_maintenance_off
  end
end

#default_deployObject Also known as: deploy

Public: Execute common deploy steps.

Default deploy consists of:

  • Activating maintenance page

  • Updating repository tag

  • Pushing repository to Heroku

  • Running database migrations

  • Restarting application on Heroku

  • Deactivating maintenance page

  • Accessing application URL to warm Heroku dyno

Alias: #deploy



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/paratrooper/deploy.rb', line 189

def default_deploy
  setup
  update_repo_tag
  push_repo
  maintenance_mode do
    run_migrations
    app_restart
  end
  warm_instance
  teardown
end

#maintenance_mode(&block) ⇒ Object



113
114
115
116
117
# File 'lib/paratrooper/deploy.rb', line 113

def maintenance_mode(&block)
  activate_maintenance_mode
  block.call if block_given?
  deactivate_maintenance_mode
end

#push_repoObject

Public: Pushes repository to Heroku.

Based on the following precedence: branch_name / tag_name / ‘master’



136
137
138
139
140
141
142
# File 'lib/paratrooper/deploy.rb', line 136

def push_repo
  reference_point = git_branch_name || git_tag_name || 'master'
  callback(:push_repo) do
    notify(:push_repo, reference_point: reference_point)
    system_call "git push -f #{deployment_remote} #{reference_point}:refs/heads/master"
  end
end

#run_migrationsObject

Public: Runs rails database migrations on your application.



146
147
148
149
150
151
152
# File 'lib/paratrooper/deploy.rb', line 146

def run_migrations
  return unless pending_migrations?
  callback(:run_migrations) do
    notify(:run_migrations)
    heroku.run_migrations
  end
end

#setupObject

Public: Hook method called first in the deploy process.



78
79
80
81
82
83
# File 'lib/paratrooper/deploy.rb', line 78

def setup
  callback(:setup) do
    notify(:setup)
    migration_check.last_deployed_commit
  end
end

#teardownObject

Public: Hook method called last in the deploy process.



87
88
89
90
91
# File 'lib/paratrooper/deploy.rb', line 87

def teardown
  callback(:teardown) do
    notify(:teardown)
  end
end

#update_repo_tagObject

Public: Creates a git tag and pushes it to repository.



121
122
123
124
125
126
127
128
129
# File 'lib/paratrooper/deploy.rb', line 121

def update_repo_tag
  unless tag_name.nil? || tag_name.empty?
    callback(:update_repo_tag) do
      notify(:update_repo_tag)
      system_call "git tag #{tag_name} #{match_tag_name} -f"
      system_call "git push -f origin #{tag_name}"
    end
  end
end

#warm_instance(wait_time = 3) ⇒ Object

Public: cURL for application URL to start your Heroku dyno.

wait_time - Integer length of time (seconds) to wait before making call

to app


169
170
171
172
173
174
175
# File 'lib/paratrooper/deploy.rb', line 169

def warm_instance(wait_time = 3)
  callback(:warm_instance) do
    notify(:warm_instance)
    sleep wait_time
    http_client.get("#{protocol}://#{app_url}")
  end
end