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

Commit

Permalink
fix: Moved global deploy parameters to app['global'] section
Browse files Browse the repository at this point in the history
From this commit all chef parameters like `keep_releases` or `symlinks`
should be provided in `app['global']` section (i.e.
node['deploy']['app_name']['global']['keep_releases'] etc.). The old
format (without the 'global' index) is still valid, but will show a
DEPRECATION warning.

Resolves: #56

BREAKING CHANGE: `app['create_dirs_before_symlink']`,
`app['purge_before_symlink']`, `app['rollback_on_error']` and
`app['symlinks']` are now `app['global']['create_dirs_before_symlink']`,
`app['global']['purge_before_symlink']`,
`app['global']['rollback_on_error']` and `app['global']['symlink']`. The
old format still works, but it shows DEPRECATION warning. It will be
removed in one of the next major releases.
  • Loading branch information
ajgon committed Oct 27, 2016
1 parent 06e59c6 commit b4f8d6b
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 49 deletions.
24 changes: 11 additions & 13 deletions attributes/default.rb
@@ -1,18 +1,5 @@
# frozen_string_literal: true

# deploy
default['defaults']['deploy']['symlinks'] = {
'system' => 'public/system',
'assets' => 'public/assets',
'cache' => 'tmp/cache',
'pids' => 'tmp/pids',
'log' => 'log'
}
default['defaults']['deploy']['create_dirs_before_symlink'] =
%w(tmp public config ../../shared/cache ../../shared/assets)
default['defaults']['deploy']['purge_before_symlink'] = %w(log tmp/cache tmp/pids public/system public/assets)
default['defaults']['deploy']['rollback_on_error'] = true

# ruby

default['build-essential']['compile_time'] = true
Expand All @@ -24,6 +11,17 @@

# global
default['defaults']['global']['environment'] = 'production'
default['defaults']['global']['symlinks'] = {
'system' => 'public/system',
'assets' => 'public/assets',
'cache' => 'tmp/cache',
'pids' => 'tmp/pids',
'log' => 'log'
}
default['defaults']['global']['create_dirs_before_symlink'] =
%w(tmp public config ../../shared/cache ../../shared/assets)
default['defaults']['global']['purge_before_symlink'] = %w(log tmp/cache tmp/pids public/system public/assets)
default['defaults']['global']['rollback_on_error'] = true

# database
## common
Expand Down
40 changes: 35 additions & 5 deletions docs/source/attributes.rst
Expand Up @@ -40,17 +40,47 @@ Application attributes
global
~~~~~~

| Global parameters apply to the whole application, and can be used by
any section
| (framework, appserver etc.).
Global parameters apply to the whole application, and can be used by any section
(framework, appserver etc.).

- ``app['global']['environment']``

