Class: GitPusshuTen::Commands::Apache

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

Instance Attribute Summary

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) ⇒ Apache

Returns a new instance of Apache.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitpusshuten/modules/apache/command.rb', line 21

def initialize(*objects)
  super
  
  @command = cli.arguments.shift
  
  help if command.nil? or e.name.nil?
  
  ##
  # Default Configuration
  @installation_dir         = "/etc/apache2"
  @configuration_directory = @installation_dir
  @configuration_file      = File.join(@configuration_directory, 'apache2.conf')
end

Instance Method Details

#create_vhost_template_file!Object

Creates a vhost template file if it doesn’t already exist.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/gitpusshuten/modules/apache/command.rb', line 338

def create_vhost_template_file!
  FileUtils.mkdir_p(File.join(local.gitpusshuten_dir, 'apache'))
  vhost_file  = File.join(local.gitpusshuten_dir, 'apache', "#{e.name}.vhost")
  
  create_file = true
  if File.exist?(vhost_file)
    warning "#{y(vhost_file)} already exists, do you want to overwrite it?"
    create_file = yes?
  end
  
  if create_file
    File.open(vhost_file, 'w') do |file|
      file << "<VirtualHost *:80>\n"
      file << "\s\sServerName mydomain.com\n"
      file << "\s\sServerAlias www.mydomain.com\n"
      file << "\s\sDocumentRoot #{e.app_dir}/public\n"
      file << "\s\s<Directory #{e.app_dir}/public>\n"
      file << "\s\s\s\sAllowOverride all\n"
      file << "\s\s\s\sOptions -MultiViews\n"
      file << "\s\s</Directory>\n"
      file << "</VirtualHost>\n"
    end
    message "The vhost has been created in #{y(vhost_file)}."
  end
end

#perform_create_vhost!Object

Creates a vhost



196
197
198
# File 'lib/gitpusshuten/modules/apache/command.rb', line 196

def perform_create_vhost!
  create_vhost_template_file!
end

#perform_delete_vhost!Object

Deletes a vhost



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/gitpusshuten/modules/apache/command.rb', line 182

def perform_delete_vhost!
  vhost_file = File.join(@configuration_directory, 'sites-enabled', "#{e.sanitized_app_name}.#{e.name}.vhost")
  if e.file?(vhost_file) # prompts root
    message "Deleting #{y(vhost_file)}!"
    e.execute_as_root("rm #{vhost_file}")
    perform_reload!
  else
    message "#{y(vhost_file)} does not exist."
    exit
  end
end

#perform_download_config!Object

Alias to perform_download_configuration!



103
104
105
# File 'lib/gitpusshuten/modules/apache/command.rb', line 103

def perform_download_config!
  perform_download_configuration!
end

#perform_download_configuration!Object

Downloads the Apache2 configuration file



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gitpusshuten/modules/apache/command.rb', line 87

def perform_download_configuration!
  if not e.file?('/etc/apache2/apache2.conf')
    error "Could not find the Apache2 configuration file in #{y('/etc/apache2/apache2.conf')}"
    exit
  end
  
  local_apache_dir = File.join(local.gitpusshuten_dir, 'apache')
  FileUtils.mkdir_p(local_apache_dir)
  Spinner.return :message => "Downloading Apache2 configuration file to #{y(local_apache_dir)}.." do
    e.scp_as_root(:download, "/etc/apache2/apache2.conf", local_apache_dir)
    g('Done!')
  end
end

#perform_download_vhost!Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/gitpusshuten/modules/apache/command.rb', line 133

def perform_download_vhost!
  remote_vhost = File.join(@configuration_directory, "sites-enabled", "#{e.sanitized_app_name}.#{e.name}.vhost")
  if not e.file?(remote_vhost) #prompts root
    error "There is no vhost currently present in #{y(remote_vhost)}."
    exit
  end
  
  FileUtils.mkdir_p(File.join(local.gitpusshuten_dir, 'apache'))
  local_vhost = File.join(local.gitpusshuten_dir, 'apache', "#{e.name}.vhost")
  if File.exist?(local_vhost)
    warning "#{y(local_vhost)} already exists. Do you want to overwrite it?"
    exit unless yes?
  end
  
  Spinner.return :message => "Downloading vhost.." do
    e.scp_as_root(:download, remote_vhost, local_vhost)
    g("Finished downloading!")
  end
  message "You can find the vhost in: #{y(local_vhost)}."
end

#perform_install!Object

Installs the Apache2 web server



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitpusshuten/modules/apache/command.rb', line 37

