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

Commit

Permalink
Finished database support in recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Apr 10, 2016
1 parent e23f2d4 commit cf955a0
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 16 deletions.
2 changes: 2 additions & 0 deletions libraries/all.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true
libdir = File.expand_path('..', __FILE__)
require File.join(libdir, 'core_ext')
require File.join(libdir, 'helpers')
Dir[File.join(libdir, 'drivers', 'dsl', '**', '*.rb')].each { |f| require f }
Dir[File.join(libdir, '*', '**', 'base.rb')].each { |f| require f }
Dir[File.join(libdir, '*', '**', '*.rb')].each { |f| require f }
12 changes: 12 additions & 0 deletions libraries/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ def self.descendants
end
end

class Array
def self.wrap(object)
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary || [object]
else
[object]
end
end
end

class String
def constantize
split('::').inject(Object) { |a, e| a.const_get(e) }
Expand Down
16 changes: 12 additions & 4 deletions libraries/drivers/db/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,28 @@ def initialize(app, node, options = {})

# rubocop:disable Metrics/AbcSize
def out
deploy_db_data = JSON.parse(node['deploy'][app['shortname']]['database'].to_json, symbolize_names: true) || {}
if @connection_data_source == :adapter
return deploy_db_data.merge(adapter: adapter).merge(
database: deploy_db_data[:database] || app['data_sources'].first['database_name']
return out_defaults.merge(
database: out_defaults[:database] || app['data_sources'].first['database_name']
)
end

deploy_db_data.merge(
out_defaults.merge(
adapter: adapter, username: options[:rds]['db_user'], password: options[:rds]['db_password'],
host: options[:rds]['address'], database: app['data_sources'].first['database_name']
)
end
# rubocop:enable Metrics/AbcSize

def out_defaults
base = JSON.parse(node['deploy'][app['shortname']]['database'].to_json, symbolize_names: true) || {}
{
encoding: 'utf8',
host: 'localhost',
reconnect: true
}.merge(base).merge(adapter: adapter)
end

def setup(context)
handle_packages(context)
end
Expand Down
53 changes: 53 additions & 0 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true
def applications
if Chef::Config[:solo]
Chef::Log.warn('This recipe uses search. Chef Solo does not support search.')
end
search(:aws_opsworks_app)
end

def rdses
if Chef::Config[:solo]
Chef::Log.warn('This recipe uses search. Chef Solo does not support search.')
end
search(:aws_opsworks_rds_db_instance)
end

def www_group
value_for_platform_family(
'debian' => 'www-data'
)
end

def create_deploy_dir(application, subdir = '/')
dir = File.join('/', 'srv', 'www', application['shortname'], subdir)
directory dir do
mode '0755'
recursive true
owner node['deployer']['user'] || 'root'
group www_group

action :create
not_if { File.directory?(dir) }
end
dir
end

def deploy_dir(application)
create_deploy_dir(application)
end

def every_enabled_application
node['deploy'].each do |deploy_app_shortname, deploy|
application = applications.detect { |app| app['shortname'] == deploy_app_shortname }
next unless application
deploy = deploy[application['shortname']]
yield application, deploy
end
end

def every_enabled_rds
rdses.each do |rds|
yield rds
end
end
3 changes: 2 additions & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
long_description 'Installs/Configures opsworks_ruby'
version '0.1.0'

depends 'packages'
depends 'deployer'
depends 'application_ruby'

source_url 'https://github.com/ajgon/opsworks_ruby'
issues_url 'https://github.com/ajgon/opsworks_ruby/issues'
18 changes: 18 additions & 0 deletions recipes/configure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true
#
# Cookbook Name:: opsworks_ruby
# Recipe:: configure
#

every_enabled_application do |application, _deploy|
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
template File.join(create_deploy_dir(application, File.join('shared', 'config')), 'database.yml') do
source 'database.yml.erb'
mode '0660'
owner node['deployer']['user'] || 'root'
group www_group
variables(database: database.out, environment: application['attributes']['rails_env'])
end
end
end
13 changes: 2 additions & 11 deletions recipes/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@
# Recipe:: setup
#

if Chef::Config[:solo]
Chef::Log.warn('This recipe uses search. Chef Solo does not support search.')
end

applications = search(:aws_opsworks_app)
rdses = search(:aws_opsworks_rds_db_instance)

node['deploy'].each do |deploy_app_id, _deploy|
application = applications.detect { |app| app['id'] == deploy_app_id }
next unless application
rdses.each do |rds|
every_enabled_application do |application|
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
database.setup(self)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/unit/libraries/drivers/db/postgresql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
context 'connection data' do
after(:each) do
expect(@item.out).to eq(
encoding: 'utf8',
reconnect: true,
adapter: 'postgresql',
username: 'dbuser',
password: '03c1bc98cdd5eb2f9c75',
Expand Down
33 changes: 33 additions & 0 deletions spec/unit/recipes/configure_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true
#
# Cookbook Name:: opsworks_ruby
# Spec:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.

require 'spec_helper'

describe 'opsworks_ruby::configure' do
let(:chef_run) do
ChefSpec::SoloRunner.new 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])
stub_search(:aws_opsworks_rds_db_instance, '*:*').and_return([aws_opsworks_rds_db_instance])
stub_node { |n| n.merge(node) }
end

context 'Database' do
context 'Postgresql' do
it 'creates proper database.yml template' do
db_config = Drivers::Db::Postgresql.new(aws_opsworks_app, node, rds: aws_opsworks_rds_db_instance).out
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 }.to_json).to_yaml
)
end
end
end
end
7 changes: 7 additions & 0 deletions templates/default/database.yml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%=
config = {}
(['development', 'production'] + Array.wrap(@environment)).select(&:present?).uniq.each do |env|
config[env] = @database
end
JSON.parse(config.to_json).to_yaml
-%>

0 comments on commit cf955a0

Please sign in to comment.