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

Commit b65ccc6

Browse files
committed
Add Docker & Cloud66 Deploy options
Closes #6 and #7
1 parent 2b040d5 commit b65ccc6

File tree

14 files changed

+206
-5
lines changed

14 files changed

+206
-5
lines changed

pathfinder.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require_relative 'recipes/rollbar'
1313
require_relative 'recipes/sidekiq'
1414
require_relative 'recipes/simple_form'
15+
require_relative 'recipes/deploy'
1516
require_relative 'recipes/utils'
1617

1718
class Pathfinder
@@ -38,6 +39,8 @@ def generate_initializers
3839
end
3940

4041
def ask_for_recipes
42+
deploy_answer = @template.ask('Deploy with:', limited_to: %w(docker cloud66 none))
43+
add_recipe(Recipes::Deploy.new(self, type: deploy_answer))
4144
add_recipe(Recipes::Postgres.new(self))
4245

4346
add_recipe(Recipes::CarrierWave.new(self)) if @template.yes?('Do you want to use Carrierwave?')

recipes/base.rb

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

1212
def init_file
1313
end
14+
15+
private
16+
17+
def relative_file_content(path)
18+
caller_path = caller_locations.first.path
19+
puts caller_path
20+
File.read(File.join(File.dirname(caller_path), path))
21+
end
1422
end
1523
end

recipes/bower_rails.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ def init_file
3131
}
3232
TEXT
3333
end
34+
35+
@template.run 'rm config/initializers/bower_rails.rb'
36+
@template.initializer 'bower_rails.rb' do <<~CODE
37+
BowerRails.configure do |bower_rails|
38+
bower_rails.install_before_precompile = true
39+
bower_rails.resolve_before_precompile = true
40+
end
41+
CODE
42+
end
3443
@template.rake 'bower:install'
3544
@template.rake 'bower:resolve'
3645
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.

0 commit comments

Comments
 (0)