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

Commit

Permalink
Added MySQL Driver support
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Apr 23, 2016
1 parent 05e3a75 commit 72d4b9f
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 153 deletions.
68 changes: 42 additions & 26 deletions README.md
@@ -1,7 +1,9 @@
# opsworks_ruby Cookbook

[![Chef cookbook](https://img.shields.io/cookbook/v/opsworks_ruby.svg)](https://supermarket.chef.io/cookbooks/opsworks_ruby)
[![Build Status](https://travis-ci.org/ajgon/opsworks_ruby.svg?branch=master)](https://travis-ci.org/ajgon/opsworks_ruby)
[![Coverage Status](https://coveralls.io/repos/github/ajgon/opsworks_ruby/badge.svg?branch=master)](https://coveralls.io/github/ajgon/opsworks_ruby?branch=master)
[![license](https://img.shields.io/github/license/ajgon/opsworks_ruby.svg?maxAge=2592000)](https://opsworks-ruby.mit-license.org/)

A [chef](https://www.chef.io/) cookbook to deploy Ruby applications to Amazon OpsWorks.

Expand All @@ -11,8 +13,22 @@ This cookbook is design 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).

**Currently only PostgreSQL database, GIT SCM, Rails framework, Unicorn
appserver and nginx webserver are supported.** New drivers will be added soon.
## Support

* Database
* MySQL
* PostgreSQL
* SCM
* git
* Framework
* Ruby on Rails
* App server
* Unicorn
* Web server
* nginx
* Worker
* Null (no worker)
* sidekiq

## Requirements

Expand Down Expand Up @@ -51,10 +67,9 @@ you don't need to use them. The chef will do all the job, and determine them
for you.

* `app['database']['adapter']`
* **Supported values:** `postgresql`
* **Supported values:** `postgresql`, `mysql`
* **Default:** `postgresql`
* ActiveRecord adapter which will be used for database connection. Currently
only PostgreSQL is supported.
* ActiveRecord adapter which will be used for database connection.
* `app['database']['username']`
* Username used to authenticate to the DB
* `app['database']['password']`
Expand Down Expand Up @@ -147,27 +162,6 @@ Configuration parameters for the ruby application server. Currently only
* [`app['appserver']['worker_processes']`](https://unicorn.bogomips.org/TUNING.html)
* **Default:** `4`

### worker

Configuration for ruby workers. Currenty `Null` (no worker) and `Sidekiq`
are supported. Every worker is covered by `monitd` daemon out-of-the-box.

* `app['worker']['adapter']`
* **Default:** `null`
* **Supported values:** `null`, `sidekiq`
* Worker used to perform background tasks. `null` means no worker enabled.
* `app['worker']['process_count']`
* ** Default:** `2`
* How many separate worker processes will be launched.
* `app['worker']['syslog']`
* **Default:** `true`
* **Supported values:** `true`, `false`
* Log worker output to syslog?
* `app['worker']['config']`
* Configuration parameters which will be directly passed to the worker.
For example, for `sidekiq` they will be serialized to
[`sidekiq.yml` config file](https://github.com/mperham/sidekiq/wiki/Advanced-Options#the-sidekiq-configuration-file).

### webserver

Webserver configuration. Proxy passing to application is handled out-of-the-box.
Expand Down Expand Up @@ -215,6 +209,27 @@ Currently only nginx is supported.
well. If your application needs a support for those browsers, set this
parameter to `true`.

### worker

Configuration for ruby workers. Currenty `Null` (no worker) and `Sidekiq`
are supported. Every worker is covered by `monitd` daemon out-of-the-box.

* `app['worker']['adapter']`
* **Default:** `null`
* **Supported values:** `null`, `sidekiq`
* Worker used to perform background tasks. `null` means no worker enabled.
* `app['worker']['process_count']`
* ** Default:** `2`
* How many separate worker processes will be launched.
* `app['worker']['syslog']`
* **Default:** `true`
* **Supported values:** `true`, `false`
* Log worker output to syslog?
* `app['worker']['config']`
* Configuration parameters which will be directly passed to the worker.
For example, for `sidekiq` they will be serialized to
[`sidekiq.yml` config file](https://github.com/mperham/sidekiq/wiki/Advanced-Options#the-sidekiq-configuration-file).

Since this driver is basically a wrapper for [nginx cookbook](https://github.com/miketheman/nginx/tree/2.7.x),
you can also configure [`node['nginx']` attributes](https://github.com/miketheman/nginx/tree/2.7.x#attributes)
as well (notice that `node['deploy'][<application_shortname>]` logic doesn't
Expand All @@ -239,4 +254,5 @@ for details.
## Author and License

Author: Igor Rzegocki <[igor@rzegocki.pl](mailto:igor@rzegocki.pl)>

License: [MIT](http://opsworks-ruby.mit-license.org/)
10 changes: 10 additions & 0 deletions libraries/drivers_db_mysql.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true
module Drivers
module Db
class Mysql < Base
adapter :mysql2
allowed_engines :mysql, :mysql2
packages debian: 'libmysqlclient-dev', rhel: 'mysql-devel'
end
end
end
50 changes: 50 additions & 0 deletions spec/unit/examples/db_parameters_and_connection.rb
@@ -0,0 +1,50 @@
# frozen_string_literal: true
RSpec.shared_examples 'db parameters and connection' do |rdbms, options = {}|
it 'receives and exposes app, node and database bag' do
driver = described_class.new(aws_opsworks_app, node, rds: aws_opsworks_rds_db_instance(engine: rdbms))

expect(driver.app).to eq aws_opsworks_app
expect(driver.node).to eq node
expect(driver.options[:rds]).to eq aws_opsworks_rds_db_instance(engine: rdbms)
end

it 'raises error when no rds is present' do
expect do
described_class.new(aws_opsworks_app, node, dummy_option: true).out
end.to raise_error ArgumentError, ':rds option is not set.'
end

context 'connection data' do
it 'taken from engine' do
item = described_class.new(
aws_opsworks_app,
node(deploy: { dummy_project: { database: { adapter: rdbms } } }),
rds: aws_opsworks_rds_db_instance(engine: rdbms)
)
expect(item.out).to eq(
encoding: 'utf8',
reconnect: true,
adapter: options[:adapter] || rdbms,
username: 'dbuser',
password: '03c1bc98cdd5eb2f9c75',
host: 'dummy-project.c298jfowejf.us-west-2.rds.amazon.com',
database: 'dummydb'
)
end

it 'taken from adapter' do
item = described_class.new(
aws_opsworks_app,
node(deploy: { dummy_project: { database: { adapter: rdbms } } }),
rds: aws_opsworks_rds_db_instance(engine: nil)
)
expect(item.out).to eq(
encoding: 'utf8',
reconnect: true,
adapter: options[:adapter] || rdbms,
host: 'localhost',
database: 'dummydb'
)
end
end
end
93 changes: 93 additions & 0 deletions spec/unit/examples/db_validate_adapter_and_engine.rb
@@ -0,0 +1,93 @@
# frozen_string_literal: true
RSpec.shared_examples 'db validate adapter and engine' do |rdbms|
context "#{rdbms}: validate adapter and engine" do
it 'adapter = missing, engine = missing' do
expect do
described_class.new(
aws_opsworks_app, node(deploy: { dummy_project: {} }), rds: aws_opsworks_rds_db_instance(engine: nil)
).out
end.to raise_error ArgumentError,
"Missing :app or :node engine, expected #{described_class.allowed_engines.inspect}."
end

it 'adapter = missing, engine = wrong' do
expect do
described_class.new(
aws_opsworks_app, node(deploy: { dummy_project: {} }), rds: aws_opsworks_rds_db_instance(engine: 'wrong')
).out
end.to raise_error ArgumentError,
"Incorrect :app engine, expected #{described_class.allowed_engines.inspect}, got 'wrong'."
end

it 'adapter = missing, engine = correct' do
expect do
described_class.new(
aws_opsworks_app, node(deploy: { dummy_project: {} }), rds: aws_opsworks_rds_db_instance(engine: rdbms)
).out
end.not_to raise_error
end

it 'adapter = wrong, engine = missing' do
expect do
described_class.new(
aws_opsworks_app,
node(deploy: { dummy_project: { database: { adapter: 'wrong' } } }),
rds: aws_opsworks_rds_db_instance(engine: nil)
).out
end.to raise_error ArgumentError,
"Incorrect :node engine, expected #{described_class.allowed_engines.inspect}, got 'wrong'."
end

it 'adapter = wrong, engine = wrong' do
expect do
described_class.new(
aws_opsworks_app,
node(deploy: { dummy_project: { database: { adapter: 'wrong' } } }),
rds: aws_opsworks_rds_db_instance(engine: 'wrong')
).out
end.to raise_error ArgumentError,
"Incorrect :app engine, expected #{described_class.allowed_engines.inspect}, got 'wrong'."
end

it 'adapter = wrong, engine = correct' do
expect do
described_class.new(
aws_opsworks_app,
node(deploy: { dummy_project: { database: { adapter: 'wrong' } } }),
rds: aws_opsworks_rds_db_instance(engine: rdbms)
).out
end.not_to raise_error
end

it 'adapter = correct, engine = missing' do
expect do
described_class.new(
aws_opsworks_app,
node(deploy: { dummy_project: { database: { adapter: rdbms } } }),
rds: aws_opsworks_rds_db_instance(engine: nil)
).out
end.not_to raise_error
end

it 'adapter = correct, engine = wrong' do
expect do
described_class.new(
aws_opsworks_app,
node(deploy: { dummy_project: { database: { adapter: rdbms } } }),
rds: aws_opsworks_rds_db_instance(engine: 'wrong')
).out
end.to raise_error ArgumentError,
"Incorrect :app engine, expected #{described_class.allowed_engines.inspect}, got 'wrong'."
end

it 'adapter = correct, engine = correct' do
expect do
described_class.new(
aws_opsworks_app,
node(deploy: { dummy_project: { database: { adapter: rdbms } } }),
rds: aws_opsworks_rds_db_instance(engine: rdbms)
).out
end.not_to raise_error
end
end
end
9 changes: 9 additions & 0 deletions spec/unit/libraries/drivers_db_mysql_spec.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true
require 'spec_helper'
require 'unit/examples/db_validate_adapter_and_engine'
require 'unit/examples/db_parameters_and_connection'

describe Drivers::Db::Mysql do
include_examples 'db validate adapter and engine', 'mysql'
include_examples 'db parameters and connection', 'mysql', adapter: 'mysql2'
end

0 comments on commit 72d4b9f

Please sign in to comment.