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