Permalink
Browse files

Did a massive amount of cleanup on the Octopusci::Config.

I cleaned up the lib/octopusci/config.rb code a huge amount and
provided rspec tests to cover all of the functionality of the
Octopusci::ConfigStore. Beyond that I updated the Gemfile.lock
so that developers are using the latest versions of all of the
gems.
  • Loading branch information...
1 parent 1bc5fc4 commit 21337a57e0aef2f49f21e7d5e8fbdefe9a5aa3cf @cyphactor committed Oct 15, 2011
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- octopusci (0.0.1)
+ octopusci (0.2.3)
actionmailer
activerecord
json
@@ -14,42 +14,40 @@ PATH
GEM
remote: http://rubygems.org/
specs:
- actionmailer (3.1.0)
- actionpack (= 3.1.0)
+ actionmailer (3.1.1)
+ actionpack (= 3.1.1)
mail (~> 2.3.0)
- actionpack (3.1.0)
- activemodel (= 3.1.0)
- activesupport (= 3.1.0)
+ actionpack (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.2)
- rack-cache (~> 1.0.3)
+ rack-cache (~> 1.1)
rack-mount (~> 0.8.2)
rack-test (~> 0.6.1)
- sprockets (~> 2.0.0)
- activemodel (3.1.0)
- activesupport (= 3.1.0)
- bcrypt-ruby (~> 3.0.0)
+ sprockets (~> 2.0.2)
+ activemodel (3.1.1)
+ activesupport (= 3.1.1)
builder (~> 3.0.0)
i18n (~> 0.6)
- activerecord (3.1.0)
- activemodel (= 3.1.0)
- activesupport (= 3.1.0)
+ activerecord (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
arel (~> 2.2.1)
tzinfo (~> 0.3.29)
- activesupport (3.1.0)
+ activesupport (3.1.1)
multi_json (~> 1.0)
arel (2.2.1)
- bcrypt-ruby (3.0.1)
builder (3.0.0)
diff-lcs (1.1.3)
erubis (2.7.0)
growl (1.0.3)
- guard (0.7.0)
+ guard (0.8.4)
thor (~> 0.14.6)
- guard-rspec (0.4.5)
- guard (>= 0.4.0)
+ guard-rspec (0.5.0)
+ guard (>= 0.8.4)
hike (1.2.1)
i18n (0.6.0)
json (1.6.1)
@@ -61,11 +59,13 @@ GEM
multi_json (1.0.3)
mysql (2.8.1)
polyglot (0.3.2)
- rack (1.3.3)
- rack-cache (1.0.3)
+ rack (1.3.4)
+ rack-cache (1.1)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
+ rack-protection (1.1.4)
+ rack
rack-test (0.6.1)
rack (>= 1.0)
rake (0.9.2)
@@ -86,10 +86,11 @@ GEM
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
- sinatra (1.2.6)
- rack (~> 1.1)
- tilt (< 2.0, >= 1.2.2)
- sprockets (2.0.0)
+ sinatra (1.3.1)
+ rack (~> 1.3, >= 1.3.4)
+ rack-protection (~> 1.1, >= 1.1.2)
+ tilt (~> 1.3, >= 1.3.3)
+ sprockets (2.0.2)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
@@ -99,7 +100,7 @@ GEM
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
- tzinfo (0.3.29)
+ tzinfo (0.3.30)
vegas (0.1.8)
rack (>= 1.0.0)
View
@@ -26,8 +26,8 @@ require 'octopusci'
# valid initial state. But, also in the case that octopusci-tentacles was
# killed it nukes the potentially screwed up state the redis data is in and
# initializes it the proper state based on the config.
-if Octopusci::CONFIG.has_key?('stages')
- Octopusci::StageLocker.load(Octopusci::CONFIG['stages'])
+if Octopusci::Config.has_key?('stages')
+ Octopusci::StageLocker.load(Octopusci::Config['stages'])
end
case ARGV[0]
View
@@ -4,7 +4,7 @@ require 'octopusci/server'
# Set the AUTH env variable to your basic auth password to protect Resque.
Resque::Server.use Rack::Auth::Basic do |username, password|
- (username == Octopusci::CONFIG['http_basic']['username']) && (password == Octopusci::CONFIG['http_basic']['password'])
+ (username == Octopusci::Config['http_basic']['username']) && (password == Octopusci::Config['http_basic']['password'])
end
run Rack::URLMap.new("/" => Octopusci::Server.new, "/resque" => Resque::Server.new)
View
@@ -2,24 +2,34 @@
require 'active_record'
module Octopusci
- class Config
+ class ConfigStore
class MissingConfigField < RuntimeError; end
- class ConfigNotInitialized < RuntimeError; end
- def initialize()
+ def initialize
+ reset()
+ end
+
+ def reset
@options = {}
end
-
- # read the configuration values into the object from a YML file.
- def load_yaml(yaml_file)
- @options = YAML.load_file(yaml_file)
+
+ def options
+ @options
+ end
+
+ def load(yaml_file = nil, &block)
+ load_yaml(yaml_file) if !yaml_file.nil?
+ yield self if block
+ after_load()
+ end
+
+ def reload(yaml_file = nil, &block)
+ reset()
+ load(yaml_file, &block)
end
# allow options to be accessed as if this object is a Hash.
def [](key_name)
- if @options.nil?
- raise ConfigNotInitialized, "Can't access the '#{key_name}' field because the config hasn't been initialized."
- end
if !@options.has_key?(key_name.to_s())
raise MissingConfigField, "'#{key_name}' is NOT defined as a config field."
end
@@ -46,50 +56,63 @@ def method_missing(key_name, *args)
return self[key_name_str]
end
end
- end
-
- # On evaluation of the module it defines a new singleton of Config.
- if (!defined?(CONFIG))
- CONFIG = Config.new()
- end
- def self.configure(yaml_file = nil, &block)
- CONFIG.load_yaml(yaml_file) if !yaml_file.nil?
- yield CONFIG if block
+ def after_load(&block)
+ if block
+ @after_load = block
+ elsif @after_load
+ @after_load.call
+ end
+ end
+
+ private
+
+ # read the configuration values into the object from a YML file.
+ def load_yaml(yaml_file)
+ @options.merge!(YAML.load_file(yaml_file))
+ end
+ end
- Notifier.default :from => Octopusci::CONFIG['smtp']['notification_from_email']
- Notifier.delivery_method = :smtp
- Notifier.smtp_settings = {
- :address => Octopusci::CONFIG['smtp']['address'],
- :port => Octopusci::CONFIG['smtp']['port'].to_s,
- :authentication => Octopusci::CONFIG['smtp']['authentication'],
- :enable_starttls_auto => Octopusci::CONFIG['smtp']['enable_starttls_auto'],
- :user_name => Octopusci::CONFIG['smtp']['user_name'],
- :password => Octopusci::CONFIG['smtp']['password'],
- :raise_delivery_errors => Octopusci::CONFIG['smtp']['raise_delivery_errors']
- }
- Notifier.logger = Logger.new(STDOUT)
+ # On evaluation of the module it defines a new singleton of Config.
+ if (!defined?(::Octopusci::Config))
+ ::Octopusci::Config = ConfigStore.new()
end
end
-# Load the actual config file
-Octopusci.configure("/etc/octopusci/config.yml")
+# Setup the config after load callback
+Octopusci::Config.after_load do
+ if Octopusci::Config['stages'] == nil
+ raise "You have defined stages as an option but have no items in it."
+ end
-if Octopusci::CONFIG['stages'] == nil
- raise "You have defined stages as an option but have no items in it."
-end
+ ActiveRecord::Base.establish_connection(
+ :adapter => Octopusci::Config['db']['adapter'],
+ :host => Octopusci::Config['db']['host'],
+ :database => Octopusci::Config['db']['database'],
+ :username => Octopusci::Config['db']['username'],
+ :password => Octopusci::Config['db']['password']
+ )
-ActiveRecord::Base.establish_connection(
- :adapter => Octopusci::CONFIG['db']['adapter'],
- :host => Octopusci::CONFIG['db']['host'],
- :database => Octopusci::CONFIG['db']['database'],
- :username => Octopusci::CONFIG['db']['username'],
- :password => Octopusci::CONFIG['db']['password']
-)
+ Octopusci::Notifier.default :from => Octopusci::Config['smtp']['notification_from_email']
+ Octopusci::Notifier.delivery_method = :smtp
+ Octopusci::Notifier.smtp_settings = {
+ :address => Octopusci::Config['smtp']['address'],
+ :port => Octopusci::Config['smtp']['port'].to_s,
+ :authentication => Octopusci::Config['smtp']['authentication'],
+ :enable_starttls_auto => Octopusci::Config['smtp']['enable_starttls_auto'],
+ :user_name => Octopusci::Config['smtp']['user_name'],
+ :password => Octopusci::Config['smtp']['password'],
+ :raise_delivery_errors => Octopusci::Config['smtp']['raise_delivery_errors']
+ }
+ Octopusci::Notifier.logger = Logger.new(STDOUT)
-Dir.open(Octopusci::CONFIG['general']['jobs_path']) do |d|
- job_file_names = d.entries.reject { |e| e == '..' || e == '.' }
- job_file_names.each do |f_name|
- require Octopusci::CONFIG['general']['jobs_path'] + "/#{f_name}"
+ Dir.open(Octopusci::Config['general']['jobs_path']) do |d|
+ job_file_names = d.entries.reject { |e| e == '..' || e == '.' }
+ job_file_names.each do |f_name|
+ load Octopusci::Config['general']['jobs_path'] + "/#{f_name}"
+ end
end
end
+
+# Load the actual config file
+Octopusci::Config.load("/etc/octopusci/config.yml")
View
@@ -41,7 +41,7 @@ def self.gh_payload_to_job_attrs(gh_pl)
# this method returns nil. Otherwise, this project returns a hash of the
# project info that it found in the config.
def self.get_project_info(project_name, project_owner)
- Octopusci::CONFIG["projects"].each do |proj|
+ Octopusci::Config["projects"].each do |proj|
if (proj['name'] == project_name) && (proj['owner'] == project_owner)
return proj
end
@@ -58,7 +58,7 @@ def self.encode(str)
end
def self.workspace_path(stage)
- return Octopusci::CONFIG['general']['workspace_base_path'] + "/#{stage}"
+ return Octopusci::Config['general']['workspace_base_path'] + "/#{stage}"
end
end
end
View
@@ -10,7 +10,7 @@ def self.perform(project_name, branch_name, job_id, job_conf)
# Note: There is no logic for handling stage coming back as nil because
# it should never happen because there are the same number of resque
# workers as there are stages at all times.
- if Octopusci::CONFIG.has_key?('stages')
+ if Octopusci::Config.has_key?('stages')
# Get the next available stage from redis which locks it by removing it
# from the list of available
stage = Octopusci::StageLocker.pop
@@ -57,7 +57,7 @@ def self.perform(project_name, branch_name, job_id, job_conf)
end
end
ensure
- if Octopusci::CONFIG.has_key?('stages')
+ if Octopusci::Config.has_key?('stages')
# Unlock the stage by adding it back to the list of available stages
Octopusci::StageLocker.push(stage)
end
View
@@ -50,7 +50,7 @@ def silent_output
end
def workspace_path
- return "#{Octopusci::CONFIG['general']['workspace_base_path']}/#{self.stage}"
+ return "#{Octopusci::Config['general']['workspace_base_path']}/#{self.stage}"
end
# Relative path of the output file to the workspace base path.
@@ -59,7 +59,7 @@ def rel_output_file_path
end
def abs_output_file_path
- return "#{Octopusci::CONFIG['general']['workspace_base_path']}#{self.rel_output_file_path}"
+ return "#{Octopusci::Config['general']['workspace_base_path']}#{self.rel_output_file_path}"
end
def abs_silent_output_file_path
@@ -71,7 +71,7 @@ def repository_path
end
def abs_output_path
- return "#{Octopusci::CONFIG['general']['workspace_base_path']}/jobs/#{self.id}"
+ return "#{Octopusci::Config['general']['workspace_base_path']}/jobs/#{self.id}"
end
def code_cloned?
View
@@ -8,7 +8,7 @@ class Server < Sinatra::Base
dir = File.dirname(File.expand_path(__FILE__))
set :views, "#{dir}/server/views"
- set :public, "#{dir}/server/public"
+ set :public_folder, "#{dir}/server/public"
set :static, true
before do
@@ -25,7 +25,7 @@ def protected!
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
- @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [ Octopusci::CONFIG['http_basic']['username'], Octopusci::CONFIG['http_basic']['password'] ]
+ @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [ Octopusci::Config['http_basic']['username'], Octopusci::Config['http_basic']['password'] ]
end
end
@@ -24,7 +24,7 @@ def self.push(v)
end
def self.stages
- Octopusci::CONFIG['stages']
+ Octopusci::Config['stages']
end
def self.pool
@@ -27,7 +27,7 @@ def self.launch
worker_pids = []
- Octopusci::CONFIG['stages'].size.times do
+ Octopusci::Config['stages'].size.times do
cur_pid = Process.fork do
queues = ['octopusci:commit']
worker = Resque::Worker.new(*queues)
Oops, something went wrong.

0 comments on commit 21337a5

Please sign in to comment.