Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Added multi-platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Apr 24, 2016
1 parent 72d4b9f commit 6118154
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 65 deletions.
13 changes: 10 additions & 3 deletions README.md
Expand Up @@ -9,7 +9,7 @@ A [chef](https://www.chef.io/) cookbook to deploy Ruby applications to Amazon Op

## Quick Start

This cookbook is design to "just work". So in base case scenario, all you have
This cookbook is designed to "just work". So in base case scenario, all you have
to do is create a layer and application with assigned RDS data source, then
[add recipes to the corresponding OpsWorks actions](#recipes).

Expand Down Expand Up @@ -41,8 +41,15 @@ to do is create a layer and application with assigned RDS data source, then

### Platform

Currenty this cookbook was tested only under Ubuntu 14.04, more platforms will
be added soon. However, other Debian family distributions are assumed to work.
This cookbook was tested on the following OpsWorks platforms:

* Amazon Linux 2016.03
* Amazon Linux 2015.09
* Amazon Linux 2015.03
* Ubuntu 14.04 LTS
* Ubuntu 12.04 LTS

In addition, all recent Debian family distrubutions are assumed to work.

## Attributes

Expand Down
5 changes: 3 additions & 2 deletions attributes/default.rb
Expand Up @@ -66,9 +66,10 @@

default['defaults']['framework']['migrate'] = true
default['defaults']['framework']['migration_command'] =
'bundle exec rake db:version > /dev/null 2>&1 && bundle exec rake db:migrate || bundle exec rake db:setup'
'/usr/local/bin/bundle exec rake db:version > /dev/null 2>&1 ' \
'&& /usr/local/bin/bundle exec rake db:migrate || /usr/local/bin/bundle exec rake db:setup'
default['defaults']['framework']['assets_precompile'] = true
default['defaults']['framework']['assets_precompilation_command'] = 'bundle exec rake assets:precompile'
default['defaults']['framework']['assets_precompilation_command'] = '/usr/local/bin/bundle exec rake assets:precompile'

# worker
## common
Expand Down
2 changes: 1 addition & 1 deletion libraries/drivers_db_postgresql.rb
Expand Up @@ -4,7 +4,7 @@ module Db
class Postgresql < Base
adapter :postgresql
allowed_engines :postgres, :postgresql
packages 'libpq-dev'
packages debian: 'libpq-dev', rhel: 'postgresql94-devel'
end
end
end
9 changes: 6 additions & 3 deletions libraries/drivers_dsl_packages.rb
Expand Up @@ -5,8 +5,11 @@ module Packages
def self.included(klass)
klass.instance_eval do
def packages(*to_support)
@packages = to_support.map(&:to_s) if to_support.present?
@packages || []
@packages ||= []
(to_support || []).each do |pkg|
@packages.push((pkg.is_a?(Hash) ? pkg : { all: pkg.to_s }).stringify_keys)
end
@packages.uniq
end
end
end
Expand All @@ -17,7 +20,7 @@ def packages

def handle_packages(context)
packages.each do |pkg|
context.package pkg do
context.package(pkg.key?('all') ? pkg['all'] : pkg[node['platform_family']] || pkg[node['platform']]) do
action :install
end
end
Expand Down
5 changes: 5 additions & 0 deletions libraries/drivers_framework_base.rb
Expand Up @@ -3,6 +3,11 @@ module Drivers
module Framework
class Base < Drivers::Base
include Drivers::Dsl::Output
include Drivers::Dsl::Packages

def setup(context)
handle_packages(context)
end

def out
handle_output(raw_out)
Expand Down
1 change: 1 addition & 0 deletions libraries/drivers_framework_rails.rb
Expand Up @@ -7,6 +7,7 @@ class Rails < Drivers::Framework::Base
output filter: [
:migrate, :migration_command, :deploy_environment, :assets_precompile, :assets_precompilation_command
]
packages debian: 'zlib1g-dev', rhel: 'zlib-devel'

def raw_out
super.merge(deploy_environment: { 'RAILS_ENV' => 'production' })
Expand Down
3 changes: 3 additions & 0 deletions metadata.rb
Expand Up @@ -13,5 +13,8 @@
depends 'nginx', '~> 2.7'
depends 'application_ruby'

supports 'amazon', '>= 2015.03'
supports 'ubuntu', '>= 12.04'

source_url 'https://github.com/ajgon/opsworks_ruby'
issues_url 'https://github.com/ajgon/opsworks_ruby/issues'
20 changes: 16 additions & 4 deletions recipes/setup.rb
Expand Up @@ -8,13 +8,25 @@

# Ruby and bundler
include_recipe 'deployer'
include_recipe 'ruby-ng::dev'
if node['platform_family'] == 'debian'
include_recipe 'ruby-ng::dev'
else
ruby_pkg_version = node['ruby-ng']['ruby_version'].split('.')[0..1]
package "ruby#{ruby_pkg_version.join('')}"
package "ruby#{ruby_pkg_version.join('')}-devel"
execute "/usr/sbin/alternatives --set ruby /usr/bin/ruby#{ruby_pkg_version.join('.')}"
end

gem_package 'bundler'
link '/usr/local/bin/bundle' do
to '/usr/bin/bundle'
if node['platform_family'] == 'debian'
link '/usr/local/bin/bundle' do
to '/usr/bin/bundle'
end
else
link '/usr/local/bin/bundle' do
to '/usr/local/bin/bundler'
end
end

every_enabled_application do |application, _deploy|
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/recipes/configure_spec.rb
Expand Up @@ -9,7 +9,7 @@

describe 'opsworks_ruby::configure' do
let(:chef_run) do
ChefSpec::SoloRunner.new do |solo_node|
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['deploy'] = node['deploy']
solo_node.set['nginx'] = node['nginx']
end.converge(described_recipe)
Expand Down Expand Up @@ -121,7 +121,7 @@
end

it 'enables ssl rules for legacy browsers in nginx config' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy[aws_opsworks_app['shortname']]['webserver']['ssl_for_legacy_browsers'] = true
solo_node.set['deploy'] = deploy
Expand Down Expand Up @@ -234,7 +234,7 @@
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/recipes/deploy_spec.rb
Expand Up @@ -9,7 +9,7 @@

describe 'opsworks_ruby::deploy' do
let(:chef_run) do
ChefSpec::SoloRunner.new do |solo_node|
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['scm'].delete('ssh_wrapper')
solo_node.set['deploy'] = deploy
Expand Down Expand Up @@ -59,7 +59,7 @@
expect(chef_run).to run_execute('stop unicorn')
expect(chef_run).to run_execute('start unicorn')
expect(chef_run).to run_execute('assets:precompile').with(
command: 'bundle exec rake assets:precompile',
command: '/usr/local/bin/bundle exec rake assets:precompile',
environment: { 'RAILS_ENV' => 'production' },
cwd: "/srv/www/#{aws_opsworks_app['shortname']}/current"
)
Expand All @@ -75,7 +75,7 @@
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)

Expand All @@ -89,7 +89,7 @@
aws_opsworks_app.merge(shortname: 'a1'),
aws_opsworks_app.merge(shortname: 'a2')
])
chef_run = ChefSpec::SoloRunner.new do |solo_node|
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = { 'a1' => {}, 'a2' => {} }
solo_node.set['applications'] = ['a1']
Expand Down
148 changes: 108 additions & 40 deletions spec/unit/recipes/setup_spec.rb
Expand Up @@ -9,11 +9,16 @@

