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

Commit

Permalink
Initial version, with simple postgresql driver introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Apr 9, 2016
1 parent d8bed5c commit 5d00083
Show file tree
Hide file tree
Showing 29 changed files with 748 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
@@ -0,0 +1,16 @@
.vagrant
Berksfile.lock
*~
*#
.#*
\#*#
.*.sw[a-z]
*.un~

# Bundler
Gemfile.lock
bin/*
.bundle/*

.kitchen/
.kitchen.local.yml
21 changes: 21 additions & 0 deletions .kitchen.yml
@@ -0,0 +1,21 @@
---
driver:
name: vagrant

provisioner:
name: chef_zero

# Uncomment the following verifier to leverage Inspec instead of Busser (the
# default verifier)
# verifier:
# name: inspec

platforms:
- name: ubuntu-14.04
- name: centos-7.1

suites:
- name: default
run_list:
- recipe[opsworks_ruby::default]
attributes:
16 changes: 16 additions & 0 deletions .lvimrc
@@ -0,0 +1,16 @@
let test#ruby#rspec#options = {
\ 'nearest': '--format documentation --color',
\ 'file': '--format documentation --color',
\ 'suite': '--format documentation --color',
\}
let test#ruby#rspec#executable = 'chef exec rspec'
let test#ruby#bundle_exec = 0

let g:projectionist_heuristics = {
\ "*.rb": {
\ "libraries/*.rb": { "alternate": "spec/unit/libraries/{}_spec.rb", "type": "source" },
\ "recipes/*.rb": { "alternate": "spec/unit/recipes/{}_spec.rb", "type": "source" },
\ "spec/unit/libraries/*_spec.rb": { "alternate": "libraries/{}.rb", "type": "spec" },
\ "spec/unit/recipes/*_spec.rb": { "alternate": "recipes/{}.rb", "type": "spec" }
\ }
\ }
3 changes: 3 additions & 0 deletions Berksfile
@@ -0,0 +1,3 @@
source 'https://supermarket.chef.io'

metadata
4 changes: 4 additions & 0 deletions README.md
@@ -0,0 +1,4 @@
# opsworks_ruby

TODO: Enter the cookbook description here.

102 changes: 102 additions & 0 deletions chefignore
@@ -0,0 +1,102 @@
# Put files/directories that should be ignored in this file when uploading
# to a chef-server or supermarket.
# Lines that start with '# ' are comments.

# OS generated files #
######################
.DS_Store
Icon?
nohup.out
ehthumbs.db
Thumbs.db

# SASS #
########
.sass-cache

# EDITORS #
###########
\#*
.#*
*~
*.sw[a-z]
*.bak
REVISION
TAGS*
tmtags
*_flymake.*
*_flymake
*.tmproj
.project
.settings
mkmf.log

## COMPILED ##
##############
a.out
*.o
*.pyc
*.so
*.com
*.class
*.dll
*.exe
*/rdoc/

