Class: GitPusshuTen::Commands::Passenger

Inherits:
Base
  • Object
show all
Defined in:
lib/gitpusshuten/modules/passenger/command.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#cli, #command, #configuration, #environment, #hooks, #perform_hooks

Instance Method Summary collapse

Methods inherited from Base

#c, #command_object, description, #e, #error, example, #g, #git, #help, #local, long_description, #message, #perform!, #perform_hooks!, #post_perform!, #pre_perform!, #prompt_for_root_password!, #prompt_for_user_password!, #r, #requires_user_existence!, #silent, #standard, usage, #validate!, #warning, #y, #yes?

Constructor Details

#initialize(*objects) ⇒ Passenger

Returns a new instance of Passenger.



15
16
17
18
19
20
21
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 15

def initialize(*objects)
  super
  
  @command = cli.arguments.shift
  
  help if command.nil? or e.name.nil?
end

Instance Attribute Details

#webserverObject

Contains the webserver we’re working with Either NginX or Apache



13
14
15
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 13

def webserver
  @webserver
end

Instance Method Details

#apache?Boolean

Returns true if we’re working with Apache

Returns:

  • (Boolean)


268
269
270
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 268

def apache?
  webserver == 'Apache'
end

#find_apache2_conf!Object

Finds and sets the Apache2 Conf path



300
301
302
303
304
305
306
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 300

def find_apache2_conf!
  ##
  # Apache2 Conf path you get from Aptitude
  if e.file?('/etc/apache2/apache2.conf')
    @apache2_conf = '/etc/apache2/apache2.conf'
  end
end

#find_nginx_conf!Object

Finds and sets the NginX Conf path



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 284

def find_nginx_conf!
  ##
  # NginX Conf path you get from Passenger
  if e.file?('/etc/nginx/conf/nginx.conf')
    @nginx_conf = '/etc/nginx/conf/nginx.conf'
  end
  
  ##
  # NginX Conf path you get from Aptitude
  if e.file?('/etc/nginx/nginx.conf')
    @nginx_conf = '/etc/nginx/nginx.conf'
  end
end

#nginx?Boolean

Returns true if we’re working with NginX

Returns:

  • (Boolean)


262
263
264
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 262

def nginx?
  webserver == 'NginX'
end

#perform_install!Object

Installs Phusion Passenger



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 32

def perform_install!
  
  if not e.installed?('gem')
    error "Could not find RubyGems."
    error "Install RVM (Ruby Version Manager) and at least one Ruby version."
    error "To do this, run: #{y("heavenly rvm install for #{e.name}")}."
    exit
  end
  
  ##
  # If no web server is specified, it'll prompt the user to
  # select one of the available (NginX or Apache)
  if webserver.nil?
    message "For which web server would you like to install #{y('Phusion Passenger')}?"
    @webserver = webserver?
  end
  
  message "Starting #{y('Phusion Passenger')} installation for #{y(webserver)}!"
          
  ##
  # Install the latest Passenger Gem
  Spinner.return :message => "Installing latest Phusion Passenger Gem.." do
    e.execute_as_root("gem install passenger --no-ri --no-rdoc")
    g("Done!")
  end
  
  ##
  # Install dependencies for Passenger
  Spinner.return :message => "Ensuring #{y('Phusion Passenger')} dependencies are installed.." do
    
    ##
    # Install dependencies for installing NginX
    if nginx?
      e.install!("libcurl4-openssl-dev")
    end
    
    ##
    # Install dependencies for installing Apache
    if apache?
      e.install!("libcurl4-openssl-dev apache2-mpm-prefork apache2-prefork-dev libapr1-dev libaprutil1-dev")
    end
    
    g("Done!")
  end
  
  if not @updating
    standard "Installing #{y('Phusion Passenger')} and #{y(webserver)}."
  else
    standard "Updating #{y('Phusion Passenger')} and #{y(webserver)}."
  end
  
  Spinner.return :message => "This may take a while.." do
    
    ##
    # Run the Passenger NginX installation module if we're working with NginX
    if nginx?
      e.execute_as_root("passenger-install-nginx-module --auto --auto-download --prefix=/etc/nginx")
    end
    
    ##
    # Run the Passenger Apache installation module if we're working with Apache
    if apache?
      e.execute_as_root("passenger-install-apache2-module --auto")
    end
    
    g("Done!")
  end
  
  ##
  # Configures NginX to setup a managable vhost environment like Apache2
  if nginx?
    GitPusshuTen::Initializer.new("nginx", "setup", "#{e.name}", "environment")
  end
  
  ##
  # Inject the Passenger paths into the Apache2 configuration file
  if apache?
    Spinner.return :message => "Configuring Apache for Phusion Passenger.." do
      if not e.execute_as_root('cat /etc/apache2/apache2.conf').include?("passenger_module")
        if e.execute_as_root('passenger-config --root') =~ /\/usr\/local\/rvm\/gems\/(.+)\/gems\/passenger-.+/
          @ruby_version      = $1.chomp.strip
          @passenger_version = e.execute_as_root('passenger-config --version').chomp.strip
        end
        
        e.execute_as_root <<-PASSENGER
cat <<-CONFIG >> /etc/apache2/apache2.conf

