Class: Heroku::Command::App

Inherits:
Base
  • Object
show all
Defined in:
lib/heroku/commands/app.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #autodetected_app

Instance Method Summary collapse

Methods inherited from Base

#app_urls, #ask, #display, #error, #escape, #extract_app, #extract_app_in_dir, #extract_option, #format_date, #git_remotes, #git_url, #heroku, #initialize, #shell, #web_url

Methods included from Helpers

#home_directory, #running_on_a_mac?, #running_on_windows?

Constructor Details

This class inherits a constructor from Heroku::Command::Base

Instance Method Details

#consoleObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/heroku/commands/app.rb', line 122

def console
	app = extract_app
	cmd = args.join(' ').strip
	if cmd.empty?
		console_session(app)
	else
		display heroku.console(app, cmd)
	end
rescue RestClient::RequestTimeout
	error "Timed out. Long running requests are not supported on the console.\nPlease consider creating a rake task instead."
rescue Heroku::Client::AppCrashed => e
	error "Couldn't run console command\n#{e.message}"
end

#console_session(app) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/heroku/commands/app.rb', line 136

def console_session(app)
	heroku.console(app) do |console|
		console_history_read(app)

		display "Ruby console for #{app}.#{heroku.host}"
		while cmd = Readline.readline('>> ')
			unless cmd.nil? || cmd.strip.empty?
				console_history_add(app, cmd)
				break if cmd.downcase.strip == 'exit'
				display console.run(cmd)
			end
		end
	end
end

#createObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/heroku/commands/app.rb', line 21

def create
	remote  = extract_option('--remote', 'heroku')
	name    = args.shift.downcase.strip rescue nil
	name    = heroku.create(name, {})
	display "Created #{app_urls(name)}"
	if remote || File.exists?(Dir.pwd + '/.git')
		remote ||= 'heroku'
		return if shell('git remote').split("\n").include?(remote)
		shell "git remote add #{remote} git@#{heroku.host}:#{name}.git"
		display "Git remote #{remote} added"
	end
end

#destroyObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/heroku/commands/app.rb', line 179

def destroy
	if name = extract_option('--app')
		info = heroku.info(name)
		url  = info[:domain_name] || "http://#{info[:name]}.#{heroku.host}/"
		conf = nil

		display("Permanently destroy #{url} (y/n)? ", false)
		if ask.downcase == 'y'
			heroku.destroy(name)
			if remotes = git_remotes(Dir.pwd)
				remotes.each do |remote_name, remote_app|
					next if name != remote_app
					shell "git remote rm #{remote_name}"
				end
			end
			display "Destroyed #{name}"
		end
	else
		display "Set the app you want to destroy adding --app <app name> to this command"
	end
end

#dynosObject



157
158
159
160
161
162
163
164
165
166
# File 'lib/heroku/commands/app.rb', line 157

def dynos
	app = extract_app
	if dynos = args.shift
		current = heroku.set_dynos(app, dynos)
		display "#{app} now running on #{current} dyno#{'s' if current > 1}"
	else
		info = heroku.info(app)
		display "#{app} is running on #{info[:dynos]} dyno#{'s' if info[:dynos].to_i > 1}"
	end
end

#infoObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/heroku/commands/app.rb', line 54

def info
	name = (args.first && !args.first =~ /^\-\-/) ? args.first : extract_app
	attrs = heroku.info(name)
	
	attrs[:web_url] ||= "http://#{attrs[:name]}.#{heroku.host}/"
	attrs[:git_url] ||= "git@#{heroku.host}:#{attrs[:name]}.git"
	
	display "=== #{attrs[:name]}"
	display "Web URL:        #{attrs[:web_url]}"
	display "Domain name:    http://#{attrs[:domain_name]}/" if attrs[:domain_name]
	display "Git Repo:       #{attrs[:git_url]}"
	display "Dynos:          #{attrs[:dynos]}"
	display "Workers:        #{attrs[:workers]}"
	display "Repo size:      #{format_bytes(attrs[:repo_size])}" if attrs[:repo_size]
	display "Slug size:      #{format_bytes(attrs[:slug_size])}" if attrs[:slug_size]
	if attrs[:database_size]
		data = format_bytes(attrs[:database_size])
		if tables = attrs[:database_tables]
			data = data.gsub('(empty)', '0K') + " in #{tables} table#{'s' if tables.to_i > 1}"
		end
		display "Data size:      #{data}"
	end

	if attrs[:cron_next_run]
		display "Next cron:      #{format_date(attrs[:cron_next_run])} (scheduled)"
	end
	if attrs[:cron_finished_at]
		display "Last cron:      #{format_date(attrs[:cron_finished_at])} (finished)"
	end

	unless attrs[:addons].empty?
		display "Addons:         " + attrs[:addons].map { |a| a['description'] }.join(', ')
	end

	display "Owner:          #{attrs[:owner]}"
	collaborators = attrs[:collaborators].delete_if { |c| c[:email] == attrs[:owner] }
	unless collaborators.empty?
		first = true
		lead = "Collaborators:"
		attrs[:collaborators].each do |collaborator|
			display "#{first ? lead : ' ' * lead.length}  #{collaborator[:email]}"
			first = false
		end
	end
end

#listObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/heroku/commands/app.rb', line 6

def list
	list = heroku.list
	if list.size > 0
		display list.map {|name, owner|
			if heroku.user == owner
				name
			else
				"#{name.ljust(25)} #{owner}"
			end
		}.join("\n")
	else
		display "You have no apps."
	end
end

#openObject



100
101
102
103
104
105
106
# File 'lib/heroku/commands/app.rb', line 100

def open
	app = extract_app

	url = web_url(app)
	puts "Opening #{url}"
	Launchy.open url
end

#rakeObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/heroku/commands/app.rb', line 108

def rake
	app = extract_app
	cmd = args.join(' ')
	if cmd.length == 0
		display "Usage: heroku rake <command>"
	else
		heroku.start(app, "rake #{cmd}", attached=true).each do |chunk|
			display chunk, false
		end
	end
rescue Heroku::Client::AppCrashed => e
	error "Couldn't run rake\n#{e.message}"
end

#renameObject

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/heroku/commands/app.rb', line 34

def rename
	name    = extract_app
	newname = args.shift.downcase.strip rescue ''
	raise(CommandFailed, "Invalid name.") if newname == ''

	heroku.update(name, :name => newname)
	display app_urls(newname)

	if remotes = git_remotes(Dir.pwd)
		remotes.each do |remote_name, remote_app|
			next if remote_app != name
			shell "git remote rm #{remote_name}"
			shell "git remote add #{remote_name} git@#{heroku.host}:#{newname}.git"
			display "Git remote #{remote_name} updated"
		end
	else
		display "Don't forget to update your Git remotes on any local checkouts."
	end
end

#restartObject



151
152
153
154
155
# File 'lib/heroku/commands/app.rb', line 151

def restart
	app_name = extract_app
	heroku.restart(app_name)
	display "Servers restarted"
end

#workersObject



168
169
170
171
172
173
174
175
176
177
# File 'lib/heroku/commands/app.rb', line 168

def workers
	app = extract_app
	if workers = args.shift
		current = heroku.set_workers(app, workers)
		display "#{app} now running #{current} worker#{'s' if current != 1}"
	else
		info = heroku.info(app)
		display "#{app} is running #{info[:workers]} worker#{'s' if info[:workers].to_i != 1}"
	end
end