Class: Hatchet::App
- Inherits:
-
Object
- Object
- Hatchet::App
- Defined in:
- lib/hatchet.rb,
lib/hatchet/app.rb
Defined Under Namespace
Classes: FailedDeploy
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#repo_name ⇒ Object
readonly
Returns the value of attribute repo_name.
Class Method Summary collapse
-
.config ⇒ Object
config is read only, should be threadsafe.
Instance Method Summary collapse
- #add_database(db_name = 'heroku-postgresql:dev', match_val = "HEROKU_POSTGRESQL_[A-Z]+_URL") ⇒ Object
- #api_key ⇒ Object
- #config ⇒ Object
-
#debug? ⇒ Boolean
(also: #debugging?)
set debug: true when creating app if you don’t want it to be automatically destroyed, useful for debugging…bad for app limits.
-
#deploy(&block) ⇒ Object
creates a new app on heroku, “pushes” via anvil or git then yields to self so you can call self.run or self.deployed? Allow deploy failures on CI server by setting ENV.
- #deployed? ⇒ Boolean
- #get_config ⇒ Object
- #get_labs ⇒ Object
- #heroku ⇒ Object
- #in_directory(directory = self.directory) ⇒ Object
-
#initialize(repo_name, options = {}) ⇒ App
constructor
A new instance of App.
- #lab_is_installed?(lab) ⇒ Boolean
- #not_debugging? ⇒ Boolean (also: #no_debug?)
- #output ⇒ Object
- #push ⇒ Object (also: #push!, #push_with_retry)
- #push_without_retry! ⇒ Object
- #retry_error_message(error, attempt, max_retries) ⇒ Object
-
#run(command, timeout = nil, &block) ⇒ Object
runs a command on heroku similar to ‘$ heroku run #foo` but programatically and with more control.
- #set_config(options = {}) ⇒ Object
- #set_lab(lab) ⇒ Object
- #set_labs! ⇒ Object
-
#setup! ⇒ Object
creates a new heroku app via the API.
- #teardown! ⇒ Object
Constructor Details
#initialize(repo_name, options = {}) ⇒ App
Returns a new instance of App.
15 16 17 18 19 20 21 22 |
# File 'lib/hatchet/app.rb', line 15 def initialize(repo_name, = {}) @repo_name = repo_name @directory = config.path_for_name(@repo_name) @name = [:name] || "test-app-#{Time.now.to_f}".gsub('.', '-') @debug = [:debug] || [:debugging] @allow_failure = [:allow_failure] || false @labs = ([] << [:labs]).flatten.compact end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
3 4 5 |
# File 'lib/hatchet/app.rb', line 3 def directory @directory end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/hatchet/app.rb', line 3 def name @name end |
#repo_name ⇒ Object (readonly)
Returns the value of attribute repo_name.
3 4 5 |
# File 'lib/hatchet/app.rb', line 3 def repo_name @repo_name end |
Class Method Details
Instance Method Details
#add_database(db_name = 'heroku-postgresql:dev', match_val = "HEROKU_POSTGRESQL_[A-Z]+_URL") ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/hatchet/app.rb', line 59 def add_database(db_name = 'heroku-postgresql:dev', match_val = "HEROKU_POSTGRESQL_[A-Z]+_URL") Hatchet::RETRIES.times.retry do heroku.post_addon(name, db_name) _, value = get_config.detect {|k, v| k.match(/#{match_val}/) } set_config('DATABASE_URL' => value) end end |
#api_key ⇒ Object
170 171 172 |
# File 'lib/hatchet/app.rb', line 170 def api_key @api_key ||= ENV['HEROKU_API_KEY'] || bundle_exec {`heroku auth:token`.chomp } end |
#config ⇒ Object
29 30 31 |
# File 'lib/hatchet/app.rb', line 29 def config self.class.config end |
#debug? ⇒ Boolean Also known as: debugging?
set debug: true when creating app if you don’t want it to be automatically destroyed, useful for debugging…bad for app limits. turn on global debug by setting HATCHET_DEBUG=true in the env
83 84 85 |
# File 'lib/hatchet/app.rb', line 83 def debug? @debug || ENV['HATCHET_DEBUG'] || false end |
#deploy(&block) ⇒ Object
creates a new app on heroku, “pushes” via anvil or git then yields to self so you can call self.run or self.deployed? Allow deploy failures on CI server by setting ENV
132 133 134 135 136 137 138 139 140 |
# File 'lib/hatchet/app.rb', line 132 def deploy(&block) in_directory do self.setup! self.push_with_retry! block.call(self, heroku, output) if block.present? end ensure self.teardown! end |
#deployed? ⇒ Boolean
93 94 95 |
# File 'lib/hatchet/app.rb', line 93 def deployed? !heroku.get_ps(name).body.detect {|ps| ps["process"].include?("web") }.nil? end |
#get_config ⇒ Object
39 40 41 |
# File 'lib/hatchet/app.rb', line 39 def get_config heroku.get_config_vars(name).body end |
#get_labs ⇒ Object
47 48 49 |
# File 'lib/hatchet/app.rb', line 47 def get_labs heroku.get_features(name).body end |
#heroku ⇒ Object
174 175 176 |
# File 'lib/hatchet/app.rb', line 174 def heroku @heroku ||= Heroku::API.new(api_key: api_key) end |
#in_directory(directory = self.directory) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/hatchet/app.rb', line 119 def in_directory(directory = self.directory) Dir.mktmpdir do |tmpdir| FileUtils.cp_r("#{directory}/.", "#{tmpdir}/.") Dir.chdir(tmpdir) do yield directory end end end |
#lab_is_installed?(lab) ⇒ Boolean
43 44 45 |
# File 'lib/hatchet/app.rb', line 43 def lab_is_installed?(lab) get_labs.any? {|hash| hash["name"] == lab } end |
#not_debugging? ⇒ Boolean Also known as: no_debug?
88 89 90 |
# File 'lib/hatchet/app.rb', line 88 def not_debugging? !debug? end |
#output ⇒ Object
166 167 168 |
# File 'lib/hatchet/app.rb', line 166 def output @output end |
#push ⇒ Object Also known as: push!, push_with_retry
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/hatchet/app.rb', line 143 def push max_retries = @allow_failure ? 1 : RETRIES max_retries.times.retry do |attempt| begin @output = self.push_without_retry! rescue StandardError => error puts (error, attempt, max_retries) raise error end end end |
#push_without_retry! ⇒ Object
106 107 108 |
# File 'lib/hatchet/app.rb', line 106 def push_without_retry! raise NotImplementedError end |
#retry_error_message(error, attempt, max_retries) ⇒ Object
159 160 161 162 163 164 |
# File 'lib/hatchet/app.rb', line 159 def (error, attempt, max_retries) attempt += 1 return "" if attempt == max_retries msg = "\nRetrying failed Attempt ##{attempt}/#{max_retries} to push for '#{name}' due to error: \n"<< "#{error.class} #{error.}\n #{error.backtrace.join("\n ")}" end |
#run(command, timeout = nil, &block) ⇒ Object
runs a command on heroku similar to ‘$ heroku run #foo` but programatically and with more control
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/hatchet/app.rb', line 69 def run(command, timeout = nil, &block) heroku_command = "heroku run #{command} -a #{name}" bundle_exec do return `#{heroku_command}` if block.blank? end bundle_exec do ReplRunner.new(command, heroku_command, startup_timeout: timeout).run(&block) end end |
#set_config(options = {}) ⇒ Object
33 34 35 36 37 |
# File 'lib/hatchet/app.rb', line 33 def set_config( = {}) .each do |key, value| heroku.put_config_vars(name, key => value) end end |
#set_lab(lab) ⇒ Object
55 56 57 |
# File 'lib/hatchet/app.rb', line 55 def set_lab(lab) heroku.post_feature(lab, name) end |
#set_labs! ⇒ Object
51 52 53 |
# File 'lib/hatchet/app.rb', line 51 def set_labs! @labs.each {|lab| set_lab(lab) } end |
#setup! ⇒ Object
creates a new heroku app via the API
98 99 100 101 102 103 104 |
# File 'lib/hatchet/app.rb', line 98 def setup! return self if @app_is_setup heroku.post_app(name: name) set_labs! @app_is_setup = true self end |
#teardown! ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/hatchet/app.rb', line 110 def teardown! return false unless @app_is_setup if debugging? puts "Debugging App:#{name}" return false end heroku.delete_app(name) end |