Skip to content

Commit 97de7cf

Browse files
author
Pawel Pierzchala
committed
Use ConfigurationReader to unify accesss to configuration files
1 parent b01187d commit 97de7cf

15 files changed

+56
-30
lines changed

Diff for: bin/git-proxy

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ require "yaml"
5959
ROOT_DIR = File.expand_path( File.join(File.dirname(__FILE__), "../") )
6060

6161
if Rails.env.production? && Process.uid == 0
62-
config = YAML::load_file(ROOT_DIR + "/config/gitorious.yml")
62+
config = Gitorious::ConfigurationReader.read(ROOT_DIR + "/config/gitorious.yml")
6363
git_user = config["user"] || (config[Rails.env] && config[Rails.env]["user"])
6464
if git_user
6565
uid = Etc.getpwnam(git_user).uid

Diff for: bin/migrate

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ require "pathname"
4141
gitorious_root = Pathname.new(__FILE__).dirname.realpath.to_s.gsub("/bin", "")
4242
Dir.chdir gitorious_root
4343
RAILS_ENV = ENV["RAILS_ENV"] || "production"
44-
require 'yaml'
44+
require_relative "../lib/gitorious/configuration_reader"
4545

4646
def db_config
47-
@db_config ||= YAML::load(File.open('config/database.yml'))
47+
@db_config ||= Gitorious::ConfigurationReader.read('config/database.yml')
4848
end
4949

5050
def gitorious_config
51-
@gitorious_config ||= YAML::load(File.open('config/gitorious.yml'))
51+
@gitorious_config ||= Gitorious::ConfigurationReader.read('config/gitorious.yml')
5252
end
5353

5454
def repo_path

Diff for: bin/upgrade-gitorious3-config

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ must manually put it in place afterwards
3030
`cp #{ARGV[1]} #{ARGV[1]}.bu-#{Time.now.to_i}`
3131
end
3232

33-
old = YAML.load_file(ARGV[0])
33+
old = Gitorious::ConfigurationReader.read(ARGV[0])
3434

3535
def optional(settings, setting)
3636
return "##{setting}:" if !settings.key?(setting)

Diff for: config/initializers/authentication.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
auth_configuration_path = ENV['GTS_AUTHENTICATION_YML'] || Rails.root + "config/authentication.yml"
2020

2121
if File.exist?(auth_configuration_path)
22-
config = YAML::load_file(auth_configuration_path)
22+
config = Gitorious::ConfigurationReader.read(auth_configuration_path)
2323

2424
if config && config.key?(Rails.env)
2525
config = config[Rails.env]

Diff for: config/initializers/setup_mail.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# You should have received a copy of the GNU Affero General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
#++
18-
require 'erb'
1918

2019
ActionMailer::Base.default_url_options[:protocol] = Gitorious.scheme
2120
ActionMailer::Base.default_url_options[:host] = Gitorious.host
@@ -26,7 +25,7 @@
2625

2726
smtp_config_path = (Rails.root + 'config' + 'smtp.yml').to_s
2827
if File.exist?(smtp_config_path)
29-
smtp_settings = YAML::load(ERB.new(File.read(smtp_config_path)).result)
28+
smtp_settings = Gitorious::ConfigurationReader.read(smtp_config_path)
3029
if smtp_settings
3130
ActionMailer::Base.smtp_settings = smtp_settings.symbolize_keys
3231
end

Diff for: data/hooks/messaging.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ def self.env
4949
Bundler.require(:messaging, Rails.env.to_s)
5050
ENV["GIT_DIR"] = git_dir
5151

52-
require "yaml"
5352
require "gitorious/messaging"
53+
require "gitorious/configuration_reader"
5454
require "repository_root"
5555

5656
if !defined?(Gitorious::Configuration)
57-
conf = YAML::load_file(Rails.root + "config/gitorious.yml")
58-
overrides = YAML::load_file(Rails.root + "config/gitorious.overrides.yml") rescue {}
57+
conf = Gitorious::ConfigurationReader.read(Rails.root + "config/gitorious.yml")
58+
overrides = Gitorious::ConfigurationReader.read(Rails.root + "config/gitorious.overrides.yml") rescue {}
5959
conf.merge!(overrides || {})
6060
Gitorious::Messaging.adapter = (conf[Rails.env.to_s] || {})["messaging_adapter"] || conf["messaging_adapter"]
6161
Bundler.require(Gitorious::Messaging.adapter.to_sym)
@@ -65,7 +65,7 @@ def self.env
6565
if Gitorious::Messaging.adapter == "resque"
6666
resque_config = Rails.root + "config/resque.yml"
6767
if resque_config.exist?
68-
settings = YAML::load_file(resque_config)[Rails.env.to_s]
68+
settings = Gitorious::ConfigurationReader.read(resque_config)[Rails.env.to_s]
6969
Resque.redis = settings if settings
7070
end
7171
end

Diff for: ldap-wizard/lib/auth_config_loader.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def self.load_auth_file
55
auth_file = RAILS_ROOT + "config/authentication.yml"
66
raise ConfigurationError, "No config/authentication found" unless auth_file.exist?
77

8-
auth_config = YAML::load_file(auth_file) || {}
8+
auth_config = Gitorious::ConfigurationReader.read(auth_file) || {}
99
auth_config = auth_config[RAILS_ENV] if auth_config.key?(RAILS_ENV)
1010
raise ConfigurationError, "No authentication configuration found in authentication.yml. #{auth_config.inspect}" unless auth_config
1111
raise ConfigurationError, "No authentication methods defined in authentication.yml. #{auth_config.inspect}" unless auth_config.key?("methods")

