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

Commit

Permalink
Add Docker & Cloud66 Deploy options
Browse files Browse the repository at this point in the history
Closes #6 and #7
  • Loading branch information
xredo committed Apr 6, 2017
1 parent 2b040d5 commit b65ccc6
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pathfinder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require_relative 'recipes/rollbar'
require_relative 'recipes/sidekiq'
require_relative 'recipes/simple_form'
require_relative 'recipes/deploy'
require_relative 'recipes/utils'

class Pathfinder
Expand All @@ -38,6 +39,8 @@ def generate_initializers
end

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

add_recipe(Recipes::CarrierWave.new(self)) if @template.yes?('Do you want to use Carrierwave?')
Expand Down
8 changes: 8 additions & 0 deletions recipes/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ def gems

def init_file
end

private

def relative_file_content(path)
caller_path = caller_locations.first.path
puts caller_path
File.read(File.join(File.dirname(caller_path), path))
end
end
end
9 changes: 9 additions & 0 deletions recipes/bower_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def init_file
}
TEXT
end

@template.run 'rm config/initializers/bower_rails.rb'
@template.initializer 'bower_rails.rb' do <<~CODE
BowerRails.configure do |bower_rails|
bower_rails.install_before_precompile = true
bower_rails.resolve_before_precompile = true
end
CODE
end
@template.rake 'bower:install'
@template.rake 'bower:resolve'
end
Expand Down
49 changes: 49 additions & 0 deletions recipes/deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Recipes
class Deploy < Base

def initialize(pathfinder, type: 'none')
if !%w(cloud66 docker none).include?(type)
fail 'none, docker or cloud66 are the only allowed options for deploy recipe'
end
super(pathfinder)
@type = type
end

def init_file
case @type
when 'docker' then docker_config_files
when 'cloud66' then cloud66_config_files
end
end

private

def cloud66_config_files
@template.add_file '.cloud66/bower.sh',
relative_file_content('deploy/cloud66/bower.sh')
@template.add_file '.cloud66/cache_permissions.sh',
relative_file_content('deploy/cloud66/cache_permissions.sh')
@template.add_file '.cloud66/deploy_hooks.yml',
relative_file_content('deploy/cloud66/deploy_hooks.yml')
end

def docker_config_files
@template.add_file 'docker/rails-env.conf',
relative_file_content('deploy/docker/rails-env.conf')
@template.add_file '.dockerignore',
relative_file_content('deploy/docker/.dockerignore')
@template.append_file 'README.md',
relative_file_content('deploy/docker/README.md')
add_file_and_replace_app_name('docker/fix_permissions.sh',
'deploy/docker/fix_permissions.sh')
add_file_and_replace_app_name('Dockerfile', 'deploy/docker/Dockerfile')
add_file_and_replace_app_name('docker/nginx.conf', 'deploy/docker/nginx.conf')
end

def add_file_and_replace_app_name(target_file, source_file)
@template.add_file target_file, relative_file_content(source_file)
@template.run "sed -i '' 's/app-name/#{@pathfinder.app_name}/g' #{target_file}"
end

end
end
1 change: 1 addition & 0 deletions recipes/deploy/cloud66/bower.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sudo npm install -g bower
1 change: 1 addition & 0 deletions recipes/deploy/cloud66/cache_permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chown nginx:app_writers $STACK_BASE/shared/cache && chmod 775 $STACK_BASE/shared/cache && sudo chmod g+s $STACK_BASE/shared/cache
19 changes: 19 additions & 0 deletions recipes/deploy/cloud66/deploy_hooks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
production:
first_thing:
- snippet: cloud66/node
target: rails
execute: true
- snippet: cloud66/newrelic
target: any
execute: true
- source: /.cloud66/bower.sh
destination: /tmp/bower.sh
target: rails
execute: true
after_rails:
- source: /.cloud66/cache_permissions.sh
destination: /tmp/cache_permissions.sh
target: rails
run_on: all_servers
execute: true
sudo: true
2 changes: 2 additions & 0 deletions recipes/deploy/docker/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public/uploads
vendor/bundle
57 changes: 57 additions & 0 deletions recipes/deploy/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
FROM phusion/passenger-ruby24
MAINTAINER MarsBased "hola@marsbased.com"

