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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ NginxOsx

Returns a new instance of NginxOsx.



9
10
11
12
13
14
15
16
17
# File 'lib/nginx-osx.rb', line 9

def initialize(*args)
  cmd = args.shift
  parse(args)
  if cmd && respond_to?(cmd.to_sym)
    self.send(cmd.to_sym)
  else
    help
  end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/nginx-osx.rb', line 7

def host
  @host
end

#passengerObject

Returns the value of attribute passenger.



7
8
9
# File 'lib/nginx-osx.rb', line 7

def passenger
  @passenger
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/nginx-osx.rb', line 7

def port
  @port
end

Instance Method Details

#addObject



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

def add
  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
  if host
    `ghost add #{host}`
  end
end

#current_configObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nginx-osx.rb', line 88

def current_config
  current_path = current_config_path
  vhost_path = current_config_path(true)

  path =  if File.exists? current_path
            current_path
          elsif File.exists? vhost_path
             vhost_path
          end
  if path
    puts path
    exec "cat #{path}"
  else
    puts "no config found have you run 'nginx-osx add' ?"
  end
end

#helpObject



19
20
21
# File 'lib/nginx-osx.rb', line 19

def help
  puts usage
end

#installObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/nginx-osx.rb', line 23

def install
  cmd = <<-CMD;
git clone git://github.com/crigor/admoolabs-ports.git ports
cd ports/nginx-0.7.64-passenger-2.2.8
sudo port -v install +passenger
cd ../../
rm -rf ports
CMD
  exec cmd
end

#make_currentObject



62
63
64
65
66
# File 'lib/nginx-osx.rb', line 62

def make_current
  `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

#reloadObject



82
83
84
85
86
# File 'lib/nginx-osx.rb', line 82

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



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

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

#setupObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nginx-osx.rb', line 34

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



68
69
70
# File 'lib/nginx-osx.rb', line 68

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

#stopObject



72
73
74
# File 'lib/nginx-osx.rb', line 72

def stop
  puts `sudo killall nginx`
end

#tail_error_logObject



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

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