Diff for: ldap-wizard/wizard.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require "credentials"
1818
require "ldap_configuration_presenter"
1919
require "ldap_tester"
20+
require "gitorious/configuration_reader"
2021
require "auth_config_loader"
2122

2223
require "sinatra"

Diff for: lib/gitorious/authorization/configuration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def self.configure!
3131
authorization_configuration_path = File.join(root, "config", "authorization.yml")
3232

3333
if File.exist?(authorization_configuration_path)
34-
if config = YAML::load_file(authorization_configuration_path)[Rails.env]
34+
if config = Gitorious::ConfigurationReader.read(authorization_configuration_path)[Rails.env]
3535
Gitorious::Authorization::Configuration.configure(config)
3636
else
3737
Gitorious::Authorization::Configuration.use_default_configuration

Diff for: lib/gitorious/configuration_loader.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
#++
1818
require "pathname"
19-
require "yaml"
20-
require "erb"
19+
require "gitorious/configuration_reader"
2120

2221
# See also config/gitorious_config.rb for more information about
2322
# how Gitorious is configured.
@@ -155,7 +154,7 @@ def load_config_file(path, allow_missing = false)
155154
full_path = File.join(@root, path)
156155
return [{}, {}] if allow_missing && !File.exist?(full_path)
157156

158-
settings = YAML.load(ERB.new(File.read(full_path)).result) || {}
157+
settings = Gitorious::ConfigurationReader.read(full_path) || {}
159158

160159
groups = {}
161160
groups['production'] = settings.delete('production') if settings.key?('production')

Diff for: lib/gitorious/configuration_reader.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
#--
3+
# Copyright (C) 2014 Gitorious AS
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Affero General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#++
18+
require 'yaml'
19+
require 'erb'
20+
21+
module Gitorious
22+
module ConfigurationReader
23+
def self.read(path)
24+
YAML::load(ERB.new(File.read(path)).result)
25+
end
26+
end
27+
end

Diff for: lib/gitorious/on_config.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# You should have received a copy of the GNU Affero General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
#++
18-
require 'erb'
1918

2019
module Gitorious
2120
# If the file Rails.root + config/config_file exists, load this as a
@@ -26,7 +25,7 @@ module Gitorious
2625
def self.on_config(config_file)
2726
path = Rails.root + "config/#{config_file}"
2827
if path.exist?
29-
settings = YAML::load(ERB.new(File.read(path)).result)[Rails.env]
28+
settings = Gitorious::ConfigurationReader.read(path)[Rails.env]
3029
yield settings if settings
3130
end
3231
end

Diff for: lib/tasks/backup.rake

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ namespace :backup do
122122
end
123123

124124
def db_config
125-
@db_config ||= YAML::load(File.open('config/database.yml'))
125+
require "gitorious/configuration_reader"
126+
@db_config ||= Gitorious::ConfigurationReader.read('config/database.yml')
126127
end
127128

128129
def repo_path

Diff for: script/undo_last_upgrade

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010

1111
def restore_db
12-
require 'yaml'
13-
conf = YAML::load(File.open('config/database.yml'))
12+
require_relative '../lib/gitorious/configuration_reader'
13+
conf = Gitorious::ConfigurationReader.read('config/database.yml')
1414
db_name = conf['production']['database']
1515
puts `mysql #{db_name} < /tmp/upgrade_db_backup.sql`
1616
end

Diff for: script/upgrade_to

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
# that each execution of /upgrade_to overwrites the undo point, so
1919
# only do one upgrade at a time.)
2020

21-
# Assumptions:
22-
# 1: If you are several versions behind, only upgrade one tagged version at a time.
21+
# Assumptions:
22+
# 1: If you are several versions behind, only upgrade one tagged version at a time.
2323
# 2: Always perform a manual, extra backup (/script/snapshot) before attempting an upgrade
2424
# 3: Take the Gitorious installation offline to prevent users from using it while upgrading
25-
# 4: Before going through with the upgrade, check relevant release notes for any breaking changes,
25+
# 4: Before going through with the upgrade, check relevant release notes for any breaking changes,
2626
# (particularly important for any major version upgrades)
2727

2828

@@ -31,7 +31,7 @@ if Process.uid != 0
3131
exit
3232
end
3333

34-
if !ARGV[0]
34+
if !ARGV[0]
3535
puts "Usage:"
3636
puts "script/upgrade_to <tag>"
3737
exit
@@ -40,8 +40,8 @@ end
4040
new_tag = ARGV[0]
4141

4242
def backup_db
43-
require 'yaml'
44-
conf = YAML::load(File.open('config/database.yml'))
43+
require_relative '../lib/gitorious/configuration_reader'
44+
conf = Gitorious::ConfigurationReader.read('config/database.yml')
4545
db_name = conf['production']['database']
4646
puts `mysqldump #{db_name} > /tmp/upgrade_db_backup.sql`
4747
end
@@ -57,7 +57,7 @@ end
5757

5858
puts "UPGRADE: About to attempt Gitorious upgrade. Please backup first. Proceed with upgrade? (y/n)"
5959
response = $stdin.gets.chomp
60-
if response != "y"
60+
if response != "y"
6161
puts "UPGRADE: Quit."
6262
exit
6363
end
@@ -86,7 +86,7 @@ puts "UPGRADE: Clearing asset pipeline..."
8686
puts `env RAILS_ENV=production bundle exec rake assets:clear`
8787

8888
puts "UPGRADE: Restarting Gitorious..."
89-
puts `script/restart`
89+
puts `script/restart`
9090

9191
puts "UPGRADE: Done, upgraded to #{new_tag}."
9292
puts "\nATTENTION: Please test and verify that your site works as expected. If any errors occur, rollback to the pre-upgrade state with script/undo_last_upgrade"

0 commit comments

Comments
 (0)