def perform_install!
  warning "If you are planning to use #{y('Ruby')} and #{y('Passenger')} then #{r("DON'T")} use this Apache2 installer."
  warning "Instead, use the Passenger module to install it."
  standard "\n\s\s#{y("heavenly passenger install to #{y(e.name)}")}\n\n"
  
  message "If you do not plan on using #{y('Ruby')} on this server, then this stand-alone installation should be fine."
  message "Do you want to continue?"
  exit unless yes?
  
  prompt_for_root_password!
  
  Spinner.return :message => "Installing Apache2 web server.." do
   e.install!('apache2')
    g('Done!')
  end
  message "Apache2 has been installed in #{y('/etc/apache2')}."
  message "Creating #{y('Apache')} vhost template."
  GitPusshuTen::Initializer.new('apache', 'create-vhost', 'for', "#{e.name}")
end

#perform_reload!Object

Reload Apache



80
81
82
83
# File 'lib/gitpusshuten/modules/apache/command.rb', line 80

def perform_reload!
  message "Reloading Apache Configuration."
  puts e.execute_as_root("/etc/init.d/apache2 reload")
end

#perform_restart!Object

Restarts Apache



73
74
75
76
# File 'lib/gitpusshuten/modules/apache/command.rb', line 73

def perform_restart!
  message "Restarting Apache."
  puts e.execute_as_root("/etc/init.d/apache2 restart")
end

#perform_start!Object

Starts Apache



59
60
61
62
# File 'lib/gitpusshuten/modules/apache/command.rb', line 59

def perform_start!
  message "Starting Apache."
  puts e.execute_as_root("/etc/init.d/apache2 start")
end

#perform_stop!Object

Stops Apache



66
67
68
69
# File 'lib/gitpusshuten/modules/apache/command.rb', line 66

def perform_stop!
  message "Stopping Apache."
  puts e.execute_as_root("/etc/init.d/apache2 stop")
end

#perform_update_configuration!Object

Performs the Update Configuration command This is particularly used when you change Passenger or Ruby versions so these are updated in the apache2.conf file.



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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/gitpusshuten/modules/apache/command.rb', line 204

def perform_update_configuration!
  message "Checking the #{y(@configuration_file)} for current Passenger configuration."
  config_contents = e.execute_as_root("cat '#{@configuration_file}'") # prompts root
  if not config_contents.include? 'PassengerRoot' or not config_contents.include?('PassengerRuby') or not config_contents.include?('passenger_module')
    error "Could not find Passenger configuration, has it ever been set up?"
    exit
  end
  
  message "Checking if Passenger is installed under the #{y('default')} Ruby."
  if not e.installed?('passenger')
    message "Passenger isn't installed for the current Ruby"
    Spinner.return :message => "Installing latest Phusion Passenger Gem.." do
      e.execute_as_root('gem install passenger --no-ri --no-rdoc')
      g("Done!")
    end
  end
  
  Spinner.return :message => "Finding current Phusion Passenger Gem version..." do
    if e.execute_as_root('passenger-config --version') =~ /(\d+\.\d+\..+)/
      @passenger_version = $1.chomp.strip
      g('Found!')
    else
      r('Could not find the current Passenger version.')
    end
  end
  
  exit if @passenger_version.nil?
  
  Spinner.return :message => "Finding current Ruby version for the current Phusion Passenger Gem.." do
    if e.execute_as_root('passenger-config --root') =~ /\/usr\/local\/rvm\/gems\/(.+)\/gems\/passenger-.+/
      @ruby_version = $1.chomp.strip
      g('Found!')
    else
      r("Could not find the current Ruby version under which the Passenger Gem has been installed.")
    end
  end
  
  exit if @ruby_version.nil?
  
  puts <<-INFO


  [Detected Versions]

    Ruby Version:               #{@ruby_version}
    Phusion Passenger Version:  #{@passenger_version}


  INFO
  
  message "Apache will now be configured to work with the above versions. Is this correct?"
  exit unless yes?
  
  ##
  # Checks to see if Passengers WatchDog is available in the current Passenger gem
  # If it is not, then Passenger needs to run the "passenger-install-nginx-module" so it gets installed
  if not e.directory?("/usr/local/rvm/gems/#{@ruby_version}/gems/passenger-#{@passenger_version}/agents")
    message "Phusion Passenger has not yet been installed for this Ruby's Passenger Gem."
    message "You need to update #{y('Apache')} and #{y('Passenger')} to proceed with the configuration.\n\n"
    message "Would you like to update #{y('Apache')} and #{y('Phusion Passenger')} #{y(@passenger_version)} for #{y(@ruby_version)}?"
    message "NOTE: Your current #{y('Apache')} configuration will #{g('not')} be lost."
    
    if yes?
      Spinner.return :message => "Ensuring #{y('Phusion Passenger')} and #{y('Apache')} dependencies are installed.." do
        e.install!("build-essential libcurl4-openssl-dev bison openssl libreadline5 libreadline5-dev curl git-core zlib1g zlib1g-dev libssl-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev")
        e.install!("apache2-mpm-prefork apache2-prefork-dev libapr1-dev libaprutil1-dev")
        g("Done!")
      end
      
      message "Installing Apache with the Phusion Passenger Module."
      Spinner.return :message => "Installing, this may take a while.." do
        e.execute_as_root("passenger-install-apache2-module --auto")
        g("Done!")
      end
    else
      exit
    end
  end
  
  ##
  # Creates a tmp dir
  local.create_tmp_dir!
  
  ##
  # Downloads the Apache configuration file to tmp dir
  message "Updating Phusion Passenger paths in the Apache Configuration."
  Spinner.return :message => "Configuring Apache.." do
    e.scp_as_root(:download, @configuration_file, local.tmp_dir)
    @configuration_file_name = @configuration_file.split('/').last
    
    local_configuration_file = File.join(local.tmp_dir, @configuration_file_name)
    update = File.read(local_configuration_file)
    
    update.sub! /LoadModule passenger_module \/usr\/local\/rvm\/gems\/(.+)\/gems\/passenger-(.+)\/ext\/apache2\/mod_passenger\.so/,
                "LoadModule passenger_module /usr/local/rvm/gems/#{@ruby_version}/gems/passenger-#{@passenger_version}/ext/apache2/mod_passenger.so"
    
    update.sub! /PassengerRoot \/usr\/local\/rvm\/gems\/(.+)\/gems\/passenger-(.+)/,
                "PassengerRoot /usr/local/rvm/gems/#{@ruby_version}/gems/passenger-#{@passenger_version}"
    
    update.sub! /PassengerRuby \/usr\/local\/rvm\/wrappers\/(.+)\/ruby/,
                "PassengerRuby /usr/local/rvm/wrappers/#{@ruby_version}/ruby"
    
    File.open(local_configuration_file, 'w') do |file|
      file << update
    end
    
    ##
    # Create a backup of the current configuration file
    e.execute_as_root("cp '#{@configuration_file}' '#{@configuration_file}.backup.#{Time.now.to_i}'")
    
    ##
    # Upload the updated NginX configuration file
    e.scp_as_root(:upload, local_configuration_file, @configuration_file)
    
    ##
    # Remove the local tmp directory
    local.remove_tmp_dir!
    
    g("Done!")
  end
  
  message "Apache configuration file has been updated!"
  message "#{y(@configuration_file)}\n\n"
  
  warning "If you changed #{y('Ruby versions')}, be sure that all your application gems are installed."
  warning "If you only updated #{y('Phusion Passenger')} and did not change #{y('Ruby versions')}"
  warning "then you should be able to just restart #{y('Apache')} right away since all application gems should still be in tact.\n\n"
  
  message "When ready, run the following command to restart #{y('Apache')} and have the applied updates take effect:"
  standard "\n\s\s#{y("heavenly apache restart for #{e.name}")}"
