Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
Merge pull request #973 from till/r/scholar-deploy-manager
Browse files Browse the repository at this point in the history
Refactor: move scholar to easybib_deploy_manager
  • Loading branch information
gilleyj committed Apr 13, 2016
2 parents c407aa7 + 416dbcf commit bda492b
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 55 deletions.
40 changes: 30 additions & 10 deletions easybib/providers/deploy_manager.rb
@@ -1,20 +1,19 @@
action :deploy do
apps = new_resource.apps
applications = new_resource.apps
deployments = new_resource.deployments

did_we_deploy = false

debug_log("#{new_resource.stack} (OpsWorks Stack)")

if apps.empty?
if applications.empty?
debug_log('No apps configured')
elsif deployments.empty?
debug_log('No deployments')
else
debug_log('Apps & deployments')
apps.each do |app_name, app_data|
raise "'layer' missing for '#{app_name}'" unless app_data.key?('layer')
raise "'nginx' missing for '#{app_name}'" unless app_data.key?('nginx')
applications.each do |app_name, app_data|
validate_app_data(app_data)

did_we_deploy = run_deploys(deployments, app_name, app_data)
end
Expand All @@ -29,16 +28,18 @@ def debug_log(msg)
Chef::Log.info("easybib_deploy_manager: #{msg}")
end

# Hash: deployments
# String: app_name
# Hash: app_data
# Run deployments
#
# deployments - Hash, from OpsWorks
# app_name - String
# app_data - Hash
def run_deploys(deployments, app_name, app_data)
did_we_deploy = false

deployments.each do |application, deploy|

if application != app_name
debug_log("#{application} skipped")
debug_log("#{application} skipped: #{app_name}")
next
end

Expand All @@ -55,18 +56,37 @@ def run_deploys(deployments, app_name, app_data)

did_we_deploy = true

config_template = app_data['nginx']
config_template = app_data.fetch('nginx', nil)
next if config_template.nil? # no nginx

listen_opts = get_additional('listen_opts', app_data)

easybib_nginx application do
config_template config_template
domain_name deploy['domains'].join(' ')
doc_root deploy['document_root']
htpasswd "#{deploy['deploy_to']}/current/htpasswd"
listen_opts listen_opts
notifies :reload, 'service[nginx]', :delayed
notifies node['easybib-deploy']['php-fpm']['restart-action'], 'service[php-fpm]', :delayed
end
end

did_we_deploy
end

# Extracts optional "listen_opts" for "easybib_nginx"
#
# data - Hash
#
# Returns a string or nil.
def get_additional(key, data)
data.fetch('nginx_config', {}).fetch(key, nil)
end

def validate_app_data(app_data)
raise 'Must be a hash!' unless app_data.is_a?(Hash)

raise "'layer' missing for '#{app_name}'" unless app_data.key?('layer')
raise "'nginx' missing for '#{app_name}'" unless app_data.key?('nginx')
end
6 changes: 4 additions & 2 deletions easybib/spec/easybib_deploy_manager_spec.rb
@@ -1,4 +1,4 @@
require_relative 'spec_helper'
require 'chefspec'

describe 'easybib_deploy_manager' do

Expand All @@ -11,8 +11,10 @@