ENV HOME /home/app/app-name

# Install software dependencies
RUN apt-get update
RUN apt-get install -y imagemagick

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

# Expose Nginx HTTP service
EXPOSE 80
EXPOSE 443

# Start Nginx / Passenger
RUN rm -f /etc/service/nginx/down

# Remove the default site
RUN rm /etc/nginx/sites-enabled/default

# Create app home dir
RUN mkdir -p $HOME
WORKDIR $HOME

# Install bundle of gems
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
RUN bundle install --without development test

# Add the nginx site and config
ADD docker/nginx.conf /etc/nginx/sites-enabled/app-name.conf
ADD docker/rails-env.conf /etc/nginx/main.d/rails-env.conf

# Add the Rails app
ADD . /home/app/app-name

RUN RAILS_ENV=production SECRET_KEY_BASE=NOT-IMPORTANT bin/rake assets:precompile

# Add a tmp folder for pids
RUN mkdir -p tmp/pids

# Define volumes

VOLUME $HOME/public/uploads
VOLUME $HOME/log

# Configure init scripts
RUN mkdir -p /etc/my_init.d
ADD docker/fix_permissions.sh /etc/my_init.d/fix_permissions.sh
RUN chmod +x /etc/my_init.d/fix_permissions.sh

RUN chown -R app:app $HOME

# Clean up APT and bundler when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*.
17 changes: 17 additions & 0 deletions recipes/deploy/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

## Docker

Production deploys are managed with Docker. The Dockerfile makes use of some config files that can be found in the `docker` folder.

- **fix_permissions.sh**: Makes the mountable volumes writable for the
application user.
- **rails-env.conf**: Makes env variables visible to Nginx. As the
application is running on Passenger through Nginx, every ENV variable
that needs to reach the application must be defined here.
- **nginx.conf**: Base nginx configuration. The file contains
instructions to tune the application performance.

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.

The docker-compose file is not included in the project and needs to be
configured on a project basis.
3 changes: 3 additions & 0 deletions recipes/deploy/docker/fix_permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
chown -R app:app /home/app/app-name/public/uploads
chown -R app:app /home/app/app-name/log
30 changes: 30 additions & 0 deletions recipes/deploy/docker/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
server {
listen 80 default_server;
root /home/app/app-name/public;
passenger_enabled on;
passenger_user app;
passenger_ruby /usr/bin/ruby2.4;

## Configure this with the same value of the passenger_max_pool_size
# passenger_min_instances 10;

location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
break;
}

location ~ ^/uploads/ {
expires 24h;
add_header Cache-Control public;
add_header ETag "";
break;
}
}

## The number for max_pool_size should be (TOTAL_RAM * 0.75) / RAM_PER_PROCESS
# passenger_max_pool_size 10;

## Configure this option with the app host url to preload app after deploy
# passenger_pre_start https://app-name.com;
3 changes: 3 additions & 0 deletions recipes/deploy/docker/rails-env.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# List here all the ENV variables that need to be available in the application
# Only ENV variable names are expected, not their values.
# env APP_HOST;
9 changes: 4 additions & 5 deletions recipes/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def init_file
def run_generators
case ask_framework_for_forms
when 'marsman'
@template.initializer 'simple_form.rb', simple_form_template('marsman.rb')
@template.initializer 'simple_form.rb',
relative_file_content('simple_form/marsman.rb')
when 'bootstrap'
@template.generate 'simple_form:install --bootstrap'
else
Expand All @@ -25,16 +26,14 @@ def run_generators

def add_sample_i18n
@template.run 'rm config/locales/simple_form.en.yml'
@template.append_to_file 'config/locales/en.yml', simple_form_template('en.yml')
@template.append_to_file 'config/locales/en.yml',
relative_file_content('simple_form/en.yml')
end

def ask_framework_for_forms
@template.ask('What framework do you want to use for your forms?',
limited_to: %w(marsman bootstrap default))
end

def simple_form_template(filename)
File.read(File.join(File.dirname(__FILE__), 'simple_form', filename))
end
end
end

0 comments on commit b65ccc6

Please sign in to comment.