end

#perform_upload_config!Object

Alias to perform_upload_configuration!



129
130
131
# File 'lib/gitpusshuten/modules/apache/command.rb', line 129

def perform_upload_config!
  perform_upload_configuration!
end

#perform_upload_configuration!Object

Uploads the Apache2 configuration file



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gitpusshuten/modules/apache/command.rb', line 109

def perform_upload_configuration!
  if not e.directory?('/etc/apache2')
    error "Could not find the Apache2 installation directory in #{y('/etc/apache2')}"
    exit
  end

  local_configuration_file = File.join(local.gitpusshuten_dir, 'apache', 'apache2.conf')        
  if not File.exist?(local_configuration_file)
    error "Could not find the local Apache2 configuration file in #{y(local_configuration_file)}"
    exit
  end
  
  Spinner.return :message => "Uploading Apache2 configuration file #{y(local_configuration_file)}.." do
    e.scp_as_root(:upload, local_configuration_file, "/etc/apache2/apache2.conf")
    g('Done!')
  end
end

#perform_upload_vhost!Object

Uploads a local vhost



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gitpusshuten/modules/apache/command.rb', line 156

def perform_upload_vhost!        
  vhost_file = File.join(local.gitpusshuten_dir, 'apache', "#{e.name}.vhost")
  if File.exist?(vhost_file)
    message "Uploading #{y(vhost_file)} to " +
    y(File.join(@configuration_directory, 'sites-enabled', "#{e.sanitized_app_name}.#{e.name}.vhost!"))
    
    prompt_for_root_password!
    
    Spinner.return :message => "Uploading vhost.." do
      e.scp_as_root(:upload, vhost_file, File.join(@configuration_directory, 'sites-enabled', "#{e.sanitized_app_name}.#{e.name}.vhost"))
      g("Finished uploading!")
    end
    
    perform_restart!
  else
    error "Could not locate vhost file #{y(vhost_file)}."
    error "Download an existing one from your server with:"
    standard "\n\s\s#{y("heavenly apache download-vhost for #{e.name}")}\n\n"
    error "Or create a new template by running:"
    standard "\n\s\s#{y("heavenly apache create-vhost for #{e.name}")}\n\n"
    exit
  end
end