LoadModule passenger_module /usr/local/rvm/gems/#{@ruby_version}/gems/passenger-#{@passenger_version}/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/rvm/gems/#{@ruby_version}/gems/passenger-#{@passenger_version}
PassengerRuby /usr/local/rvm/wrappers/#{@ruby_version}/ruby

CONFIG
        PASSENGER
      end
      g('Done!')
    end # spinner
  end
  
  if not @updating
    message "#{y('Phusion Passenger')} and #{y(webserver)} have been installed!"
  else
    message "#{y('Phusion Passenger')} and #{y(webserver)} have been updated!"
  end
  
  if nginx?
    message "NginX directory: #{y('/etc/nginx')}"
  end
  
  if apache?
    message "Apache directory: #{y('/etc/apache2')}"
    GitPusshuTen::Initializer.new('apache', 'create-vhost', 'for', "#{e.name}")
  end
end

#perform_restart!Object

Restarts a Passenger instance for the specified environment



25
26
27
28
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 25

def perform_restart!
  message "Restarting Passenger for #{y(c.application)} (#{y(e.name)} environment)."
  e.execute_as_user("cd #{e.app_dir}; mkdir -p tmp; touch tmp/restart.txt")
end

#perform_update!Object

Updates the Passenger Gem and NginX or Apache2 itself Compares the currently installed Passenger Gem with the latest (stable) version on RubyGems.org to see if anything newer than the current version is out. If there is, then it will continue to update NginX/Apache2 using Phussions’s NginX/Apache2 installation module.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 151

def perform_update!
  prompt_for_root_password!
  
  ##
  # If no web server is specified, it'll prompt the user to
  # select one of the available (NginX or Apache)
  if webserver.nil?
    @webserver = webserver?
  end
  
  ##
  # Check if RubyGems (RVM + Ruby) has been installed
  Spinner.return :message => "Checking if RubyGems is installed.." do
    @rubygems_installed = e.installed?('gem')
    if not @rubygems_installed
      r("Couldn't find RubyGems.")
    else
      g("Done!")
    end
  end
  
  if not @rubygems_installed
    error "Install RVM (Ruby Version Manager) and at least one Ruby version."
    error "To do this, run: #{y("heavenly rvm install for #{e.name}")}."
    exit
  end
  
  ##
  # Check if Phusion Passenger is installed
  Spinner.return :message => "Checking if Phusion Passenger is installed.." do
    @passenger_installed = e.installed?("passenger")
    if not @passenger_installed
      @ruby_version = e.execute_as_root("ruby -v").chomp
      r("Couldn't find Phusion Passenger")
    else
      g("Found!")
    end
  end
  
  if not @passenger_installed
    error "Passenger has not been installed for #{y(@ruby_version)}"
    error "If you want to install Passenger, please run the following command:"
    error y("heavenly passenger install for #{e.name}")
    exit
  end
  
  ##
  # Check if a newer version of Phusion Passenger Gem is available
  Spinner.return :message => "Checking if there's a newer (stable) Phusion Passenger available.." do
    @latest_passenger_version  = GitPusshuTen::Gem.new(:passenger).latest_version
    @current_passenger_version = e.execute_as_root("passenger-config --version").chomp.strip
    if @latest_passenger_version > @current_passenger_version
      @new_passenger_version_available = true
      y("There appears to be a newer version of Phusion Passenger available!")
    else
      g("Your Phusion Passenger is #{@current_passenger_version}. This is the latest version.")
    end
  end
  
  exit unless @new_passenger_version_available
  
  message "Phusion Passenger #{y(@latest_passenger_version)} is out!"
  message "You are currently using version #{y(@current_passenger_version)}.\n\n"
  message "Would you like to update Phusion Passenger?"
  message "This will update the #{y('Phusion Passenger Gem')} as well as #{y(webserver)} and #{y("Phusion Passenger's #{webserver} Module")}."
  
  ##
  # Prompt user for confirmation for the update
  if yes?
    
    ##
    # Search for NginX installation directory by finding the configuration file
    if nginx?
      find_nginx_conf!
      
      if not @nginx_conf
        error "Could not find the NginX configuration file in #{y('/etc/nginx')} or #{y('/etc/nginx/conf')}."
        exit 
      end
    end
    
    ##
    # Ensures the Apache2 configuration file exists
    if apache?
      find_apache2_conf!
      
      if not @apache2_conf
        error "Could not find the Apache2 configuration file in #{y('/etc/apache2')}."
        exit
      end
    end
    
    ##
    # Installation directory has been found
    message "#{y(webserver)} installation found in #{y(@nginx_conf || @apache2_conf)}."
    
    ##
    # Invoke the installation command to install NginX
    @updating = true
    perform_install!
    
    ##
    # Update the webserver configuration file
    message "The #{y(webserver)} configuration file needs to be updated with the new #{y('Passenger')} version."
    message "Invoking #{y("heavenly #{webserver.downcase} update-configuration for #{e.name}")} for you..\n\n\n"
    GitPusshuTen::Initializer.new([webserver.downcase, 'update-configuration', 'for', "#{e.name}"])
  end
end

#webserver?Boolean

Prompts the user to select a webserver

Returns:

  • (Boolean)


274
275
276
277
278
279
280
# File 'lib/gitpusshuten/modules/passenger/command.rb', line 274

def webserver?
  choose do |menu|
    menu.prompt = ''
    menu.choice('NginX')
    menu.choice('Apache')
  end
end