FIX: properly shut down services on "docker stop"
[discourse_docker.git] / templates / web.template.yml
1 env:
2 # You can have redis on a different box
3 RAILS_ENV: 'production'
4 UNICORN_WORKERS: 3
5 UNICORN_SIDEKIQS: 1
6 # slightly less aggressive than "recommendation" but works fine with oobgc
7 RUBY_GC_MALLOC_LIMIT: 40000000
8 # this ensures we have enough heap space to handle a big pile of small reqs
9 RUBY_HEAP_MIN_SLOTS: 800000
10
11 DISCOURSE_DB_SOCKET: /var/run/postgresql
12 DISCOURSE_DB_HOST:
13 DISCOURSE_DB_PORT:
14
15
16 params:
17 # SSH key is required for remote access into the container
18 version: tests-passed
19
20 home: /var/www/discourse
21 upload_size: 3m
22
23 run:
24 - exec: /usr/local/bin/ruby -e 'if ENV["DISCOURSE_SMTP_ADDRESS"] == "smtp.example.com"; puts "Aborting! Mail is not configured!"; exit 1; end'
25 - file:
26 path: /etc/runit/1.d/copy-env
27 chmod: "+x"
28 contents: |
29 #!/bin/bash
30 env > ~/boot_env
31 conf=/var/www/discourse/config/discourse.conf
32
33 # find DISCOURSE_ env vars, strip the leader, lowercase the key
34 /usr/local/bin/ruby -e 'ENV.each{|k,v| puts "#{$1.downcase} = #{v}" if k =~ /^DISCOURSE_(.*)/}' > $conf
35
36 - file:
37 path: /etc/service/unicorn/run
38 chmod: "+x"
39 contents: |
40 #!/bin/bash
41 exec 2>&1
42 # redis
43 # postgres
44 cd $home
45 chown -R discourse:www-data /shared/log/rails
46 LD_PRELOAD=/usr/lib/libjemalloc.so.1 HOME=/home/discourse USER=discourse exec chpst -u discourse:www-data -U discourse:www-data bundle exec config/unicorn_launcher -E production -c config/unicorn.conf.rb
47
48 - file:
49 path: /etc/service/nginx/run
50 chmod: "+x"
51 contents: |
52 #!/bin/sh
53 exec 2>&1
54 mkdir -p /var/log/nginx
55 exec /usr/sbin/nginx
56
57 - file:
58 path: /etc/runit/3.d/01-nginx
59 chmod: "+x"
60 contents: |
61 #!/bin/bash
62 sv stop nginx
63
64 - file:
65 path: /etc/runit/3.d/02-unicorn
66 chmod: "+x"
67 contents: |
68 #!/bin/bash
69 sv stop unicorn
70
71 - exec:
72 cd: $home
73 hook: code
74 cmd:
75 - git reset --hard
76 - git clean -f
77 - git remote set-branches --add origin master
78 - git pull
79 - git fetch origin $version
80 - git checkout $version
81 - mkdir -p tmp/pids
82 - mkdir -p tmp/sockets
83 - touch tmp/.gitkeep
84 - mkdir -p /shared/log/rails
85 - bash -c "touch -a /shared/log/rails/{production,production_errors,unicorn.stdout,unicorn.stderr}.log"
86 - bash -c "ln -s /shared/log/rails/{production,production_errors,unicorn.stdout,unicorn.stderr}.log $home/log"
87 - bash -c "mkdir -p /shared/{uploads,backups}"
88 - bash -c "ln -s /shared/{uploads,backups} $home/public"
89 - chown -R discourse:www-data /shared/log/rails /shared/uploads /shared/backups
90
91 - exec:
92 cmd:
93 - "cp $home/config/nginx.sample.conf /etc/nginx/conf.d/discourse.conf"
94 - "rm /etc/nginx/sites-enabled/default"
95 - "mkdir -p /var/nginx/cache"
96
97 - replace:
98 filename: /etc/nginx/nginx.conf
99 from: pid /run/nginx.pid;
100 to: daemon off;
101
102 - replace:
103 filename: "/etc/nginx/conf.d/discourse.conf"
104 from: /upstream[^\}]+\}/m
105 to: "upstream discourse {
106 server 127.0.0.1:3000;
107 }"
108
109 - replace:
110 filename: "/etc/nginx/conf.d/discourse.conf"
111 from: /server_name.+$/
112 to: server_name _ ;
113
114 - replace:
115 filename: "/etc/nginx/conf.d/discourse.conf"
116 from: /client_max_body_size.+$/
117 to: client_max_body_size $upload_size ;
118
119 - exec:
120 cmd: echo "done configuring web"
121 hook: web_config
122
123 - exec:
124 cd: $home
125 hook: web
126 cmd:
127 # ensure we are on latest bundler
128 - gem update bundler
129 - chown -R discourse $home
130 - su discourse -c 'bundle install --deployment --verbose --without test --without development'
131 - su discourse -c 'bundle exec rake db:migrate'
132 - su discourse -c 'bundle exec rake assets:precompile'
133
134 - file:
135 path: /usr/local/bin/discourse
136 chmod: +x
137 contents: |
138 #!/bin/bash
139 (cd /var/www/discourse && RAILS_ENV=production sudo -H -E -u discourse bundle exec script/discourse "$@")
140
141 - file:
142 path: /usr/local/bin/rails
143 chmod: +x
144 contents: |
145 #!/bin/bash
146 # If they requested a console, load pry instead
147 if [ "$@" == "c" -o "$@" == "console" ]
148 then
149 (cd /var/www/discourse && RAILS_ENV=production sudo -H -E -u discourse bundle exec pry -r ./config/environment)
150 else
151 (cd /var/www/discourse && RAILS_ENV=production sudo -H -E -u discourse bundle exec script/rails "$@")
152 fi
153
154 - file:
155 path: /usr/local/bin/rake
156 chmod: +x
157 contents: |
158 #!/bin/bash
159 (cd /var/www/discourse && RAILS_ENV=production sudo -H -E -u discourse bundle exec bin/rake "$@")
160
161 - file:
162 path: /etc/update-motd.d/10-web
163 chmod: +x
164 contents: |
165 #!/bin/bash
166 echo
167 echo Use: rails, rake or discourse to execute commands in production
168 echo
169
170 - file:
171 path: /etc/logrotate.d/rails
172 contents: |
173 /shared/log/rails/*.log
174 {
175 rotate 14
176 dateext
177 daily
178 missingok
179 notifempty
180 delaycompress
181 compress
182 postrotate
183 sv 1 unicorn
184 endscript
185 }
186
187 - replace:
188 filename: "/etc/logrotate.d/nginx"
189 from: "weekly"
190 to: "daily"
191
192 - replace:
193 filename: "/etc/logrotate.d/nginx"
194 from: "52"
195 to: "14"
196
197 # move state out of the container this fancy is done to support rapid rebuilds of containers,
198 # we store anacron and logrotate state outside the container to ensure its maintained across builds
199 # later move this snipped into an intialization script
200 # we also ensure all the symlinks we need to /shared are in place in the correct structure
201 # this allows us to bootstrap on one machine and then run on another
202 - file:
203 path: /etc/runit/1.d/00-ensure-links
204 chmod: +x
205 contents: |
206 #!/bin/bash
207 if [[ ! -L /var/lib/logrotate ]]; then
208 rm -fr /var/lib/logrotate
209 mkdir -p /shared/state/logrotate
210 ln -s /shared/state/logrotate /var/lib/logrotate
211 fi
212 if [[ ! -L /var/spool/anacron ]]; then
213 rm -fr /var/spool/anacron
214 mkdir -p /shared/state/anacron-spool
215 ln -s /shared/state/anacron-spool /var/spool/anacron
216 fi
217 if [[ ! -d /shared/log/rails ]]; then
218 mkdir -p /shared/log/rails
219 chown -R discourse:www-data /shared/log/rails
220 fi
221 if [[ ! -d /shared/uploads ]]; then
222 mkdir -p /shared/uploads
223 chown -R discourse:www-data /shared/uploads
224 fi
225 if [[ ! -d /shared/backups ]]; then
226 mkdir -p /shared/backups
227 chown -R discourse:www-data /shared/backups
228 fi