- **Type:** string
- **Default:** ``production``
- Sets the “deploy environment” for all the app-related (for example
``RAILS_ENV``
- Sets the “deploy environment” for all the app-related (for example ``RAILS_ENV``
in Rails) actions in the project (server, worker, etc.)

- ``app['global']['symlinks']``

- **Type:** key-value
- **Default:** ``{ "system": "public/system", "assets": "public/assets", "cache": "tmp/cache", "pids": "tmp/pids", "log": "log" }``
- **Important Notice:** Any values for this parameter will be *merged* to the defaults
- List of symlinks created to the ``shared`` directory. The format is ``{"shared_path": "release_path"}``.
For example ``{"system", "public/system"}`` means: Link ``/src/www/app_name/current/public/system`` to
``/src/www/app_name/shared/system``.

- ``app['global']['create_dirs_before_symlink']``

- **Type:** array
- **Default:** ``["tmp", "public", "config", "../../shared/cache", "../../shared/assets"]``
- **Important Notice:** Any values for this parameter will be *appended* to the defaults
- List of directories to be created before symlinking. Paths are relative to ``release_path``.
For example ``tmp`` becomes ``/srv/www/app_name/current/tmp``.

- ``app['global']['purge_before_symlink']``

- **Type:** array
- **Default:** ``["log", "tmp/cache", "tmp/pids", "public/system", "public/assets"]``
- **Important Notice:** Any values for this parameter will be *appended* to the defaults
- List of directories to be wiped out before symlinking. Paths are relative to ``release_path``.
For example ``tmp`` becomes ``/srv/www/app_name/current/tmp``.

- ``app['global']['rollback_on_error']``

- **Type:** boolean
- **Default:** ``true``
- When set to true, any failed deploy will be removed from ``releases`` directory.

database
~~~~~~~~

Expand Down
6 changes: 3 additions & 3 deletions libraries/drivers_appserver_base.rb
Expand Up @@ -59,7 +59,7 @@ def manual_action(action)

# rubocop:disable Metrics/AbcSize
def add_appserver_config
opts = { deploy_dir: deploy_dir(app), out: out, deploy_env: globals[:environment],
opts = { deploy_dir: deploy_dir(app), out: out, deploy_env: deploy_env,
webserver: Drivers::Webserver::Factory.build(context, app).adapter,
appserver_config: appserver_config }

Expand All @@ -74,8 +74,8 @@ def add_appserver_config
# rubocop:enable Metrics/AbcSize

def add_appserver_service_script
opts = { deploy_dir: deploy_dir(app), app_shortname: app['shortname'], deploy_env: globals[:environment],
name: adapter, command: appserver_command, environment: environment }
opts = { deploy_dir: deploy_dir(app), app_shortname: app['shortname'], name: adapter, environment: environment,
command: appserver_command, deploy_env: deploy_env }

context.template File.join(opts[:deploy_dir], File.join('shared', 'scripts', "#{opts[:name]}.service")) do
owner node['deployer']['user']
Expand Down
4 changes: 4 additions & 0 deletions libraries/drivers_base.rb
Expand Up @@ -74,6 +74,10 @@ def adapter
self.class.adapter
end

def deploy_env
globals(:environment, app['shortname'])
end

def validate_app_engine
return validate_node_engine if app_engine.blank?
validate_engine(:app)
Expand Down
2 changes: 1 addition & 1 deletion libraries/drivers_framework_base.rb
Expand Up @@ -78,7 +78,7 @@ def link_sqlite_database

def database_url
deploy_to = deploy_dir(app)
database_url = "sqlite://#{deploy_to}/shared/db/#{app['shortname']}_#{globals[:environment]}.sqlite"
database_url = "sqlite://#{deploy_to}/shared/db/#{app['shortname']}_#{deploy_env}.sqlite"

Array.wrap(options[:databases]).each do |db|
next unless db.applicable_for_configuration?
Expand Down
7 changes: 4 additions & 3 deletions libraries/drivers_framework_hanami.rb
Expand Up @@ -15,7 +15,8 @@ def raw_out
'/usr/local/bin/bundle exec hanami db migrate'

super.merge(
deploy_environment: { 'HANAMI_ENV' => globals[:environment], 'DATABASE_URL' => database_url },
deploy_environment:
{ 'HANAMI_ENV' => deploy_env, 'DATABASE_URL' => database_url },
assets_precompilation_command: assets_command,
migration_command: migration_command
)
Expand All @@ -36,7 +37,7 @@ def build_env
deploy_to = deploy_dir(app)
env = environment

context.template File.join(deploy_to, 'shared', 'config', ".env.#{globals[:environment]}") do
context.template File.join(deploy_to, 'shared', 'config', ".env.#{deploy_env}") do
owner node['deployer']['user']
group www_group
source 'dot_env.erb'
Expand All @@ -46,7 +47,7 @@ def build_env

def link_env
deploy_to = deploy_dir(app)
env_name = globals[:environment]
env_name = deploy_env

context.link File.join(deploy_to, 'current', ".env.#{env_name}") do
to File.join(deploy_to, 'shared', 'config', ".env.#{env_name}")
Expand Down
2 changes: 1 addition & 1 deletion libraries/drivers_framework_null.rb
Expand Up @@ -7,7 +7,7 @@ class Null < Drivers::Framework::Base
output filter: [:deploy_environment]

def raw_out
super.merge(deploy_environment: { 'RACK_ENV' => globals[:environment] })
super.merge(deploy_environment: { 'RACK_ENV' => deploy_env })
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion libraries/drivers_framework_padrino.rb
Expand Up @@ -10,7 +10,7 @@ class Padrino < Drivers::Framework::Base

def raw_out
super.merge(
deploy_environment: { 'RACK_ENV' => globals[:environment], 'DATABASE_URL' => database_url },
deploy_environment: { 'RACK_ENV' => deploy_env, 'DATABASE_URL' => database_url },
assets_precompile: node['deploy'][app['shortname']]['framework']['assets_precompile']
)
end
Expand Down
6 changes: 3 additions & 3 deletions libraries/drivers_framework_rails.rb
Expand Up @@ -11,7 +11,7 @@ class Rails < Drivers::Framework::Base
packages debian: 'zlib1g-dev', rhel: 'zlib-devel'

def raw_out
super.merge(deploy_environment: { 'RAILS_ENV' => globals[:environment] })
super.merge(deploy_environment: { 'RAILS_ENV' => deploy_env })
end

def configure
Expand All @@ -32,14 +32,14 @@ def database_yml(db)
return unless db.applicable_for_configuration?

database = db.out
deploy_env = globals[:environment]
deploy_environment = deploy_env

context.template File.join(deploy_dir(app), 'shared', 'config', 'database.yml') do
source 'database.yml.erb'
mode '0660'
owner node['deployer']['user'] || 'root'
group www_group
variables(database: database, environment: deploy_env)
variables(database: database, environment: deploy_environment)
end
end

Expand Down
21 changes: 17 additions & 4 deletions libraries/helpers.rb
Expand Up @@ -14,10 +14,23 @@ def rdses
search(:aws_opsworks_rds_db_instance)
end

def globals
{
environment: 'production'
}.merge((node['deploy'][app['shortname']].try(:[], 'global') || node['defaults']['global'] || {}).symbolize_keys)
def globals(index, application)
globals = (node['deploy'][application].try(:[], 'global') || {}).symbolize_keys
return globals[index.to_sym] unless globals[index.to_sym].nil?

old_item = old_globals(index, application)
return old_item unless old_item.nil?
node['defaults']['global'][index.to_s]
end

def old_globals(index, application)
return unless node['deploy'][application][index.to_s]
message =
"DEPRECATION WARNING: node['deploy']['#{application}']['#{index}'] is deprecated and will be removed. " \
"Please use node['deploy']['#{application}']['global']['#{index}'] instead."
Chef::Log.warn(message)
STDERR.puts(message)
node['deploy'][application][index.to_s]
end

def fire_hook(name, options)
Expand Down
22 changes: 13 additions & 9 deletions recipes/deploy.rb
Expand Up @@ -5,7 +5,7 @@
include_recipe 'opsworks_ruby::configure'

# rubocop:disable Metrics/BlockLength
every_enabled_application do |application, app_data|
every_enabled_application do |application, _app_data|
databases = []
every_enabled_rds(self, application) do |rds|
databases.push(Drivers::Db::Factory.build(self, application, rds: rds))
Expand All @@ -26,23 +26,27 @@
group www_group
environment application['environment'].merge(framework.out[:deploy_environment] || {})

if app_data[:rollback_on_error].nil?
rollback_on_error node['defaults']['deploy']['rollback_on_error']
if globals(:rollback_on_error, application['shortname']).nil?
rollback_on_error node['defaults']['global']['rollback_on_error']
else
rollback_on_error app_data[:rollback_on_error]
rollback_on_error globals(:rollback_on_error, application['shortname'])
end

keep_releases app_data[:keep_releases]
keep_releases globals(:keep_releases, application['shortname'])
create_dirs_before_symlink(
(
node['defaults']['deploy']['create_dirs_before_symlink'] + Array.wrap(app_data[:create_dirs_before_symlink])
node['defaults']['global']['create_dirs_before_symlink'] +
Array.wrap(globals(:create_dirs_before_symlink, application['shortname']))
).uniq
)
purge_before_symlink(
(node['defaults']['deploy']['purge_before_symlink'] + Array.wrap(app_data[:purge_before_symlink])).uniq
(
node['defaults']['global']['purge_before_symlink'] +
Array.wrap(globals(:purge_before_symlink, application['shortname']))
).uniq
)
symlink_before_migrate app_data[:symlink_before_migrate]
symlinks(node['defaults']['deploy']['symlinks'].merge(app_data[:symlinks] || {}))
symlink_before_migrate globals(:symlink_before_migrate, application['shortname'])
symlinks(node['defaults']['global']['symlinks'].merge(globals(:symlinks, application['shortname']) || {}))

scm.out.each do |scm_key, scm_value|
send(scm_key, scm_value) if respond_to?(scm_key)
Expand Down
12 changes: 6 additions & 6 deletions spec/fixtures/node.rb
Expand Up @@ -12,7 +12,10 @@ def node(override = {})
deploy: {
dummy_project: {
global: {
environment: 'staging'
environment: 'staging',
create_dirs_before_symlink: %(../shared/test),
purge_before_symlink: %w(public/test),
symlinks: { 'test' => 'public/test' }
},
# database: {
# adapter: 'postgresql',
Expand Down Expand Up @@ -57,14 +60,11 @@ def node(override = {})
adapter: 'sidekiq',
require: 'lorem_ipsum.rb',
queues: 'test_queue'
},
create_dirs_before_symlink: %(../shared/test),
purge_before_symlink: %w(public/test),
symlinks: { 'test' => 'public/test' }
}
}
},
defaults: {
deploy: {
global: {
symlinks: {
system: 'public/system',
assets: 'public/assets',
Expand Down
39 changes: 39 additions & 0 deletions spec/unit/recipes/deploy_spec.rb
Expand Up @@ -24,6 +24,45 @@
expect(chef_run).to include_recipe('opsworks_ruby::configure')
end

context 'DEPRECATION' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['keep_releases'] = 10
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
let(:chef_run_rhel) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['keep_releases'] = 10
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
let(:logs) { [] }

before do
allow(Chef::Log).to receive(:warn) do |message|
logs.push message
end
end

after do
expect(logs).to include(
'DEPRECATION WARNING: node[\'deploy\'][\'dummy_project\'][\'keep_releases\'] is deprecated ' \
'and will be removed. Please use node[\'deploy\'][\'dummy_project\'][\'global\'][\'keep_releases\'] instead.'
)
end

it 'debian: Shows warning' do
chef_run
end

it 'rhel: Shows warning' do
chef_run_rhel
end
end

context 'Postgresql + Git + Unicorn + Nginx + Sidekiq' do
it 'creates git wrapper script' do
expect(chef_run).to create_template('/tmp/ssh-git-wrapper.sh')
Expand Down

0 comments on commit b4f8d6b

Please sign in to comment.