lib/rda/nginx.rb in rda-0.3.3 vs lib/rda/nginx.rb in rda-0.4.0.pre
- old
+ new
@@ -1,145 +1,54 @@
module Rda
class Nginx < Thor
include Thor::Actions
+ include Helper
- def self.source_root
- File.dirname(__FILE__)
- end
-
desc "setup", "Set up your rails application"
def setup(options = {})
return unless installed?
- @hostname, @environment = options["hostname"], options["environment"]
-
- create_setup_load_paths
mkdir_for_sites
- set_passenger_user_and_group
include_sites_enabled
-
- template("templates/nginx", "#{conf_path}/sites-available/#{hostname}")
- link_file("#{conf_path}/sites-available/#{hostname}", "#{conf_path}/sites-enabled/#{hostname}")
-
- unless configured?('/etc/hosts', "127.0.0.1 #{hostname}")
- append_file "/etc/hosts", "127.0.0.1 #{hostname}"
- end
end
- desc "discard", "Remove the settings of your rails application from nginx"
- def discard(options = {})
- return unless installed?
+ def self.setup?
+ conf_dir = Rda.config.nginx.conf_dir
- @hostname = options["hostname"]
-
- %W(enabled available).each do |n|
- remove_file "#{conf_path}/sites-#{n}/#{hostname}"
- end
-
- gsub_file("/etc/hosts", "127.0.0.1 #{hostname}", '')
- remove_file "#{Rda::Rails.root}/config/setup_load_paths.rb"
+ File.directory?(File.join(conf_dir, 'sites-available')) &&
+ File.directory?(File.join(conf_dir, 'sites-enabled'))
end
private
def installed?
- File.directory?(conf_path) if conf_path
+ File.exists?(conf_path) if conf_path
end
- def conf_path
- return @conf_path if @conf_path
+ def conf_dir
+ Rda.config.nginx.conf_dir
+ end
- if available_paths.empty?
- prompt_not_found
-
- return
+ def conf_path
+ if File.exists? File.join(conf_dir, 'nginx.conf')
+ return File.join(conf_dir, 'nginx.conf')
end
- @conf_path = available_paths.first
- begin
- @conf_path = ask_for_choosing_one if available_paths.size > 1
- rescue SystemExit
- $stderr.puts "ERROR: You need to choose a config directory of Nginx!"
- return
- end
-
- @conf_path
+ $stderr.puts "ERROR: Missing `nginx.conf` in `#{conf_dir}`."
end
- def hostname
- @hostname || "#{Rda::Rails.app_name}.local"
- end
-
- def available_paths
- search_paths = Rda.config.nginx_conf_paths || []
- @paths ||= search_paths.select { |p| File.directory? p if p } unless search_paths.empty?
- end
-
- def prompt_not_found
- $stderr.puts "ERROR: Config directory of Nginx is not found in the following paths:\n\n"
- Rda.config.nginx_conf_paths.each { |p| $stderr.puts "* #{p}" }
- $stderr.puts "\n"
- end
-
- def ask_for_choosing_one
- available_paths.each_with_index { |p, i| puts "#{i + 1}) #{p}" }
- puts "\n"
- chosen = ask "Found more than one config directory of Nginx, please choose one to setup:"
-
- index = chosen.to_i - 1
-
- index >= 0 && index < available_paths.size ? available_paths[index] : exit
- end
-
- def create_setup_load_paths
- copy_file "templates/setup_load_paths.rb", "#{Rda::Rails.root}/config/setup_load_paths.rb"
- end
-
def mkdir_for_sites
%W(available enabled).each do |n|
- dir = conf_path + "/sites-#{n}"
+ dir = conf_dir + "/sites-#{n}"
empty_directory(dir) unless File.directory?(dir)
end
end
- def set_passenger_user_and_group
- conf = conf_path + '/nginx.conf'
-
- unless configured?(conf, 'passenger_default_user')
- gsub_file conf, /http \{/, <<-PASSENGER
-http {
- passenger_default_user root;
- PASSENGER
- end
-
- unless configured?(conf, 'passenger_default_group')
- gsub_file conf, /http \{/, <<-PASSENGER
-http {
- passenger_default_group root;
- PASSENGER
- end
- end
-
def include_sites_enabled
- conf = conf_path + '/nginx.conf'
- unless configured?(conf, "include #{conf_path}/sites-enabled/*;")
+ unless configured?(conf_path, "include #{conf_dir}/sites-enabled/*;")
gsub_file conf, /http \{/, <<-INCLUDE_SITES_ENABLED
http {
- include #{conf_path}/sites-enabled/*;
+ include #{conf_dir}/sites-enabled/*;
INCLUDE_SITES_ENABLED
end
- end
-
- def configured?(fname, conf)
- File.open(fname) do |f|
- f.readlines.each do |l|
- if l.strip.start_with?(conf)
- $stderr.puts "INFO: #{conf} has already been set!"
-
- return true
- end
- end
- end
-
- false
end
end
end