discourse has to own /home/discourse otherwise rubygem issues
[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: 10m
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 - exec: /usr/local/bin/ruby -e 'if ENV["DISCOURSE_HOSTNAME"] == "discourse.example.com"; puts "Aborting! Domain is not configured!"; exit 1; end'
26 - exec: chown -R discourse /home/discourse
27 - file:
28 path: /etc/runit/1.d/copy-env
29 chmod: "+x"
30 contents: |
31 #!/bin/bash
32 env > ~/boot_env
33 conf=/var/www/discourse/config/discourse.conf
34
35 # find DISCOURSE_ env vars, strip the leader, lowercase the key
36 /usr/local/bin/ruby -e 'ENV.each{|k,v| puts "#{$1.downcase} = #{v}" if k =~ /^DISCOURSE_(.*)/}' > $conf
37
38 - file:
39 path: /etc/runit/1.d/ensure-web-nginx-read
40 chmod: "+x"
41 contents: |
42 #!/bin/bash
43 mkdir -p /var/log/nginx
44 chgrp -R www-data /var/log/nginx
45 chgrp www-data /var/log/nginx
46
47 - file:
48 path: /etc/service/unicorn/run
49 chmod: "+x"
50 contents: |
51 #!/bin/bash
52 exec 2>&1
53 # redis
54 # postgres
55 cd $home
56 chown -R discourse:www-data /shared/log/rails
57 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
58
59 - file:
60 path: /etc/service/nginx/run
61 chmod: "+x"
62 contents: |
63 #!/bin/sh
64 exec 2>&1
65 exec /usr/sbin/nginx
66
67 - file:
68 path: /etc/runit/3.d/01-nginx
69 chmod: "+x"
70 contents: |
71 #!/bin/bash
72 sv stop nginx
73
74 - file:
75 path: /etc/runit/3.d/02-unicorn
76 chmod: "+x"
77 contents: |
78 #!/bin/bash
79 sv stop unicorn
80
81 - exec:
82 cd: $home
83 hook: code
84 cmd:
85 - git reset --hard
86 - git clean -f
87 - git remote set-branches --add origin master
88 - git pull
89 - git fetch origin $version
90 - git checkout $version
91 - mkdir -p tmp/pids
92 - mkdir -p tmp/sockets
93 - touch tmp/.gitkeep
94 - mkdir -p /shared/log/rails
95 - bash -c "touch -a /shared/log/rails/{production,production_errors,unicorn.stdout,unicorn.stderr}.log"
96 - bash -c "ln -s /shared/log/rails/{production,production_errors,unicorn.stdout,unicorn.stderr}.log $home/log"
97 - bash -c "mkdir -p /shared/{uploads,backups}"
98 - bash -c "ln -s /shared/{uploads,backups} $home/public"
99 - chown -R discourse:www-data /shared/log/rails /shared/uploads /shared/backups
100
101 - exec:
102 cmd:
103 - "cp $home/config/nginx.sample.conf /etc/nginx/conf.d/discourse.conf"
104 - "rm /etc/nginx/sites-enabled/default"
105 - "mkdir -p /var/nginx/cache"
106
107 - replace:
108 filename: /etc/nginx/nginx.conf
109 from: pid /run/nginx.pid;
110 to: daemon off;
111
112 - replace:
113 filename: "/etc/nginx/conf.d/discourse.conf"
114 from: /upstream[^\}]+\}/m
115 to: "upstream discourse {
116 server 127.0.0.1:3000;
117 }"
118
119 - replace:
120 filename: "/etc/nginx/conf.d/discourse.conf"
121 from: /server_name.+$/
122 to: server_name _ ;
123
124 - replace:
125 filename: "/etc/nginx/conf.d/discourse.conf"
126 from: /client_max_body_size.+$/
127 to: client_max_body_size $upload_size ;
128
129 - exec:
130 cmd: echo "done configuring web"
131 hook: web_config
132
133 - exec:
134 cd: $home
135 hook: web
136 cmd:
137 # ensure we are on latest bundler
138 - gem update bundler
139 - chown -R discourse $home
140
141 - exec:
142 cd: $home
143 hook: bundle_exec
144 cmd:
145 - su discourse -c 'bundle install --deployment --verbose --without test --without development'
146 - su discourse -c 'bundle exec rake db:migrate'
147 - su discourse -c 'bundle exec rake assets:precompile'
148
149 - file:
150 path: /usr/local/bin/discourse
151 chmod: +x
152 contents: |
153 #!/bin/bash
154 (cd /var/www/discourse && RAILS_ENV=production sudo -H -E -u discourse bundle exec script/discourse "$@")
155
156 - file:
157 path: /usr/local/bin/rails
158 chmod: +x
159 contents: |
160 #!/bin/bash
161 # If they requested a console, load pry instead
162 if [ "$*" == "c" -o "$*" == "console" ]
163 then
164 (cd /var/www/discourse && RAILS_ENV=production sudo -H -E -u discourse bundle exec pry -r ./config/environment)
165 else
166 (cd /var/www/discourse && RAILS_ENV=production sudo -H -E -u discourse bundle exec script/rails "$@")
167 fi
168
169 - file:
170 path: /usr/local/bin/rake
171 chmod: +x
172 contents: |
173 #!/bin/bash
174 (cd /var/www/discourse && RAILS_ENV=production sudo -H -E -u discourse bundle exec bin/rake "$@")
175
176 - file:
177 path: /etc/update-motd.d/10-web
178 chmod: +x
179 contents: |
180 #!/bin/bash
181 echo
182 echo Use: rails, rake or discourse to execute commands in production
183 echo
184
185 - file:
186 path: /etc/logrotate.d/rails
187 contents: |
188 /shared/log/rails/*.log
189 {
190 rotate 14
191 dateext
192 daily
193 missingok
194 notifempty
195 delaycompress
196 compress
197 postrotate
198 sv 1 unicorn
199 endscript
200 }
201
202 - file:
203 path: /etc/logrotate.d/nginx
204 contents: |
205 /var/log/nginx/*.log {
206 daily
207 missingok
208 rotate 14
209 compress
210 delaycompress
211 notifempty
212 create 0640 www-data www-data
213 sharedscripts
214 postrotate
215 sv 1 nginx
216 endscript
217 }
218
219 # move state out of the container this fancy is done to support rapid rebuilds of containers,
220 # we store anacron and logrotate state outside the container to ensure its maintained across builds
221 # later move this snipped into an intialization script
222 # we also ensure all the symlinks we need to /shared are in place in the correct structure
223 # this allows us to bootstrap on one machine and then run on another
224 - file:
225 path: /etc/runit/1.d/00-ensure-links
226 chmod: +x
227 contents: |
228 #!/bin/bash
229 if [[ ! -L /var/lib/logrotate ]]; then
230 rm -fr /var/lib/logrotate
231 mkdir -p /shared/state/logrotate
232 ln -s /shared/state/logrotate /var/lib/logrotate
233 fi
234 if [[ ! -L /var/spool/anacron ]]; then
235 rm -fr /var/spool/anacron
236 mkdir -p /shared/state/anacron-spool
237 ln -s /shared/state/anacron-spool /var/spool/anacron
238 fi
239 if [[ ! -d /shared/log/rails ]]; then
240 mkdir -p /shared/log/rails
241 chown -R discourse:www-data /shared/log/rails
242 fi
243 if [[ ! -d /shared/uploads ]]; then
244 mkdir -p /shared/uploads
245 chown -R discourse:www-data /shared/uploads
246 fi
247 if [[ ! -d /shared/backups ]]; then
248 mkdir -p /shared/backups
249 chown -R discourse:www-data /shared/backups
250 fi
251
252 # change login directory to Discourse home
253 - file:
254 path: /root/.bash_profile
255 chmod: 644
256 contents: |
257 cd $home