Class: NginxOsx

Inherits:
Object
  • Object
show all
Defined in:
lib/nginx-osx.rb

Constant Summary collapse

TEMPLATES_DIR =
File.join(File.dirname(__FILE__), '../', 'templates')

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ NginxOsx

Returns a new instance of NginxOsx.



7
8
9
10
11
12
13
14
# File 'lib/nginx-osx.rb', line 7

def initialize(args)
  cmd = args.is_a?(Array) ? args[0] : args
  if cmd && respond_to?(cmd.to_sym)
    self.send(cmd.to_sym) 
  else
    help
  end
end

Instance Method Details

#addObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/nginx-osx.rb', line 64

def add
  port = ENV['PORT'] || '3000'
  config = ''
  File.open(File.join(TEMPLATES_DIR, 'nginx.vhost.conf.erb')) do |f|
    config = f.read
  end
  File.open(current_config_path, 'w+') do |f|
   f.puts ERB.new(config).result(binding)
  end
end

#current_configObject



101
102
103
104
# File 'lib/nginx-osx.rb', line 101

def current_config
  puts current_config_path
  exec "cat #{current_config_path}"
end

#helpObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nginx-osx.rb', line 16

def help
  usage = "USAGE:\nnginx-osx [cmd]\n  \n  install\n    - install nginx via macports\n  setup\n    - setup the basic nginx.conf file and the vhost directories\n  add\n    - add the current directory as a vhost\n  run\n    - run the current directory as the default vhost (symlinks in the vhost)\n  start\n    - start nginx\n  stop\n    - stop nginx\n  restart\n    - restart and check the config for errors\n  reload\n    - reload the config and check for errors\n  current_config\n    - show vhost config of current directory\n  tail_error_log\n    - tail the main nginx error log\n  USAGE\n  puts usage\nend\n"

#installObject



45
46
47
# File 'lib/nginx-osx.rb', line 45

def install
exec "sudo port install nginx"
end

#reloadObject



95
96
97
98
99
# File 'lib/nginx-osx.rb', line 95

def reload
  `sudo /opt/local/sbin/nginx -t`
  pid = `ps ax | grep 'nginx: master' | grep -v grep | awk '{print $1}'`
  puts `sudo kill -HUP #{pid}` if pid
end

#restartObject



89
90
91
92
93
# File 'lib/nginx-osx.rb', line 89

def restart
  `sudo killall nginx`
  `sudo /opt/local/sbin/nginx -t`
  puts `sudo /opt/local/sbin/nginx`
end

#runObject



75
76
77
78
79
# File 'lib/nginx-osx.rb', line 75

def run
  `sudo ln -fs #{current_config_path} /opt/local/etc/nginx/vhosts/current.conf`
  `sudo /opt/local/sbin/nginx -t`
  puts `sudo /opt/local/sbin/nginx`
end

#setupObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nginx-osx.rb', line 49

def setup
  config = ''
  File.open(File.join(TEMPLATES_DIR, 'nginx.conf.erb')) do |f|
    config = f.read
  end
  File.open('/opt/local/etc/nginx/nginx.conf', 'w+') do |f|
   f.puts ERB.new(config).result(binding)
  end
  `mkdir -p /opt/local/etc/nginx/vhosts`
  `mkdir -p /opt/local/etc/nginx/configs`
  `mkdir -p /var/log/nginx`
  `sudo cp /opt/local/etc/nginx/mime.types.example /opt/local/etc/nginx/mime.types`
  `sudo /opt/local/sbin/nginx -t`
end

#startObject



81
82
83
# File 'lib/nginx-osx.rb', line 81

def start
  puts `sudo /opt/local/sbin/nginx`
end

#stopObject



85
86
87
# File 'lib/nginx-osx.rb', line 85

def stop
  puts `sudo killall nginx`
end

#tail_error_logObject



106
107
108
# File 'lib/nginx-osx.rb', line 106

def tail_error_log
  exec "tail -f /var/log/nginx/default.error.log"
end