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

Commit

Permalink
feat(framework): "Padrino" support added
Browse files Browse the repository at this point in the history
Resolves #44

Signed-off-by: Igor Rzegocki <igor@rzegocki.pl>
  • Loading branch information
ajgon committed Sep 19, 2016
1 parent 33bdeec commit e99a7c6
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 82 deletions.
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -26,6 +26,7 @@ then [add recipes to the corresponding OpsWorks actions](#recipes).
* Framework
* Null (no framework)
* hanami.rb
* Padrino
* Ruby on Rails
* App server
* Null (no appserver)
Expand Down Expand Up @@ -145,7 +146,7 @@ Pre-optimalization for specific frameworks (like migrations, cache etc.).
Currently `hanami.rb` and `Rails` are supported.

* `app['framework']['adapter']`
* **Supported values:** `null`, `rails`
* **Supported values:** `null`, `hanami`, `padrino`, `rails`
* **Default:** `rails`
* Ruby framework used in project.
* `app['framework']['migrate']`
Expand All @@ -162,6 +163,22 @@ Currently `hanami.rb` and `Rails` are supported.
* `app['framework']['assets_precompilation_command']`
* A command which will be invoked to precompile assets.

#### padrino

For Padrino, slight adjustments needs to be made. Since there are many database
adapters supported, instead of creating configuration for each one, the
`DATABASE_URL` environmental variable is provided. You need to parse it in your
`config/database.rb` file and properly pass to the configuration options.
For example, for ActiveRecord:

```ruby
database_url = ENV['DATABASE_URL'] && ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(ENV['DATABASE_URL']).to_hash
ActiveRecord::Base.configurations[:production] = database_url || {
:adapter => 'sqlite3',
:database => Padrino.root('db', 'dummy_app_production.db')
}
```

#### rails

* `app['framework']['envs_in_console']`
Expand Down
38 changes: 38 additions & 0 deletions libraries/drivers_framework_base.rb
Expand Up @@ -21,6 +21,44 @@ def raw_out

def validate_app_engine
end

protected

def assets_precompile
output = out
deploy_to = deploy_dir(app)
env = environment.merge('HOME' => node['deployer']['home'])

context.execute 'assets:precompile' do
command output[:assets_precompilation_command]
user node['deployer']['user']
cwd File.join(deploy_to, 'current')
group www_group
environment env
end
end

# rubocop:disable Metrics/AbcSize
def database_url
deploy_to = deploy_dir(app)
database_url = "sqlite://#{deploy_to}/current/db/#{app['shortname']}_#{globals[:environment]}.sqlite"

Array.wrap(options[:databases]).each do |db|
next unless db.applicable_for_configuration?

database_url =
"#{db.out[:adapter]}://#{db.out[:username]}:#{db.out[:password]}@#{db.out[:host]}/#{db.out[:database]}"

database_url = "sqlite://#{deploy_to}/current/#{db.out[:database]}" if db.out[:adapter].start_with?('sqlite')
end

database_url
end
# rubocop:enable Metrics/AbcSize

def environment
app['environment'].merge(out[:deploy_environment])
end
end
end
end
34 changes: 0 additions & 34 deletions libraries/drivers_framework_hanami.rb
Expand Up @@ -7,7 +7,6 @@ class Hanami < 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
assets_command = node['deploy'][app['shortname']]['framework']['assets_precompilation_command'] ||
Expand Down Expand Up @@ -54,39 +53,6 @@ def link_env
ignore_failure true
end
end

def assets_precompile
output = out
deploy_to = deploy_dir(app)
env = environment.merge('HOME' => node['deployer']['home'])

context.execute 'assets:precompile' do
command output[:assets_precompilation_command]
user node['deployer']['user']
cwd File.join(deploy_to, 'current')
group www_group
environment env
end
end

def database_url
database_url = "sqlite://db/#{app['shortname']}_#{globals[:environment]}.sqlite"

Array.wrap(options[:databases]).each do |db|
next unless db.applicable_for_configuration?

database_url =
"#{db.out[:adapter]}://#{db.out[:username]}:#{db.out[:password]}@#{db.out[:host]}/#{db.out[:database]}"

database_url = "sqlite://#{db.out[:database]}" if db.out[:adapter].start_with?('sqlite')
end

database_url
end

def environment
app['environment'].merge(out[:deploy_environment])
end
end
end
end
39 changes: 39 additions & 0 deletions libraries/drivers_framework_padrino.rb
@@ -0,0 +1,39 @@
# frozen_string_literal: true
module Drivers
module Framework
class Padrino < Drivers::Framework::Base
adapter :padrino
allowed_engines :padrino
output filter: [
:migrate, :migration_command, :deploy_environment, :assets_precompile, :assets_precompilation_command
]

def raw_out
super.merge(
deploy_environment: { 'RACK_ENV' => globals[:environment], 'DATABASE_URL' => database_url },
assets_precompile: node['deploy'][app['shortname']]['framework']['assets_precompile']
)
end

def deploy_before_restart
assets_precompile if out[:assets_precompile]
end

private

def assets_precompile
output = out
deploy_to = deploy_dir(app)
env = environment.merge('HOME' => node['deployer']['home'])

context.execute 'assets:precompile' do
command output[:assets_precompilation_command]
user node['deployer']['user']
cwd File.join(deploy_to, 'current')
group www_group
environment env
end
end
end
end
end
14 changes: 0 additions & 14 deletions libraries/drivers_framework_rails.rb
Expand Up @@ -47,20 +47,6 @@ def database_yml(db)
end
end

def assets_precompile
output = out
deploy_to = deploy_dir(app)
env = environment.merge('HOME' => node['deployer']['home'])

context.execute 'assets:precompile' do
command output[:assets_precompilation_command]
user node['deployer']['user']
cwd File.join(deploy_to, 'current')
group www_group
environment env
end
end

def setup_rails_console
return unless out[:envs_in_console]
deploy_to = deploy_dir(app)
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/libraries/drivers_framework_hanami_spec.rb
Expand Up @@ -15,7 +15,8 @@
assets_precompile: true,
assets_precompilation_command: '/usr/local/bin/bundle exec hanami assets precompile',
deploy_environment: {
'HANAMI_ENV' => 'staging', 'DATABASE_URL' => 'sqlite://db/dummy_project_staging.sqlite'
'HANAMI_ENV' => 'staging',
'DATABASE_URL' => 'sqlite:///srv/www/dummy_project/current/db/dummy_project_staging.sqlite'
},
migration_command: '/usr/local/bin/bundle exec hanami db migrate',
migrate: false
Expand Down
25 changes: 25 additions & 0 deletions spec/unit/libraries/drivers_framework_padrino_spec.rb
@@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'spec_helper'

describe Drivers::Framework::Padrino do
it 'receives and exposes app and node' do
driver = described_class.new(dummy_context(node), aws_opsworks_app)

expect(driver.app).to eq aws_opsworks_app
expect(driver.send(:node)).to eq node
expect(driver.options).to eq({})
end

it 'returns proper out data' do
expect(described_class.new(dummy_context(node), aws_opsworks_app).out).to eq(
assets_precompile: nil,
assets_precompilation_command: 'bundle exec rake assets:precompile',
deploy_environment: {
'RACK_ENV' => 'staging',
'DATABASE_URL' => 'sqlite:///srv/www/dummy_project/current/db/dummy_project_staging.sqlite'
},
migration_command: 'rake db:migrate',
migrate: false
)
end
end
61 changes: 29 additions & 32 deletions spec/unit/recipes/configure_spec.rb
Expand Up @@ -609,7 +609,7 @@
end
end

context 'Sqlite3 + Thin + delayed_job' do
context 'Sqlite3 + Thin + padrino + delayed_job' do
let(:dummy_node) do
node(
deploy: {
Expand All @@ -618,7 +618,7 @@
environment: 'staging',
appserver: node['deploy']['dummy_project']['appserver'].merge('adapter' => 'thin'),
webserver: node['deploy']['dummy_project']['webserver'],
framework: node['deploy']['dummy_project']['framework'],
framework: node['deploy']['dummy_project']['framework'].merge('adapter' => 'padrino'),
worker: node['deploy']['dummy_project']['worker'].merge('adapter' => 'delayed_job')
}
}
Expand All @@ -642,16 +642,6 @@
stub_search(:aws_opsworks_rds_db_instance, '*:*').and_return([])
end

it 'creates proper database.yml template' do
db_config = Drivers::Db::Sqlite.new(chef_run, aws_opsworks_app(data_sources: [])).out
expect(db_config[:adapter]).to eq 'sqlite3'
expect(db_config[:database]).to eq 'db/data.sqlite3'
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/database.yml").with_content(
JSON.parse({ development: db_config, production: db_config, staging: db_config }.to_json).to_yaml
)
end

