Skip to content

Commit

Permalink
Merge branch 'master' into test_apps
Browse files Browse the repository at this point in the history
  • Loading branch information
HashNuke committed Nov 23, 2011
2 parents 06dc18b + 5157def commit af56867
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,4 +1,7 @@
Gemfile.lock
\#*#
doc/**
pkg/**
coverage.data
pkg
.rvmrc
12 changes: 7 additions & 5 deletions app/controllers/bushido/envs_controller.rb
Expand Up @@ -10,17 +10,19 @@ def update

else

ENV[params[:id]] = params[:value]
@value = ENV[params[:id]]
var = params[:id].upcase

ENV[var] = params[:value]
@value = ENV[var]

respond_to do |format|
if @value != ENV[params[:id]]
if @value != ENV[var]
format.html{render :layout => false, :text => true, :status => :unprocessable_entity}
format.json{render :status => :unprocessable_entity}
else
Bushido::Data.fire(params[:id], {params[:id] => ENV[params[:id]]})
Bushido::Data.fire(var, {var => ENV[var]})
format.html{render :text => true}
format.json{render :json => {params[:id] => ENV[params[:id]]}}
format.json{render :json => {var => ENV[var]}}
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions lib/bushido.rb
Expand Up @@ -4,14 +4,14 @@ module Bushido #:nodoc:
require 'json'
require 'highline/import'
require 'orm_adapter'

require 'engine'
if defined?(Rails) && Rails::VERSION::MAJOR == 3
require 'engine'
require "action_dispatch"
end
require "rails/routes"
require "bushido/base"
require "bushido/config"
require "bushido/smtp"
require "bushido/action_mailer"
require "bushido/hooks"
require "bushido/platform"
Expand All @@ -26,7 +26,15 @@ module Bushido #:nodoc:
require "bushido/middleware"
require "bushido/models"
require "bushido/schema"


# Manually require the controllers for rails 2
if defined?(Rails) && Rails::VERSION::MAJOR == 2
base_dir = "#{File.dirname(__FILE__)}/.."

require "#{base_dir}/app/controllers/bushido/data_controller"
require "#{base_dir}/app/controllers/bushido/envs_controller"
end

# Default way to setup Bushido. Run rails generate bushido_install to create
# a fresh initializer with all configuration values.
def self.setup
Expand Down
28 changes: 26 additions & 2 deletions lib/bushido/action_mailer.rb
@@ -1,8 +1,32 @@
module ActionMailer
class Base
private
def perform_delivery_bushido(mail)
Bushido::Command.post_command(Bushido::Base.send_email_url, mail)
def perform_delivery_bushido(mail)
if mail.to.nil?
unless logger.nil?
logger.error "This mail isn't addressed to anyone! Dropping"
end

return false
end

result = Bushido::App.mail_allowed?

if result
logger.info result.inspect unless logger.nil?
if result["success"] == true
Bushido::SMTP.setup_action_mailer_smtp!

unless logger.nil?
logger.info "App allowed to send email, sending via SMTP"
__send__("perform_delivery_smtp", mail) if perform_deliveries
end
else
logger.info "Unable to send email: #{result['message']}" unless logger.nil?
end
else
logger.info "Unable to contact Bushido to verify email credentials" unless logger.nil?
end
end
end
end
7 changes: 7 additions & 0 deletions lib/bushido/app.rb
Expand Up @@ -187,6 +187,13 @@ def logs
end


# Check if the app is allowed to send emails
# Apps are rate-limited according to their tier
def mail_allowed?
Bushido::Command.get_command(Bushido::Base.allowed_email_url)
end


def ssh_key #:nodoc:
get({:gift => "ssh_key"})["ssh_key"]
end
Expand Down
4 changes: 1 addition & 3 deletions lib/bushido/base.rb
@@ -1,10 +1,9 @@
module Bushido

class Base
class << self
url_pairs = {
:unity=>[:valid, :exists, :invite, :pending_invites, :remove],
:email=>[:send]
:email=>[:send, :allowed]
}

# NOTE Cannot use define_singleton_method since ruby 1.8 compatibility is a must
Expand All @@ -17,5 +16,4 @@ class << self
end
end
end

end
2 changes: 2 additions & 0 deletions lib/bushido/middleware.rb
Expand Up @@ -15,6 +15,7 @@ def initialize(app, opts = {})
end

def call(env)
@bushido_invitation_token = (Rack::Request.new(env).params[:invitation_token].nil? ) ? '' : Rack::Request.new(env).params[:invitation_token]
@bushido_claimed = (ENV['BUSHIDO_CLAIMED'].nil? or ENV['BUSHIDO_CLAIMED'].blank?) ? false : true

status, headers, response = @app.call(env)
Expand All @@ -28,6 +29,7 @@ def call(env)
<script type="text/javascript">
var _bushido_app = '#{@bushido_app_name}';
var _bushido_claimed = #{@bushido_claimed.to_s};
var _bushido_invitation_token = '#{@bushido_invitation_token}';
var _bushido_metrics_token = '#{@bushido_metrics_token}';
(function() {
var bushido = document.createElement('script'); bushido.type = 'text/javascript'; bushido.async = true;
Expand Down
13 changes: 11 additions & 2 deletions lib/bushido/platform.rb
Expand Up @@ -13,9 +13,18 @@ def publish_url
"#{host}/apps/#{name}/bus"
end

def protocol
ENV['BUSHIDO_PROTOCOL'] || "https"
end

def port
ENV['BUSHIDO_PORT']
end

def host
port = ENV['BUSHIDO_PORT'] ? ":#{ENV['BUSHIDO_PORT']}" : ""
"http://#{ENV['BUSHIDO_HOST'] || 'bushi.do'}#{port}"
bushido_port = port ? ":#{port}" : ""
bushido_host = ENV['BUSHIDO_HOST'] || 'bushi.do'
"#{protocol}://#{bushido_host}#{bushido_port}"
end

def on_bushido?
Expand Down
24 changes: 24 additions & 0 deletions lib/bushido/smtp.rb
@@ -0,0 +1,24 @@
module Bushido
class SMTP
class << self
[:tls, :server, :port, :domain, :authentication, :user, :password].each do |method_name|
define_method "#{method_name}".to_sym do
ENV["SMTP_#{method_name.to_s.upcase}"]
end
end

def setup_action_mailer_smtp!
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => Bushido::SMTP.tls,
:tls => Bushido::SMTP.tls,
:address => Bushido::SMTP.server,
:port => Bushido::SMTP.port,
:domain => Bushido::SMTP.domain,
:authentication => Bushido::SMTP.authentication,
:user_name => Bushido::SMTP.user,
:password => Bushido::SMTP.password,
}
end
end
end
end
23 changes: 14 additions & 9 deletions lib/engine.rb
@@ -1,16 +1,21 @@
require 'bushido'
require 'rails'
require 'rails/routes'

module Bushido
class Engine < Rails::Engine
if defined?(Rails) && Rails::VERSION::MAJOR == 3
require 'rails'
require 'rails/routes'

module Bushido
class Engine < Rails::Engine

initializer "bushido.add_middleware" do |app|

# Only include our middleware if its on our platform
unless ENV['BUSHIDO_APP'].nil?
app.middleware.use Bushido::Middleware
initializer "bushido.add_middleware" do |app|
# Only include our middleware if its on our platform
unless ENV['BUSHIDO_APP'].nil?
app.middleware.use Bushido::Middleware
end
end
end
end
else
# Rails 2 handling
Rails.configuration.middleware.use 'Bushido::Middleware'
end

0 comments on commit af56867

Please sign in to comment.