describe 'opsworks_ruby::setup' do
let(:chef_run) do
ChefSpec::SoloRunner.new do |solo_node|
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['deploy'] = node['deploy']
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)
end
let(:chef_run_rhel) do
ChefSpec::SoloRunner.new(platform: 'amazon', version: '2015.03') do |solo_node|
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)
end

before do
stub_search(:aws_opsworks_app, '*:*').and_return([aws_opsworks_app])
Expand All @@ -27,59 +32,118 @@
end

context 'Rubies' do
it 'installs ruby 2.0' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.0' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run).to install_package('ruby2.0')
expect(chef_run).to install_package('ruby2.0-dev')
end

it 'installs ruby 2.1' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.1' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run).to install_package('ruby2.1')
expect(chef_run).to install_package('ruby2.1-dev')
context 'Debian' do
it 'installs ruby 2.0' do
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.0' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run).to install_package('ruby2.0')
expect(chef_run).to install_package('ruby2.0-dev')
end

it 'installs ruby 2.1' do
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.1' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run).to install_package('ruby2.1')
expect(chef_run).to install_package('ruby2.1-dev')
end

it 'installs ruby 2.2' do
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.2' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run).to install_package('ruby2.2')
expect(chef_run).to install_package('ruby2.2-dev')
end

it 'installs ruby 2.3' do
expect(chef_run).to install_package('ruby2.3')
expect(chef_run).to install_package('ruby2.3-dev')
end
end

