Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit ddd6862

Browse files
xredopablorc
authored andcommitted
Add Docker & Cloud66 Deploy options
Closes #6 and #7
1 parent c57db44 commit ddd6862

File tree

12 files changed

+204
-1
lines changed

12 files changed

+204
-1
lines changed

recipes/base.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,13 @@ def gems
2323

2424
def cook
2525
end
26+
27+
private
28+
29+
def relative_file_content(path)
30+
caller_path = caller_locations.first.path
31+
puts caller_path
32+
File.read(File.join(File.dirname(caller_path), path))
33+
end
2634
end
2735
end

recipes/deploy.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Recipes
2+
class Deploy < Base
3+
4+
def initialize(pathfinder, type: 'none')
5+
if !%w(cloud66 docker none).include?(type)
6+
fail 'none, docker or cloud66 are the only allowed options for deploy recipe'
7+
end
8+
super(pathfinder)
9+
@type = type
10+
end
11+
12+
def init_file
13+
case @type
14+
when 'docker' then docker_config_files
15+
when 'cloud66' then cloud66_config_files
16+
end
17+
end
18+
19+
private
20+
21+
def cloud66_config_files
22+
@template.add_file '.cloud66/bower.sh',
23+
relative_file_content('deploy/cloud66/bower.sh')
24+
@template.add_file '.cloud66/cache_permissions.sh',
25+
relative_file_content('deploy/cloud66/cache_permissions.sh')
26+
@template.add_file '.cloud66/deploy_hooks.yml',
27+
relative_file_content('deploy/cloud66/deploy_hooks.yml')
28+
end
29+
30+
def docker_config_files
31+
@template.add_file 'docker/rails-env.conf',
32+
relative_file_content('deploy/docker/rails-env.conf')
33+
@template.add_file '.dockerignore',
34+
relative_file_content('deploy/docker/.dockerignore')
35+
@template.append_file 'README.md',
36+
relative_file_content('deploy/docker/README.md')
37+
add_file_and_replace_app_name('docker/fix_permissions.sh',
38+
'deploy/docker/fix_permissions.sh')
39+
add_file_and_replace_app_name('Dockerfile', 'deploy/docker/Dockerfile')
40+
add_file_and_replace_app_name('docker/nginx.conf', 'deploy/docker/nginx.conf')
41+
end
42+
43+
def add_file_and_replace_app_name(target_file, source_file)
44+
@template.add_file target_file, relative_file_content(source_file)
45+
@template.run "sed -i '' 's/app-name/#{@pathfinder.app_name}/g' #{target_file}"
46+
end
47+
48+
end
49+
end

recipes/deploy/cloud66/bower.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sudo npm install -g bower
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
chown nginx:app_writers $STACK_BASE/shared/cache && chmod 775 $STACK_BASE/shared/cache && sudo chmod g+s $STACK_BASE/shared/cache
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
production:
2+
first_thing:
3+
- snippet: cloud66/node
4+
target: rails
5+
execute: true
6+
- snippet: cloud66/newrelic
7+
target: any
8+
execute: true
9+
- source: /.cloud66/bower.sh
10+
destination: /tmp/bower.sh
11+
target: rails
12+
execute: true
13+
after_rails:
14+
- source: /.cloud66/cache_permissions.sh
15+
destination: /tmp/cache_permissions.sh
16+
target: rails
17+
run_on: all_servers
18+
execute: true
19+
sudo: true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public/uploads
2+
vendor/bundle

recipes/deploy/docker/Dockerfile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
FROM phusion/passenger-ruby24
2+
MAINTAINER MarsBased "hola@marsbased.com"
3+
4+
ENV HOME /home/app/app-name
5+
6+
# Install software dependencies
7+
RUN apt-get update
8+
RUN apt-get install -y imagemagick
9+
10+
# Use baseimage-docker's init system.
11+
CMD ["/sbin/my_init"]
12+
13+
# Expose Nginx HTTP service
14+
EXPOSE 80
15+
EXPOSE 443
16+
17+
# Start Nginx / Passenger
18+
RUN rm -f /etc/service/nginx/down
19+
20+
# Remove the default site
21+
RUN rm /etc/nginx/sites-enabled/default
22+
23+
# Create app home dir
24+
RUN mkdir -p $HOME
25+
WORKDIR $HOME
26+
27+
# Install bundle of gems
28+
ADD Gemfile Gemfile
29+
ADD Gemfile.lock Gemfile.lock
30+
RUN bundle install --without development test
31+
32+
# Add the nginx site and config
33+
ADD docker/nginx.conf /etc/nginx/sites-enabled/app-name.conf
34+
ADD docker/rails-env.conf /etc/nginx/main.d/rails-env.conf
35+
36+
# Add the Rails app
37+
ADD . /home/app/app-name
38+
39+
RUN RAILS_ENV=production SECRET_KEY_BASE=NOT-IMPORTANT bin/rake assets:precompile
40+
41+
# Add a tmp folder for pids
42+
RUN mkdir -p tmp/pids
43+
44+
# Define volumes
45+
46+
VOLUME $HOME/public/uploads
47+
VOLUME $HOME/log
48+
49+
# Configure init scripts
50+
RUN mkdir -p /etc/my_init.d
51+
ADD docker/fix_permissions.sh /etc/my_init.d/fix_permissions.sh
52+
RUN chmod +x /etc/my_init.d/fix_permissions.sh
53+
54+
RUN chown -R app:app $HOME
55+
56+
# Clean up APT and bundler when done.
57+
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*.

recipes/deploy/docker/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
## Docker
3+
4+
Production deploys are managed with Docker. The Dockerfile makes use of some config files that can be found in the `docker` folder.
5+
6+
- **fix_permissions.sh**: Makes the mountable volumes writable for the
7+
application user.
8+
- **rails-env.conf**: Makes env variables visible to Nginx. As the
9+
application is running on Passenger through Nginx, every ENV variable
10+
that needs to reach the application must be defined here.
11+
- **nginx.conf**: Base nginx configuration. The file contains
12+
instructions to tune the application performance.
13+
14+
It's recommended to use Dockerhub (or a private docker repository) to store the application images and then use docker-compose to orchestrate the deployment.
15+
16+
The docker-compose file is not included in the project and needs to be
17+
configured on a project basis.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
chown -R app:app /home/app/app-name/public/uploads
3+
chown -R app:app /home/app/app-name/log

recipes/deploy/docker/nginx.conf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
server {
2+
listen 80 default_server;
3+
root /home/app/app-name/public;
4+
passenger_enabled on;
5+
passenger_user app;
6+
passenger_ruby /usr/bin/ruby2.4;
7+
8+
## Configure this with the same value of the passenger_max_pool_size
9+
# passenger_min_instances 10;
10+
11+
location ~ ^/assets/ {
12+
expires 1y;
13+
add_header Cache-Control public;
14+
add_header ETag "";
15+
break;
16+
}
17+
18+
location ~ ^/uploads/ {
19+
expires 24h;
20+
add_header Cache-Control public;
21+
add_header ETag "";
22+
break;
23+
}
24+
}
25+
26+
## The number for max_pool_size should be (TOTAL_RAM * 0.75) / RAM_PER_PROCESS
27+
# passenger_max_pool_size 10;
28+
29+
## Configure this option with the app host url to preload app after deploy
30+
# passenger_pre_start https://app-name.com;

0 commit comments

Comments
 (0)