let(:runner) do
ChefSpec::Runner.new(
:platform => 'ubuntu',
:version => '14.04',
:cookbook_path => cookbook_paths,
:log_level => :debug,
:log_level => :error,
:step_into => %w(
easybib_deploy_manager
easybib_deploy
Expand Down
18 changes: 18 additions & 0 deletions stack-scholar/attributes/default.rb
@@ -1,3 +1,21 @@
force_default['haproxy']['ssl'] = 'on'
force_default['haproxy']['type'] = '1.5'
force_default['ssl-deploy']['ssl-role'] = 'lb'

default['stack-scholar']['applications'] = {
:scholar => {
:layer => nil,
:nginx => 'scholar.conf.erb',
:nginx_config => {
:listen_opts => 'default_server'
}
},
:scholar_admin => {
:layer => 'nginxphpapp',
:nginx => 'silex.conf.erb'
},
:scholar_realtime => {
:layer => nil,
:nginx => nil
}
}
53 changes: 11 additions & 42 deletions stack-scholar/recipes/deploy.rb
@@ -1,49 +1,18 @@
include_recipe 'php-fpm::service'
include_recipe 'nginx-app::service'

node['deploy'].each do |application, deploy|
listen_opts = nil
node.set['stack-scholar']['applications']['scholar']['layer'] = [
'nginxphpapp',
node['easybib_deploy']['supervisor_role']
]

case application
when 'scholar_admin'
next unless allow_deploy(application, 'scholar_admin', 'nginxphpapp')
when 'scholar_realtime'
if node['haproxy']['websocket_layers'].nil?
next
end

next unless allow_deploy(application, 'scholar_realtime', node['haproxy']['websocket_layers'].keys)
when 'scholar'
listen_opts = 'default_server'
supervisor_role = node['easybib_deploy']['supervisor_role']
next unless allow_deploy(application, 'scholar', ['nginxphpapp', supervisor_role])
else
Chef::Log.info("deploy::scholar - #{application} skipped")
next
end

Chef::Log.info("deploy::#{application} - Deployment started.")
Chef::Log.info("deploy::#{application} - Deploying as user: #{deploy['user']} and #{deploy['group']}")

easybib_deploy application do
deploy_data deploy
app application
end

config_template = if application == 'scholar'
'scholar.conf.erb'
else
'silex.conf.erb'
end
unless node['haproxy']['websocket_layers'].nil?
node.set['stack-scholar']['applications']['scholar_realtime']['layer'] = node['haproxy']['websocket_layers'].keys
end

easybib_nginx application do
config_template config_template
domain_name deploy['domains'].join(' ')
doc_root deploy['document_root']
htpasswd "#{deploy['deploy_to']}/current/htpasswd"
listen_opts listen_opts
notifies :reload, 'service[nginx]', :delayed
notifies node['easybib-deploy']['php-fpm']['restart-action'], 'service[php-fpm]', :delayed
end
Chef::Log.info(node['stack-scholar']['applications'])

easybib_deploy_manager get_cluster_name do
apps node['stack-scholar']['applications']
deployments node['deploy']
end
39 changes: 39 additions & 0 deletions stack-scholar/spec/bugs_spec.rb
@@ -0,0 +1,39 @@
require 'chefspec'
require 'json'

describe 'fixtures::brokenapps' do

let(:cookbook_paths) do
[
File.expand_path("#{File.dirname(__FILE__)}/../../"),
File.expand_path("#{File.dirname(__FILE__)}/")
]
end

let(:runner) do
ChefSpec::Runner.new(
:cookbook_path => cookbook_paths,
:step_into => %w(easybib_deploy_manager)
)
end
let(:chef_run) { runner.converge(described_recipe) }
let(:node) { runner.node }

let(:fixtures) { "#{File.dirname(__FILE__)}/fixtures" }

before do
node.set['opsworks']['stack']['name'] = 'stack-chefspec'
node.set['opsworks']['instance']['layers'] = ['node-chefspec']
end

describe 'broken apps' do
before do
node.set['deploy'] = JSON.parse(File.read("#{fixtures}/deploy.json"))
node.set['stack-chefspec']['applications'] = JSON.parse(File.read("#{fixtures}/brokenapps.json"))
end

it 'chef throws an exception' do
expect { chef_run }.to raise_error(RuntimeError)
end
end
end
10 changes: 9 additions & 1 deletion stack-scholar/spec/deploy_spec.rb
Expand Up @@ -2,11 +2,19 @@

describe 'stack-scholar::deploy' do

let(:runner) { ChefSpec::Runner.new }
let(:runner) do
ChefSpec::Runner.new(
:step_into => %w(easybib_deploy_manager)
)
end
let(:chef_run) { runner.converge(described_recipe) }
let(:node) { runner.node }
let(:deploy_data) { { 'deploy_to' => '/bla/dir', 'document_root' => 'www', 'domains' => ['foo.tld'] } }

before do
node.set['opsworks']['stack']['name'] = 'chefspec'
end

describe 'scholar_admin deployment in correct layer' do
before do
node.set['opsworks']['instance']['layers'] = ['nginxphpapp']
Expand Down
3 changes: 3 additions & 0 deletions stack-scholar/spec/fixtures/brokenapps.json
@@ -0,0 +1,3 @@
{
"scholar_realtime": ["consumer"]
}
56 changes: 56 additions & 0 deletions stack-scholar/spec/fixtures/deploy.json
@@ -0,0 +1,56 @@
{
"scholar_admin": {
"deploy_to": "/srv/www/scholar_admin",
"chef_provider": "Timestamped",
"keep_releases": 5,
"current_path": "/srv/www/scholar_admin/current",
"document_root": "web",
"ignore_bundler_groups": ["test", "development"],
"absolute_document_root": "/srv/www/scholar_admin/current/web/",
"rake": "/usr/local/bin/rake",
"migrate": false,
"migrate_command": "",
"rails_env": "production",
"action": "deploy",
"user": "deploy",
"group": "deploy",
"shell": "/no/login",
"home": "/var/www",
"sleep_before_restart": 0,
"stack": {
"needs_reload": true
},
"enable_submodules": true,
"shallow_clone": false,
"delete_cached_copy": true,
"purge_before_symlink": ["log", "tmp/pids", "public/system"],
"create_dirs_before_symlink": ["tmp", "public", "config"],
"symlink_before_migrate": {},
"symlinks": {
"system": "public/system",
"pids": "tmp/pids",
"log": "log"
},
"environment": {
"HOME": "/var/www"
},
"environment_variables": {},
"ssl_support": false,
"application": "scholar_admin",
"application_type": "other",
"auto_bundle_on_deploy": true,
"deploying_user": "arn:aws:iam::12345:user/till",
"domains": ["example.org", "scholar_admin"],
"mounted_at": null,
"restart_command": "echo 'restarting app'",
"ssl_certificate": null,
"ssl_certificate_key": null,
"ssl_certificate_ca": null,
"scm": {
"scm_type": "s3",
"repository": "https://deploy/zip",
"user": "",
"password": ""
}
}
}
4 changes: 4 additions & 0 deletions stack-scholar/spec/fixtures/metadata.rb
@@ -0,0 +1,4 @@
name 'fixtures'
version '0.99.0'

depends 'easybib'
4 changes: 4 additions & 0 deletions stack-scholar/spec/fixtures/recipes/brokenapps.rb
@@ -0,0 +1,4 @@
easybib_deploy_manager get_cluster_name do
apps node['stack-chefspec']['applications']
deployments node['deploy']
end

0 comments on commit bda492b

Please sign in to comment.