it 'installs ruby 2.2' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.2' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run).to install_package('ruby2.2')
expect(chef_run).to install_package('ruby2.2-dev')
end

it 'installs ruby 2.3' do
expect(chef_run).to install_package('ruby2.3')
expect(chef_run).to install_package('ruby2.3-dev')
context 'rhel' do
it 'installs ruby 2.0' do
chef_run_rhel = ChefSpec::SoloRunner.new(platform: 'amazon', version: '2015.03') do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.0' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run_rhel).to install_package('ruby20')
expect(chef_run_rhel).to install_package('ruby20-devel')
expect(chef_run_rhel).to run_execute('/usr/sbin/alternatives --set ruby /usr/bin/ruby2.0')
end

it 'installs ruby 2.1' do
chef_run_rhel = ChefSpec::SoloRunner.new(platform: 'amazon', version: '2015.03') do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.1' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run_rhel).to install_package('ruby21')
expect(chef_run_rhel).to install_package('ruby21-devel')
expect(chef_run_rhel).to run_execute('/usr/sbin/alternatives --set ruby /usr/bin/ruby2.1')
end

it 'installs ruby 2.2' do
chef_run_rhel = ChefSpec::SoloRunner.new(platform: 'amazon', version: '2015.03') do |solo_node|
solo_node.set['ruby'] = { 'version' => '2.2' }
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = node['deploy']
end.converge(described_recipe)

expect(chef_run_rhel).to install_package('ruby22')
expect(chef_run_rhel).to install_package('ruby22-devel')
expect(chef_run_rhel).to run_execute('/usr/sbin/alternatives --set ruby /usr/bin/ruby2.2')
end

it 'installs ruby 2.3' do
expect(chef_run_rhel).to install_package('ruby23')
expect(chef_run_rhel).to install_package('ruby23-devel')
expect(chef_run_rhel).to run_execute('/usr/sbin/alternatives --set ruby /usr/bin/ruby2.3')
end
end
end

context 'Gems' do
it 'bundler' do
it 'debian bundler' do
expect(chef_run).to install_gem_package(:bundler)
expect(chef_run).to create_link('/usr/local/bin/bundle')
expect(chef_run).to create_link('/usr/local/bin/bundle').with(to: '/usr/bin/bundle')
end

it 'rhel bundler' do
expect(chef_run_rhel).to install_gem_package(:bundler)
expect(chef_run_rhel).to create_link('/usr/local/bin/bundle').with(to: '/usr/local/bin/bundler')
end
end

context 'Postgresql + git + nginx' do
it 'installs required packages' do
it 'installs required packages for debian' do
expect(chef_run).to install_package('nginx')
expect(chef_run).to install_package('zlib1g-dev')
expect(chef_run).to install_package('git')
expect(chef_run).to install_package('libpq-dev')
end

it 'installs required packages for rhel' do
expect(chef_run_rhel).to install_package('nginx')
expect(chef_run_rhel).to install_package('zlib-devel')
expect(chef_run_rhel).to install_package('git')
expect(chef_run_rhel).to install_package('postgresql94-devel')
end

it 'defines service which starts nginx' do
expect(chef_run).to start_service('nginx')
end
Expand All @@ -90,13 +154,17 @@
stub_search(:aws_opsworks_rds_db_instance, '*:*').and_return([aws_opsworks_rds_db_instance(engine: 'mysql')])
end

it 'installs required packages' do
it 'installs required packages for debian' do
expect(chef_run).to install_package('libmysqlclient-dev')
end

it 'installs required packages for rhel' do
expect(chef_run_rhel).to install_package('mysql-devel')
end
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)

Expand Down

0 comments on commit 6118154

Please sign in to comment.