# Testing #
###########
.watchr
.rspec
spec/*
spec/fixtures/*
test/*
features/*
examples/*
Guardfile
Procfile
.kitchen*
.rubocop.yml
spec/*
Rakefile
.travis.yml
.foodcritic
.codeclimate.yml

# SCM #
#######
.git
*/.git
.gitignore
.gitmodules
.gitconfig
.gitattributes
.svn
*/.bzr/*
*/.hg/*
*/.svn/*

# Berkshelf #
#############
Berksfile
Berksfile.lock
cookbooks/*
tmp

# Cookbooks #
#############
CONTRIBUTING*
CHANGELOG*
TESTING*
MAINTAINERS.toml

# Strainer #
############
Colanderfile
Strainerfile
.colander
.strainer

# Vagrant #
###########
.vagrant
Vagrantfile
3 changes: 3 additions & 0 deletions libraries/all.rb
@@ -0,0 +1,3 @@
libdir = File.expand_path('..', __FILE__)
require File.join(libdir, 'core_ext')
Dir[File.join(libdir, '*', '**', '*.rb')].each { |f| require f }
43 changes: 43 additions & 0 deletions libraries/core_ext.rb
@@ -0,0 +1,43 @@
class Object
def try(*a, &b)
try!(*a, &b) if a.empty? || respond_to?(a.first)
end

def try!(*a, &b)
if a.empty? && block_given?
if b.arity == 0
instance_eval(&b)
else
yield self
end
else
public_send(*a, &b)
end
end

def blank?
respond_to?(:empty?) ? !!empty? : !self
end

def present?
!blank?
end

def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end

class String
def constantize
split('::').inject(Object) {|o,c| o.const_get c}
end

def classify
gsub(/(?:^.|_.)/) { |s| s[-1].upcase }
end

def underscore
gsub(/[A-Z]/) { |s| "_#{s.downcase}" }.sub(/^_/, '')
end
end
25 changes: 25 additions & 0 deletions libraries/drivers/base.rb
@@ -0,0 +1,25 @@
module Drivers
class Base
def initialize(app, node, options = {})
@app = app
@node = node
@options = options
end

# Dummy methods for children to redefine
def setup
end

def configure
end

def deploy
end

def undeploy
end

def shutdown
end
end
end
75 changes: 75 additions & 0 deletions libraries/drivers/db/base.rb
@@ -0,0 +1,75 @@
module Drivers
module Db
class Base < Drivers::Base
attr_reader :app, :node, :options

def initialize(app, node, options = {})
super
raise ArgumentError, ':rds option is not set.' unless options[:rds]
@connection_data_source = validate_engine
end

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']
)
end

deploy_db_data.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

protected

def self.allowed_engines(*engines)
@allowed_engines = engines.map(&:to_s) if engines.present?
@allowed_engines || []
end

def self.adapter(adapter = nil)
@adapter = adapter if adapter.present?
(@adapter || self.class.name.underscore).to_s
end

def allowed_engines
self.class.allowed_engines
end

def adapter
self.class.adapter
end

def validate_engine
rds_engine = options[:rds]['engine']

return validate_adapter if rds_engine.blank?

unless allowed_engines.include?(rds_engine)
raise ArgumentError, "Incorrect :rds engine, expected #{allowed_engines.inspect}, got '#{rds_engine}'."
end

:rds
end

def validate_adapter
connection_data = node['deploy'][app['shortname']]['database']
adapter_engine = connection_data.try(:[], 'adapter')

raise ArgumentError, "Missing :rds engine, expected #{allowed_engines.inspect}." if adapter_engine.blank?
unless allowed_engines.include?(adapter_engine)
raise ArgumentError,
"Incorrect engine provided by adapter, expected #{allowed_engines.inspect}, got '#{adapter_engine}'."
end

:adapter
end
end
end
end
16 changes: 16 additions & 0 deletions libraries/drivers/db/factory.rb
@@ -0,0 +1,16 @@
module Drivers
module Db
class Factory
def self.build(app, node, options = {})
raise ArgumentError, ':rds option is not set.' unless options[:rds]
engine = Drivers::Db::Base.descendants.detect do |db_driver|
db_driver.allowed_engines.include?(
options[:rds]['engine'] || node['deploy'][app['shortname']]['database']['adapter']
)
end
raise StandardError, 'There is no supported Db driver for given configuration.' if engine.blank?
engine.new(app, node, options)
end
end
end
end
8 changes: 8 additions & 0 deletions libraries/drivers/db/postgresql.rb
@@ -0,0 +1,8 @@
module Drivers
module Db
class Postgresql < Base
adapter :postgresql
allowed_engines :postgres, :postgresql
end
end
end
21 changes: 21 additions & 0 deletions libraries/drivers/dsl/base.rb
@@ -0,0 +1,21 @@
module Drivers
module Dsl
module Base
@@params = {}

def self.included(klass)
klass.instance_eval do
def param(name, options = {})
name = name.to_sym
@@params[name] = options[:default]
send(:define_method, name) do |*values|
@@params[name] = options[:default].is_a?(Array) ? values : values.first unless values.empty?
@@params[name]
end
end
end
end
end
end
end

0 comments on commit 5d00083

Please sign in to comment.