it 'creates proper thin.yml file' do
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/thin.yml")
Expand All @@ -674,6 +664,9 @@
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/thin.service")
.with_content('ENV[\'ENV_VAR1\'] = "test"')
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/thin.service")
.with_content('ENV[\'RACK_ENV\'] = "staging"')
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/thin.service")
.with_content("APP_NAME=\"#{aws_opsworks_app['shortname']}\"")
Expand Down Expand Up @@ -753,16 +746,17 @@
.to render_file("/etc/monit/conf.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
.with_content(
'start program = "/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" bin/delayed_job start ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 0 --queues=test_queue 2>&1 ' \
'| logger -t delayed_job-dummy_project-1\'" with timeout 90 seconds'
'ENV_VAR2="some data" RACK_ENV="staging" DATABASE_URL="sqlite:///srv/www/dummy_project/current/db/' \
'data.sqlite3" bin/delayed_job start --pid-dir=/srv/www/dummy_project/shared/pids/ -i 0 --queues=test_queue' \
' 2>&1 | logger -t delayed_job-dummy_project-1\'" with timeout 90 seconds'
)
expect(chef_run)
.to render_file("/etc/monit/conf.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
.with_content(
'stop program = "/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" bin/delayed_job stop ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 0\'" with timeout 90 seconds'
'ENV_VAR2="some data" RACK_ENV="staging" DATABASE_URL="sqlite:///srv/www/dummy_project/current/db/' \
'data.sqlite3" bin/delayed_job stop --pid-dir=/srv/www/dummy_project/shared/pids/ -i 0\'" ' \
'with timeout 90 seconds'
)
expect(chef_run)
.to render_file("/etc/monit/conf.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
Expand All @@ -774,16 +768,17 @@
.to render_file("/etc/monit/conf.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
.with_content(
'start program = "/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" bin/delayed_job start ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 1 --queues=test_queue 2>&1 ' \
'| logger -t delayed_job-dummy_project-2\'" with timeout 90 seconds'
'ENV_VAR2="some data" RACK_ENV="staging" DATABASE_URL="sqlite:///srv/www/dummy_project/current/db/' \
'data.sqlite3" bin/delayed_job start --pid-dir=/srv/www/dummy_project/shared/pids/ -i 1 --queues=test_queue' \
' 2>&1 | logger -t delayed_job-dummy_project-2\'" with timeout 90 seconds'
)
expect(chef_run)
.to render_file("/etc/monit/conf.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
.with_content(
'stop program = "/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" bin/delayed_job stop ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 1\'" with timeout 90 seconds'
'ENV_VAR2="some data" RACK_ENV="staging" DATABASE_URL="sqlite:///srv/www/dummy_project/current/db/' \
'data.sqlite3" bin/delayed_job stop --pid-dir=/srv/www/dummy_project/shared/pids/ -i 1\'" ' \
'with timeout 90 seconds'
)
expect(chef_run)
.to render_file("/etc/monit/conf.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
Expand All @@ -804,16 +799,17 @@
.to render_file("/etc/monit.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
.with_content(
'start program = "/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" bin/delayed_job start ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 0 --queues=test_queue 2>&1 ' \
'| logger -t delayed_job-dummy_project-1\'" with timeout 90 seconds'
'ENV_VAR2="some data" RACK_ENV="staging" DATABASE_URL="sqlite:///srv/www/dummy_project/current/db/' \
'data.sqlite3" bin/delayed_job start --pid-dir=/srv/www/dummy_project/shared/pids/ -i 0 ' \
'--queues=test_queue 2>&1 | logger -t delayed_job-dummy_project-1\'" with timeout 90 seconds'
)
expect(chef_run_rhel)
.to render_file("/etc/monit.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
.with_content(
'stop program = "/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" bin/delayed_job stop ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 0\'" with timeout 90 seconds'
'ENV_VAR2="some data" RACK_ENV="staging" DATABASE_URL="sqlite:///srv/www/dummy_project/current/db/' \
'data.sqlite3" bin/delayed_job stop --pid-dir=/srv/www/dummy_project/shared/pids/ -i 0\'" ' \
'with timeout 90 seconds'
)
expect(chef_run_rhel)
.to render_file("/etc/monit.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
Expand All @@ -825,16 +821,17 @@
.to render_file("/etc/monit.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
.with_content(
'start program = "/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" bin/delayed_job start ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 1 --queues=test_queue 2>&1 ' \
'| logger -t delayed_job-dummy_project-2\'" with timeout 90 seconds'
'ENV_VAR2="some data" RACK_ENV="staging" DATABASE_URL="sqlite:///srv/www/dummy_project/current/db/' \
'data.sqlite3" bin/delayed_job start --pid-dir=/srv/www/dummy_project/shared/pids/ -i 1 ' \
'--queues=test_queue 2>&1 | logger -t delayed_job-dummy_project-2\'" with timeout 90 seconds'
)
expect(chef_run_rhel)
.to render_file("/etc/monit.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
.with_content(
'stop program = "/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" bin/delayed_job stop ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 1\'" with timeout 90 seconds'
'ENV_VAR2="some data" RACK_ENV="staging" DATABASE_URL="sqlite:///srv/www/dummy_project/current/db/' \
'data.sqlite3" bin/delayed_job stop --pid-dir=/srv/www/dummy_project/shared/pids/ -i 1\'" ' \
'with timeout 90 seconds'
)
expect(chef_run_rhel)
.to render_file("/etc/monit.d/delayed_job_#{aws_opsworks_app['shortname']}.monitrc")
Expand Down

0 comments on commit e99a7c6

Please sign in to comment.