Class: GitPusshuTen::Commands::Nginx
- Defined in:
- lib/gitpusshuten/modules/nginx/command.rb
Instance Attribute Summary
Attributes inherited from Base
#cli, #command, #configuration, #environment, #hooks, #perform_hooks
Instance Method Summary collapse
-
#create_vhost_template_file! ⇒ Object
Creates a vhost template file if it doesn’t already exist.
-
#ensure_nginx_executable_is_installed! ⇒ Object
Installs the Nginx executable if it does not exist.
-
#find_nginx! ⇒ Object
Finds and sets the NginX Conf path.
-
#initialize(*objects) ⇒ Nginx
constructor
A new instance of Nginx.
-
#perform_create_vhost! ⇒ Object
Creates a vhost.
-
#perform_delete_vhost! ⇒ Object
Deletes a vhost.
-
#perform_download_config! ⇒ Object
Alias to perform_download_configuration!.
-
#perform_download_configuration! ⇒ Object
Downloads the NginX configuration file.
- #perform_download_vhost! ⇒ Object
-
#perform_install! ⇒ Object
Installs the NginX web server.
-
#perform_reload! ⇒ Object
Reload Nginx.
-
#perform_restart! ⇒ Object
Restarts Nginx.
-
#perform_setup! ⇒ Object
Sets up a vHost directory and injects a snippet into the nginx.conf.
-
#perform_start! ⇒ Object
Starts Nginx.
-
#perform_stop! ⇒ Object
Stops Nginx.
-
#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 nginx.conf file.
-
#perform_upload_config! ⇒ Object
Alias to perform_upload_configuration!.
-
#perform_upload_configuration! ⇒ Object
Uploads the NginX configuration file.
-
#perform_upload_vhost! ⇒ Object
Updates a local vhost.
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) ⇒ Nginx
Returns a new instance of Nginx.
22 23 24 25 26 27 28 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 22 def initialize(*objects) super @command = cli.arguments.shift help if command.nil? or e.name.nil? end |
Instance Method Details
#create_vhost_template_file! ⇒ Object
Creates a vhost template file if it doesn’t already exist.
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 409 def create_vhost_template_file! FileUtils.mkdir_p(File.join(local.gitpusshuten_dir, 'nginx')) vhost_file = File.join(local.gitpusshuten_dir, 'nginx', "#{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 << "server {\n" file << "\s\slisten 80;\n" file << "\s\sserver_name mydomain.com www.mydomain.com;\n" file << "\s\sroot #{e.app_dir}/public;\n" file << "\s\s# passenger_enabled on; # For Phusion Passenger users\n" file << "}\n" end "The vhost has been created in #{y(vhost_file)}." end end |
#ensure_nginx_executable_is_installed! ⇒ Object
Installs the Nginx executable if it does not exist
398 399 400 401 402 403 404 405 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 398 def ensure_nginx_executable_is_installed! if not e.file?("/etc/init.d/nginx") "Installing NginX executable for starting/stopping/restarting/reloading Nginx." e.download_packages!('$HOME', :root) e.execute_as_root("cp $HOME/gitpusshuten-packages/modules/nginx/nginx /etc/init.d/nginx") e.clean_up_packages!('$HOME', :root) end end |
#find_nginx! ⇒ Object
Finds and sets the NginX Conf path
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 434 def find_nginx! ## # NginX Conf path you get from Passenger if e.file?('/etc/nginx/conf/nginx.conf') @nginx_conf = '/etc/nginx/conf/nginx.conf' @nginx_conf_dir = '/etc/nginx/conf' end ## # NginX Conf path you get from Aptitude if e.file?('/etc/nginx/nginx.conf') @nginx_conf = '/etc/nginx/nginx.conf' @nginx_conf_dir = '/etc/nginx' end ## # Determines whether the configuration file was found or not if @nginx_conf.nil? error "Could not find the NginX configuration file. Has NginX been installed?" exit end ## # Set additional configuration @nginx_conf_name = @nginx_conf.split('/').last @nginx_vhosts_dir = @nginx_conf_dir + '/sites-enabled' end |
#perform_create_vhost! ⇒ Object
Creates a vhost
226 227 228 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 226 def perform_create_vhost! create_vhost_template_file! end |
#perform_delete_vhost! ⇒ Object
Deletes a vhost
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 208 def perform_delete_vhost! find_nginx! vhost_file = File.join(@nginx_vhosts_dir, "#{e.sanitized_app_name}.#{e.name}.vhost") if e.file?(vhost_file) Spinner.return :message => "Deleting #{y(vhost_file)}!" do e.execute_as_root("rm #{vhost_file}") g('Done!') end perform_reload! else "#{y(vhost_file)} does not exist." exit end end |
#perform_download_config! ⇒ Object
Alias to perform_download_configuration!
145 146 147 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 145 def perform_download_config! perform_download_configuration! end |
#perform_download_configuration! ⇒ Object
Downloads the NginX configuration file
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 128 def perform_download_configuration! find_nginx! if not @nginx_conf error "Could not find the NginX configuration file in #{y(@nginx_conf)}" exit end local_nginx_dir = File.join(local.gitpusshuten_dir, 'nginx') FileUtils.mkdir_p(local_nginx_dir) Spinner.return :message => "Downloading NginX configuration file to #{y(local_nginx_dir)}.." do e.scp_as_root(:download, @nginx_conf, local_nginx_dir) g('Done!') end end |
#perform_download_vhost! ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 372 def perform_download_vhost! load_configuration! find_correct_paths! remote_vhost = File.join(@nginx_vhosts_dir, "#{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 local_vhost = File.join(local.gitpusshuten_dir, 'nginx', "#{e.name}.vhost") if File.exist?(local_vhost) warning "#{y(local_vhost)} already exists. Do you want to overwrite it?" exit unless yes? end FileUtils.mkdir_p(File.join(local.gitpusshuten_dir, 'nginx')) Spinner.return :message => "Downloading vhost.." do e.scp_as_root(:download, remote_vhost, local_vhost) g("Finished downloading!") end "You can find the vhost in: #{y(local_vhost)}." end |
#perform_install! ⇒ Object
Installs the NginX web server
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 32 def perform_install! warning "If you are planning to use #{y('Ruby')} and #{y('Passenger')} then #{r("DON'T")} use this NginX installer." warning "Instead, use the Passenger module to install it." standard "\n\s\s#{y("heavenly passenger install to #{y(e.name)}")}\n\n" "If you do not plan on using #{y('Ruby')} on this server, then this stand-alone installation should be fine." "Do you want to continue?" exit unless yes? prompt_for_root_password! Spinner.return :message => "Installing NginX web server.." do e.install!('nginx') g('Done!') end "NginX has been installed in #{y('/etc/nginx')}." GitPusshuTen::Initializer.new('nginx', 'setup', 'for', "#{e.name}") end |
#perform_reload! ⇒ Object
Reload Nginx
78 79 80 81 82 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 78 def perform_reload! ensure_nginx_executable_is_installed! "Reloading Nginx Configuration." puts e.execute_as_root("/etc/init.d/nginx reload") end |
#perform_restart! ⇒ Object
Restarts Nginx
69 70 71 72 73 74 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 69 def perform_restart! ensure_nginx_executable_is_installed! "Restarting Nginx." perform_stop! perform_start! end |
#perform_setup! ⇒ Object
Sets up a vHost directory and injects a snippet into the nginx.conf
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 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 86 def perform_setup! find_nginx! if not e.execute_as_root("cat '#{@nginx_conf}'").include?('GIT PUSSHUTEN GENERATED NGINX CONFIGURATION FILE') ## # Make a backup of the old nginx.conf Spinner.return :message => "Creating a backup of the current NginX configuration file.." do e.execute_as_root("cp '#{@nginx_conf}' '#{@nginx_conf}.backup.#{Time.now.to_i}'") g('Done!') end ## # Replace the old NginX configuration file with the pre-configured one Spinner.return :message => "Generating pre-configured NginX configuration file.." do e.download_packages!("$HOME", :root) e.execute_as_root("cp $HOME/gitpusshuten-packages/modules/nginx/nginx.conf '#{@nginx_conf}'") e.clean_up_packages!("$HOME", :root) g('Done!') end ## # Create the vhosts dir on the server Spinner.return :message => "Creating #{@nginx_vhosts_dir} directory.." do e.execute_as_root("mkdir -p #{@nginx_vhosts_dir}") g('Done!') end if e.installed?('passenger') perform_update_configuration! end end ## # Create NginX directory # Create NginX vhost file (if it doesn't already exist) create_vhost_template_file! end |
#perform_start! ⇒ Object
Starts Nginx
53 54 55 56 57 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 53 def perform_start! ensure_nginx_executable_is_installed! "Starting Nginx." puts e.execute_as_root("/etc/init.d/nginx start") end |
#perform_stop! ⇒ Object
Stops Nginx
61 62 63 64 65 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 61 def perform_stop! ensure_nginx_executable_is_installed! "Stopping Nginx." puts e.execute_as_root("/etc/init.d/nginx 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 nginx.conf file.
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 335 336 337 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 363 364 365 366 367 368 369 370 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 234 def perform_update_configuration! find_nginx! "Checking the #{y(@nginx_conf)} for current Passenger configuration." config_contents = e.execute_as_root("cat '#{@nginx_conf}'") if not config_contents.include? 'passenger_root' or not config_contents.include?('passenger_ruby') error "Could not find Passenger configuration, has it ever been set up?" exit end "Checking if Passenger is installed under the #{y('default')} Ruby." if not e.installed?('passenger') "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 "NginX will now be configured to work with the above versions. Is that OK?" 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") "Phusion Passenger has not yet been installed for this Ruby's Passenger Gem." "You need to update #{y('NginX')} and #{y('Passenger')} to proceed with the configuration.\n\n" "Would you like to update #{y('NginX')} and #{y('Phusion Passenger')} #{y(@passenger_version)} for #{y(@ruby_version)}?" "NOTE: Your current #{y('NginX')} configuration will #{g('not')} be lost." if yes? Spinner.return :message => "Ensuring #{y('Phusion Passenger')} and #{y('NginX')} 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") g("Done!") end "Installing NginX with the Phusion Passenger Module." Spinner.return :message => "Installing, this may take a while.." do e.execute_as_root("passenger-install-nginx-module --auto --auto-download --prefix='/etc/nginx'") g("Done!") end else exit end end ## # Creates a tmp dir local.create_tmp_dir! ## # Downloads the NGINX configuration file to tmp dir "Updating Phusion Passenger paths in the NginX Configuration." Spinner.return :message => "Configuring NginX.." do e.scp_as_root(:download, @nginx_conf, local.tmp_dir) local_configuration_file = File.join(local.tmp_dir, @nginx_conf_name) update = File.read(local_configuration_file) update.sub! /passenger_root \/usr\/local\/rvm\/gems\/(.+)\/gems\/passenger\-(.+)\;/, "passenger_root /usr/local/rvm/gems/#{@ruby_version}/gems/passenger-#{@passenger_version};" update.sub! /passenger_ruby \/usr\/local\/rvm\/wrappers\/(.+)\/ruby\;/, "passenger_ruby /usr/local/rvm/wrappers/#{@ruby_version}/ruby;" ## # Uncomment any Phusion Passenger configuration if it is commented out # since Phusion Passenger is enabled update.sub!(/\#.+(passenger_ruby.+\;)/) { $1 } update.sub!(/\#.+(passenger_root.+\;)/) { $1 } update.sub!(/\#.+(passenger_spawn_method.+\;)/) { $1 } update.sub!(/\#.+(passenger_min_instances.+\;)/) { $1 } update.sub!(/\#.+(passenger_max_pool_size.+\;)/) { $1 } update.sub!(/\#.+(passenger_pool_idle_time.+\;)/) { $1 } File.open(local_configuration_file, 'w') do |file| file << update end ## # Create a backup of the current configuration file e.execute_as_root("cp '#{@nginx_conf}' '#{@nginx_conf}.backup.#{Time.now.to_i}'") ## # Upload the updated NginX configuration file e.scp_as_root(:upload, local_configuration_file, @nginx_conf) ## # Remove the local tmp directory local.remove_tmp_dir! g("Done!") end "NginX configuration file has been updated!" "#{y(@nginx_conf)}\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('NginX')} right away since all application gems should still be in tact.\n\n" "When ready, run the following command to restart #{y('NginX')} and have the applied updates take effect:" standard "\n\s\s#{y("heavenly nginx restart for #{e.name}")}\n\n" end |
#perform_upload_config! ⇒ Object
Alias to perform_upload_configuration!
172 173 174 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 172 def perform_upload_config! perform_upload_configuration! end |
#perform_upload_configuration! ⇒ Object
Uploads the NginX configuration file
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 151 def perform_upload_configuration! find_nginx! if not e.directory?('/etc/nginx') error "Could not find the NginX installation directory in #{y('/etc/nginx')}" exit end local_configuration_file = File.join(local.gitpusshuten_dir, 'nginx', 'nginx.conf') if not File.exist?(local_configuration_file) error "Could not find the local NginX configuration file in #{y(local_configuration_file)}" exit end Spinner.return :message => "Uploading NginX configuration file #{y(local_configuration_file)}.." do e.scp_as_root(:upload, local_configuration_file, @nginx_conf) g('Done!') end end |
#perform_upload_vhost! ⇒ Object
Updates a local vhost
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 |
# File 'lib/gitpusshuten/modules/nginx/command.rb', line 178 def perform_upload_vhost! find_nginx! if not e.directory?(@nginx_vhosts_dir) error "Could not upload your vhost because the vhost directory does not exist on the server." error "Did you run #{y("heavenly nginx setup for #{e.name}")} yet?" exit end vhost_file = File.join(local.gitpusshuten_dir, 'nginx', "#{e.name}.vhost") if File.exist?(vhost_file) "Uploading #{y(vhost_file)} to " + y(File.join(@nginx_vhosts_dir, "#{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(@nginx_vhosts_dir, "#{e.sanitized_app_name}.#{e.name}.vhost")) g("Finished uploading!") end perform_restart! else error "Could not locate vhost file #{y(vhost_file)}." error "Did you run #{y("heavenly nginx setup for #{e.name}")} yet?" exit end end |